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>
26 #include <afs/assert.h>
29 #include <sys/param.h>
31 #if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
34 #include <afs/afsutil.h>
37 #include <afs/afsint.h>
41 #include "viceinode.h"
43 #include "partition.h"
45 #include <afs/errors.h>
47 /*@+fcnmacros +macrofcndecl@*/
50 extern off64_t afs_lseek(int FD, off64_t O, int F);
51 #endif /*S_SPLINT_S */
52 #define afs_lseek(FD, O, F) lseek64(FD, (off64_t)(O), F)
53 #define afs_stat stat64
54 #define afs_fstat fstat64
55 #define afs_open open64
56 #define afs_fopen fopen64
57 #else /* !O_LARGEFILE */
59 extern off_t afs_lseek(int FD, off_t O, int F);
60 #endif /*S_SPLINT_S */
61 #define afs_lseek(FD, O, F) lseek(FD, (off_t)(O), F)
63 #define afs_fstat fstat
65 #define afs_fopen fopen
66 #endif /* !O_LARGEFILE */
67 /*@=fcnmacros =macrofcndecl@*/
69 /*@printflike@*/ extern void Log(const char *format, ...);
72 #define LOCK_SH 1 /* shared lock */
73 #define LOCK_EX 2 /* exclusive lock */
74 #define LOCK_NB 4 /* don't block when locking */
75 #define LOCK_UN 8 /* unlock */
82 * This function emulates a subset of flock()
85 emul_flock(int fd, int cmd)
88 memset(&f, 0, sizeof (f));
97 return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
100 #define flock(f,c) emul_flock(f,c)
103 extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
108 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
117 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
118 FDH_REALLYCLOSE(fdP);
122 nBytes = FDH_READ(fdP, buf, size);
128 namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
137 if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
138 FDH_REALLYCLOSE(fdP);
141 nBytes = FDH_WRITE(fdP, buf, size);
148 /* Inode number format:
149 * low 26 bits - vnode number - all 1's if volume special file.
151 * next 3 bits spare (0's)
152 * high 32 bits - uniquifier (regular) or type if spare
154 #define NAMEI_VNODEMASK 0x003ffffff
155 #define NAMEI_TAGMASK 0x7
156 #define NAMEI_TAGSHIFT 26
157 #define NAMEI_UNIQMASK 0xffffffff
158 #define NAMEI_UNIQSHIFT 32
159 #define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
160 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
162 /* dir1 is the high 8 bits of the 26 bit vnode */
163 #define VNO_DIR1(vno) ((vno >> 14) & 0xff)
164 /* dir2 is the next 9 bits */
165 #define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
166 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
168 #define NAMEI_SPECDIR "special"
169 #define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
171 #define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
172 * group, not counting temporary (move) volumes.
173 * This is the number of separate files, all having
174 * the same vnode number, which can occur in a volume
185 static int namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite);
187 static int GetFreeTag(IHandle_t * ih, int vno);
189 /* namei_HandleToInodeDir
191 * Construct the path name of the directory holding the inode data.
192 * Format: /<vicepx>/INODEDIR
195 #define PNAME_BLEN 64
197 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
199 char *tmp = name->n_base;
201 memset(name, '\0', sizeof(*name));
203 (void)volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
204 tmp += VICE_PREFIX_SIZE;
205 tmp += ih->ih_dev > 25 ? 2 : 1;
208 (void)strcpy(tmp, INODEDIR);
209 (void)strcpy(name->n_path, name->n_base);
212 #define addtoname(N, C) \
214 strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
219 namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
223 namei_HandleToInodeDir(name, ih);
224 (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
225 (void)strcpy(name->n_voldir1, tmp);
226 addtoname(name, name->n_voldir1);
227 (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
228 (void)strcpy(name->n_voldir2, tmp);
229 addtoname(name, name->n_voldir2);
232 /* namei_HandleToName
234 * Constructs a file name for the fully qualified handle.
235 * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
238 namei_HandleToName(namei_t * name, IHandle_t * ih)
241 int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
243 namei_HandleToVolDir(name, ih);
245 if (vno == NAMEI_VNODESPECIAL) {
246 (void)strcpy(name->n_dir1, NAMEI_SPECDIR);
247 addtoname(name, name->n_dir1);
248 name->n_dir2[0] = '\0';
250 (void)int32_to_flipbase64(str, VNO_DIR1(vno));
251 (void)strcpy(name->n_dir1, str);
252 addtoname(name, name->n_dir1);
253 (void)int32_to_flipbase64(str, VNO_DIR2(vno));
254 (void)strcpy(name->n_dir2, str);
255 addtoname(name, name->n_dir2);
257 (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
258 (void)strcpy(name->n_inode, str);
259 addtoname(name, name->n_inode);
262 /* The following is a warning to tell sys-admins to not muck about in this
265 #define VICE_README "These files and directories are a part of the AFS \
266 namespace. Modifying them\nin any way will result in loss of AFS data,\n\
267 ownership and permissions included.\n"
269 namei_ViceREADME(char *partition)
274 /* Create the inode directory if we're starting for the first time */
275 (void)afs_snprintf(filename, sizeof filename, "%s/%s", partition,
277 mkdir(filename, 0700);
279 (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
281 fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
283 (void)write(fd, VICE_README, strlen(VICE_README));
290 #define create_dir() \
292 if (mkdir(tmp, 0700)<0) { \
293 if (errno != EEXIST) \
301 #define create_nextdir(A) \
303 strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
306 /* namei_CreateDataDirectories
308 * If creating the file failed because of ENOENT or ENOTDIR, try
309 * creating all the directories first.
312 namei_CreateDataDirectories(namei_t * name, int *created)
318 (void)strcpy(tmp, name->n_base);
321 create_nextdir(name->n_voldir1);
322 create_nextdir(name->n_voldir2);
323 create_nextdir(name->n_dir1);
324 if (name->n_dir2[0]) {
325 create_nextdir(name->n_dir2);
330 /* delTree(): Deletes an entire tree of directories (no files)
332 * root : Full path to the subtree. Should be big enough for PATH_MAX
333 * tree : the subtree to be deleted is rooted here. Specifies only the
334 * subtree beginning at tree (not the entire path). It should be
335 * a pointer into the "root" buffer.
337 * errp : errno of the first error encountered during the directory cleanup.
338 * *errp should have been initialized to 0.
341 * -1 : If errors were encountered during cleanup and error is set to
345 * If there are errors, we try to work around them and delete as many
346 * directories as possible. We don't attempt to remove directories that still
347 * have non-dir entries in them.
350 delTree(char *root, char *tree, int *errp)
358 /* delete the children first */
359 cp = strchr(tree, '/');
361 delTree(root, cp + 1, errp);
364 cp = tree + strlen(tree); /* move cp to the end of string tree */
366 /* now delete all entries in this dir */
367 if ((ds = opendir(root)) != (DIR *) NULL) {
369 while ((dirp = readdir(ds))) {
370 /* ignore . and .. */
371 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
373 /* since root is big enough, we reuse the space to
374 * concatenate the dirname to the current tree
377 strcat(root, dirp->d_name);
378 if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
379 /* delete this subtree */
380 delTree(root, cp + 1, errp);
382 *errp = *errp ? *errp : errno;
384 /* recover path to our cur tree by truncating it to
389 /* if (!errno) -- closedir not implicit if we got an error */
393 /* finally axe the current dir */
395 *errp = *errp ? *errp : errno;
397 #ifndef AFS_PTHREAD_ENV /* let rx get some work done */
399 #endif /* !AFS_PTHREAD_ENV */
404 /* if we encountered errors during cleanup, we return a -1 */
412 /* namei_RemoveDataDirectories
414 * Returns 0 on success.
415 * Returns -1 on error. Typically, callers ignore this error bcause we
416 * can continue running if the removes fail. The salvage process will
417 * finish tidying up for us. We only use the n_base and n_voldir1 entries
418 * and only do rmdir's.
422 namei_RemoveDataDirectories(namei_t * name)
424 char pbuf[MAXPATHLEN], *path = pbuf;
425 int prefixlen = strlen(name->n_base), err = 0;
427 strcpy(path, name->n_path);
429 /* move past the prefix */
430 path = path + prefixlen + 1; /* skip over the trailing / */
432 /* now delete all dirs upto path */
433 return delTree(pbuf, path, &err);
437 /* Create the file in the name space.
439 * Parameters stored as follows:
441 * p1 - volid - implied in containing directory.
442 * p2 - vnode - name is <vno:31-23>/<vno:22-15>/<vno:15-0><uniq:31-5><tag:2-0>
443 * p3 - uniq -- bits 4-0 are in mode bits 4-0
444 * p4 - dv ---- dv:15-0 in uid, dv:29-16 in gid, dv:31-30 in mode:6-5
446 * p1 - volid - creation time - dwHighDateTime
447 * p2 - vnode - -1 means special, file goes in "S" subdirectory.
448 * p3 - type -- name is <type>.<tag> where tag is a file name unqiquifier.
449 * p4 - parid - parent volume id - implied in containing directory.
451 * Return value is the inode number or (Inode)-1 if error.
452 * We "know" there is only one link table, so return EEXIST if there already
453 * is a link table. It's up to the calling code to test errno and increment
459 * This function is called by VCreateVolume to hide the implementation
460 * details of the inode numbers. This only allows for 7 volume special
461 * types, but if we get that far, this could should be dead by then.
464 namei_MakeSpecIno(int volid, int type)
467 ino = NAMEI_INODESPECIAL;
468 type &= NAMEI_TAGMASK;
469 ino |= ((Inode) type) << NAMEI_TAGSHIFT;
470 ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
474 /* SetOGM - set owner group and mode bits from parm and tag
476 * owner - low 15 bits of parm.
477 * group - next 15 bits of parm.
478 * mode - 2 bits of parm, then lowest = 3 bits of tag.
481 SetOGM(int fd, int parm, int tag)
483 int owner, group, mode;
485 owner = parm & 0x7fff;
486 group = (parm >> 15) & 0x7fff;
487 if (fchown(fd, owner, group) < 0)
490 mode = (parm >> 27) & 0x18;
492 if (fchmod(fd, mode) < 0)
499 /* GetOGM - get parm and tag from owner, group and mode bits. */
501 GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
503 *parm = status->st_uid | (status->st_gid << 15);
504 *parm |= (status->st_mode & 0x18) << 27;
505 *tag = status->st_mode & 0x7;
509 GetOGM(int fd, int *parm, int *tag)
511 struct afs_stat status;
512 if (afs_fstat(fd, &status) < 0)
515 GetOGMFromStat(&status, parm, tag);
519 int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
521 /* Derive the name and create it O_EXCL. If that fails we have an error.
522 * Get the tag from a free column in the link table.
525 namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
538 memset((void *)&tmp, 0, sizeof(IHandle_t));
541 tmp.ih_dev = volutil_GetPartitionID(part);
542 if (tmp.ih_dev == -1) {
548 /* Parameters for special file:
549 * p1 - volume id - goes into owner/group/mode
552 * p4 - parent volume id
557 tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
558 tmp.ih_ino = namei_MakeSpecIno(p1, p3);
560 int vno = p2 & NAMEI_VNODEMASK;
561 /* Parameters for regular file:
573 /* If GetFreeTag succeeds, it atomically sets link count to 1. */
574 tag = GetFreeTag(lh, p2);
578 /* name is <uniq(p3)><tag><vno(p2)> */
580 tmp.ih_ino = (Inode) p2;
581 tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
582 tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
587 namei_HandleToName(&name, &tmp);
588 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
590 if (errno == ENOTDIR || errno == ENOENT) {
591 if (namei_CreateDataDirectories(&name, &created_dir) < 0)
593 fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
601 if (SetOGM(fd, ogm_parm, tag) < 0) {
607 if (p2 == -1 && p3 == VI_LINKTABLE) {
608 /* hack at tmp to setup for set link count call. */
609 memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
612 code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
620 if (code || (fd < 0)) {
624 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
629 return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
635 namei_iopen(IHandle_t * h)
640 /* Convert handle to file name. */
641 namei_HandleToName(&name, h);
642 fd = afs_open(name.n_path, O_RDWR, 0666);
646 /* Need to detect vol special file and just unlink. In those cases, the
647 * handle passed in _is_ for the inode. We only check p1 for the special
651 namei_dec(IHandle_t * ih, Inode ino, int p1)
658 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
661 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
663 /* Verify this is the right file. */
664 IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
673 if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
674 FDH_REALLYCLOSE(fdP);
680 /* If it's the link table itself, decrement the link count. */
681 if (type == VI_LINKTABLE) {
682 if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
683 FDH_REALLYCLOSE(fdP);
689 if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
691 FDH_REALLYCLOSE(fdP);
697 FDH_REALLYCLOSE(fdP);
703 namei_HandleToName(&name, tmp);
704 if ((code = unlink(name.n_path)) == 0) {
705 if (type == VI_LINKTABLE) {
706 /* Try to remove directory. If it fails, that's ok.
707 * Salvage will clean up.
709 (void)namei_RemoveDataDirectories(&name);
712 FDH_REALLYCLOSE(fdP);
715 /* Get a file descriptor handle for this Inode */
721 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
722 FDH_REALLYCLOSE(fdP);
728 if (namei_SetLinkCount(fdP, ino, count, 1) < 0) {
729 FDH_REALLYCLOSE(fdP);
734 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
735 Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
736 th->ih_dev, th->ih_vid, (int64_t) th->ih_ino);
739 /* If we're less than 0, someone presumably unlinked;
740 don't bother setting count to 0, but we need to drop a lock */
741 if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
742 FDH_REALLYCLOSE(fdP);
748 IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
750 namei_HandleToName(&name, th);
752 code = unlink(name.n_path);
761 namei_inc(IHandle_t * h, Inode ino, int p1)
767 if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
768 int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
769 if (type != VI_LINKTABLE)
774 /* Get a file descriptor handle for this Inode */
780 if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
789 if (namei_SetLinkCount(fdP, ino, count, 1) < 0)
793 FDH_REALLYCLOSE(fdP);
800 /************************************************************************
801 * File Name Structure
802 ************************************************************************
804 * Each AFS file needs a unique name and it needs to be findable with
805 * minimal lookup time. Note that the constraint on the number of files and
806 * directories in a volume is the size of the vnode index files and the
807 * max file size AFS supports (for internal files) of 2^31. Since a record
808 * in the small vnode index file is 64 bytes long, we can have at most
809 * (2^31)/64 or 33554432 files. A record in the large index file is
810 * 256 bytes long, giving a maximum of (2^31)/256 = 8388608 directories.
811 * Another layout parameter is that there is roughly a 16 to 1 ratio between
812 * the number of files and the number of directories.
814 * Using this information we can see that a layout of 256 directories, each
815 * with 512 subdirectories and each of those having 512 files gives us
816 * 256*512*512 = 67108864 AFS files and directories.
818 * The volume, vnode, uniquifier and data version, as well as the tag
819 * are required, either for finding the file or for salvaging. It's best to
820 * restrict the name to something that can be mapped into 64 bits so the
821 * "Inode" is easily comparable (using "==") to other "Inodes". The tag
822 * is used to distinguish between different versions of the same file
823 * which are currently in the RW and clones of a volume. See "Link Table
824 * Organization" below for more information on the tag. The tag is
825 * required in the name of the file to ensure a unique name.
827 * We can store data in the uid, gid and mode bits of the files, provided
828 * the directories have root only access. This gives us 15 bits for each
829 * of uid and gid (GNU chown considers 65535 to mean "don't change").
830 * There are 9 available mode bits. Adn we need to store a total of
831 * 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
832 * or 131 bits somewhere.
834 * The format of a file name for a regular file is:
835 * /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
836 * V1 - low 8 bits of RW volume id
837 * V2 - all bits of RW volume id
838 * AA - high 8 bits of vnode number.
839 * BB - next 9 bits of vnode number.
840 * <tag><uniq><vno> - file name
842 * Volume special files are stored in a separate directory:
843 * /vicepX/AFSIDat/V1/V2/special/<tag><uniq><vno>
846 * The vnode is hashed into the directory using the high bits of the
847 * vnode number. This is so that consecutively created vnodes are in
848 * roughly the same area on the disk. This will at least be optimal if
849 * the user is creating many files in the same AFS directory. The name
850 * should be formed so that the leading characters are different as quickly
851 * as possible, leading to faster discards of incorrect matches in the
857 /************************************************************************
858 * Link Table Organization
859 ************************************************************************
861 * The link table volume special file is used to hold the link counts that
862 * are held in the inodes in inode based AFS vice filesystems. For user
863 * space access, the link counts are being kept in a separate
864 * volume special file. The file begins with the usual version stamp
865 * information and is then followed by one row per vnode number. vnode 0
866 * is used to hold the link count of the link table itself. That is because
867 * the same link table is shared among all the volumes of the volume group
868 * and is deleted only when the last volume of a volume group is deleted.
870 * Within each row, the columns are 3 bits wide. They can each hold a 0 based
871 * link count from 0 through 7. Each colume represents a unique instance of
872 * that vnode. Say we have a file shared between the RW and a RO and a
873 * different version of the file (or a different uniquifer) for the BU volume.
874 * Then one column would be holding the link count of 2 for the RW and RO
875 * and a different column would hold the link count of 1 for the BU volume.
876 * Note that we allow only 5 volumes per file, giving 15 bits used in the
879 #define LINKTABLE_WIDTH 2
880 #define LINKTABLE_SHIFT 1 /* log 2 = 1 */
883 namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
885 int toff = (int)(ino & NAMEI_VNODEMASK);
886 int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
888 *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8); /* * 2 + sizeof stamp */
889 *index = (tindex << 1) + tindex;
893 /* namei_GetLinkCount
894 * If lockit is set, lock the file and leave it locked upon a successful
898 namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
900 unsigned short row = 0;
905 /* there's no linktable yet. the salvager will create one later */
906 if (h->fd_fd == -1 && fixup)
908 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
911 if (flock(h->fd_fd, LOCK_EX) < 0)
915 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
916 goto bad_getLinkByte;
918 rc = read(h->fd_fd, (char *)&row, sizeof(row));
919 if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
921 if (rc == 0 && fixup) {
923 if (fstat(h->fd_fd, &st) || st.st_size >= offset+sizeof(row))
924 goto bad_getLinkByte;
925 FDH_TRUNC(h, offset+sizeof(row));
927 rc = write(h->fd_fd, (char *)&row, sizeof(row));
929 if (rc != sizeof(row)) {
930 goto bad_getLinkByte;
933 if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
935 if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
936 goto bad_getLinkByte;
937 rc = write(h->fd_fd, (char *)&row, sizeof(row));
938 if (rc != sizeof(row))
939 goto bad_getLinkByte;
942 return (int)((row >> index) & NAMEI_TAGMASK);
946 flock(h->fd_fd, LOCK_UN);
951 namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
953 return namei_GetLinkCount2(h, ino, lockit, 0, 1);
956 /* Return a free column index for this vnode. */
958 GetFreeTag(IHandle_t * ih, int vno)
972 /* Only one manipulates at a time. */
973 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
974 FDH_REALLYCLOSE(fdP);
978 offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
979 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
983 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
984 if (code != sizeof(row)) {
990 /* Now find a free column in this row and claim it. */
991 for (col = 0; col < NAMEI_MAXVOLS; col++) {
992 coldata = 7 << (col * 3);
993 if ((row & coldata) == 0)
996 if (col >= NAMEI_MAXVOLS) {
1001 coldata = 1 << (col * 3);
1004 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1007 if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
1011 flock(fdP->fd_fd, LOCK_UN);
1012 FDH_REALLYCLOSE(fdP);
1016 flock(fdP->fd_fd, LOCK_UN);
1017 FDH_REALLYCLOSE(fdP);
1023 /* namei_SetLinkCount
1024 * If locked is set, assume file is locked. Otherwise, lock file before
1025 * proceeding to modify it.
1028 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
1036 namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
1039 if (flock(fdP->fd_fd, LOCK_EX) < 0) {
1043 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1045 goto bad_SetLinkCount;
1049 code = read(fdP->fd_fd, (char *)&row, sizeof(row));
1050 if (code != sizeof(row)) {
1053 goto bad_SetLinkCount;
1060 row &= (unsigned short)~junk;
1061 row |= (unsigned short)count;
1063 if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
1065 goto bad_SetLinkCount;
1068 if (write(fdP->fd_fd, (char *)&row, sizeof(short)) != sizeof(short)) {
1070 goto bad_SetLinkCount;
1078 flock(fdP->fd_fd, LOCK_UN);
1084 /* ListViceInodes - write inode data to a results file. */
1085 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
1087 static int DecodeVolumeName(char *name, int *vid);
1088 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
1089 int (*write_fun) (FILE *,
1090 struct ViceInodeInfo *,
1091 char *, char *), FILE * fp,
1092 int (*judgeFun) (struct ViceInodeInfo *,
1094 int singleVolumeNumber, void *rock);
1099 * Write the inode data to the results file.
1101 * Returns -2 on error, 0 on success.
1103 * This is written as a callback simply so that other listing routines
1104 * can use the same inode reading code.
1107 WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
1110 n = fwrite(info, sizeof(*info), 1, fp);
1111 return (n == 1) ? 0 : -2;
1115 int mode_errors; /* Number of errors found in mode bits on directories. */
1117 VerifyDirPerms(char *path)
1119 struct afs_stat status;
1121 if (afs_stat(path, &status) < 0) {
1122 Log("Unable to stat %s. Please manually verify mode bits for this"
1123 " directory\n", path);
1125 if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
1131 * Fill the results file with the requested inode information.
1135 * -1 - complete failure, salvage should terminate.
1136 * -2 - not enough space on partition, salvager has error message for this.
1138 * This code optimizes single volume salvages by just looking at that one
1139 * volume's directory.
1141 * If the resultFile is NULL, then don't call the write routine.
1144 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
1145 int (*judgeInode) (struct ViceInodeInfo * info, int vid, void *rock),
1146 int singleVolumeNumber, int *forcep, int forceR, char *wpath,
1149 FILE *fp = (FILE *) - 1;
1151 struct afs_stat status;
1153 *forcep = 0; /* no need to salvage until further notice */
1156 fp = afs_fopen(resultFile, "w");
1158 Log("Unable to create inode description file %s\n", resultFile);
1163 /* Verify protections on directories. */
1165 VerifyDirPerms(mountedOn);
1168 namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp, judgeInode,
1169 singleVolumeNumber, rock);
1179 if (fflush(fp) == EOF) {
1180 Log("Unable to successfully flush inode file for %s\n", mountedOn);
1184 if (fsync(fileno(fp)) == -1) {
1185 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
1189 if (fclose(fp) == EOF) {
1190 Log("Unable to successfully close inode file for %s\n", mountedOn);
1195 * Paranoia: check that the file is really the right size
1197 if (afs_stat(resultFile, &status) == -1) {
1198 Log("Unable to successfully stat inode file for %s\n", mountedOn);
1201 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1202 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1203 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1211 /* namei_ListAFSFiles
1213 * Collect all the matching AFS files on the drive.
1214 * If singleVolumeNumber is non-zero, just return files for that volume.
1216 * Returns <0 on error, else number of files found to match.
1219 namei_ListAFSFiles(char *dev,
1220 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1222 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1223 int singleVolumeNumber, void *rock)
1229 struct dirent *dp1, *dp2;
1232 static void FreeZLCList(void);
1235 memset((void *)&ih, 0, sizeof(IHandle_t));
1236 ih.ih_dev = volutil_GetPartitionID(dev);
1238 if (singleVolumeNumber) {
1239 ih.ih_vid = singleVolumeNumber;
1240 namei_HandleToVolDir(&name, &ih);
1242 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1243 singleVolumeNumber, rock);
1247 /* Find all volume data directories and descend through them. */
1248 namei_HandleToInodeDir(&name, &ih);
1250 dirp1 = opendir(name.n_path);
1253 while ((dp1 = readdir(dirp1))) {
1254 if (*dp1->d_name == '.')
1256 (void)strcpy(path2, name.n_path);
1257 (void)strcat(path2, "/");
1258 (void)strcat(path2, dp1->d_name);
1259 dirp2 = opendir(path2);
1261 while ((dp2 = readdir(dirp2))) {
1262 if (*dp2->d_name == '.')
1264 if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
1266 namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
1283 /* namei_ListAFSSubDirs
1288 * > = 0 - number of AFS files found.
1291 namei_ListAFSSubDirs(IHandle_t * dirIH,
1292 int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
1294 int (*judgeFun) (struct ViceInodeInfo *, int, void *),
1295 int singleVolumeNumber, void *rock)
1297 IHandle_t myIH = *dirIH;
1299 char path1[512], path2[512], path3[512];
1300 DIR *dirp1, *dirp2, *dirp3;
1301 struct dirent *dp1, *dp2, *dp3;
1302 struct ViceInodeInfo info;
1303 FdHandle_t linkHandle;
1307 static void AddToZLCDeleteList(char dir, char *name);
1308 static void DeleteZLCFiles(char *path);
1311 namei_HandleToVolDir(&name, &myIH);
1312 (void)strcpy(path1, name.n_path);
1314 /* Do the directory containing the special files first to pick up link
1317 (void)strcat(path1, "/");
1318 (void)strcat(path1, NAMEI_SPECDIR);
1320 linkHandle.fd_fd = -1;
1321 dirp1 = opendir(path1);
1323 while ((dp1 = readdir(dirp1))) {
1324 if (*dp1->d_name == '.')
1326 if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
1328 if (info.u.param[2] != VI_LINKTABLE) {
1331 /* Open this handle */
1332 (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
1334 linkHandle.fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
1336 namei_GetLinkCount2(&linkHandle, (Inode) 0, 1, 1, Testing);
1338 if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
1341 if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
1342 if (linkHandle.fd_fd >= 0)
1343 close(linkHandle.fd_fd);
1352 /* Now run through all the other subdirs */
1353 namei_HandleToVolDir(&name, &myIH);
1354 (void)strcpy(path1, name.n_path);
1356 dirp1 = opendir(path1);
1358 while ((dp1 = readdir(dirp1))) {
1359 if (*dp1->d_name == '.')
1361 if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
1364 /* Now we've got a next level subdir. */
1365 (void)strcpy(path2, path1);
1366 (void)strcat(path2, "/");
1367 (void)strcat(path2, dp1->d_name);
1368 dirp2 = opendir(path2);
1370 while ((dp2 = readdir(dirp2))) {
1371 if (*dp2->d_name == '.')
1374 /* Now we've got to the actual data */
1375 (void)strcpy(path3, path2);
1376 (void)strcat(path3, "/");
1377 (void)strcat(path3, dp2->d_name);
1378 dirp3 = opendir(path3);
1380 while ((dp3 = readdir(dirp3))) {
1381 if (*dp3->d_name == '.')
1384 (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
1387 namei_GetLinkCount2(&linkHandle,
1388 info.inodeNumber, 1, 1, Testing);
1389 if (info.linkCount == 0) {
1391 Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
1392 AddToZLCDeleteList((char)i, dp3->d_name);
1394 Log("Found 0 link count file %s/%s.\n", path3,
1400 && !(*judgeFun) (&info, singleVolumeNumber, rock))
1403 if ((*writeFun) (fp, &info, path3, dp3->d_name) <
1405 close(linkHandle.fd_fd);
1422 if (linkHandle.fd_fd >= 0)
1423 close(linkHandle.fd_fd);
1425 /* Then why does this directory exist? Blow it away. */
1426 namei_HandleToVolDir(&name, dirIH);
1427 namei_RemoveDataDirectories(&name);
1434 DecodeVolumeName(char *name, int *vid)
1436 if (strlen(name) <= 2)
1438 *vid = (int)flipbase64_to_int64(name);
1445 * Get the inode number from the name.
1449 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
1452 struct afs_stat status;
1454 lb64_string_t check;
1456 (void)strcpy(fpath, dpath);
1457 (void)strcat(fpath, "/");
1458 (void)strcat(fpath, name);
1460 if (afs_stat(fpath, &status) < 0) {
1464 info->byteCount = status.st_size;
1465 info->inodeNumber = (Inode) flipbase64_to_int64(name);
1467 int64_to_flipbase64(check, info->inodeNumber);
1468 if (strcmp(name, check))
1471 GetOGMFromStat(&status, &parm, &tag);
1472 if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
1473 /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
1474 info->u.param[0] = parm;
1475 info->u.param[1] = -1;
1476 info->u.param[2] = tag;
1477 info->u.param[3] = volid;
1479 /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
1480 info->u.param[0] = volid;
1481 info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
1482 info->u.param[2] = (int)((info->inodeNumber >> NAMEI_UNIQSHIFT)
1483 & (Inode) NAMEI_UNIQMASK);
1484 info->u.param[3] = parm;
1490 * Convert the VolumeInfo file from RO to RW
1491 * this routine is called by namei_convertROtoRWvolume()
1495 convertVolumeInfo(fdr, fdw, vid)
1500 struct VolumeDiskData vd;
1503 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1504 sizeof(struct VolumeDiskData)) {
1505 Log("1 convertVolumeInfo: read failed for %lu with code %d\n", vid,
1509 vd.restoredFromId = vd.id; /* remember the RO volume here */
1511 vd.id = vd.parentId;
1514 vd.uniquifier += 5000; /* just in case there are still file copies from
1515 * the old RW volume around */
1516 p = strrchr(vd.name, '.');
1517 if (p && !strcmp(p, ".readonly")) {
1520 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1521 sizeof(struct VolumeDiskData)) {
1522 Log("1 convertVolumeInfo: write failed for %lu with code %d\n", vid,
1530 * Convert a RO-volume into a RW-volume
1532 * This function allows to recover very fast from the loss of a partition
1533 * from RO-copies if all RO-Copies exist on another partition.
1534 * Then these RO-volumes can be made to the new RW-volumes.
1535 * Backup of RW-volumes then consists in "vos release".
1537 * We must make sure in this partition exists only the RO-volume which
1538 * is typical for remote replicas.
1540 * Then the linktable is already ok,
1541 * the vnode files need to be renamed
1542 * the volinfo file needs to be replaced by another one with
1543 * slightly different contents and new name.
1544 * The volume header file of the RO-volume in the /vicep<x> directory
1545 * is destroyed by this call. A new header file for the RW-volume must
1546 * be created after return from this routine.
1550 namei_ConvertROtoRWvolume(char *pname, afs_int32 volumeId)
1552 #ifdef FSSYNC_BUILD_CLIENT
1554 char dir_name[512], oldpath[512], newpath[512];
1564 int code, fd, fd2, found;
1569 struct DiskPartition64 *partP;
1570 struct ViceInodeInfo info;
1571 struct VolumeDiskHeader h;
1573 char headername[16];
1574 afs_int32 error = 0;
1576 (void)afs_snprintf(headername, sizeof headername, VFORMAT, volumeId);
1577 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", pname, headername);
1578 fd = open(oldpath, O_RDONLY);
1580 Log("1 namei_ConvertROtoRWvolume: Couldn't open header for RO-volume %lu.\n", volumeId);
1583 if (read(fd, &h, sizeof(h)) != sizeof(h)) {
1584 Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n", volumeId);
1589 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1591 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1592 partP = partP->next);
1594 Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1597 ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
1598 IH_INIT(ih, partP->device, h.parent, ino);
1600 namei_HandleToName(&n, ih);
1601 strcpy(dir_name, n.n_path);
1602 p = strrchr(dir_name, '/');
1604 dirp = opendir(dir_name);
1606 Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
1610 while ((dp = readdir(dirp))) {
1611 /* struct ViceInodeInfo info; */
1613 if (*dp->d_name == '.')
1615 if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
1616 Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
1617 dir_name, dp->d_name);
1621 if (info.u.param[1] != -1) {
1622 Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
1626 if (info.u.param[0] != volumeId) {
1627 if (info.u.param[0] == ih->ih_vid) {
1628 if (info.u.param[2] == VI_LINKTABLE) { /* link table */
1633 Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n", dir_name, dp->d_name, info.u.param[0]);
1637 if (info.u.param[2] == VI_VOLINFO) { /* volume info file */
1638 strcpy(infoName, dp->d_name);
1640 } else if (info.u.param[2] == VI_SMALLINDEX) { /* small vnodes file */
1641 strcpy(smallName, dp->d_name);
1643 } else if (info.u.param[2] == VI_LARGEINDEX) { /* large vnodes file */
1644 strcpy(largeName, dp->d_name);
1648 Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
1654 if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
1655 Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
1660 * If we come here then there was only a RO-volume and we can safely
1664 memset(&t_ih, 0, sizeof(t_ih));
1665 t_ih.ih_dev = ih->ih_dev;
1666 t_ih.ih_vid = ih->ih_vid;
1668 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
1669 fd = afs_open(oldpath, O_RDWR, 0);
1671 Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
1675 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
1676 namei_HandleToName(&n, &t_ih);
1677 fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
1679 Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
1683 code = convertVolumeInfo(fd, fd2, ih->ih_vid);
1690 SetOGM(fd2, ih->ih_vid, 1);
1693 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
1694 namei_HandleToName(&n, &t_ih);
1695 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
1696 fd = afs_open(newpath, O_RDWR, 0);
1698 Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
1701 SetOGM(fd, ih->ih_vid, 2);
1703 link(newpath, n.n_path);
1706 t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
1707 namei_HandleToName(&n, &t_ih);
1708 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
1709 fd = afs_open(newpath, O_RDWR, 0);
1711 Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
1714 SetOGM(fd, ih->ih_vid, 3);
1716 link(newpath, n.n_path);
1722 h.volumeInfo_hi = h.id;
1723 h.smallVnodeIndex_hi = h.id;
1724 h.largeVnodeIndex_hi = h.id;
1725 h.linkTable_hi = h.id;
1726 (void)afs_snprintf(headername, sizeof headername, VFORMAT, h.id);
1727 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", pname, headername);
1728 fd = open(newpath, O_CREAT | O_EXCL | O_RDWR, 0644);
1730 Log("1 namei_ConvertROtoRWvolume: Couldn't create header for RW-volume %lu.\n", h.id);
1733 if (write(fd, &h, sizeof(h)) != sizeof(h)) {
1734 Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume\
1740 if (unlink(oldpath) < 0) {
1741 Log("1 namei_ConvertROtoRWvolume: Couldn't unlink RO header, error = %d\n", error);
1743 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1744 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1751 * returns a static string used to print either 32 or 64 bit inode numbers.
1754 PrintInode(char *s, Inode ino)
1756 static afs_ino_str_t result;
1760 (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
1767 /* Routines to facilitate removing zero link count files. */
1768 #define MAX_ZLC_NAMES 32
1769 #define MAX_ZLC_NAMELEN 16
1770 typedef struct zlcList_s {
1771 struct zlcList_s *zlc_next;
1773 char zlc_names[MAX_ZLC_NAMES][MAX_ZLC_NAMELEN];
1776 static zlcList_t *zlcAnchor = NULL;
1777 static zlcList_t *zlcCur = NULL;
1780 AddToZLCDeleteList(char dir, char *name)
1782 assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
1784 if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
1785 if (zlcCur && zlcCur->zlc_next)
1786 zlcCur = zlcCur->zlc_next;
1788 zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
1794 zlcCur->zlc_next = tmp;
1798 zlcCur->zlc_next = NULL;
1802 (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
1807 DeleteZLCFiles(char *path)
1813 for (z = zlcAnchor; z; z = z->zlc_next) {
1814 for (i = 0; i < z->zlc_n; i++) {
1815 (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
1816 if (namei_unlink(fname) < 0) {
1817 Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
1821 z->zlc_n = 0; /* Can reuse space. */
1834 tnext = i->zlc_next;
1838 zlcCur = zlcAnchor = NULL;
1842 #endif /* AFS_NAMEI_ENV */