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>
25 #include <afs/assert.h>
28 #include <sys/param.h>
31 #include <sys/lockf.h>
36 #include <afs/afsutil.h>
39 #include <afs/afsint.h>
43 #include "viceinode.h"
45 #include "partition.h"
46 #include <afs/errors.h>
48 /*@+fcnmacros +macrofcndecl@*/
51 extern off64_t afs_lseek(int FD, off64_t O, int F);
52 #endif /*S_SPLINT_S */
53 #define afs_lseek(FD, O, F) lseek64(FD, (off64_t)(O), F)
54 #define afs_stat stat64
55 #define afs_fstat fstat64
56 #define afs_open open64
57 #define afs_fopen fopen64
58 #else /* !O_LARGEFILE */
60 extern off_t afs_lseek(int FD, off_t O, int F);
61 #endif /*S_SPLINT_S */
62 #define afs_lseek(FD, O, F) lseek(FD, (off_t)(O), F)
64 #define afs_fstat fstat
66 #define afs_fopen fopen
67 #endif /* !O_LARGEFILE */
68 /*@=fcnmacros =macrofcndecl@*/
70 /*@printflike@*/ extern void Log(const char *format, ...);
72 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
75 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
84 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
89 nBytes = FDH_READ(fdP, buf, size);
95 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
104 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
105 FDH_REALLYCLOSE(fdP);
108 nBytes = FDH_WRITE(fdP, buf, size);
115 /* Inode number format:
116 * low 26 bits - vnode number - all 1's if volume special file.
118 * next 3 bits spare (0's)
119 * high 32 bits - uniquifier (regular) or type if spare
121 #define NAMEI_VNODEMASK 0x003ffffff
122 #define NAMEI_TAGMASK 0x7
123 #define NAMEI_TAGSHIFT 26
124 #define NAMEI_UNIQMASK 0xffffffff
125 #define NAMEI_UNIQSHIFT 32
126 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
127 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
129 /* dir1 is the high 8 bits of the 26 bit vnode */
130 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
131 /* dir2 is the next 9 bits */
132 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
133 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
135 #define NAMEI_SPECDIR "special"
136 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
138 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
139 * group, not counting temporary (move) volumes.
140 * This is the number of separate files, all having
141 * the same vnode number, which can occur in a volume
152 int namei_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked);
153 static int GetFreeTag(IHandle_t * ih, int vno);
155 /* namei_HandleToInodeDir
157 * Construct the path name of the directory holding the inode data.
158 * Format: /<vicepx>/INODEDIR
161 #define PNAME_BLEN 64
163 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
165 char *tmp = name->n_base;
167 memset(name, '\0', sizeof(*name));
169 (void)volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
170 tmp += VICE_PREFIX_SIZE;
171 tmp += ih->ih_dev > 25 ? 2 : 1;
174 (void)strcpy(tmp, INODEDIR);
175 (void)strcpy(name->n_path, name->n_base);
178 #define addtoname(N, C) \
180 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
185 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
189 namei_HandleToInodeDir(name, ih);
190 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
191 (void)strcpy(name->n_voldir1, tmp);
192 addtoname(name, name->n_voldir1);
193 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
194 (void)strcpy(name->n_voldir2, tmp);
195 addtoname(name, name->n_voldir2);
198 /* namei_HandleToName
200 * Constructs a file name for the fully qualified handle.
201 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
204 namei_HandleToName(namei_t * name, IHandle_t * ih)
207 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
209 namei_HandleToVolDir(name, ih);
211 if (vno == NAMEI_VNODESPECIAL) {
212 (void)strcpy(name->n_dir1, NAMEI_SPECDIR);
213 addtoname(name, name->n_dir1);
214 name->n_dir2[0] = '\0';
216 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
217 (void)strcpy(name->n_dir1, str);
218 addtoname(name, name->n_dir1);
219 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
220 (void)strcpy(name->n_dir2, str);
221 addtoname(name, name->n_dir2);
223 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
224 (void)strcpy(name->n_inode, str);
225 addtoname(name, name->n_inode);
228 /* The following is a warning to tell sys-admins to not muck about in this
231 #define VICE_README "These files and directories are a part of the AFS \
232 namespace. Modifying them\nin any way will result in loss of AFS data.\n"
234 namei_ViceREADME(char *partition)
239 /* Create the inode directory if we're starting for the first time */
240 (void)afs_snprintf(filename, sizeof filename, "%s/%s", partition,
242 mkdir(filename, 0700);
244 (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
246 fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
248 (void)write(fd, VICE_README, strlen(VICE_README));
255 #define create_dir() \
257 if (mkdir(tmp, 0700)<0) { \
258 if (errno != EEXIST) \
266 #define create_nextdir(A) \
268 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
271 /* namei_CreateDataDirectories
273 * If creating the file failed because of ENOENT or ENOTDIR, try
274 * creating all the directories first.
277 namei_CreateDataDirectories(namei_t * name, int *created)
283 (void)strcpy(tmp, name->n_base);
286 create_nextdir(name->n_voldir1);
287 create_nextdir(name->n_voldir2);
288 create_nextdir(name->n_dir1);
289 if (name->n_dir2[0]) {
290 create_nextdir(name->n_dir2);
295 /* delTree(): Deletes an entire tree of directories (no files)
297 * root : Full path to the subtree. Should be big enough for PATH_MAX
298 * tree : the subtree to be deleted is rooted here. Specifies only the
299 * subtree beginning at tree (not the entire path). It should be
300 * a pointer into the "root" buffer.
302 * errp : errno of the first error encountered during the directory cleanup.
303 * *errp should have been initialized to 0.
306 * -1 : If errors were encountered during cleanup and error is set to
310 * If there are errors, we try to work around them and delete as many
311 * directories as possible. We don't attempt to remove directories that still
312 * have non-dir entries in them.
315 delTree(char *root, char *tree, int *errp)
323 /* delete the children first */
324 cp = strchr(tree, '/');
326 delTree(root, cp + 1, errp);
329 cp = tree + strlen(tree); /* move cp to the end of string tree */
331 /* now delete all entries in this dir */
332 if ((ds = opendir(root)) != (DIR *) NULL) {
334 while ((dirp = readdir(ds))) {
335 /* ignore . and .. */
336 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
338 /* since root is big enough, we reuse the space to
339 * concatenate the dirname to the current tree
342 strcat(root, dirp->d_name);
343 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
344 /* delete this subtree */
345 delTree(root, cp + 1, errp);
347 *errp = *errp ? *errp : errno;
349 /* recover path to our cur tree by truncating it to
354 /* if (!errno) -- closedir not implicit if we got an error */
358 /* finally axe the current dir */
360 *errp = *errp ? *errp : errno;
362 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
364 #endif /* !AFS_PTHREAD_ENV */
369 /* if we encountered errors during cleanup, we return a -1 */
377 /* namei_RemoveDataDirectories
379 * Returns 0 on success.
380 * Returns -1 on error. Typically, callers ignore this error bcause we
381 * can continue running if the removes fail. The salvage process will
382 * finish tidying up for us. We only use the n_base and n_voldir1 entries
383 * and only do rmdir's.
387 namei_RemoveDataDirectories(namei_t * name)
389 char pbuf[MAXPATHLEN], *path = pbuf;
390 int prefixlen = strlen(name->n_base), err = 0;
392 strcpy(path, name->n_path);
394 /* move past the prefix */
395 path = path + prefixlen + 1; /* skip over the trailing / */
397 /* now delete all dirs upto path */
398 return delTree(pbuf, path, &err);
402 /* Create the file in the name space.
404 * Parameters stored as follows:
406 * p1 - volid - implied in containing directory.
407 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
408 * p3 - uniq -- bits 4-0 are in mode bits 4-0
409 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
411 * p1 - volid - creation time - dwHighDateTime
412 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
413 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
414 * p4 - parid - parent volume id - implied in containing directory.
416 * Return value is the inode number or (Inode)-1 if error.
417 * We "know" there is only one link table, so return EEXIST if there already
418 * is a link table. It's up to the calling code to test errno and increment
424 * This function is called by VCreateVolume to hide the implementation
425 * details of the inode numbers. This only allows for 7 volume special
426 * types, but if we get that far, this could should be dead by then.
429 namei_MakeSpecIno(int volid, int type)
432 ino = NAMEI_INODESPECIAL;
433 type &= NAMEI_TAGMASK;
434 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
435 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
439 /* SetOGM - set owner group and mode bits from parm and tag
441 * owner - low 15 bits of parm.
442 * group - next 15 bits of parm.
443 * mode - 2 bits of parm, then lowest = 3 bits of tag.
446 SetOGM(int fd, int parm, int tag)
448 int owner, group, mode;
450 owner = parm & 0x7fff;
451 group = (parm >> 15) & 0x7fff;
452 if (fchown(fd, owner, group) < 0)
455 mode = (parm >> 27) & 0x18;
457 if (fchmod(fd, mode) < 0)
464 /* GetOGM - get parm and tag from owner, group and mode bits. */
466 GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
468 *parm = status->st_uid | (status->st_gid << 15);
469 *parm |= (status->st_mode & 0x18) << 27;
470 *tag = status->st_mode & 0x7;
474 GetOGM(int fd, int *parm, int *tag)
476 struct afs_stat status;
477 if (afs_fstat(fd, &status) < 0)
480 GetOGMFromStat(&status, parm, tag);
484 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
486 /* Derive the name and create it O_EXCL. If that fails we have an error.
487 * Get the tag from a free column in the link table.
490 namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
503 memset((void *)&tmp, 0, sizeof(IHandle_t));
506 tmp.ih_dev = volutil_GetPartitionID(part);
507 if (tmp.ih_dev == -1) {
513 /* Parameters for special file:
514 * p1 - volume id - goes into owner/group/mode
517 * p4 - parent volume id
522 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
523 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
525 int vno = p2 & NAMEI_VNODEMASK;
526 /* Parameters for regular file:
538 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
539 tag = GetFreeTag(lh, p2);
543 /* name is <uniq(p3)><tag><vno(p2)> */
545 tmp.ih_ino = (Inode) p2;
546 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
547 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
552 namei_HandleToName(&name, &tmp);
553 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
555 if (errno == ENOTDIR || errno == ENOENT) {
556 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
558 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
566 if (SetOGM(fd, ogm_parm, tag) < 0) {
572 if (p2 == -1 && p3 == VI_LINKTABLE) {
573 /* hack at tmp to setup for set link count call. */
575 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
583 if (code || (fd < 0)) {
587 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
592 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
598 namei_iopen(IHandle_t * h)
603 /* Convert handle to file name. */
604 namei_HandleToName(&name, h);
605 fd = afs_open(name.n_path, O_RDWR, 0666);
609 /* Need to detect vol special file and just unlink. In those cases, the
610 * handle passed in _is_ for the inode. We only check p1 for the special
614 namei_dec(IHandle_t * ih, Inode ino, int p1)
621 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
624 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
626 /* Verify this is the right file. */
627 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
636 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
637 FDH_REALLYCLOSE(fdP);
643 /* If it's the link table itself, decrement the link count. */
644 if (type == VI_LINKTABLE) {
645 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
646 FDH_REALLYCLOSE(fdP);
652 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
654 FDH_REALLYCLOSE(fdP);
660 FDH_REALLYCLOSE(fdP);
666 namei_HandleToName(&name, tmp);
667 if ((code = unlink(name.n_path)) == 0) {
668 if (type == VI_LINKTABLE) {
669 /* Try to remove directory. If it fails, that's ok.
670 * Salvage will clean up.
672 (void)namei_RemoveDataDirectories(&name);
675 FDH_REALLYCLOSE(fdP);
678 /* Get a file descriptor handle for this Inode */
684 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
685 FDH_REALLYCLOSE(fdP);
691 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
692 FDH_REALLYCLOSE(fdP);
698 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
700 /* This triggers in the fileserver on the volume index vnodes */
701 if (th->ih_refcnt > 1)
702 Log("Warning: Leaked ref on ihandle dev %d vid %d ino %lld\n",
703 th->ih_dev, th->ih_vid, (int64_t) th->ih_ino);
705 namei_HandleToName(&name, th);
707 code = unlink(name.n_path);
716 namei_inc(IHandle_t * h, Inode ino, int p1)
722 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
723 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
724 if (type != VI_LINKTABLE)
729 /* Get a file descriptor handle for this Inode */
735 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
744 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
748 FDH_REALLYCLOSE(fdP);
755 /************************************************************************
756 * File Name Structure
757 ************************************************************************
759 * Each AFS file needs a unique name and it needs to be findable with
760 * minimal lookup time. Note that the constraint on the number of files and
761 * directories in a volume is the size of the vnode index files and the
762 * max file size AFS supports (for internal files) of 2^31. Since a record
763 * in the small vnode index file is 64 bytes long, we can have at most
764 * (2^31)/64 or 33554432 files. A record in the large index file is
765 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
766 * Another layout parameter is that there is roughly a 16 to 1 ratio between
767 * the number of files and the number of directories.
769 * Using this information we can see that a layout of 256 directories, each
770 * with 512 subdirectories and each of those having 512 files gives us
771 * 256*512*512 = 67108864 AFS files and directories.
773 * The volume, vnode, uniquifier and data version, as well as the tag
774 * are required, either for finding the file or for salvaging. It's best to
775 * restrict the name to something that can be mapped into 64 bits so the
776 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
777 * is used to distinguish between different versions of the same file
778 * which are currently in the RW and clones of a volume. See "Link Table
779 * Organization" below for more information on the tag. The tag is
780 * required in the name of the file to ensure a unique name.
782 * We can store data in the uid, gid and mode bits of the files, provided
783 * the directories have root only access. This gives us 15 bits for each
784 * of uid and gid (GNU chown considers 65535 to mean "don't change").
785 * There are 9 available mode bits. Adn we need to store a total of
786 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
787 * or 131 bits somewhere.
789 * The format of a file name for a regular file is:
790 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
791 * V1 - low 8 bits of RW volume id
792 * V2 - all bits of RW volume id
793 * AA - high 8 bits of vnode number.
794 * BB - next 9 bits of vnode number.
795 * <tag><uniq><vno> - file name
797 * Volume special files are stored in a separate directory:
798 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
801 * The vnode is hashed into the directory using the high bits of the
802 * vnode number. This is so that consecutively created vnodes are in
803 * roughly the same area on the disk. This will at least be optimal if
804 * the user is creating many files in the same AFS directory. The name
805 * should be formed so that the leading characters are different as quickly
806 * as possible, leading to faster discards of incorrect matches in the
812 /************************************************************************
813 * Link Table Organization
814 ************************************************************************
816 * The link table volume special file is used to hold the link counts that
817 * are held in the inodes in inode based AFS vice filesystems. For user
818 * space access, the link counts are being kept in a separate
819 * volume special file. The file begins with the usual version stamp
820 * information and is then followed by one row per vnode number. vnode 0
821 * is used to hold the link count of the link table itself. That is because
822 * the same link table is shared among all the volumes of the volume group
823 * and is deleted only when the last volume of a volume group is deleted.
825 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
826 * link count from 0 through 7. Each colume represents a unique instance of
827 * that vnode. Say we have a file shared between the RW and a RO and a
828 * different version of the file (or a different uniquifer) for the BU volume.
829 * Then one column would be holding the link count of 2 for the RW and RO
830 * and a different column would hold the link count of 1 for the BU volume.
831 * Note that we allow only 5 volumes per file, giving 15 bits used in the
834 #define LINKTABLE_WIDTH 2
835 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
838 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
840 int toff = (int)(ino & NAMEI_VNODEMASK);
841 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
843 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
844 *index = (tindex << 1) + tindex;
848 /* namei_GetLinkCount
849 * If lockit is set, lock the file and leave it locked upon a successful
853 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
855 unsigned short row = 0;
859 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
862 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
863 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
865 if (flock(h->fd_fd, LOCK_EX) < 0)
870 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
871 goto bad_getLinkByte;
873 if (read(h->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
874 goto bad_getLinkByte;
877 return (int)((row >> index) & NAMEI_TAGMASK);
881 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
882 lockf(h->fd_fd, F_ULOCK, 0);
884 flock(h->fd_fd, LOCK_UN);
889 /* Return a free column index for this vnode. */
891 GetFreeTag(IHandle_t * ih, int vno)
905 /* Only one manipulates at a time. */
906 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
907 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
909 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
911 FDH_REALLYCLOSE(fdP);
915 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
916 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
920 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
921 if (code != sizeof(row)) {
927 /* Now find a free column in this row and claim it. */
928 for (col = 0; col < NAMEI_MAXVOLS; col++) {
929 coldata = 7 << (col * 3);
930 if ((row & coldata) == 0)
933 if (col >= NAMEI_MAXVOLS)
936 coldata = 1 << (col * 3);
939 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
942 if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
946 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
947 lockf(fdP->fd_fd, F_ULOCK, 0);
949 flock(fdP->fd_fd, LOCK_UN);
951 FDH_REALLYCLOSE(fdP);
955 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
956 lockf(fdP->fd_fd, F_ULOCK, 0);
958 flock(fdP->fd_fd, LOCK_UN);
960 FDH_REALLYCLOSE(fdP);
966 /* namei_SetLinkCount
967 * If locked is set, assume file is locked. Otherwise, lock file before
968 * proceeding to modify it.
971 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
979 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
982 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
983 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
985 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
990 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
992 goto bad_SetLinkCount;
996 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
997 if (code != sizeof(row)) {
1000 goto bad_SetLinkCount;
1007 row &= (unsigned short)~junk;
1008 row |= (unsigned short)count;
1010 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1012 goto bad_SetLinkCount;
1015 if (write(fdP->fd_fd, (char *)&row, sizeof(short)) != sizeof(short)) {
1017 goto bad_SetLinkCount;
1025 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
1026 lockf(fdP->fd_fd, F_ULOCK, 0);
1028 flock(fdP->fd_fd, LOCK_UN);
1035 /* ListViceInodes - write inode data to a results file. */
1036 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1038 static int DecodeVolumeName(char *name, int *vid);
1039 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1040 int (*write_fun) (FILE *,
1041 struct ViceInodeInfo *,
1042 char *, char *), FILE * fp,
1043 int (*judgeFun) (struct ViceInodeInfo *,
1045 int singleVolumeNumber, void *rock);
1050 * Write the inode data to the results file.
1052 * Returns -2 on error, 0 on success.
1054 * This is written as a callback simply so that other listing routines
1055 * can use the same inode reading code.
1058 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1061 n = fwrite(info, sizeof(*info), 1, fp);
1062 return (n == 1) ? 0 : -2;
1066 int mode_errors; /* Number of errors found in mode bits on directories. */
1068 VerifyDirPerms(char *path)
1070 struct afs_stat status;
1072 if (afs_stat(path, &status) < 0) {
1073 Log("Unable to stat %s. Please manually verify mode bits for this"
1074 " directory\n", path);
1076 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1082 * Fill the results file with the requested inode information.
1086 * -1 - complete failure, salvage should terminate.
1087 * -2 - not enough space on partition, salvager has error message for this.
1089 * This code optimizes single volume salvages by just looking at that one
1090 * volume's directory.
1092 * If the resultFile is NULL, then don't call the write routine.
1095 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1096 int (*judgeInode) (struct ViceInodeInfo * info, int vid, void *rock),
1097 int singleVolumeNumber, int *forcep, int forceR, char *wpath,
1100 FILE *fp = (FILE *) - 1;
1102 struct afs_stat status;
1105 fp = afs_fopen(resultFile, "w");
1107 Log("Unable to create inode description file %s\n", resultFile);
1112 /* Verify protections on directories. */
1114 VerifyDirPerms(mountedOn);
1117 namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp, judgeInode,
1118 singleVolumeNumber, rock);
1128 if (fflush(fp) == EOF) {
1129 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1133 if (fsync(fileno(fp)) == -1) {
1134 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1138 if (fclose(fp) == EOF) {
1139 Log("Unable to successfully close inode file for %s\n", mountedOn);
1144 * Paranoia: check that the file is really the right size
1146 if (afs_stat(resultFile, &status) == -1) {
1147 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1150 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1151 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1152 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1160 /* namei_ListAFSFiles
1162 * Collect all the matching AFS files on the drive.
1163 * If singleVolumeNumber is non-zero, just return files for that volume.
1165 * Returns <0 on error, else number of files found to match.
1168 namei_ListAFSFiles(char *dev,
1169 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1171 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1172 int singleVolumeNumber, void *rock)
1178 struct dirent *dp1, *dp2;
1181 static void FreeZLCList(void);
1184 memset((void *)&ih, 0, sizeof(IHandle_t));
1185 ih.ih_dev = volutil_GetPartitionID(dev);
1187 if (singleVolumeNumber) {
1188 ih.ih_vid = singleVolumeNumber;
1189 namei_HandleToVolDir(&name, &ih);
1191 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1192 singleVolumeNumber, rock);
1196 /* Find all volume data directories and descend through them. */
1197 namei_HandleToInodeDir(&name, &ih);
1199 dirp1 = opendir(name.n_path);
1202 while ((dp1 = readdir(dirp1))) {
1203 if (*dp1->d_name == '.')
1205 (void)strcpy(path2, name.n_path);
1206 (void)strcat(path2, "/");
1207 (void)strcat(path2, dp1->d_name);
1208 dirp2 = opendir(path2);
1210 while ((dp2 = readdir(dirp2))) {
1211 if (*dp2->d_name == '.')
1213 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1215 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1232 /* namei_ListAFSSubDirs
1237 * > = 0 - number of AFS files found.
1240 namei_ListAFSSubDirs(IHandle_t * dirIH,
1241 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1243 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1244 int singleVolumeNumber, void *rock)
1246 IHandle_t myIH = *dirIH;
1248 char path1[512], path2[512], path3[512];
1249 DIR *dirp1, *dirp2, *dirp3;
1250 struct dirent *dp1, *dp2, *dp3;
1251 struct ViceInodeInfo info;
1252 FdHandle_t linkHandle;
1256 static void AddToZLCDeleteList(char dir, char *name);
1257 static void DeleteZLCFiles(char *path);
1260 namei_HandleToVolDir(&name, &myIH);
1261 (void)strcpy(path1, name.n_path);
1263 /* Do the directory containing the special files first to pick up link
1266 (void)strcat(path1, "/");
1267 (void)strcat(path1, NAMEI_SPECDIR);
1269 linkHandle.fd_fd = -1;
1270 dirp1 = opendir(path1);
1272 while ((dp1 = readdir(dirp1))) {
1273 if (*dp1->d_name == '.')
1275 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
1277 if (info.u.param[2] != VI_LINKTABLE) {
1280 /* Open this handle */
1281 (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
1283 linkHandle.fd_fd = afs_open(path2, O_RDONLY, 0666);
1285 namei_GetLinkCount(&linkHandle, (Inode) 0, 0);
1287 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1290 if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
1291 if (linkHandle.fd_fd >= 0)
1292 close(linkHandle.fd_fd);
1301 /* Now run through all the other subdirs */
1302 namei_HandleToVolDir(&name, &myIH);
1303 (void)strcpy(path1, name.n_path);
1305 dirp1 = opendir(path1);
1307 while ((dp1 = readdir(dirp1))) {
1308 if (*dp1->d_name == '.')
1310 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1313 /* Now we've got a next level subdir. */
1314 (void)strcpy(path2, path1);
1315 (void)strcat(path2, "/");
1316 (void)strcat(path2, dp1->d_name);
1317 dirp2 = opendir(path2);
1319 while ((dp2 = readdir(dirp2))) {
1320 if (*dp2->d_name == '.')
1323 /* Now we've got to the actual data */
1324 (void)strcpy(path3, path2);
1325 (void)strcat(path3, "/");
1326 (void)strcat(path3, dp2->d_name);
1327 dirp3 = opendir(path3);
1329 while ((dp3 = readdir(dirp3))) {
1330 if (*dp3->d_name == '.')
1333 (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
1336 namei_GetLinkCount(&linkHandle,
1337 info.inodeNumber, 0);
1338 if (info.linkCount == 0) {
1340 Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
1341 AddToZLCDeleteList((char)i, dp3->d_name);
1343 Log("Found 0 link count file %s/%s.\n", path3,
1349 && !(*judgeFun) (&info, singleVolumeNumber, rock))
1352 if ((*writeFun) (fp, &info, path3, dp3->d_name) <
1354 close(linkHandle.fd_fd);
1371 if (linkHandle.fd_fd >= 0)
1372 close(linkHandle.fd_fd);
1374 /* Then why does this directory exist? Blow it away. */
1375 namei_HandleToVolDir(&name, dirIH);
1376 namei_RemoveDataDirectories(&name);
1383 DecodeVolumeName(char *name, int *vid)
1385 if (strlen(name) <= 2)
1387 *vid = (int)flipbase64_to_int64(name);
1394 * Get the inode number from the name.
1398 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
1401 struct afs_stat status;
1404 (void)strcpy(fpath, dpath);
1405 (void)strcat(fpath, "/");
1406 (void)strcat(fpath, name);
1408 if (afs_stat(fpath, &status) < 0) {
1412 info->byteCount = status.st_size;
1413 info->inodeNumber = (Inode) flipbase64_to_int64(name);
1415 GetOGMFromStat(&status, &parm, &tag);
1416 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1417 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1418 info->u.param[0] = parm;
1419 info->u.param[1] = -1;
1420 info->u.param[2] = tag;
1421 info->u.param[3] = volid;
1423 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1424 info->u.param[0] = volid;
1425 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1426 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1427 & (Inode) NAMEI_UNIQMASK);
1428 info->u.param[3] = parm;
1434 * Convert the VolumeInfo file from RO to RW
1435 * this routine is called by namei_convertROtoRWvolume()
1439 convertVolumeInfo(fdr, fdw, vid)
1444 struct VolumeDiskData vd;
1447 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1448 sizeof(struct VolumeDiskData)) {
1449 Log("1 convertVolumeInfo: read failed for %lu with code %d\n", vid,
1453 vd.restoredFromId = vd.id; /* remember the RO volume here */
1455 vd.id = vd.parentId;
1458 vd.uniquifier += 5000; /* just in case there are still file copies from
1459 * the old RW volume around */
1460 p = strrchr(vd.name, '.');
1461 if (p && !strcmp(p, ".readonly")) {
1464 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1465 sizeof(struct VolumeDiskData)) {
1466 Log("1 convertVolumeInfo: write failed for %lu with code %d\n", vid,
1474 * Convert a RO-volume into a RW-volume
1476 * This function allows to recover very fast from the loss of a partition
1477 * from RO-copies if all RO-Copies exist on another partition.
1478 * Then these RO-volumes can be made to the new RW-volumes.
1479 * Backup of RW-volumes then consists in "vos release".
1481 * We must make sure in this partition exists only the RO-volume which
1482 * is typical for remote replicas.
1484 * Then the linktable is already ok,
1485 * the vnode files need to be renamed
1486 * the volinfo file needs to be replaced by another one with
1487 * slightly different contents and new name.
1488 * The volume header file of the RO-volume in the /vicep<x> directory
1489 * is destroyed by this call. A new header file for the RW-volume must
1490 * be created after return from this routine.
1494 namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
1497 char dir_name[512], oldpath[512], newpath[512];
1510 struct ViceInodeInfo info;
1512 namei_HandleToName(&n, h);
1513 strcpy(dir_name, n.n_path);
1514 p = strrchr(dir_name, '/');
1516 dirp = opendir(dir_name);
1518 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
1522 while ((dp = readdir(dirp))) {
1523 /* struct ViceInodeInfo info; */
1525 if (*dp->d_name == '.')
1527 if (DecodeInode(dir_name, dp->d_name, &info, h->ih_vid) < 0) {
1528 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1529 dir_name, dp->d_name);
1533 if (info.u.param[1] != -1) {
1534 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
1538 if (info.u.param[0] != vid) {
1539 if (info.u.param[0] == h->ih_vid) {
1540 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1545 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n", dir_name, dp->d_name, info.u.param[0]);
1549 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1550 strcpy(infoName, dp->d_name);
1552 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1553 strcpy(smallName, dp->d_name);
1555 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1556 strcpy(largeName, dp->d_name);
1560 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
1566 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1567 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
1572 * If we come here then there was only a RO-volume and we can safely
1576 bzero(&t_ih, sizeof(t_ih));
1577 t_ih.ih_dev = h->ih_dev;
1578 t_ih.ih_vid = h->ih_vid;
1580 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
1581 fd = afs_open(oldpath, O_RDWR, 0);
1583 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1587 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_VOLINFO);
1588 namei_HandleToName(&n, &t_ih);
1589 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1591 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
1595 code = convertVolumeInfo(fd, fd2, h->ih_vid);
1602 SetOGM(fd2, h->ih_vid, 1);
1605 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_SMALLINDEX);
1606 namei_HandleToName(&n, &t_ih);
1607 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
1608 fd = afs_open(newpath, O_RDWR, 0);
1610 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
1613 SetOGM(fd, h->ih_vid, 2);
1615 link(newpath, n.n_path);
1618 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_LARGEINDEX);
1619 namei_HandleToName(&n, &t_ih);
1620 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
1621 fd = afs_open(newpath, O_RDWR, 0);
1623 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
1626 SetOGM(fd, h->ih_vid, 3);
1628 link(newpath, n.n_path);
1637 * returns a static string used to print either 32 or 64 bit inode numbers.
1640 PrintInode(char *s, Inode ino)
1642 static afs_ino_str_t result;
1646 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
1653 /* Routines to facilitate removing zero link count files. */
1654 #define MAX_ZLC_NAMES 32
1655 #define MAX_ZLC_NAMELEN 16
1656 typedef struct zlcList_s {
1657 struct zlcList_s *zlc_next;
1659 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1662 static zlcList_t *zlcAnchor = NULL;
1663 static zlcList_t *zlcCur = NULL;
1666 AddToZLCDeleteList(char dir, char *name)
1668 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1670 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1671 if (zlcCur && zlcCur->zlc_next)
1672 zlcCur = zlcCur->zlc_next;
1674 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1680 zlcCur->zlc_next = tmp;
1684 zlcCur->zlc_next = NULL;
1688 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1693 DeleteZLCFiles(char *path)
1699 for (z = zlcAnchor; z; z = z->zlc_next) {
1700 for (i = 0; i < z->zlc_n; i++) {
1701 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1702 if (namei_unlink(fname) < 0) {
1703 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1707 z->zlc_n = 0; /* Can reuse space. */
1720 tnext = i->zlc_next;
1724 zlcCur = zlcAnchor = NULL;
1728 #endif /* AFS_NAMEI_ENV */