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. */
576 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
584 if (code || (fd < 0)) {
588 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
593 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
599 namei_iopen(IHandle_t * h)
604 /* Convert handle to file name. */
605 namei_HandleToName(&name, h);
606 fd = afs_open(name.n_path, O_RDWR, 0666);
610 /* Need to detect vol special file and just unlink. In those cases, the
611 * handle passed in _is_ for the inode. We only check p1 for the special
615 namei_dec(IHandle_t * ih, Inode ino, int p1)
622 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
625 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
627 /* Verify this is the right file. */
628 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
637 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
638 FDH_REALLYCLOSE(fdP);
644 /* If it's the link table itself, decrement the link count. */
645 if (type == VI_LINKTABLE) {
646 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
647 FDH_REALLYCLOSE(fdP);
653 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
655 FDH_REALLYCLOSE(fdP);
661 FDH_REALLYCLOSE(fdP);
667 namei_HandleToName(&name, tmp);
668 if ((code = unlink(name.n_path)) == 0) {
669 if (type == VI_LINKTABLE) {
670 /* Try to remove directory. If it fails, that's ok.
671 * Salvage will clean up.
673 (void)namei_RemoveDataDirectories(&name);
676 FDH_REALLYCLOSE(fdP);
679 /* Get a file descriptor handle for this Inode */
685 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
686 FDH_REALLYCLOSE(fdP);
692 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
693 FDH_REALLYCLOSE(fdP);
699 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
701 /* This triggers in the fileserver on the volume index vnodes */
702 if (th->ih_refcnt > 1)
703 Log("Warning: Leaked ref on ihandle dev %d vid %d ino %lld\n",
704 th->ih_dev, th->ih_vid, (int64_t) th->ih_ino);
706 namei_HandleToName(&name, th);
708 code = unlink(name.n_path);
717 namei_inc(IHandle_t * h, Inode ino, int p1)
723 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
724 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
725 if (type != VI_LINKTABLE)
730 /* Get a file descriptor handle for this Inode */
736 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
745 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
749 FDH_REALLYCLOSE(fdP);
756 /************************************************************************
757 * File Name Structure
758 ************************************************************************
760 * Each AFS file needs a unique name and it needs to be findable with
761 * minimal lookup time. Note that the constraint on the number of files and
762 * directories in a volume is the size of the vnode index files and the
763 * max file size AFS supports (for internal files) of 2^31. Since a record
764 * in the small vnode index file is 64 bytes long, we can have at most
765 * (2^31)/64 or 33554432 files. A record in the large index file is
766 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
767 * Another layout parameter is that there is roughly a 16 to 1 ratio between
768 * the number of files and the number of directories.
770 * Using this information we can see that a layout of 256 directories, each
771 * with 512 subdirectories and each of those having 512 files gives us
772 * 256*512*512 = 67108864 AFS files and directories.
774 * The volume, vnode, uniquifier and data version, as well as the tag
775 * are required, either for finding the file or for salvaging. It's best to
776 * restrict the name to something that can be mapped into 64 bits so the
777 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
778 * is used to distinguish between different versions of the same file
779 * which are currently in the RW and clones of a volume. See "Link Table
780 * Organization" below for more information on the tag. The tag is
781 * required in the name of the file to ensure a unique name.
783 * We can store data in the uid, gid and mode bits of the files, provided
784 * the directories have root only access. This gives us 15 bits for each
785 * of uid and gid (GNU chown considers 65535 to mean "don't change").
786 * There are 9 available mode bits. Adn we need to store a total of
787 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
788 * or 131 bits somewhere.
790 * The format of a file name for a regular file is:
791 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
792 * V1 - low 8 bits of RW volume id
793 * V2 - all bits of RW volume id
794 * AA - high 8 bits of vnode number.
795 * BB - next 9 bits of vnode number.
796 * <tag><uniq><vno> - file name
798 * Volume special files are stored in a separate directory:
799 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
802 * The vnode is hashed into the directory using the high bits of the
803 * vnode number. This is so that consecutively created vnodes are in
804 * roughly the same area on the disk. This will at least be optimal if
805 * the user is creating many files in the same AFS directory. The name
806 * should be formed so that the leading characters are different as quickly
807 * as possible, leading to faster discards of incorrect matches in the
813 /************************************************************************
814 * Link Table Organization
815 ************************************************************************
817 * The link table volume special file is used to hold the link counts that
818 * are held in the inodes in inode based AFS vice filesystems. For user
819 * space access, the link counts are being kept in a separate
820 * volume special file. The file begins with the usual version stamp
821 * information and is then followed by one row per vnode number. vnode 0
822 * is used to hold the link count of the link table itself. That is because
823 * the same link table is shared among all the volumes of the volume group
824 * and is deleted only when the last volume of a volume group is deleted.
826 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
827 * link count from 0 through 7. Each colume represents a unique instance of
828 * that vnode. Say we have a file shared between the RW and a RO and a
829 * different version of the file (or a different uniquifer) for the BU volume.
830 * Then one column would be holding the link count of 2 for the RW and RO
831 * and a different column would hold the link count of 1 for the BU volume.
832 * Note that we allow only 5 volumes per file, giving 15 bits used in the
835 #define LINKTABLE_WIDTH 2
836 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
839 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
841 int toff = (int)(ino & NAMEI_VNODEMASK);
842 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
844 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
845 *index = (tindex << 1) + tindex;
849 /* namei_GetLinkCount
850 * If lockit is set, lock the file and leave it locked upon a successful
854 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
856 unsigned short row = 0;
860 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
863 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
864 if (lockf(h->fd_fd, F_LOCK, 0) < 0)
866 if (flock(h->fd_fd, LOCK_EX) < 0)
871 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
872 goto bad_getLinkByte;
874 if (read(h->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
875 goto bad_getLinkByte;
878 return (int)((row >> index) & NAMEI_TAGMASK);
882 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
883 lockf(h->fd_fd, F_ULOCK, 0);
885 flock(h->fd_fd, LOCK_UN);
890 /* Return a free column index for this vnode. */
892 GetFreeTag(IHandle_t * ih, int vno)
906 /* Only one manipulates at a time. */
907 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
908 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
910 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
912 FDH_REALLYCLOSE(fdP);
916 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
917 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
921 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
922 if (code != sizeof(row)) {
928 /* Now find a free column in this row and claim it. */
929 for (col = 0; col < NAMEI_MAXVOLS; col++) {
930 coldata = 7 << (col * 3);
931 if ((row & coldata) == 0)
934 if (col >= NAMEI_MAXVOLS)
937 coldata = 1 << (col * 3);
940 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
943 if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
947 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
948 lockf(fdP->fd_fd, F_ULOCK, 0);
950 flock(fdP->fd_fd, LOCK_UN);
952 FDH_REALLYCLOSE(fdP);
956 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
957 lockf(fdP->fd_fd, F_ULOCK, 0);
959 flock(fdP->fd_fd, LOCK_UN);
961 FDH_REALLYCLOSE(fdP);
967 /* namei_SetLinkCount
968 * If locked is set, assume file is locked. Otherwise, lock file before
969 * proceeding to modify it.
972 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
980 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
983 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
984 if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
986 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
991 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
993 goto bad_SetLinkCount;
997 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
998 if (code != sizeof(row)) {
1001 goto bad_SetLinkCount;
1008 row &= (unsigned short)~junk;
1009 row |= (unsigned short)count;
1011 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1013 goto bad_SetLinkCount;
1016 if (write(fdP->fd_fd, (char *)&row, sizeof(short)) != sizeof(short)) {
1018 goto bad_SetLinkCount;
1026 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
1027 lockf(fdP->fd_fd, F_ULOCK, 0);
1029 flock(fdP->fd_fd, LOCK_UN);
1036 /* ListViceInodes - write inode data to a results file. */
1037 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1039 static int DecodeVolumeName(char *name, int *vid);
1040 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1041 int (*write_fun) (FILE *,
1042 struct ViceInodeInfo *,
1043 char *, char *), FILE * fp,
1044 int (*judgeFun) (struct ViceInodeInfo *,
1046 int singleVolumeNumber, void *rock);
1051 * Write the inode data to the results file.
1053 * Returns -2 on error, 0 on success.
1055 * This is written as a callback simply so that other listing routines
1056 * can use the same inode reading code.
1059 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1062 n = fwrite(info, sizeof(*info), 1, fp);
1063 return (n == 1) ? 0 : -2;
1067 int mode_errors; /* Number of errors found in mode bits on directories. */
1069 VerifyDirPerms(char *path)
1071 struct afs_stat status;
1073 if (afs_stat(path, &status) < 0) {
1074 Log("Unable to stat %s. Please manually verify mode bits for this"
1075 " directory\n", path);
1077 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1083 * Fill the results file with the requested inode information.
1087 * -1 - complete failure, salvage should terminate.
1088 * -2 - not enough space on partition, salvager has error message for this.
1090 * This code optimizes single volume salvages by just looking at that one
1091 * volume's directory.
1093 * If the resultFile is NULL, then don't call the write routine.
1096 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1097 int (*judgeInode) (struct ViceInodeInfo * info, int vid, void *rock),
1098 int singleVolumeNumber, int *forcep, int forceR, char *wpath,
1101 FILE *fp = (FILE *) - 1;
1103 struct afs_stat status;
1106 fp = afs_fopen(resultFile, "w");
1108 Log("Unable to create inode description file %s\n", resultFile);
1113 /* Verify protections on directories. */
1115 VerifyDirPerms(mountedOn);
1118 namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp, judgeInode,
1119 singleVolumeNumber, rock);
1129 if (fflush(fp) == EOF) {
1130 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1134 if (fsync(fileno(fp)) == -1) {
1135 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1139 if (fclose(fp) == EOF) {
1140 Log("Unable to successfully close inode file for %s\n", mountedOn);
1145 * Paranoia: check that the file is really the right size
1147 if (afs_stat(resultFile, &status) == -1) {
1148 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1151 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1152 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1153 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1161 /* namei_ListAFSFiles
1163 * Collect all the matching AFS files on the drive.
1164 * If singleVolumeNumber is non-zero, just return files for that volume.
1166 * Returns <0 on error, else number of files found to match.
1169 namei_ListAFSFiles(char *dev,
1170 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1172 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1173 int singleVolumeNumber, void *rock)
1179 struct dirent *dp1, *dp2;
1182 static void FreeZLCList(void);
1185 memset((void *)&ih, 0, sizeof(IHandle_t));
1186 ih.ih_dev = volutil_GetPartitionID(dev);
1188 if (singleVolumeNumber) {
1189 ih.ih_vid = singleVolumeNumber;
1190 namei_HandleToVolDir(&name, &ih);
1192 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1193 singleVolumeNumber, rock);
1197 /* Find all volume data directories and descend through them. */
1198 namei_HandleToInodeDir(&name, &ih);
1200 dirp1 = opendir(name.n_path);
1203 while ((dp1 = readdir(dirp1))) {
1204 if (*dp1->d_name == '.')
1206 (void)strcpy(path2, name.n_path);
1207 (void)strcat(path2, "/");
1208 (void)strcat(path2, dp1->d_name);
1209 dirp2 = opendir(path2);
1211 while ((dp2 = readdir(dirp2))) {
1212 if (*dp2->d_name == '.')
1214 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1216 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1233 /* namei_ListAFSSubDirs
1238 * > = 0 - number of AFS files found.
1241 namei_ListAFSSubDirs(IHandle_t * dirIH,
1242 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1244 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1245 int singleVolumeNumber, void *rock)
1247 IHandle_t myIH = *dirIH;
1249 char path1[512], path2[512], path3[512];
1250 DIR *dirp1, *dirp2, *dirp3;
1251 struct dirent *dp1, *dp2, *dp3;
1252 struct ViceInodeInfo info;
1253 FdHandle_t linkHandle;
1257 static void AddToZLCDeleteList(char dir, char *name);
1258 static void DeleteZLCFiles(char *path);
1261 namei_HandleToVolDir(&name, &myIH);
1262 (void)strcpy(path1, name.n_path);
1264 /* Do the directory containing the special files first to pick up link
1267 (void)strcat(path1, "/");
1268 (void)strcat(path1, NAMEI_SPECDIR);
1270 linkHandle.fd_fd = -1;
1271 dirp1 = opendir(path1);
1273 while ((dp1 = readdir(dirp1))) {
1274 if (*dp1->d_name == '.')
1276 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
1278 if (info.u.param[2] != VI_LINKTABLE) {
1281 /* Open this handle */
1282 (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
1284 linkHandle.fd_fd = afs_open(path2, O_RDONLY, 0666);
1286 namei_GetLinkCount(&linkHandle, (Inode) 0, 0);
1288 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1291 if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
1292 if (linkHandle.fd_fd >= 0)
1293 close(linkHandle.fd_fd);
1302 /* Now run through all the other subdirs */
1303 namei_HandleToVolDir(&name, &myIH);
1304 (void)strcpy(path1, name.n_path);
1306 dirp1 = opendir(path1);
1308 while ((dp1 = readdir(dirp1))) {
1309 if (*dp1->d_name == '.')
1311 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1314 /* Now we've got a next level subdir. */
1315 (void)strcpy(path2, path1);
1316 (void)strcat(path2, "/");
1317 (void)strcat(path2, dp1->d_name);
1318 dirp2 = opendir(path2);
1320 while ((dp2 = readdir(dirp2))) {
1321 if (*dp2->d_name == '.')
1324 /* Now we've got to the actual data */
1325 (void)strcpy(path3, path2);
1326 (void)strcat(path3, "/");
1327 (void)strcat(path3, dp2->d_name);
1328 dirp3 = opendir(path3);
1330 while ((dp3 = readdir(dirp3))) {
1331 if (*dp3->d_name == '.')
1334 (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
1337 namei_GetLinkCount(&linkHandle,
1338 info.inodeNumber, 0);
1339 if (info.linkCount == 0) {
1341 Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
1342 AddToZLCDeleteList((char)i, dp3->d_name);
1344 Log("Found 0 link count file %s/%s.\n", path3,
1350 && !(*judgeFun) (&info, singleVolumeNumber, rock))
1353 if ((*writeFun) (fp, &info, path3, dp3->d_name) <
1355 close(linkHandle.fd_fd);
1372 if (linkHandle.fd_fd >= 0)
1373 close(linkHandle.fd_fd);
1375 /* Then why does this directory exist? Blow it away. */
1376 namei_HandleToVolDir(&name, dirIH);
1377 namei_RemoveDataDirectories(&name);
1384 DecodeVolumeName(char *name, int *vid)
1386 if (strlen(name) <= 2)
1388 *vid = (int)flipbase64_to_int64(name);
1395 * Get the inode number from the name.
1399 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
1402 struct afs_stat status;
1405 (void)strcpy(fpath, dpath);
1406 (void)strcat(fpath, "/");
1407 (void)strcat(fpath, name);
1409 if (afs_stat(fpath, &status) < 0) {
1413 info->byteCount = status.st_size;
1414 info->inodeNumber = (Inode) flipbase64_to_int64(name);
1416 GetOGMFromStat(&status, &parm, &tag);
1417 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1418 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1419 info->u.param[0] = parm;
1420 info->u.param[1] = -1;
1421 info->u.param[2] = tag;
1422 info->u.param[3] = volid;
1424 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1425 info->u.param[0] = volid;
1426 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1427 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1428 & (Inode) NAMEI_UNIQMASK);
1429 info->u.param[3] = parm;
1435 * Convert the VolumeInfo file from RO to RW
1436 * this routine is called by namei_convertROtoRWvolume()
1440 convertVolumeInfo(fdr, fdw, vid)
1445 struct VolumeDiskData vd;
1448 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1449 sizeof(struct VolumeDiskData)) {
1450 Log("1 convertVolumeInfo: read failed for %lu with code %d\n", vid,
1454 vd.restoredFromId = vd.id; /* remember the RO volume here */
1456 vd.id = vd.parentId;
1459 vd.uniquifier += 5000; /* just in case there are still file copies from
1460 * the old RW volume around */
1461 p = strrchr(vd.name, '.');
1462 if (p && !strcmp(p, ".readonly")) {
1465 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1466 sizeof(struct VolumeDiskData)) {
1467 Log("1 convertVolumeInfo: write failed for %lu with code %d\n", vid,
1475 * Convert a RO-volume into a RW-volume
1477 * This function allows to recover very fast from the loss of a partition
1478 * from RO-copies if all RO-Copies exist on another partition.
1479 * Then these RO-volumes can be made to the new RW-volumes.
1480 * Backup of RW-volumes then consists in "vos release".
1482 * We must make sure in this partition exists only the RO-volume which
1483 * is typical for remote replicas.
1485 * Then the linktable is already ok,
1486 * the vnode files need to be renamed
1487 * the volinfo file needs to be replaced by another one with
1488 * slightly different contents and new name.
1489 * The volume header file of the RO-volume in the /vicep<x> directory
1490 * is destroyed by this call. A new header file for the RW-volume must
1491 * be created after return from this routine.
1495 namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
1498 char dir_name[512], oldpath[512], newpath[512];
1511 struct ViceInodeInfo info;
1513 namei_HandleToName(&n, h);
1514 strcpy(dir_name, n.n_path);
1515 p = strrchr(dir_name, '/');
1517 dirp = opendir(dir_name);
1519 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
1523 while ((dp = readdir(dirp))) {
1524 /* struct ViceInodeInfo info; */
1526 if (*dp->d_name == '.')
1528 if (DecodeInode(dir_name, dp->d_name, &info, h->ih_vid) < 0) {
1529 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1530 dir_name, dp->d_name);
1534 if (info.u.param[1] != -1) {
1535 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
1539 if (info.u.param[0] != vid) {
1540 if (info.u.param[0] == h->ih_vid) {
1541 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1546 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n", dir_name, dp->d_name, info.u.param[0]);
1550 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1551 strcpy(infoName, dp->d_name);
1553 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1554 strcpy(smallName, dp->d_name);
1556 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1557 strcpy(largeName, dp->d_name);
1561 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
1567 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1568 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
1573 * If we come here then there was only a RO-volume and we can safely
1577 bzero(&t_ih, sizeof(t_ih));
1578 t_ih.ih_dev = h->ih_dev;
1579 t_ih.ih_vid = h->ih_vid;
1581 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
1582 fd = afs_open(oldpath, O_RDWR, 0);
1584 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1588 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_VOLINFO);
1589 namei_HandleToName(&n, &t_ih);
1590 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1592 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
1596 code = convertVolumeInfo(fd, fd2, h->ih_vid);
1603 SetOGM(fd2, h->ih_vid, 1);
1606 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_SMALLINDEX);
1607 namei_HandleToName(&n, &t_ih);
1608 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
1609 fd = afs_open(newpath, O_RDWR, 0);
1611 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
1614 SetOGM(fd, h->ih_vid, 2);
1616 link(newpath, n.n_path);
1619 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_LARGEINDEX);
1620 namei_HandleToName(&n, &t_ih);
1621 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
1622 fd = afs_open(newpath, O_RDWR, 0);
1624 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
1627 SetOGM(fd, h->ih_vid, 3);
1629 link(newpath, n.n_path);
1638 * returns a static string used to print either 32 or 64 bit inode numbers.
1641 PrintInode(char *s, Inode ino)
1643 static afs_ino_str_t result;
1647 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
1654 /* Routines to facilitate removing zero link count files. */
1655 #define MAX_ZLC_NAMES 32
1656 #define MAX_ZLC_NAMELEN 16
1657 typedef struct zlcList_s {
1658 struct zlcList_s *zlc_next;
1660 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1663 static zlcList_t *zlcAnchor = NULL;
1664 static zlcList_t *zlcCur = NULL;
1667 AddToZLCDeleteList(char dir, char *name)
1669 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1671 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1672 if (zlcCur && zlcCur->zlc_next)
1673 zlcCur = zlcCur->zlc_next;
1675 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1681 zlcCur->zlc_next = tmp;
1685 zlcCur->zlc_next = NULL;
1689 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1694 DeleteZLCFiles(char *path)
1700 for (z = zlcAnchor; z; z = z->zlc_next) {
1701 for (i = 0; i < z->zlc_n; i++) {
1702 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1703 if (namei_unlink(fname) < 0) {
1704 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1708 z->zlc_n = 0; /* Can reuse space. */
1721 tnext = i->zlc_next;
1725 zlcCur = zlcAnchor = NULL;
1729 #endif /* AFS_NAMEI_ENV */