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>
33 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
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\
233 ownership and permissions included.\n"
235 namei_ViceREADME(char *partition)
240 /* Create the inode directory if we're starting for the first time */
241 (void)afs_snprintf(filename, sizeof filename, "%s/%s", partition,
243 mkdir(filename, 0700);
245 (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
247 fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
249 (void)write(fd, VICE_README, strlen(VICE_README));
256 #define create_dir() \
258 if (mkdir(tmp, 0700)<0) { \
259 if (errno != EEXIST) \
267 #define create_nextdir(A) \
269 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
272 /* namei_CreateDataDirectories
274 * If creating the file failed because of ENOENT or ENOTDIR, try
275 * creating all the directories first.
278 namei_CreateDataDirectories(namei_t * name, int *created)
284 (void)strcpy(tmp, name->n_base);
287 create_nextdir(name->n_voldir1);
288 create_nextdir(name->n_voldir2);
289 create_nextdir(name->n_dir1);
290 if (name->n_dir2[0]) {
291 create_nextdir(name->n_dir2);
296 /* delTree(): Deletes an entire tree of directories (no files)
298 * root : Full path to the subtree. Should be big enough for PATH_MAX
299 * tree : the subtree to be deleted is rooted here. Specifies only the
300 * subtree beginning at tree (not the entire path). It should be
301 * a pointer into the "root" buffer.
303 * errp : errno of the first error encountered during the directory cleanup.
304 * *errp should have been initialized to 0.
307 * -1 : If errors were encountered during cleanup and error is set to
311 * If there are errors, we try to work around them and delete as many
312 * directories as possible. We don't attempt to remove directories that still
313 * have non-dir entries in them.
316 delTree(char *root, char *tree, int *errp)
324 /* delete the children first */
325 cp = strchr(tree, '/');
327 delTree(root, cp + 1, errp);
330 cp = tree + strlen(tree); /* move cp to the end of string tree */
332 /* now delete all entries in this dir */
333 if ((ds = opendir(root)) != (DIR *) NULL) {
335 while ((dirp = readdir(ds))) {
336 /* ignore . and .. */
337 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
339 /* since root is big enough, we reuse the space to
340 * concatenate the dirname to the current tree
343 strcat(root, dirp->d_name);
344 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
345 /* delete this subtree */
346 delTree(root, cp + 1, errp);
348 *errp = *errp ? *errp : errno;
350 /* recover path to our cur tree by truncating it to
355 /* if (!errno) -- closedir not implicit if we got an error */
359 /* finally axe the current dir */
361 *errp = *errp ? *errp : errno;
363 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
365 #endif /* !AFS_PTHREAD_ENV */
370 /* if we encountered errors during cleanup, we return a -1 */
378 /* namei_RemoveDataDirectories
380 * Returns 0 on success.
381 * Returns -1 on error. Typically, callers ignore this error bcause we
382 * can continue running if the removes fail. The salvage process will
383 * finish tidying up for us. We only use the n_base and n_voldir1 entries
384 * and only do rmdir's.
388 namei_RemoveDataDirectories(namei_t * name)
390 char pbuf[MAXPATHLEN], *path = pbuf;
391 int prefixlen = strlen(name->n_base), err = 0;
393 strcpy(path, name->n_path);
395 /* move past the prefix */
396 path = path + prefixlen + 1; /* skip over the trailing / */
398 /* now delete all dirs upto path */
399 return delTree(pbuf, path, &err);
403 /* Create the file in the name space.
405 * Parameters stored as follows:
407 * p1 - volid - implied in containing directory.
408 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
409 * p3 - uniq -- bits 4-0 are in mode bits 4-0
410 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
412 * p1 - volid - creation time - dwHighDateTime
413 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
414 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
415 * p4 - parid - parent volume id - implied in containing directory.
417 * Return value is the inode number or (Inode)-1 if error.
418 * We "know" there is only one link table, so return EEXIST if there already
419 * is a link table. It's up to the calling code to test errno and increment
425 * This function is called by VCreateVolume to hide the implementation
426 * details of the inode numbers. This only allows for 7 volume special
427 * types, but if we get that far, this could should be dead by then.
430 namei_MakeSpecIno(int volid, int type)
433 ino = NAMEI_INODESPECIAL;
434 type &= NAMEI_TAGMASK;
435 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
436 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
440 /* SetOGM - set owner group and mode bits from parm and tag
442 * owner - low 15 bits of parm.
443 * group - next 15 bits of parm.
444 * mode - 2 bits of parm, then lowest = 3 bits of tag.
447 SetOGM(int fd, int parm, int tag)
449 int owner, group, mode;
451 owner = parm & 0x7fff;
452 group = (parm >> 15) & 0x7fff;
453 if (fchown(fd, owner, group) < 0)
456 mode = (parm >> 27) & 0x18;
458 if (fchmod(fd, mode) < 0)
465 /* GetOGM - get parm and tag from owner, group and mode bits. */
467 GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
469 *parm = status->st_uid | (status->st_gid << 15);
470 *parm |= (status->st_mode & 0x18) << 27;
471 *tag = status->st_mode & 0x7;
475 GetOGM(int fd, int *parm, int *tag)
477 struct afs_stat status;
478 if (afs_fstat(fd, &status) < 0)
481 GetOGMFromStat(&status, parm, tag);
485 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
487 /* Derive the name and create it O_EXCL. If that fails we have an error.
488 * Get the tag from a free column in the link table.
491 namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
504 memset((void *)&tmp, 0, sizeof(IHandle_t));
507 tmp.ih_dev = volutil_GetPartitionID(part);
508 if (tmp.ih_dev == -1) {
514 /* Parameters for special file:
515 * p1 - volume id - goes into owner/group/mode
518 * p4 - parent volume id
523 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
524 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
526 int vno = p2 & NAMEI_VNODEMASK;
527 /* Parameters for regular file:
539 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
540 tag = GetFreeTag(lh, p2);
544 /* name is <uniq(p3)><tag><vno(p2)> */
546 tmp.ih_ino = (Inode) p2;
547 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
548 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
553 namei_HandleToName(&name, &tmp);
554 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
556 if (errno == ENOTDIR || errno == ENOENT) {
557 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
559 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
567 if (SetOGM(fd, ogm_parm, tag) < 0) {
573 if (p2 == -1 && p3 == VI_LINKTABLE) {
574 /* hack at tmp to setup for set link count call. */
575 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
578 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
586 if (code || (fd < 0)) {
590 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
595 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
601 namei_iopen(IHandle_t * h)
606 /* Convert handle to file name. */
607 namei_HandleToName(&name, h);
608 fd = afs_open(name.n_path, O_RDWR, 0666);
612 /* Need to detect vol special file and just unlink. In those cases, the
613 * handle passed in _is_ for the inode. We only check p1 for the special
617 namei_dec(IHandle_t * ih, Inode ino, int p1)
624 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
627 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
629 /* Verify this is the right file. */
630 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
639 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
640 FDH_REALLYCLOSE(fdP);
646 /* If it's the link table itself, decrement the link count. */
647 if (type == VI_LINKTABLE) {
648 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
649 FDH_REALLYCLOSE(fdP);
655 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
657 FDH_REALLYCLOSE(fdP);
663 FDH_REALLYCLOSE(fdP);
669 namei_HandleToName(&name, tmp);
670 if ((code = unlink(name.n_path)) == 0) {
671 if (type == VI_LINKTABLE) {
672 /* Try to remove directory. If it fails, that's ok.
673 * Salvage will clean up.
675 (void)namei_RemoveDataDirectories(&name);
678 FDH_REALLYCLOSE(fdP);
681 /* Get a file descriptor handle for this Inode */
687 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
688 FDH_REALLYCLOSE(fdP);
694 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
695 FDH_REALLYCLOSE(fdP);
700 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
701 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
702 th->ih_dev, th->ih_vid, (int64_t) th->ih_ino);
705 /* If we're less than 0, someone presumably unlinked;
706 don't bother setting count to 0, but we need to drop a lock */
707 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
708 FDH_REALLYCLOSE(fdP);
714 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
716 namei_HandleToName(&name, th);
718 code = unlink(name.n_path);
727 namei_inc(IHandle_t * h, Inode ino, int p1)
733 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
734 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
735 if (type != VI_LINKTABLE)
740 /* Get a file descriptor handle for this Inode */
746 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
755 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
759 FDH_REALLYCLOSE(fdP);
766 /************************************************************************
767 * File Name Structure
768 ************************************************************************
770 * Each AFS file needs a unique name and it needs to be findable with
771 * minimal lookup time. Note that the constraint on the number of files and
772 * directories in a volume is the size of the vnode index files and the
773 * max file size AFS supports (for internal files) of 2^31. Since a record
774 * in the small vnode index file is 64 bytes long, we can have at most
775 * (2^31)/64 or 33554432 files. A record in the large index file is
776 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
777 * Another layout parameter is that there is roughly a 16 to 1 ratio between
778 * the number of files and the number of directories.
780 * Using this information we can see that a layout of 256 directories, each
781 * with 512 subdirectories and each of those having 512 files gives us
782 * 256*512*512 = 67108864 AFS files and directories.
784 * The volume, vnode, uniquifier and data version, as well as the tag
785 * are required, either for finding the file or for salvaging. It's best to
786 * restrict the name to something that can be mapped into 64 bits so the
787 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
788 * is used to distinguish between different versions of the same file
789 * which are currently in the RW and clones of a volume. See "Link Table
790 * Organization" below for more information on the tag. The tag is
791 * required in the name of the file to ensure a unique name.
793 * We can store data in the uid, gid and mode bits of the files, provided
794 * the directories have root only access. This gives us 15 bits for each
795 * of uid and gid (GNU chown considers 65535 to mean "don't change").
796 * There are 9 available mode bits. Adn we need to store a total of
797 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
798 * or 131 bits somewhere.
800 * The format of a file name for a regular file is:
801 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
802 * V1 - low 8 bits of RW volume id
803 * V2 - all bits of RW volume id
804 * AA - high 8 bits of vnode number.
805 * BB - next 9 bits of vnode number.
806 * <tag><uniq><vno> - file name
808 * Volume special files are stored in a separate directory:
809 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
812 * The vnode is hashed into the directory using the high bits of the
813 * vnode number. This is so that consecutively created vnodes are in
814 * roughly the same area on the disk. This will at least be optimal if
815 * the user is creating many files in the same AFS directory. The name
816 * should be formed so that the leading characters are different as quickly
817 * as possible, leading to faster discards of incorrect matches in the
823 /************************************************************************
824 * Link Table Organization
825 ************************************************************************
827 * The link table volume special file is used to hold the link counts that
828 * are held in the inodes in inode based AFS vice filesystems. For user
829 * space access, the link counts are being kept in a separate
830 * volume special file. The file begins with the usual version stamp
831 * information and is then followed by one row per vnode number. vnode 0
832 * is used to hold the link count of the link table itself. That is because
833 * the same link table is shared among all the volumes of the volume group
834 * and is deleted only when the last volume of a volume group is deleted.
836 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
837 * link count from 0 through 7. Each colume represents a unique instance of
838 * that vnode. Say we have a file shared between the RW and a RO and a
839 * different version of the file (or a different uniquifer) for the BU volume.
840 * Then one column would be holding the link count of 2 for the RW and RO
841 * and a different column would hold the link count of 1 for the BU volume.
842 * Note that we allow only 5 volumes per file, giving 15 bits used in the
845 #define LINKTABLE_WIDTH 2
846 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
849 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
851 int toff = (int)(ino & NAMEI_VNODEMASK);
852 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
854 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
855 *index = (tindex << 1) + tindex;
859 /* namei_GetLinkCount
860 * If lockit is set, lock the file and leave it locked upon a successful
864 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
866 unsigned short row = 0;
870 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
873 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
874 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
876 if (flock(h->fd_fd, LOCK_EX) < 0)
881 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
882 goto bad_getLinkByte;
884 if (read(h->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
885 goto bad_getLinkByte;
888 return (int)((row >> index) & NAMEI_TAGMASK);
892 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
893 lockf(h->fd_fd, F_ULOCK, 0);
895 flock(h->fd_fd, LOCK_UN);
900 /* Return a free column index for this vnode. */
902 GetFreeTag(IHandle_t * ih, int vno)
916 /* Only one manipulates at a time. */
917 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
918 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
920 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
922 FDH_REALLYCLOSE(fdP);
926 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
927 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
931 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
932 if (code != sizeof(row)) {
938 /* Now find a free column in this row and claim it. */
939 for (col = 0; col < NAMEI_MAXVOLS; col++) {
940 coldata = 7 << (col * 3);
941 if ((row & coldata) == 0)
944 if (col >= NAMEI_MAXVOLS)
947 coldata = 1 << (col * 3);
950 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
953 if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
957 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
958 lockf(fdP->fd_fd, F_ULOCK, 0);
960 flock(fdP->fd_fd, LOCK_UN);
962 FDH_REALLYCLOSE(fdP);
966 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
967 lockf(fdP->fd_fd, F_ULOCK, 0);
969 flock(fdP->fd_fd, LOCK_UN);
971 FDH_REALLYCLOSE(fdP);
977 /* namei_SetLinkCount
978 * If locked is set, assume file is locked. Otherwise, lock file before
979 * proceeding to modify it.
982 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
990 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
993 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
994 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
996 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
1001 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1003 goto bad_SetLinkCount;
1007 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
1008 if (code != sizeof(row)) {
1011 goto bad_SetLinkCount;
1018 row &= (unsigned short)~junk;
1019 row |= (unsigned short)count;
1021 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1023 goto bad_SetLinkCount;
1026 if (write(fdP->fd_fd, (char *)&row, sizeof(short)) != sizeof(short)) {
1028 goto bad_SetLinkCount;
1036 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
1037 lockf(fdP->fd_fd, F_ULOCK, 0);
1039 flock(fdP->fd_fd, LOCK_UN);
1046 /* ListViceInodes - write inode data to a results file. */
1047 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1049 static int DecodeVolumeName(char *name, int *vid);
1050 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1051 int (*write_fun) (FILE *,
1052 struct ViceInodeInfo *,
1053 char *, char *), FILE * fp,
1054 int (*judgeFun) (struct ViceInodeInfo *,
1056 int singleVolumeNumber, void *rock);
1061 * Write the inode data to the results file.
1063 * Returns -2 on error, 0 on success.
1065 * This is written as a callback simply so that other listing routines
1066 * can use the same inode reading code.
1069 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1072 n = fwrite(info, sizeof(*info), 1, fp);
1073 return (n == 1) ? 0 : -2;
1077 int mode_errors; /* Number of errors found in mode bits on directories. */
1079 VerifyDirPerms(char *path)
1081 struct afs_stat status;
1083 if (afs_stat(path, &status) < 0) {
1084 Log("Unable to stat %s. Please manually verify mode bits for this"
1085 " directory\n", path);
1087 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1093 * Fill the results file with the requested inode information.
1097 * -1 - complete failure, salvage should terminate.
1098 * -2 - not enough space on partition, salvager has error message for this.
1100 * This code optimizes single volume salvages by just looking at that one
1101 * volume's directory.
1103 * If the resultFile is NULL, then don't call the write routine.
1106 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1107 int (*judgeInode) (struct ViceInodeInfo * info, int vid, void *rock),
1108 int singleVolumeNumber, int *forcep, int forceR, char *wpath,
1111 FILE *fp = (FILE *) - 1;
1113 struct afs_stat status;
1116 fp = afs_fopen(resultFile, "w");
1118 Log("Unable to create inode description file %s\n", resultFile);
1123 /* Verify protections on directories. */
1125 VerifyDirPerms(mountedOn);
1128 namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp, judgeInode,
1129 singleVolumeNumber, rock);
1139 if (fflush(fp) == EOF) {
1140 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1144 if (fsync(fileno(fp)) == -1) {
1145 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1149 if (fclose(fp) == EOF) {
1150 Log("Unable to successfully close inode file for %s\n", mountedOn);
1155 * Paranoia: check that the file is really the right size
1157 if (afs_stat(resultFile, &status) == -1) {
1158 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1161 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1162 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1163 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1171 /* namei_ListAFSFiles
1173 * Collect all the matching AFS files on the drive.
1174 * If singleVolumeNumber is non-zero, just return files for that volume.
1176 * Returns <0 on error, else number of files found to match.
1179 namei_ListAFSFiles(char *dev,
1180 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1182 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1183 int singleVolumeNumber, void *rock)
1189 struct dirent *dp1, *dp2;
1192 static void FreeZLCList(void);
1195 memset((void *)&ih, 0, sizeof(IHandle_t));
1196 ih.ih_dev = volutil_GetPartitionID(dev);
1198 if (singleVolumeNumber) {
1199 ih.ih_vid = singleVolumeNumber;
1200 namei_HandleToVolDir(&name, &ih);
1202 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1203 singleVolumeNumber, rock);
1207 /* Find all volume data directories and descend through them. */
1208 namei_HandleToInodeDir(&name, &ih);
1210 dirp1 = opendir(name.n_path);
1213 while ((dp1 = readdir(dirp1))) {
1214 if (*dp1->d_name == '.')
1216 (void)strcpy(path2, name.n_path);
1217 (void)strcat(path2, "/");
1218 (void)strcat(path2, dp1->d_name);
1219 dirp2 = opendir(path2);
1221 while ((dp2 = readdir(dirp2))) {
1222 if (*dp2->d_name == '.')
1224 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1226 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1243 /* namei_ListAFSSubDirs
1248 * > = 0 - number of AFS files found.
1251 namei_ListAFSSubDirs(IHandle_t * dirIH,
1252 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1254 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1255 int singleVolumeNumber, void *rock)
1257 IHandle_t myIH = *dirIH;
1259 char path1[512], path2[512], path3[512];
1260 DIR *dirp1, *dirp2, *dirp3;
1261 struct dirent *dp1, *dp2, *dp3;
1262 struct ViceInodeInfo info;
1263 FdHandle_t linkHandle;
1267 static void AddToZLCDeleteList(char dir, char *name);
1268 static void DeleteZLCFiles(char *path);
1271 namei_HandleToVolDir(&name, &myIH);
1272 (void)strcpy(path1, name.n_path);
1274 /* Do the directory containing the special files first to pick up link
1277 (void)strcat(path1, "/");
1278 (void)strcat(path1, NAMEI_SPECDIR);
1280 linkHandle.fd_fd = -1;
1281 dirp1 = opendir(path1);
1283 while ((dp1 = readdir(dirp1))) {
1284 if (*dp1->d_name == '.')
1286 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
1288 if (info.u.param[2] != VI_LINKTABLE) {
1291 /* Open this handle */
1292 (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
1294 linkHandle.fd_fd = afs_open(path2, O_RDONLY, 0666);
1296 namei_GetLinkCount(&linkHandle, (Inode) 0, 0);
1298 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1301 if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
1302 if (linkHandle.fd_fd >= 0)
1303 close(linkHandle.fd_fd);
1312 /* Now run through all the other subdirs */
1313 namei_HandleToVolDir(&name, &myIH);
1314 (void)strcpy(path1, name.n_path);
1316 dirp1 = opendir(path1);
1318 while ((dp1 = readdir(dirp1))) {
1319 if (*dp1->d_name == '.')
1321 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1324 /* Now we've got a next level subdir. */
1325 (void)strcpy(path2, path1);
1326 (void)strcat(path2, "/");
1327 (void)strcat(path2, dp1->d_name);
1328 dirp2 = opendir(path2);
1330 while ((dp2 = readdir(dirp2))) {
1331 if (*dp2->d_name == '.')
1334 /* Now we've got to the actual data */
1335 (void)strcpy(path3, path2);
1336 (void)strcat(path3, "/");
1337 (void)strcat(path3, dp2->d_name);
1338 dirp3 = opendir(path3);
1340 while ((dp3 = readdir(dirp3))) {
1341 if (*dp3->d_name == '.')
1344 (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
1347 namei_GetLinkCount(&linkHandle,
1348 info.inodeNumber, 0);
1349 if (info.linkCount == 0) {
1351 Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
1352 AddToZLCDeleteList((char)i, dp3->d_name);
1354 Log("Found 0 link count file %s/%s.\n", path3,
1360 && !(*judgeFun) (&info, singleVolumeNumber, rock))
1363 if ((*writeFun) (fp, &info, path3, dp3->d_name) <
1365 close(linkHandle.fd_fd);
1382 if (linkHandle.fd_fd >= 0)
1383 close(linkHandle.fd_fd);
1385 /* Then why does this directory exist? Blow it away. */
1386 namei_HandleToVolDir(&name, dirIH);
1387 namei_RemoveDataDirectories(&name);
1394 DecodeVolumeName(char *name, int *vid)
1396 if (strlen(name) <= 2)
1398 *vid = (int)flipbase64_to_int64(name);
1405 * Get the inode number from the name.
1409 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
1412 struct afs_stat status;
1415 (void)strcpy(fpath, dpath);
1416 (void)strcat(fpath, "/");
1417 (void)strcat(fpath, name);
1419 if (afs_stat(fpath, &status) < 0) {
1423 info->byteCount = status.st_size;
1424 info->inodeNumber = (Inode) flipbase64_to_int64(name);
1426 GetOGMFromStat(&status, &parm, &tag);
1427 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1428 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1429 info->u.param[0] = parm;
1430 info->u.param[1] = -1;
1431 info->u.param[2] = tag;
1432 info->u.param[3] = volid;
1434 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1435 info->u.param[0] = volid;
1436 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1437 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1438 & (Inode) NAMEI_UNIQMASK);
1439 info->u.param[3] = parm;
1445 * Convert the VolumeInfo file from RO to RW
1446 * this routine is called by namei_convertROtoRWvolume()
1450 convertVolumeInfo(fdr, fdw, vid)
1455 struct VolumeDiskData vd;
1458 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1459 sizeof(struct VolumeDiskData)) {
1460 Log("1 convertVolumeInfo: read failed for %lu with code %d\n", vid,
1464 vd.restoredFromId = vd.id; /* remember the RO volume here */
1466 vd.id = vd.parentId;
1469 vd.uniquifier += 5000; /* just in case there are still file copies from
1470 * the old RW volume around */
1471 p = strrchr(vd.name, '.');
1472 if (p && !strcmp(p, ".readonly")) {
1475 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1476 sizeof(struct VolumeDiskData)) {
1477 Log("1 convertVolumeInfo: write failed for %lu with code %d\n", vid,
1485 * Convert a RO-volume into a RW-volume
1487 * This function allows to recover very fast from the loss of a partition
1488 * from RO-copies if all RO-Copies exist on another partition.
1489 * Then these RO-volumes can be made to the new RW-volumes.
1490 * Backup of RW-volumes then consists in "vos release".
1492 * We must make sure in this partition exists only the RO-volume which
1493 * is typical for remote replicas.
1495 * Then the linktable is already ok,
1496 * the vnode files need to be renamed
1497 * the volinfo file needs to be replaced by another one with
1498 * slightly different contents and new name.
1499 * The volume header file of the RO-volume in the /vicep<x> directory
1500 * is destroyed by this call. A new header file for the RW-volume must
1501 * be created after return from this routine.
1505 namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
1508 char dir_name[512], oldpath[512], newpath[512];
1521 struct ViceInodeInfo info;
1523 namei_HandleToName(&n, h);
1524 strcpy(dir_name, n.n_path);
1525 p = strrchr(dir_name, '/');
1527 dirp = opendir(dir_name);
1529 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
1533 while ((dp = readdir(dirp))) {
1534 /* struct ViceInodeInfo info; */
1536 if (*dp->d_name == '.')
1538 if (DecodeInode(dir_name, dp->d_name, &info, h->ih_vid) < 0) {
1539 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1540 dir_name, dp->d_name);
1544 if (info.u.param[1] != -1) {
1545 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
1549 if (info.u.param[0] != vid) {
1550 if (info.u.param[0] == h->ih_vid) {
1551 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1556 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n", dir_name, dp->d_name, info.u.param[0]);
1560 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1561 strcpy(infoName, dp->d_name);
1563 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1564 strcpy(smallName, dp->d_name);
1566 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1567 strcpy(largeName, dp->d_name);
1571 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
1577 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1578 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
1583 * If we come here then there was only a RO-volume and we can safely
1587 bzero(&t_ih, sizeof(t_ih));
1588 t_ih.ih_dev = h->ih_dev;
1589 t_ih.ih_vid = h->ih_vid;
1591 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
1592 fd = afs_open(oldpath, O_RDWR, 0);
1594 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1598 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_VOLINFO);
1599 namei_HandleToName(&n, &t_ih);
1600 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1602 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
1606 code = convertVolumeInfo(fd, fd2, h->ih_vid);
1613 SetOGM(fd2, h->ih_vid, 1);
1616 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_SMALLINDEX);
1617 namei_HandleToName(&n, &t_ih);
1618 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
1619 fd = afs_open(newpath, O_RDWR, 0);
1621 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
1624 SetOGM(fd, h->ih_vid, 2);
1626 link(newpath, n.n_path);
1629 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_LARGEINDEX);
1630 namei_HandleToName(&n, &t_ih);
1631 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
1632 fd = afs_open(newpath, O_RDWR, 0);
1634 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
1637 SetOGM(fd, h->ih_vid, 3);
1639 link(newpath, n.n_path);
1648 * returns a static string used to print either 32 or 64 bit inode numbers.
1651 PrintInode(char *s, Inode ino)
1653 static afs_ino_str_t result;
1657 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
1664 /* Routines to facilitate removing zero link count files. */
1665 #define MAX_ZLC_NAMES 32
1666 #define MAX_ZLC_NAMELEN 16
1667 typedef struct zlcList_s {
1668 struct zlcList_s *zlc_next;
1670 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1673 static zlcList_t *zlcAnchor = NULL;
1674 static zlcList_t *zlcCur = NULL;
1677 AddToZLCDeleteList(char dir, char *name)
1679 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1681 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1682 if (zlcCur && zlcCur->zlc_next)
1683 zlcCur = zlcCur->zlc_next;
1685 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1691 zlcCur->zlc_next = tmp;
1695 zlcCur->zlc_next = NULL;
1699 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1704 DeleteZLCFiles(char *path)
1710 for (z = zlcAnchor; z; z = z->zlc_next) {
1711 for (i = 0; i < z->zlc_n; i++) {
1712 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1713 if (namei_unlink(fname) < 0) {
1714 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1718 z->zlc_n = 0; /* Can reuse space. */
1731 tnext = i->zlc_next;
1735 zlcCur = zlcAnchor = NULL;
1739 #endif /* AFS_NAMEI_ENV */