2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* I/O operations for the Unix open by name (namei) interface. */
12 #include <afsconfig.h>
13 #include <afs/param.h>
24 #include <afs/assert.h>
27 #include <sys/param.h>
29 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
32 #include <afs/afsutil.h>
35 #include <afs/afsint.h>
39 #include "viceinode.h"
41 #include "partition.h"
43 #include "volume_inline.h"
45 #include <afs/errors.h>
47 /*@+fcnmacros +macrofcndecl@*/
50 #endif /*S_SPLINT_S */
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 #endif /*S_SPLINT_S */
59 #define afs_fstat fstat
61 #define afs_fopen fopen
62 #endif /* !O_LARGEFILE */
63 /*@=fcnmacros =macrofcndecl@*/
66 #define LOCK_SH 1 /* shared lock */
67 #define LOCK_EX 2 /* exclusive lock */
68 #define LOCK_NB 4 /* don't block when locking */
69 #define LOCK_UN 8 /* unlock */
76 * This function emulates a subset of flock()
79 emul_flock(int fd, int cmd)
82 memset(&f, 0, sizeof (f));
91 return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
94 #define flock(f,c) emul_flock(f,c)
101 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
110 nBytes = FDH_PREAD(fdP, buf, size, offset);
116 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
125 nBytes = FDH_PWRITE(fdP, buf, size, offset);
132 /* Inode number format:
133 * low 26 bits - vnode number - all 1's if volume special file.
135 * next 3 bits spare (0's)
136 * high 32 bits - uniquifier (regular) or type if spare
138 #define NAMEI_VNODEMASK 0x003ffffff
139 #define NAMEI_TAGMASK 0x7
140 #define NAMEI_TAGSHIFT 26
141 #define NAMEI_UNIQMASK 0xffffffff
142 #define NAMEI_UNIQSHIFT 32
143 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
144 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
146 /* dir1 is the high 8 bits of the 26 bit vnode */
147 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
148 /* dir2 is the next 9 bits */
149 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
150 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
152 #define NAMEI_SPECDIR "special"
153 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
155 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
156 * group, not counting temporary (move) volumes.
157 * This is the number of separate files, all having
158 * the same vnode number, which can occur in a volume
169 static int namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite);
171 static int GetFreeTag(IHandle_t * ih, int vno);
173 /* namei_HandleToInodeDir
175 * Construct the path name of the directory holding the inode data.
176 * Format: /<vicepx>/INODEDIR
179 #define PNAME_BLEN 64
181 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
185 memset(name, '\0', sizeof(*name));
188 * Add the /vicepXX string to the start of name->n_base and then calculate
189 * offset as the number of bytes we know we added.
191 * FIXME: This embeds knowledge of the vice partition naming scheme and
192 * mapping from device numbers. There needs to be an API that tells us
195 volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
196 offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
197 name->n_base[offset] = '/';
199 strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
200 strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
203 #define addtoname(N, C) \
205 strlcat((N)->n_path, "/", sizeof((N)->n_path)); \
206 strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
211 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
215 namei_HandleToInodeDir(name, ih);
216 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
217 strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
218 addtoname(name, name->n_voldir1);
219 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
220 strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
221 addtoname(name, name->n_voldir2);
224 /* namei_HandleToName
226 * Constructs a file name for the fully qualified handle.
227 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
230 namei_HandleToName(namei_t * name, IHandle_t * ih)
233 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
235 namei_HandleToVolDir(name, ih);
237 if (vno == NAMEI_VNODESPECIAL) {
238 strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
239 addtoname(name, name->n_dir1);
240 name->n_dir2[0] = '\0';
242 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
243 strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
244 addtoname(name, name->n_dir1);
245 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
246 strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
247 addtoname(name, name->n_dir2);
249 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
250 strlcpy(name->n_inode, str, sizeof(name->n_inode));
251 addtoname(name, name->n_inode);
254 /* The following is a warning to tell sys-admins to not muck about in this
257 #define VICE_README "These files and directories are a part of the AFS \
258 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
259 ownership and permissions included.\n"
261 namei_ViceREADME(char *partition)
266 /* Create the inode directory if we're starting for the first time */
267 (void)afs_snprintf(filename, sizeof filename, "%s/%s", partition,
269 mkdir(filename, 0700);
271 (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
273 fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
275 (void)write(fd, VICE_README, strlen(VICE_README));
282 #define create_dir() \
284 if (mkdir(tmp, 0700)<0) { \
285 if (errno != EEXIST) \
293 #define create_nextdir(A) \
295 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
298 /* namei_CreateDataDirectories
300 * If creating the file failed because of ENOENT or ENOTDIR, try
301 * creating all the directories first.
304 namei_CreateDataDirectories(namei_t * name, int *created)
310 strlcpy(tmp, name->n_base, sizeof(tmp));
313 create_nextdir(name->n_voldir1);
314 create_nextdir(name->n_voldir2);
315 create_nextdir(name->n_dir1);
316 if (name->n_dir2[0]) {
317 create_nextdir(name->n_dir2);
322 /* delTree(): Deletes an entire tree of directories (no files)
324 * root : Full path to the subtree. Should be big enough for PATH_MAX
325 * tree : the subtree to be deleted is rooted here. Specifies only the
326 * subtree beginning at tree (not the entire path). It should be
327 * a pointer into the "root" buffer.
329 * errp : errno of the first error encountered during the directory cleanup.
330 * *errp should have been initialized to 0.
333 * -1 : If errors were encountered during cleanup and error is set to
337 * If there are errors, we try to work around them and delete as many
338 * directories as possible. We don't attempt to remove directories that still
339 * have non-dir entries in them.
342 delTree(char *root, char *tree, int *errp)
350 /* delete the children first */
351 cp = strchr(tree, '/');
353 delTree(root, cp + 1, errp);
356 cp = tree + strlen(tree); /* move cp to the end of string tree */
358 /* now delete all entries in this dir */
359 if ((ds = opendir(root)) != (DIR *) NULL) {
361 while ((dirp = readdir(ds))) {
362 /* ignore . and .. */
363 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
365 /* since root is big enough, we reuse the space to
366 * concatenate the dirname to the current tree
369 strcat(root, dirp->d_name);
370 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
371 /* delete this subtree */
372 delTree(root, cp + 1, errp);
374 *errp = *errp ? *errp : errno;
376 /* recover path to our cur tree by truncating it to
381 /* if (!errno) -- closedir not implicit if we got an error */
385 /* finally axe the current dir */
387 *errp = *errp ? *errp : errno;
389 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
391 #endif /* !AFS_PTHREAD_ENV */
396 /* if we encountered errors during cleanup, we return a -1 */
404 /* namei_RemoveDataDirectories
406 * Returns 0 on success.
407 * Returns -1 on error. Typically, callers ignore this error bcause we
408 * can continue running if the removes fail. The salvage process will
409 * finish tidying up for us. We only use the n_base and n_voldir1 entries
410 * and only do rmdir's.
414 namei_RemoveDataDirectories(namei_t * name)
416 char pbuf[MAXPATHLEN], *path = pbuf;
417 int prefixlen = strlen(name->n_base), err = 0;
419 strlcpy(path, name->n_path, sizeof(pbuf));
421 /* move past the prefix */
422 path = path + prefixlen + 1; /* skip over the trailing / */
424 /* now delete all dirs upto path */
425 return delTree(pbuf, path, &err);
429 /* Create the file in the name space.
431 * Parameters stored as follows:
433 * p1 - volid - implied in containing directory.
434 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
435 * p3 - uniq -- bits 4-0 are in mode bits 4-0
436 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
438 * p1 - volid - creation time - dwHighDateTime
439 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
440 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
441 * p4 - parid - parent volume id - implied in containing directory.
443 * Return value is the inode number or (Inode)-1 if error.
444 * We "know" there is only one link table, so return EEXIST if there already
445 * is a link table. It's up to the calling code to test errno and increment
451 * This function is called by VCreateVolume to hide the implementation
452 * details of the inode numbers. This only allows for 7 volume special
453 * types, but if we get that far, this could should be dead by then.
456 namei_MakeSpecIno(int volid, int type)
459 ino = NAMEI_INODESPECIAL;
460 type &= NAMEI_TAGMASK;
461 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
462 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
466 /* SetOGM - set owner group and mode bits from parm and tag
468 * owner - low 15 bits of parm.
469 * group - next 15 bits of parm.
470 * mode - 2 bits of parm, then lowest = 3 bits of tag.
473 SetOGM(int fd, int parm, int tag)
475 int owner, group, mode;
477 owner = parm & 0x7fff;
478 group = (parm >> 15) & 0x7fff;
479 if (fchown(fd, owner, group) < 0)
482 mode = (parm >> 27) & 0x18;
484 if (fchmod(fd, mode) < 0)
491 /* GetOGM - get parm and tag from owner, group and mode bits. */
493 GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
495 *parm = status->st_uid | (status->st_gid << 15);
496 *parm |= (status->st_mode & 0x18) << 27;
497 *tag = status->st_mode & 0x7;
501 GetOGM(int fd, int *parm, int *tag)
503 struct afs_stat status;
504 if (afs_fstat(fd, &status) < 0)
507 GetOGMFromStat(&status, parm, tag);
511 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
513 /* Derive the name and create it O_EXCL. If that fails we have an error.
514 * Get the tag from a free column in the link table.
517 namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
530 memset((void *)&tmp, 0, sizeof(IHandle_t));
533 tmp.ih_dev = volutil_GetPartitionID(part);
534 if (tmp.ih_dev == -1) {
540 /* Parameters for special file:
541 * p1 - volume id - goes into owner/group/mode
544 * p4 - parent volume id
549 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
550 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
552 int vno = p2 & NAMEI_VNODEMASK;
553 /* Parameters for regular file:
565 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
566 tag = GetFreeTag(lh, p2);
570 /* name is <uniq(p3)><tag><vno(p2)> */
572 tmp.ih_ino = (Inode) p2;
573 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
574 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
579 namei_HandleToName(&name, &tmp);
580 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
582 if (errno == ENOTDIR || errno == ENOENT) {
583 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
585 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
593 if (SetOGM(fd, ogm_parm, tag) < 0) {
599 if (p2 == -1 && p3 == VI_LINKTABLE) {
600 /* hack at tmp to setup for set link count call. */
601 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
604 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
612 if (code || (fd < 0)) {
616 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
621 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
627 namei_iopen(IHandle_t * h)
632 /* Convert handle to file name. */
633 namei_HandleToName(&name, h);
634 fd = afs_open(name.n_path, O_RDWR, 0666);
638 /* Need to detect vol special file and just unlink. In those cases, the
639 * handle passed in _is_ for the inode. We only check p1 for the special
643 namei_dec(IHandle_t * ih, Inode ino, int p1)
650 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
653 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
655 /* Verify this is the right file. */
656 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
665 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
666 FDH_REALLYCLOSE(fdP);
672 /* If it's the link table itself, decrement the link count. */
673 if (type == VI_LINKTABLE) {
674 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
675 FDH_REALLYCLOSE(fdP);
681 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
683 FDH_REALLYCLOSE(fdP);
689 FDH_REALLYCLOSE(fdP);
695 namei_HandleToName(&name, tmp);
696 if ((code = unlink(name.n_path)) == 0) {
697 if (type == VI_LINKTABLE) {
698 /* Try to remove directory. If it fails, that's ok.
699 * Salvage will clean up.
701 (void)namei_RemoveDataDirectories(&name);
704 FDH_REALLYCLOSE(fdP);
707 /* Get a file descriptor handle for this Inode */
713 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
714 FDH_REALLYCLOSE(fdP);
720 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
721 FDH_REALLYCLOSE(fdP);
726 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
727 Log("Warning: Lost ref on ihandle dev %d vid %d ino %" AFS_INT64_FMT "\n",
728 th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
731 /* If we're less than 0, someone presumably unlinked;
732 don't bother setting count to 0, but we need to drop a lock */
733 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
734 FDH_REALLYCLOSE(fdP);
740 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
742 namei_HandleToName(&name, th);
744 code = unlink(name.n_path);
753 namei_inc(IHandle_t * h, Inode ino, int p1)
759 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
760 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
761 if (type != VI_LINKTABLE)
766 /* Get a file descriptor handle for this Inode */
772 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
781 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
785 FDH_REALLYCLOSE(fdP);
793 namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
799 /* Convert handle to file name. */
800 namei_HandleToName(&nameLink, hLink);
801 namei_HandleToName(&nameTarget, hTarget);
803 unlink(nameLink.n_path);
804 code = link(nameTarget.n_path, nameLink.n_path);
809 namei_copy_on_write(IHandle_t *h)
811 afs_int32 fd, code = 0;
814 struct afs_stat tstat;
817 namei_HandleToName(&name, h);
818 if (afs_stat(name.n_path, &tstat) < 0)
820 if (tstat.st_nlink > 1) { /* do a copy on write */
829 afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
830 fd = afs_open(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
842 size = tstat.st_size;
845 tlen = size > 8192 ? 8192 : size;
846 if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
848 if (write(fd, buf, tlen) != tlen)
854 FDH_REALLYCLOSE(fdP);
860 code = rename(path, name.n_path);
866 /************************************************************************
867 * File Name Structure
868 ************************************************************************
870 * Each AFS file needs a unique name and it needs to be findable with
871 * minimal lookup time. Note that the constraint on the number of files and
872 * directories in a volume is the size of the vnode index files and the
873 * max file size AFS supports (for internal files) of 2^31. Since a record
874 * in the small vnode index file is 64 bytes long, we can have at most
875 * (2^31)/64 or 33554432 files. A record in the large index file is
876 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
877 * Another layout parameter is that there is roughly a 16 to 1 ratio between
878 * the number of files and the number of directories.
880 * Using this information we can see that a layout of 256 directories, each
881 * with 512 subdirectories and each of those having 512 files gives us
882 * 256*512*512 = 67108864 AFS files and directories.
884 * The volume, vnode, uniquifier and data version, as well as the tag
885 * are required, either for finding the file or for salvaging. It's best to
886 * restrict the name to something that can be mapped into 64 bits so the
887 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
888 * is used to distinguish between different versions of the same file
889 * which are currently in the RW and clones of a volume. See "Link Table
890 * Organization" below for more information on the tag. The tag is
891 * required in the name of the file to ensure a unique name.
893 * We can store data in the uid, gid and mode bits of the files, provided
894 * the directories have root only access. This gives us 15 bits for each
895 * of uid and gid (GNU chown considers 65535 to mean "don't change").
896 * There are 9 available mode bits. Adn we need to store a total of
897 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
898 * or 131 bits somewhere.
900 * The format of a file name for a regular file is:
901 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
902 * V1 - low 8 bits of RW volume id
903 * V2 - all bits of RW volume id
904 * AA - high 8 bits of vnode number.
905 * BB - next 9 bits of vnode number.
906 * <tag><uniq><vno> - file name
908 * Volume special files are stored in a separate directory:
909 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
912 * The vnode is hashed into the directory using the high bits of the
913 * vnode number. This is so that consecutively created vnodes are in
914 * roughly the same area on the disk. This will at least be optimal if
915 * the user is creating many files in the same AFS directory. The name
916 * should be formed so that the leading characters are different as quickly
917 * as possible, leading to faster discards of incorrect matches in the
923 /************************************************************************
924 * Link Table Organization
925 ************************************************************************
927 * The link table volume special file is used to hold the link counts that
928 * are held in the inodes in inode based AFS vice filesystems. For user
929 * space access, the link counts are being kept in a separate
930 * volume special file. The file begins with the usual version stamp
931 * information and is then followed by one row per vnode number. vnode 0
932 * is used to hold the link count of the link table itself. That is because
933 * the same link table is shared among all the volumes of the volume group
934 * and is deleted only when the last volume of a volume group is deleted.
936 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
937 * link count from 0 through 7. Each colume represents a unique instance of
938 * that vnode. Say we have a file shared between the RW and a RO and a
939 * different version of the file (or a different uniquifer) for the BU volume.
940 * Then one column would be holding the link count of 2 for the RW and RO
941 * and a different column would hold the link count of 1 for the BU volume.
942 * Note that we allow only 5 volumes per file, giving 15 bits used in the
945 #define LINKTABLE_WIDTH 2
946 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
949 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
951 int toff = (int)(ino & NAMEI_VNODEMASK);
952 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
954 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
955 *index = (tindex << 1) + tindex;
959 /* namei_GetLinkCount
960 * If lockit is set, lock the file and leave it locked upon a successful
964 namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
966 unsigned short row = 0;
971 /* there's no linktable yet. the salvager will create one later */
972 if (h->fd_fd == -1 && fixup)
974 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
977 if (flock(h->fd_fd, LOCK_EX) < 0)
981 rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
982 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
984 if (rc == 0 && fixup) {
986 if (fstat(h->fd_fd, &st) || st.st_size >= offset+sizeof(row))
987 goto bad_getLinkByte;
988 FDH_TRUNC(h, offset+sizeof(row));
990 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
992 if (rc != sizeof(row)) {
993 goto bad_getLinkByte;
996 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
998 rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
999 if (rc != sizeof(row))
1000 goto bad_getLinkByte;
1003 return (int)((row >> index) & NAMEI_TAGMASK);
1007 flock(h->fd_fd, LOCK_UN);
1012 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
1014 return namei_GetLinkCount2(h, ino, lockit, 0, 1);
1017 /* Return a free column index for this vnode. */
1019 GetFreeTag(IHandle_t * ih, int vno)
1033 /* Only one manipulates at a time. */
1034 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
1035 FDH_REALLYCLOSE(fdP);
1039 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
1041 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1042 if (nBytes != sizeof(row)) {
1048 /* Now find a free column in this row and claim it. */
1049 for (col = 0; col < NAMEI_MAXVOLS; col++) {
1050 coldata = 7 << (col * 3);
1051 if ((row & coldata) == 0)
1054 if (col >= NAMEI_MAXVOLS) {
1059 coldata = 1 << (col * 3);
1062 if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
1066 flock(fdP->fd_fd, LOCK_UN);
1067 FDH_REALLYCLOSE(fdP);
1071 flock(fdP->fd_fd, LOCK_UN);
1072 FDH_REALLYCLOSE(fdP);
1078 /* namei_SetLinkCount
1079 * If locked is set, assume file is locked. Otherwise, lock file before
1080 * proceeding to modify it.
1083 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1089 ssize_t nBytes = -1;
1091 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1094 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
1099 nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
1100 if (nBytes != sizeof(row)) {
1103 goto bad_SetLinkCount;
1110 row &= (unsigned short)~junk;
1111 row |= (unsigned short)count;
1113 if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
1115 goto bad_SetLinkCount;
1123 flock(fdP->fd_fd, LOCK_UN);
1129 /* ListViceInodes - write inode data to a results file. */
1130 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1131 unsigned int volid);
1132 static int DecodeVolumeName(char *name, unsigned int *vid);
1133 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1134 int (*write_fun) (FILE *,
1135 struct ViceInodeInfo *,
1136 char *, char *), FILE * fp,
1137 int (*judgeFun) (struct ViceInodeInfo *,
1138 afs_uint32 vid, void *),
1139 afs_uint32 singleVolumeNumber, void *rock);
1144 * Write the inode data to the results file.
1146 * Returns -2 on error, 0 on success.
1148 * This is written as a callback simply so that other listing routines
1149 * can use the same inode reading code.
1152 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1155 n = fwrite(info, sizeof(*info), 1, fp);
1156 return (n == 1) ? 0 : -2;
1160 int mode_errors; /* Number of errors found in mode bits on directories. */
1162 VerifyDirPerms(char *path)
1164 struct afs_stat status;
1166 if (afs_stat(path, &status) < 0) {
1167 Log("Unable to stat %s. Please manually verify mode bits for this"
1168 " directory\n", path);
1170 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1176 * Fill the results file with the requested inode information.
1180 * -1 - complete failure, salvage should terminate.
1181 * -2 - not enough space on partition, salvager has error message for this.
1183 * This code optimizes single volume salvages by just looking at that one
1184 * volume's directory.
1186 * If the resultFile is NULL, then don't call the write routine.
1189 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
1190 int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
1191 afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
1195 struct afs_stat status;
1197 *forcep = 0; /* no need to salvage until further notice */
1199 /* Verify protections on directories. */
1201 VerifyDirPerms(mountedOn);
1204 namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
1205 singleVolumeNumber, rock);
1214 if (fflush(inodeFile) == EOF) {
1215 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1218 if (fsync(fileno(inodeFile)) == -1) {
1219 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1224 * Paranoia: check that the file is really the right size
1226 if (afs_fstat(fileno(inodeFile), &status) == -1) {
1227 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1230 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1231 Log("Wrong size (%d instead of %lu) in inode file for %s\n",
1232 (int) status.st_size,
1233 (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
1241 /* namei_ListAFSFiles
1243 * Collect all the matching AFS files on the drive.
1244 * If singleVolumeNumber is non-zero, just return files for that volume.
1246 * Returns <0 on error, else number of files found to match.
1249 namei_ListAFSFiles(char *dev,
1250 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1252 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1253 afs_uint32 singleVolumeNumber, void *rock)
1259 struct dirent *dp1, *dp2;
1262 static void FreeZLCList(void);
1265 memset((void *)&ih, 0, sizeof(IHandle_t));
1266 ih.ih_dev = volutil_GetPartitionID(dev);
1268 if (singleVolumeNumber) {
1269 ih.ih_vid = singleVolumeNumber;
1270 namei_HandleToVolDir(&name, &ih);
1272 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1273 singleVolumeNumber, rock);
1277 /* Find all volume data directories and descend through them. */
1278 namei_HandleToInodeDir(&name, &ih);
1280 dirp1 = opendir(name.n_path);
1283 while ((dp1 = readdir(dirp1))) {
1284 if (*dp1->d_name == '.')
1286 afs_snprintf(path2, sizeof(path2), "%s/%s", name.n_path,
1288 dirp2 = opendir(path2);
1290 while ((dp2 = readdir(dirp2))) {
1291 if (*dp2->d_name == '.')
1293 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1295 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1312 /* namei_ListAFSSubDirs
1317 * > = 0 - number of AFS files found.
1320 namei_ListAFSSubDirs(IHandle_t * dirIH,
1321 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1323 int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
1324 afs_uint32 singleVolumeNumber, void *rock)
1326 IHandle_t myIH = *dirIH;
1328 char path1[512], path2[512], path3[512];
1329 DIR *dirp1, *dirp2, *dirp3;
1330 struct dirent *dp1, *dp2, *dp3;
1331 struct ViceInodeInfo info;
1332 FdHandle_t linkHandle;
1336 static void AddToZLCDeleteList(char dir, char *name);
1337 static void DeleteZLCFiles(char *path);
1340 namei_HandleToVolDir(&name, &myIH);
1341 strlcpy(path1, name.n_path, sizeof(path1));
1343 /* Do the directory containing the special files first to pick up link
1346 (void)strcat(path1, "/");
1347 (void)strcat(path1, NAMEI_SPECDIR);
1349 linkHandle.fd_fd = -1;
1350 dirp1 = opendir(path1);
1352 while ((dp1 = readdir(dirp1))) {
1353 if (*dp1->d_name == '.')
1355 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
1357 if (info.u.param[2] != VI_LINKTABLE) {
1360 /* Open this handle */
1361 (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
1363 linkHandle.fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1365 namei_GetLinkCount2(&linkHandle, (Inode) 0, 1, 1, Testing);
1367 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1370 if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
1371 if (linkHandle.fd_fd >= 0)
1372 close(linkHandle.fd_fd);
1381 /* Now run through all the other subdirs */
1382 namei_HandleToVolDir(&name, &myIH);
1383 strlcpy(path1, name.n_path, sizeof(path1));
1385 dirp1 = opendir(path1);
1387 while ((dp1 = readdir(dirp1))) {
1388 if (*dp1->d_name == '.')
1390 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1393 /* Now we've got a next level subdir. */
1394 afs_snprintf(path2, sizeof(path2), "%s/%s", path1, dp1->d_name);
1395 dirp2 = opendir(path2);
1397 while ((dp2 = readdir(dirp2))) {
1398 if (*dp2->d_name == '.')
1401 /* Now we've got to the actual data */
1402 afs_snprintf(path3, sizeof(path3), "%s/%s", path2,
1404 dirp3 = opendir(path3);
1406 while ((dp3 = readdir(dirp3))) {
1407 if (*dp3->d_name == '.')
1410 (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
1413 namei_GetLinkCount2(&linkHandle,
1414 info.inodeNumber, 1, 1, Testing);
1415 if (info.linkCount == 0) {
1417 Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
1418 AddToZLCDeleteList((char)i, dp3->d_name);
1420 Log("Found 0 link count file %s/%s.\n", path3,
1426 && !(*judgeFun) (&info, singleVolumeNumber, rock))
1429 if ((*writeFun) (fp, &info, path3, dp3->d_name) <
1431 close(linkHandle.fd_fd);
1448 if (linkHandle.fd_fd >= 0)
1449 close(linkHandle.fd_fd);
1451 /* Then why does this directory exist? Blow it away. */
1452 namei_HandleToVolDir(&name, dirIH);
1453 namei_RemoveDataDirectories(&name);
1460 DecodeVolumeName(char *name, unsigned int *vid)
1462 if (strlen(name) < 1)
1464 *vid = (unsigned int)flipbase64_to_int64(name);
1471 * Get the inode number from the name.
1475 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1479 struct afs_stat status;
1481 lb64_string_t check;
1483 afs_snprintf(fpath, sizeof(fpath), "%s/%s", dpath, name);
1485 if (afs_stat(fpath, &status) < 0) {
1489 info->byteCount = status.st_size;
1490 info->inodeNumber = (Inode) flipbase64_to_int64(name);
1492 int64_to_flipbase64(check, info->inodeNumber);
1493 if (strcmp(name, check))
1496 GetOGMFromStat(&status, &parm, &tag);
1497 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1498 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1499 info->u.param[0] = parm;
1500 info->u.param[1] = -1;
1501 info->u.param[2] = tag;
1502 info->u.param[3] = volid;
1504 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1505 info->u.param[0] = volid;
1506 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1507 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1508 & (Inode) NAMEI_UNIQMASK);
1509 info->u.param[3] = parm;
1515 * Convert the VolumeInfo file from RO to RW
1516 * this routine is called by namei_convertROtoRWvolume()
1519 #ifdef FSSYNC_BUILD_CLIENT
1521 convertVolumeInfo(int fdr, int fdw, afs_uint32 vid)
1523 struct VolumeDiskData vd;
1526 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1527 sizeof(struct VolumeDiskData)) {
1528 Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
1529 afs_printable_uint32_lu(vid),
1533 vd.restoredFromId = vd.id; /* remember the RO volume here */
1535 vd.id = vd.parentId;
1539 vd.uniquifier += 5000; /* just in case there are still file copies from
1540 * the old RW volume around */
1541 p = strrchr(vd.name, '.');
1542 if (p && !strcmp(p, ".readonly")) {
1545 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1546 sizeof(struct VolumeDiskData)) {
1547 Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
1548 afs_printable_uint32_lu(vid),
1557 * Convert a RO-volume into a RW-volume
1559 * This function allows to recover very fast from the loss of a partition
1560 * from RO-copies if all RO-Copies exist on another partition.
1561 * Then these RO-volumes can be made to the new RW-volumes.
1562 * Backup of RW-volumes then consists in "vos release".
1564 * We must make sure in this partition exists only the RO-volume which
1565 * is typical for remote replicas.
1567 * Then the linktable is already ok,
1568 * the vnode files need to be renamed
1569 * the volinfo file needs to be replaced by another one with
1570 * slightly different contents and new name.
1571 * The volume header file of the RO-volume in the /vicep<x> directory
1572 * is destroyed by this call. A new header file for the RW-volume must
1573 * be created after return from this routine.
1577 namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
1580 #ifdef FSSYNC_BUILD_CLIENT
1582 char dir_name[512], oldpath[512], newpath[512];
1597 struct DiskPartition64 *partP;
1598 struct ViceInodeInfo info;
1599 struct VolumeDiskHeader h;
1600 # ifdef AFS_DEMAND_ATTACH_FS
1602 # endif /* AFS_DEMAND_ATTACH_FS */
1604 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1605 partP = partP->next);
1607 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1612 # ifdef AFS_DEMAND_ATTACH_FS
1613 locktype = VVolLockType(V_VOLUPD, 1);
1614 code = VLockVolumeByIdNB(volumeId, partP, locktype);
1620 # endif /* AFS_DEMAND_ATTACH_FS */
1622 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
1623 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
1624 afs_printable_uint32_lu(volumeId));
1629 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1631 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
1632 IH_INIT(ih, partP->device, h.parent, ino);
1634 namei_HandleToName(&n, ih);
1635 strlcpy(dir_name, n.n_path, sizeof(dir_name));
1636 p = strrchr(dir_name, '/');
1638 dirp = opendir(dir_name);
1640 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
1645 while ((dp = readdir(dirp))) {
1646 /* struct ViceInodeInfo info; */
1648 if (*dp->d_name == '.')
1650 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
1651 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1652 dir_name, dp->d_name);
1657 if (info.u.param[1] != -1) {
1658 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
1663 if (info.u.param[0] != volumeId) {
1664 if (info.u.param[0] == ih->ih_vid) {
1665 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1670 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s"
1671 " for volume %lu\n", dir_name, dp->d_name,
1672 afs_printable_uint32_lu(info.u.param[0]));
1677 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1678 strlcpy(infoName, dp->d_name, sizeof(infoName));
1680 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1681 strlcpy(smallName, dp->d_name, sizeof(smallName));
1683 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1684 strlcpy(largeName, dp->d_name, sizeof(largeName));
1688 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
1695 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1696 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
1702 * If we come here then there was only a RO-volume and we can safely
1706 memset(&t_ih, 0, sizeof(t_ih));
1707 t_ih.ih_dev = ih->ih_dev;
1708 t_ih.ih_vid = ih->ih_vid;
1710 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
1711 fd = afs_open(oldpath, O_RDWR, 0);
1713 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1718 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
1719 namei_HandleToName(&n, &t_ih);
1720 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1722 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
1727 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
1735 SetOGM(fd2, ih->ih_vid, 1);
1738 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
1739 namei_HandleToName(&n, &t_ih);
1740 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
1741 fd = afs_open(newpath, O_RDWR, 0);
1743 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
1747 SetOGM(fd, ih->ih_vid, 2);
1749 link(newpath, n.n_path);
1752 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
1753 namei_HandleToName(&n, &t_ih);
1754 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
1755 fd = afs_open(newpath, O_RDWR, 0);
1757 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
1761 SetOGM(fd, ih->ih_vid, 3);
1763 link(newpath, n.n_path);
1769 h.volumeInfo_hi = h.id;
1770 h.smallVnodeIndex_hi = h.id;
1771 h.largeVnodeIndex_hi = h.id;
1772 h.linkTable_hi = h.id;
1774 if (VCreateVolumeDiskHeader(&h, partP)) {
1775 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
1776 afs_printable_uint32_lu(h.id));
1781 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
1782 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
1783 afs_printable_uint32_lu(volumeId));
1786 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1787 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1790 # ifdef AFS_DEMAND_ATTACH_FS
1792 VUnlockVolumeById(volumeId, partP);
1794 # endif /* AFS_DEMAND_ATTACH_FS */
1802 * returns a static string used to print either 32 or 64 bit inode numbers.
1805 PrintInode(char *s, Inode ino)
1807 static afs_ino_str_t result;
1811 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
1818 /* Routines to facilitate removing zero link count files. */
1819 #define MAX_ZLC_NAMES 32
1820 #define MAX_ZLC_NAMELEN 16
1821 typedef struct zlcList_s {
1822 struct zlcList_s *zlc_next;
1824 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1827 static zlcList_t *zlcAnchor = NULL;
1828 static zlcList_t *zlcCur = NULL;
1831 AddToZLCDeleteList(char dir, char *name)
1833 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1835 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1836 if (zlcCur && zlcCur->zlc_next)
1837 zlcCur = zlcCur->zlc_next;
1839 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1845 zlcCur->zlc_next = tmp;
1849 zlcCur->zlc_next = NULL;
1853 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1858 DeleteZLCFiles(char *path)
1864 for (z = zlcAnchor; z; z = z->zlc_next) {
1865 for (i = 0; i < z->zlc_n; i++) {
1866 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1867 if (namei_unlink(fname) < 0) {
1868 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1872 z->zlc_n = 0; /* Can reuse space. */
1885 tnext = i->zlc_next;
1889 zlcCur = zlcAnchor = NULL;
1893 #endif /* AFS_NAMEI_ENV */