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>
30 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
33 #include <afs/afsutil.h>
36 #include <afs/afsint.h>
40 #include "viceinode.h"
42 #include "partition.h"
43 #include <afs/errors.h>
45 /*@+fcnmacros +macrofcndecl@*/
48 extern off64_t afs_lseek(int FD, off64_t O, int F);
49 #endif /*S_SPLINT_S */
50 #define afs_lseek(FD, O, F) lseek64(FD, (off64_t)(O), F)
51 #define afs_stat stat64
52 #define afs_fstat fstat64
53 #define afs_open open64
54 #define afs_fopen fopen64
55 #else /* !O_LARGEFILE */
57 extern off_t afs_lseek(int FD, off_t O, int F);
58 #endif /*S_SPLINT_S */
59 #define afs_lseek(FD, O, F) lseek(FD, (off_t)(O), F)
61 #define afs_fstat fstat
63 #define afs_fopen fopen
64 #endif /* !O_LARGEFILE */
65 /*@=fcnmacros =macrofcndecl@*/
67 /*@printflike@*/ extern void Log(const char *format, ...);
70 #define LOCK_SH 1 /* shared lock */
71 #define LOCK_EX 2 /* exclusive lock */
72 #define LOCK_NB 4 /* don't block when locking */
73 #define LOCK_UN 8 /* unlock */
80 * This function emulates a subset of flock()
83 emul_flock(int fd, int cmd)
86 memset(&f, 0, sizeof (f));
95 return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
98 #define flock(f,c) emul_flock(f,c)
101 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
106 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
115 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
116 FDH_REALLYCLOSE(fdP);
120 nBytes = FDH_READ(fdP, buf, size);
126 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
135 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
136 FDH_REALLYCLOSE(fdP);
139 nBytes = FDH_WRITE(fdP, buf, size);
146 /* Inode number format:
147 * low 26 bits - vnode number - all 1's if volume special file.
149 * next 3 bits spare (0's)
150 * high 32 bits - uniquifier (regular) or type if spare
152 #define NAMEI_VNODEMASK 0x003ffffff
153 #define NAMEI_TAGMASK 0x7
154 #define NAMEI_TAGSHIFT 26
155 #define NAMEI_UNIQMASK 0xffffffff
156 #define NAMEI_UNIQSHIFT 32
157 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
158 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
160 /* dir1 is the high 8 bits of the 26 bit vnode */
161 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
162 /* dir2 is the next 9 bits */
163 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
164 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
166 #define NAMEI_SPECDIR "special"
167 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
169 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
170 * group, not counting temporary (move) volumes.
171 * This is the number of separate files, all having
172 * the same vnode number, which can occur in a volume
183 static int namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite);
185 static int GetFreeTag(IHandle_t * ih, int vno);
187 /* namei_HandleToInodeDir
189 * Construct the path name of the directory holding the inode data.
190 * Format: /<vicepx>/INODEDIR
193 #define PNAME_BLEN 64
195 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
197 char *tmp = name->n_base;
199 memset(name, '\0', sizeof(*name));
201 (void)volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
202 tmp += VICE_PREFIX_SIZE;
203 tmp += ih->ih_dev > 25 ? 2 : 1;
206 (void)strcpy(tmp, INODEDIR);
207 (void)strcpy(name->n_path, name->n_base);
210 #define addtoname(N, C) \
212 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
217 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
221 namei_HandleToInodeDir(name, ih);
222 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
223 (void)strcpy(name->n_voldir1, tmp);
224 addtoname(name, name->n_voldir1);
225 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
226 (void)strcpy(name->n_voldir2, tmp);
227 addtoname(name, name->n_voldir2);
230 /* namei_HandleToName
232 * Constructs a file name for the fully qualified handle.
233 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
236 namei_HandleToName(namei_t * name, IHandle_t * ih)
239 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
241 namei_HandleToVolDir(name, ih);
243 if (vno == NAMEI_VNODESPECIAL) {
244 (void)strcpy(name->n_dir1, NAMEI_SPECDIR);
245 addtoname(name, name->n_dir1);
246 name->n_dir2[0] = '\0';
248 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
249 (void)strcpy(name->n_dir1, str);
250 addtoname(name, name->n_dir1);
251 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
252 (void)strcpy(name->n_dir2, str);
253 addtoname(name, name->n_dir2);
255 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
256 (void)strcpy(name->n_inode, str);
257 addtoname(name, name->n_inode);
260 /* The following is a warning to tell sys-admins to not muck about in this
263 #define VICE_README "These files and directories are a part of the AFS \
264 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
265 ownership and permissions included.\n"
267 namei_ViceREADME(char *partition)
272 /* Create the inode directory if we're starting for the first time */
273 (void)afs_snprintf(filename, sizeof filename, "%s/%s", partition,
275 mkdir(filename, 0700);
277 (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
279 fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
281 (void)write(fd, VICE_README, strlen(VICE_README));
288 #define create_dir() \
290 if (mkdir(tmp, 0700)<0) { \
291 if (errno != EEXIST) \
299 #define create_nextdir(A) \
301 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
304 /* namei_CreateDataDirectories
306 * If creating the file failed because of ENOENT or ENOTDIR, try
307 * creating all the directories first.
310 namei_CreateDataDirectories(namei_t * name, int *created)
316 (void)strcpy(tmp, name->n_base);
319 create_nextdir(name->n_voldir1);
320 create_nextdir(name->n_voldir2);
321 create_nextdir(name->n_dir1);
322 if (name->n_dir2[0]) {
323 create_nextdir(name->n_dir2);
328 /* delTree(): Deletes an entire tree of directories (no files)
330 * root : Full path to the subtree. Should be big enough for PATH_MAX
331 * tree : the subtree to be deleted is rooted here. Specifies only the
332 * subtree beginning at tree (not the entire path). It should be
333 * a pointer into the "root" buffer.
335 * errp : errno of the first error encountered during the directory cleanup.
336 * *errp should have been initialized to 0.
339 * -1 : If errors were encountered during cleanup and error is set to
343 * If there are errors, we try to work around them and delete as many
344 * directories as possible. We don't attempt to remove directories that still
345 * have non-dir entries in them.
348 delTree(char *root, char *tree, int *errp)
356 /* delete the children first */
357 cp = strchr(tree, '/');
359 delTree(root, cp + 1, errp);
362 cp = tree + strlen(tree); /* move cp to the end of string tree */
364 /* now delete all entries in this dir */
365 if ((ds = opendir(root)) != (DIR *) NULL) {
367 while ((dirp = readdir(ds))) {
368 /* ignore . and .. */
369 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
371 /* since root is big enough, we reuse the space to
372 * concatenate the dirname to the current tree
375 strcat(root, dirp->d_name);
376 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
377 /* delete this subtree */
378 delTree(root, cp + 1, errp);
380 *errp = *errp ? *errp : errno;
382 /* recover path to our cur tree by truncating it to
387 /* if (!errno) -- closedir not implicit if we got an error */
391 /* finally axe the current dir */
393 *errp = *errp ? *errp : errno;
395 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
397 #endif /* !AFS_PTHREAD_ENV */
402 /* if we encountered errors during cleanup, we return a -1 */
410 /* namei_RemoveDataDirectories
412 * Returns 0 on success.
413 * Returns -1 on error. Typically, callers ignore this error bcause we
414 * can continue running if the removes fail. The salvage process will
415 * finish tidying up for us. We only use the n_base and n_voldir1 entries
416 * and only do rmdir's.
420 namei_RemoveDataDirectories(namei_t * name)
422 char pbuf[MAXPATHLEN], *path = pbuf;
423 int prefixlen = strlen(name->n_base), err = 0;
425 strcpy(path, name->n_path);
427 /* move past the prefix */
428 path = path + prefixlen + 1; /* skip over the trailing / */
430 /* now delete all dirs upto path */
431 return delTree(pbuf, path, &err);
435 /* Create the file in the name space.
437 * Parameters stored as follows:
439 * p1 - volid - implied in containing directory.
440 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
441 * p3 - uniq -- bits 4-0 are in mode bits 4-0
442 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
444 * p1 - volid - creation time - dwHighDateTime
445 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
446 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
447 * p4 - parid - parent volume id - implied in containing directory.
449 * Return value is the inode number or (Inode)-1 if error.
450 * We "know" there is only one link table, so return EEXIST if there already
451 * is a link table. It's up to the calling code to test errno and increment
457 * This function is called by VCreateVolume to hide the implementation
458 * details of the inode numbers. This only allows for 7 volume special
459 * types, but if we get that far, this could should be dead by then.
462 namei_MakeSpecIno(int volid, int type)
465 ino = NAMEI_INODESPECIAL;
466 type &= NAMEI_TAGMASK;
467 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
468 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
472 /* SetOGM - set owner group and mode bits from parm and tag
474 * owner - low 15 bits of parm.
475 * group - next 15 bits of parm.
476 * mode - 2 bits of parm, then lowest = 3 bits of tag.
479 SetOGM(int fd, int parm, int tag)
481 int owner, group, mode;
483 owner = parm & 0x7fff;
484 group = (parm >> 15) & 0x7fff;
485 if (fchown(fd, owner, group) < 0)
488 mode = (parm >> 27) & 0x18;
490 if (fchmod(fd, mode) < 0)
497 /* GetOGM - get parm and tag from owner, group and mode bits. */
499 GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
501 *parm = status->st_uid | (status->st_gid << 15);
502 *parm |= (status->st_mode & 0x18) << 27;
503 *tag = status->st_mode & 0x7;
507 GetOGM(int fd, int *parm, int *tag)
509 struct afs_stat status;
510 if (afs_fstat(fd, &status) < 0)
513 GetOGMFromStat(&status, parm, tag);
517 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
519 /* Derive the name and create it O_EXCL. If that fails we have an error.
520 * Get the tag from a free column in the link table.
523 namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
536 memset((void *)&tmp, 0, sizeof(IHandle_t));
539 tmp.ih_dev = volutil_GetPartitionID(part);
540 if (tmp.ih_dev == -1) {
546 /* Parameters for special file:
547 * p1 - volume id - goes into owner/group/mode
550 * p4 - parent volume id
555 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
556 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
558 int vno = p2 & NAMEI_VNODEMASK;
559 /* Parameters for regular file:
571 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
572 tag = GetFreeTag(lh, p2);
576 /* name is <uniq(p3)><tag><vno(p2)> */
578 tmp.ih_ino = (Inode) p2;
579 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
580 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
585 namei_HandleToName(&name, &tmp);
586 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
588 if (errno == ENOTDIR || errno == ENOENT) {
589 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
591 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
599 if (SetOGM(fd, ogm_parm, tag) < 0) {
605 if (p2 == -1 && p3 == VI_LINKTABLE) {
606 /* hack at tmp to setup for set link count call. */
608 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
616 if (code || (fd < 0)) {
620 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
625 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
631 namei_iopen(IHandle_t * h)
636 /* Convert handle to file name. */
637 namei_HandleToName(&name, h);
638 fd = afs_open(name.n_path, O_RDWR, 0666);
642 /* Need to detect vol special file and just unlink. In those cases, the
643 * handle passed in _is_ for the inode. We only check p1 for the special
647 namei_dec(IHandle_t * ih, Inode ino, int p1)
654 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
657 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
659 /* Verify this is the right file. */
660 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
669 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
670 FDH_REALLYCLOSE(fdP);
676 /* If it's the link table itself, decrement the link count. */
677 if (type == VI_LINKTABLE) {
678 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
679 FDH_REALLYCLOSE(fdP);
685 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
687 FDH_REALLYCLOSE(fdP);
693 FDH_REALLYCLOSE(fdP);
699 namei_HandleToName(&name, tmp);
700 if ((code = unlink(name.n_path)) == 0) {
701 if (type == VI_LINKTABLE) {
702 /* Try to remove directory. If it fails, that's ok.
703 * Salvage will clean up.
705 (void)namei_RemoveDataDirectories(&name);
708 FDH_REALLYCLOSE(fdP);
711 /* Get a file descriptor handle for this Inode */
717 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
718 FDH_REALLYCLOSE(fdP);
724 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
725 FDH_REALLYCLOSE(fdP);
730 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
731 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
732 th->ih_dev, th->ih_vid, (int64_t) th->ih_ino);
735 /* If we're less than 0, someone presumably unlinked;
736 don't bother setting count to 0, but we need to drop a lock */
737 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
738 FDH_REALLYCLOSE(fdP);
744 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
746 namei_HandleToName(&name, th);
748 code = unlink(name.n_path);
757 namei_inc(IHandle_t * h, Inode ino, int p1)
763 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
764 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
765 if (type != VI_LINKTABLE)
770 /* Get a file descriptor handle for this Inode */
776 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
785 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
789 FDH_REALLYCLOSE(fdP);
796 /************************************************************************
797 * File Name Structure
798 ************************************************************************
800 * Each AFS file needs a unique name and it needs to be findable with
801 * minimal lookup time. Note that the constraint on the number of files and
802 * directories in a volume is the size of the vnode index files and the
803 * max file size AFS supports (for internal files) of 2^31. Since a record
804 * in the small vnode index file is 64 bytes long, we can have at most
805 * (2^31)/64 or 33554432 files. A record in the large index file is
806 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
807 * Another layout parameter is that there is roughly a 16 to 1 ratio between
808 * the number of files and the number of directories.
810 * Using this information we can see that a layout of 256 directories, each
811 * with 512 subdirectories and each of those having 512 files gives us
812 * 256*512*512 = 67108864 AFS files and directories.
814 * The volume, vnode, uniquifier and data version, as well as the tag
815 * are required, either for finding the file or for salvaging. It's best to
816 * restrict the name to something that can be mapped into 64 bits so the
817 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
818 * is used to distinguish between different versions of the same file
819 * which are currently in the RW and clones of a volume. See "Link Table
820 * Organization" below for more information on the tag. The tag is
821 * required in the name of the file to ensure a unique name.
823 * We can store data in the uid, gid and mode bits of the files, provided
824 * the directories have root only access. This gives us 15 bits for each
825 * of uid and gid (GNU chown considers 65535 to mean "don't change").
826 * There are 9 available mode bits. Adn we need to store a total of
827 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
828 * or 131 bits somewhere.
830 * The format of a file name for a regular file is:
831 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
832 * V1 - low 8 bits of RW volume id
833 * V2 - all bits of RW volume id
834 * AA - high 8 bits of vnode number.
835 * BB - next 9 bits of vnode number.
836 * <tag><uniq><vno> - file name
838 * Volume special files are stored in a separate directory:
839 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
842 * The vnode is hashed into the directory using the high bits of the
843 * vnode number. This is so that consecutively created vnodes are in
844 * roughly the same area on the disk. This will at least be optimal if
845 * the user is creating many files in the same AFS directory. The name
846 * should be formed so that the leading characters are different as quickly
847 * as possible, leading to faster discards of incorrect matches in the
853 /************************************************************************
854 * Link Table Organization
855 ************************************************************************
857 * The link table volume special file is used to hold the link counts that
858 * are held in the inodes in inode based AFS vice filesystems. For user
859 * space access, the link counts are being kept in a separate
860 * volume special file. The file begins with the usual version stamp
861 * information and is then followed by one row per vnode number. vnode 0
862 * is used to hold the link count of the link table itself. That is because
863 * the same link table is shared among all the volumes of the volume group
864 * and is deleted only when the last volume of a volume group is deleted.
866 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
867 * link count from 0 through 7. Each colume represents a unique instance of
868 * that vnode. Say we have a file shared between the RW and a RO and a
869 * different version of the file (or a different uniquifer) for the BU volume.
870 * Then one column would be holding the link count of 2 for the RW and RO
871 * and a different column would hold the link count of 1 for the BU volume.
872 * Note that we allow only 5 volumes per file, giving 15 bits used in the
875 #define LINKTABLE_WIDTH 2
876 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
879 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
881 int toff = (int)(ino & NAMEI_VNODEMASK);
882 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
884 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
885 *index = (tindex << 1) + tindex;
889 /* namei_GetLinkCount
890 * If lockit is set, lock the file and leave it locked upon a successful
894 namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
896 unsigned short row = 0;
901 /* there's no linktable yet. the salvager will create one later */
902 if (h->fd_fd == -1 && fixup)
904 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
907 if (flock(h->fd_fd, LOCK_EX) < 0)
911 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
912 goto bad_getLinkByte;
914 rc = read(h->fd_fd, (char *)&row, sizeof(row));
915 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
917 if (rc == 0 && fixup) {
919 if (fstat(h->fd_fd, &st) || st.st_size >= offset+sizeof(row))
920 goto bad_getLinkByte;
921 FDH_TRUNC(h, offset+sizeof(row));
923 rc = write(h->fd_fd, (char *)&row, sizeof(row));
925 if (rc != sizeof(row)) {
926 goto bad_getLinkByte;
929 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
931 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
932 goto bad_getLinkByte;
933 rc = write(h->fd_fd, (char *)&row, sizeof(row));
934 if (rc != sizeof(row))
935 goto bad_getLinkByte;
938 return (int)((row >> index) & NAMEI_TAGMASK);
942 flock(h->fd_fd, LOCK_UN);
947 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
949 return namei_GetLinkCount2(h, ino, lockit, 0, 1);
952 /* Return a free column index for this vnode. */
954 GetFreeTag(IHandle_t * ih, int vno)
968 /* Only one manipulates at a time. */
969 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
970 FDH_REALLYCLOSE(fdP);
974 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
975 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
979 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
980 if (code != sizeof(row)) {
986 /* Now find a free column in this row and claim it. */
987 for (col = 0; col < NAMEI_MAXVOLS; col++) {
988 coldata = 7 << (col * 3);
989 if ((row & coldata) == 0)
992 if (col >= NAMEI_MAXVOLS)
995 coldata = 1 << (col * 3);
998 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1001 if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
1005 flock(fdP->fd_fd, LOCK_UN);
1006 FDH_REALLYCLOSE(fdP);
1010 flock(fdP->fd_fd, LOCK_UN);
1011 FDH_REALLYCLOSE(fdP);
1017 /* namei_SetLinkCount
1018 * If locked is set, assume file is locked. Otherwise, lock file before
1019 * proceeding to modify it.
1022 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1030 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1033 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
1037 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1039 goto bad_SetLinkCount;
1043 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
1044 if (code != sizeof(row)) {
1047 goto bad_SetLinkCount;
1054 row &= (unsigned short)~junk;
1055 row |= (unsigned short)count;
1057 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1059 goto bad_SetLinkCount;
1062 if (write(fdP->fd_fd, (char *)&row, sizeof(short)) != sizeof(short)) {
1064 goto bad_SetLinkCount;
1072 flock(fdP->fd_fd, LOCK_UN);
1078 /* ListViceInodes - write inode data to a results file. */
1079 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1081 static int DecodeVolumeName(char *name, int *vid);
1082 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1083 int (*write_fun) (FILE *,
1084 struct ViceInodeInfo *,
1085 char *, char *), FILE * fp,
1086 int (*judgeFun) (struct ViceInodeInfo *,
1088 int singleVolumeNumber, void *rock);
1093 * Write the inode data to the results file.
1095 * Returns -2 on error, 0 on success.
1097 * This is written as a callback simply so that other listing routines
1098 * can use the same inode reading code.
1101 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1104 n = fwrite(info, sizeof(*info), 1, fp);
1105 return (n == 1) ? 0 : -2;
1109 int mode_errors; /* Number of errors found in mode bits on directories. */
1111 VerifyDirPerms(char *path)
1113 struct afs_stat status;
1115 if (afs_stat(path, &status) < 0) {
1116 Log("Unable to stat %s. Please manually verify mode bits for this"
1117 " directory\n", path);
1119 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1125 * Fill the results file with the requested inode information.
1129 * -1 - complete failure, salvage should terminate.
1130 * -2 - not enough space on partition, salvager has error message for this.
1132 * This code optimizes single volume salvages by just looking at that one
1133 * volume's directory.
1135 * If the resultFile is NULL, then don't call the write routine.
1138 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1139 int (*judgeInode) (struct ViceInodeInfo * info, int vid, void *rock),
1140 int singleVolumeNumber, int *forcep, int forceR, char *wpath,
1143 FILE *fp = (FILE *) - 1;
1145 struct afs_stat status;
1148 fp = afs_fopen(resultFile, "w");
1150 Log("Unable to create inode description file %s\n", resultFile);
1155 /* Verify protections on directories. */
1157 VerifyDirPerms(mountedOn);
1160 namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp, judgeInode,
1161 singleVolumeNumber, rock);
1171 if (fflush(fp) == EOF) {
1172 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1176 if (fsync(fileno(fp)) == -1) {
1177 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1181 if (fclose(fp) == EOF) {
1182 Log("Unable to successfully close inode file for %s\n", mountedOn);
1187 * Paranoia: check that the file is really the right size
1189 if (afs_stat(resultFile, &status) == -1) {
1190 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1193 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1194 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1195 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1203 /* namei_ListAFSFiles
1205 * Collect all the matching AFS files on the drive.
1206 * If singleVolumeNumber is non-zero, just return files for that volume.
1208 * Returns <0 on error, else number of files found to match.
1211 namei_ListAFSFiles(char *dev,
1212 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1214 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1215 int singleVolumeNumber, void *rock)
1221 struct dirent *dp1, *dp2;
1224 static void FreeZLCList(void);
1227 memset((void *)&ih, 0, sizeof(IHandle_t));
1228 ih.ih_dev = volutil_GetPartitionID(dev);
1230 if (singleVolumeNumber) {
1231 ih.ih_vid = singleVolumeNumber;
1232 namei_HandleToVolDir(&name, &ih);
1234 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1235 singleVolumeNumber, rock);
1239 /* Find all volume data directories and descend through them. */
1240 namei_HandleToInodeDir(&name, &ih);
1242 dirp1 = opendir(name.n_path);
1245 while ((dp1 = readdir(dirp1))) {
1246 if (*dp1->d_name == '.')
1248 (void)strcpy(path2, name.n_path);
1249 (void)strcat(path2, "/");
1250 (void)strcat(path2, dp1->d_name);
1251 dirp2 = opendir(path2);
1253 while ((dp2 = readdir(dirp2))) {
1254 if (*dp2->d_name == '.')
1256 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1258 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1275 /* namei_ListAFSSubDirs
1280 * > = 0 - number of AFS files found.
1283 namei_ListAFSSubDirs(IHandle_t * dirIH,
1284 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1286 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1287 int singleVolumeNumber, void *rock)
1289 IHandle_t myIH = *dirIH;
1291 char path1[512], path2[512], path3[512];
1292 DIR *dirp1, *dirp2, *dirp3;
1293 struct dirent *dp1, *dp2, *dp3;
1294 struct ViceInodeInfo info;
1295 FdHandle_t linkHandle;
1299 static void AddToZLCDeleteList(char dir, char *name);
1300 static void DeleteZLCFiles(char *path);
1303 namei_HandleToVolDir(&name, &myIH);
1304 (void)strcpy(path1, name.n_path);
1306 /* Do the directory containing the special files first to pick up link
1309 (void)strcat(path1, "/");
1310 (void)strcat(path1, NAMEI_SPECDIR);
1312 linkHandle.fd_fd = -1;
1313 dirp1 = opendir(path1);
1315 while ((dp1 = readdir(dirp1))) {
1316 if (*dp1->d_name == '.')
1318 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
1320 if (info.u.param[2] != VI_LINKTABLE) {
1323 /* Open this handle */
1324 (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
1326 linkHandle.fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1328 namei_GetLinkCount2(&linkHandle, (Inode) 0, 1, 1, Testing);
1330 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1333 if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
1334 if (linkHandle.fd_fd >= 0)
1335 close(linkHandle.fd_fd);
1344 /* Now run through all the other subdirs */
1345 namei_HandleToVolDir(&name, &myIH);
1346 (void)strcpy(path1, name.n_path);
1348 dirp1 = opendir(path1);
1350 while ((dp1 = readdir(dirp1))) {
1351 if (*dp1->d_name == '.')
1353 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1356 /* Now we've got a next level subdir. */
1357 (void)strcpy(path2, path1);
1358 (void)strcat(path2, "/");
1359 (void)strcat(path2, dp1->d_name);
1360 dirp2 = opendir(path2);
1362 while ((dp2 = readdir(dirp2))) {
1363 if (*dp2->d_name == '.')
1366 /* Now we've got to the actual data */
1367 (void)strcpy(path3, path2);
1368 (void)strcat(path3, "/");
1369 (void)strcat(path3, dp2->d_name);
1370 dirp3 = opendir(path3);
1372 while ((dp3 = readdir(dirp3))) {
1373 if (*dp3->d_name == '.')
1376 (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
1379 namei_GetLinkCount2(&linkHandle,
1380 info.inodeNumber, 1, 1, Testing);
1381 if (info.linkCount == 0) {
1383 Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
1384 AddToZLCDeleteList((char)i, dp3->d_name);
1386 Log("Found 0 link count file %s/%s.\n", path3,
1392 && !(*judgeFun) (&info, singleVolumeNumber, rock))
1395 if ((*writeFun) (fp, &info, path3, dp3->d_name) <
1397 close(linkHandle.fd_fd);
1414 if (linkHandle.fd_fd >= 0)
1415 close(linkHandle.fd_fd);
1417 /* Then why does this directory exist? Blow it away. */
1418 namei_HandleToVolDir(&name, dirIH);
1419 namei_RemoveDataDirectories(&name);
1426 DecodeVolumeName(char *name, int *vid)
1428 if (strlen(name) <= 2)
1430 *vid = (int)flipbase64_to_int64(name);
1437 * Get the inode number from the name.
1441 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
1444 struct afs_stat status;
1447 (void)strcpy(fpath, dpath);
1448 (void)strcat(fpath, "/");
1449 (void)strcat(fpath, name);
1451 if (afs_stat(fpath, &status) < 0) {
1455 info->byteCount = status.st_size;
1456 info->inodeNumber = (Inode) flipbase64_to_int64(name);
1458 GetOGMFromStat(&status, &parm, &tag);
1459 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1460 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1461 info->u.param[0] = parm;
1462 info->u.param[1] = -1;
1463 info->u.param[2] = tag;
1464 info->u.param[3] = volid;
1466 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1467 info->u.param[0] = volid;
1468 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1469 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1470 & (Inode) NAMEI_UNIQMASK);
1471 info->u.param[3] = parm;
1477 * Convert the VolumeInfo file from RO to RW
1478 * this routine is called by namei_convertROtoRWvolume()
1482 convertVolumeInfo(fdr, fdw, vid)
1487 struct VolumeDiskData vd;
1490 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1491 sizeof(struct VolumeDiskData)) {
1492 Log("1 convertVolumeInfo: read failed for %lu with code %d\n", vid,
1496 vd.restoredFromId = vd.id; /* remember the RO volume here */
1498 vd.id = vd.parentId;
1501 vd.uniquifier += 5000; /* just in case there are still file copies from
1502 * the old RW volume around */
1503 p = strrchr(vd.name, '.');
1504 if (p && !strcmp(p, ".readonly")) {
1507 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1508 sizeof(struct VolumeDiskData)) {
1509 Log("1 convertVolumeInfo: write failed for %lu with code %d\n", vid,
1517 * Convert a RO-volume into a RW-volume
1519 * This function allows to recover very fast from the loss of a partition
1520 * from RO-copies if all RO-Copies exist on another partition.
1521 * Then these RO-volumes can be made to the new RW-volumes.
1522 * Backup of RW-volumes then consists in "vos release".
1524 * We must make sure in this partition exists only the RO-volume which
1525 * is typical for remote replicas.
1527 * Then the linktable is already ok,
1528 * the vnode files need to be renamed
1529 * the volinfo file needs to be replaced by another one with
1530 * slightly different contents and new name.
1531 * The volume header file of the RO-volume in the /vicep<x> directory
1532 * is destroyed by this call. A new header file for the RW-volume must
1533 * be created after return from this routine.
1537 namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
1540 char dir_name[512], oldpath[512], newpath[512];
1553 struct ViceInodeInfo info;
1555 namei_HandleToName(&n, h);
1556 strcpy(dir_name, n.n_path);
1557 p = strrchr(dir_name, '/');
1559 dirp = opendir(dir_name);
1561 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
1565 while ((dp = readdir(dirp))) {
1566 /* struct ViceInodeInfo info; */
1568 if (*dp->d_name == '.')
1570 if (DecodeInode(dir_name, dp->d_name, &info, h->ih_vid) < 0) {
1571 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1572 dir_name, dp->d_name);
1576 if (info.u.param[1] != -1) {
1577 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
1581 if (info.u.param[0] != vid) {
1582 if (info.u.param[0] == h->ih_vid) {
1583 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1588 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n", dir_name, dp->d_name, info.u.param[0]);
1592 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1593 strcpy(infoName, dp->d_name);
1595 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1596 strcpy(smallName, dp->d_name);
1598 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1599 strcpy(largeName, dp->d_name);
1603 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
1609 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1610 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
1615 * If we come here then there was only a RO-volume and we can safely
1619 memset(&t_ih, 0, sizeof(t_ih));
1620 t_ih.ih_dev = h->ih_dev;
1621 t_ih.ih_vid = h->ih_vid;
1623 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
1624 fd = afs_open(oldpath, O_RDWR, 0);
1626 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1630 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_VOLINFO);
1631 namei_HandleToName(&n, &t_ih);
1632 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1634 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
1638 code = convertVolumeInfo(fd, fd2, h->ih_vid);
1645 SetOGM(fd2, h->ih_vid, 1);
1648 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_SMALLINDEX);
1649 namei_HandleToName(&n, &t_ih);
1650 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
1651 fd = afs_open(newpath, O_RDWR, 0);
1653 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
1656 SetOGM(fd, h->ih_vid, 2);
1658 link(newpath, n.n_path);
1661 t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_LARGEINDEX);
1662 namei_HandleToName(&n, &t_ih);
1663 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
1664 fd = afs_open(newpath, O_RDWR, 0);
1666 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
1669 SetOGM(fd, h->ih_vid, 3);
1671 link(newpath, n.n_path);
1680 * returns a static string used to print either 32 or 64 bit inode numbers.
1683 PrintInode(char *s, Inode ino)
1685 static afs_ino_str_t result;
1689 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
1696 /* Routines to facilitate removing zero link count files. */
1697 #define MAX_ZLC_NAMES 32
1698 #define MAX_ZLC_NAMELEN 16
1699 typedef struct zlcList_s {
1700 struct zlcList_s *zlc_next;
1702 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1705 static zlcList_t *zlcAnchor = NULL;
1706 static zlcList_t *zlcCur = NULL;
1709 AddToZLCDeleteList(char dir, char *name)
1711 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1713 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1714 if (zlcCur && zlcCur->zlc_next)
1715 zlcCur = zlcCur->zlc_next;
1717 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1723 zlcCur->zlc_next = tmp;
1727 zlcCur->zlc_next = NULL;
1731 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1736 DeleteZLCFiles(char *path)
1742 for (z = zlcAnchor; z; z = z->zlc_next) {
1743 for (i = 0; i < z->zlc_n; i++) {
1744 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1745 if (namei_unlink(fname) < 0) {
1746 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1750 z->zlc_n = 0; /* Can reuse space. */
1763 tnext = i->zlc_next;
1767 zlcCur = zlcAnchor = NULL;
1771 #endif /* AFS_NAMEI_ENV */