/*
* Copyright 2000, International Business Machines Corporation and others.
* All Rights Reserved.
- *
+ *
* This software has been released under the terms of the IBM Public
* License. For details, see the LICENSE file in the top-level source
* directory or online at http://www.openafs.org/dl/license10.html
#include <afsconfig.h>
#include <afs/param.h>
+#include <roken.h>
+
#ifdef AFS_NAMEI_ENV
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <dirent.h>
-#include <afs/assert.h>
-#include <string.h>
-#include <sys/file.h>
-#include <sys/param.h>
-#include <lock.h>
-#if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
-#include <unistd.h>
+
+#ifdef HAVE_SYS_FILE_H
+# include <sys/file.h>
+#endif
+
+#ifdef AFS_NT40_ENV
+#define DELETE_ZLC
+#include <windows.h>
+#include <winnt.h>
+#include <winbase.h>
+#include <direct.h>
+#endif
+
+#include <afs/opr.h>
+#include <rx/rx_queue.h>
+#ifdef AFS_PTHREAD_ENV
+# include <opr/lock.h>
#endif
+#include <lock.h>
#include <afs/afsutil.h>
#include <lwp.h>
#include "nfs.h"
#include "common.h"
#include <afs/errors.h>
-/*@+fcnmacros +macrofcndecl@*/
-#ifdef O_LARGEFILE
-#ifdef S_SPLINT_S
-extern off64_t afs_lseek(int FD, off64_t O, int F);
-#endif /*S_SPLINT_S */
-#define afs_lseek(FD, O, F) lseek64(FD, (off64_t)(O), F)
-#define afs_stat stat64
-#define afs_fstat fstat64
-#define afs_open open64
-#define afs_fopen fopen64
-#else /* !O_LARGEFILE */
-#ifdef S_SPLINT_S
-extern off_t afs_lseek(int FD, off_t O, int F);
-#endif /*S_SPLINT_S */
-#define afs_lseek(FD, O, F) lseek(FD, (off_t)(O), F)
-#define afs_stat stat
-#define afs_fstat fstat
-#define afs_open open
-#define afs_fopen fopen
-#endif /* !O_LARGEFILE */
-/*@=fcnmacros =macrofcndecl@*/
+#ifdef AFS_NT40_ENV
+#include <afs/errmap_nt.h>
+#endif
#ifndef LOCK_SH
#define LOCK_SH 1 /* shared lock */
#define LOCK_UN 8 /* unlock */
#endif
-#ifndef HAVE_FLOCK
-#include <fcntl.h>
-
-/*
- * This function emulates a subset of flock()
- */
-int
-emul_flock(int fd, int cmd)
-{ struct flock f;
-
- memset(&f, 0, sizeof (f));
-
- if (cmd & LOCK_UN)
- f.l_type = F_UNLCK;
- if (cmd & LOCK_SH)
- f.l_type = F_RDLCK;
- if (cmd & LOCK_EX)
- f.l_type = F_WRLCK;
-
- return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
-}
-
-#define flock(f,c) emul_flock(f,c)
+#ifdef AFS_SALSRV_ENV
+#include <pthread.h>
+#include <afs/work_queue.h>
+#include <vol/vol-salvage.h>
#endif
int Testing=0;
+static void namei_UnlockLinkCount(FdHandle_t * fdP, Inode ino);
afs_sfsize_t
namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
if (fdP == NULL)
return -1;
- if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
+ nBytes = FDH_PREAD(fdP, buf, size, offset);
+ if (nBytes < 0)
FDH_REALLYCLOSE(fdP);
- return -1;
- }
-
- nBytes = FDH_READ(fdP, buf, size);
- FDH_CLOSE(fdP);
+ else
+ FDH_CLOSE(fdP);
return nBytes;
}
if (fdP == NULL)
return -1;
- if (FDH_SEEK(fdP, offset, SEEK_SET) < 0) {
+ nBytes = FDH_PWRITE(fdP, buf, size, offset);
+ if (nBytes < 0)
FDH_REALLYCLOSE(fdP);
- return -1;
- }
- nBytes = FDH_WRITE(fdP, buf, size);
- FDH_CLOSE(fdP);
+ else
+ FDH_CLOSE(fdP);
return nBytes;
}
-
-
+#ifdef AFS_NT40_ENV
+/* Inode number format:
+ * low 32 bits - if a regular file or directory, the vnode; else the type.
+ * 32-36 - uniquifier tag and index into counts array for this vnode. Only
+ * two of the available bits are currently used. The rest are
+ * present in case we ever increase the number of types of volumes
+ * in the volume group.
+ * bit 37 : 1 == special, 0 == regular
+ */
+# define NAMEI_VNODEMASK 0x00ffffffff
+# define NAMEI_TAGSHIFT 32
+# define NAMEI_INODESPECIAL 0x2000000000
+# define NAMEI_SPECDIR "R"
+# define NAMEI_SPECDIRC 'R'
+#else /* !AFS_NT40_ENV */
/* Inode number format:
* low 26 bits - vnode number - all 1's if volume special file.
- * next 3 bits - tag
+ * next 3 bits - tag
* next 3 bits spare (0's)
* high 32 bits - uniquifier (regular) or type if spare
*/
-#define NAMEI_VNODEMASK 0x003ffffff
-#define NAMEI_TAGMASK 0x7
-#define NAMEI_TAGSHIFT 26
-#define NAMEI_UNIQMASK 0xffffffff
-#define NAMEI_UNIQSHIFT 32
-#define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
-#define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
-
+# define NAMEI_VNODEMASK 0x003ffffff
+# define NAMEI_TAGSHIFT 26
+# define NAMEI_UNIQMASK 0xffffffff
+# define NAMEI_UNIQSHIFT 32
+# define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
/* dir1 is the high 8 bits of the 26 bit vnode */
-#define VNO_DIR1(vno) ((vno >> 14) & 0xff)
+# define VNO_DIR1(vno) ((vno >> 14) & 0xff)
/* dir2 is the next 9 bits */
-#define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
+# define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
/* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
+# define NAMEI_SPECDIR "special"
+#endif /* !AFS_NT40_ENV */
+#define NAMEI_TAGMASK 0x7
+#define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
-#define NAMEI_SPECDIR "special"
#define NAMEI_SPECDIRLEN (sizeof(NAMEI_SPECDIR)-1)
#define NAMEI_MAXVOLS 5 /* Maximum supported number of volumes per volume
* group at once.
*/
-
+#ifndef AFS_NT40_ENV
typedef struct {
int ogm_owner;
int ogm_group;
int ogm_mode;
} namei_ogm_t;
-
-static int namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite);
+#endif
static int GetFreeTag(IHandle_t * ih, int vno);
* Format: /<vicepx>/INODEDIR
*
*/
+#ifdef AFS_NT40_ENV
+static void
+namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
+{
+ memset(name, '\0', sizeof(*name));
+ nt_DevToDrive(name->n_drive, ih->ih_dev);
+ strlcpy(name->n_path, name->n_drive, sizeof(name->n_path));
+}
+
+#else
+/* Format: /<vicepx>/INODEDIR */
#define PNAME_BLEN 64
static void
namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
*/
volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
- name->n_base[offset] = '/';
+ name->n_base[offset] = OS_DIRSEPC;
offset++;
strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
}
+#endif
-#define addtoname(N, C) \
-do { \
- strlcat((N)->n_path, "/", sizeof((N)->n_path)); \
- strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
+#define addtoname(N, C) \
+do { \
+ if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC) \
+ strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path)); \
+ strlcat((N)->n_path, (C), sizeof((N)->n_path)); \
} while(0)
+#ifdef AFS_NT40_ENV
+static void
+namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
+{
+ /* X:\Vol_XXXXXXX.data */
+ b32_string_t str1;
+ char *namep;
+
+ namei_HandleToInodeDir(name, ih);
+ /* nt_drive added to name by namei_HandleToInodeDir() */
+ namep = name->n_voldir;
+ (void)memcpy(namep, "\\Vol_", 5);
+ namep += 5;
+ (void)strcpy(namep, int_to_base32(str1, ih->ih_vid));
+ namep += strlen(namep);
+ memcpy(namep, ".data", 5);
+ namep += 5;
+ *namep = '\0';
+ addtoname(name, name->n_voldir);
+}
+#else
static void
namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
{
strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
addtoname(name, name->n_voldir2);
}
+#endif
/* namei_HandleToName
*
* Constructs a file name for the fully qualified handle.
- * Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special
*/
+#ifdef AFS_NT40_ENV
+/* Note that special files end up in X:\Vol_XXXXXXX.data\R */
+void
+namei_HandleToName(namei_t * name, IHandle_t * ih)
+{
+ int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
+ int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
+ int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
+ b32_string_t str1;
+ char *namep;
+ namei_HandleToVolDir(name, ih);
+
+ if (special) {
+ name->n_dir[0] = NAMEI_SPECDIRC;
+ } else {
+ if (vno & 0x1)
+ name->n_dir[0] = 'Q';
+ else
+ name->n_dir[0] = ((vno & 0x1f) >> 1) + 'A';
+
+ }
+ name->n_dir[1] = '\0';
+ addtoname(name, name->n_dir);
+ /* X:\Vol_XXXXXXX.data\X\V_XXXXXXX.XXX */
+ namep = name->n_inode;
+ (void)memcpy(namep, "\\V_", 3);
+ namep += 3;
+ (void)strcpy(namep, int_to_base32(str1, vno));
+ namep += strlen(namep);
+ *(namep++) = '.';
+ (void)strcpy(namep, int_to_base32(str1, tag));
+ namep += strlen(namep);
+ addtoname(name, name->n_inode);
+}
+#else
+/* Note that special files end up in /vicepX/InodeDir/Vxx/V*.data/special */
void
namei_HandleToName(namei_t * name, IHandle_t * ih)
{
- lb64_string_t str;
int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
+ lb64_string_t str;
namei_HandleToVolDir(name, ih);
strlcpy(name->n_inode, str, sizeof(name->n_inode));
addtoname(name, name->n_inode);
}
+#endif
+#ifndef AFS_NT40_ENV
/* The following is a warning to tell sys-admins to not muck about in this
* name space.
*/
namei_ViceREADME(char *partition)
{
char filename[32];
- int fd;
+ int fd, len, e = 0;
/* Create the inode directory if we're starting for the first time */
- (void)afs_snprintf(filename, sizeof filename, "%s/%s", partition,
- INODEDIR);
+ snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
+ INODEDIR);
mkdir(filename, 0700);
- (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
- INODEDIR);
- fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
- if (fd >= 0) {
- (void)write(fd, VICE_README, strlen(VICE_README));
- close(fd);
+ snprintf(filename, sizeof filename,
+ "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
+ partition, INODEDIR);
+ fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
+ if (fd != INVALID_FD) {
+ len = strlen(VICE_README);
+ if (OS_WRITE(fd, VICE_README, len) != len)
+ e = errno;
+ OS_CLOSE(fd);
+ if (e)
+ errno = e;
}
return (errno);
}
+#endif
+
+/* namei_CreateDataDirectories
+ *
+ * If creating the file failed because of ENOENT or ENOTDIR, try
+ * creating all the directories first.
+ */
+#ifdef AFS_NT40_ENV
+static int
+namei_CreateDataDirectories(namei_t * name, int *created)
+{
+ char tmp[256];
+ char *s;
+ int i;
+ *created = 0;
+ snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
+
+ if (mkdir(tmp) < 0) {
+ if (errno != EEXIST)
+ return -1;
+ } else
+ *created = 1;
+ s = tmp;
+ s += strlen(tmp);
+
+ *s++ = OS_DIRSEPC;
+ *(s + 1) = '\0';
+ for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
+ *s = (char)i;
+ if (mkdir(tmp) < 0 && errno != EEXIST)
+ return -1;
+ }
+ return 0;
+}
+#else
#define create_dir() \
do { \
if (mkdir(tmp, 0700)<0) { \
#define create_nextdir(A) \
do { \
- strcat(tmp, "/"); strcat(tmp, A); create_dir(); \
+ strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir(); \
} while(0)
-/* namei_CreateDataDirectories
- *
- * If creating the file failed because of ENOENT or ENOTDIR, try
- * creating all the directories first.
- */
static int
namei_CreateDataDirectories(namei_t * name, int *created)
{
}
return 0;
}
+#endif
+#ifndef AFS_NT40_ENV
/* delTree(): Deletes an entire tree of directories (no files)
* Input:
* root : Full path to the subtree. Should be big enough for PATH_MAX
* Output:
* errp : errno of the first error encountered during the directory cleanup.
* *errp should have been initialized to 0.
- *
+ *
* Return Values:
- * -1 : If errors were encountered during cleanup and error is set to
+ * -1 : If errors were encountered during cleanup and error is set to
* the first errno.
* 0 : Success.
*
if (*tree) {
/* delete the children first */
- cp = strchr(tree, '/');
+ cp = strchr(tree, OS_DIRSEPC);
if (cp) {
delTree(root, cp + 1, errp);
*cp = '\0';
cp = tree + strlen(tree); /* move cp to the end of string tree */
/* now delete all entries in this dir */
- if ((ds = opendir(root)) != (DIR *) NULL) {
+ if ((ds = opendir(root)) != NULL) {
errno = 0;
while ((dirp = readdir(ds))) {
/* ignore . and .. */
if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, ".."))
continue;
/* since root is big enough, we reuse the space to
- * concatenate the dirname to the current tree
+ * concatenate the dirname to the current tree
*/
- strcat(root, "/");
+ strcat(root, OS_DIRSEP);
strcat(root, dirp->d_name);
if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
/* delete this subtree */
} else
*errp = *errp ? *errp : errno;
- /* recover path to our cur tree by truncating it to
- * its original len
+ /* recover path to our cur tree by truncating it to
+ * its original len
*/
*cp = 0;
}
return 0;
}
+#endif
/* namei_RemoveDataDirectories
* Return Values:
* Returns 0 on success.
- * Returns -1 on error. Typically, callers ignore this error bcause we
+ * Returns -1 on error. Typically, callers ignore this error because we
* can continue running if the removes fail. The salvage process will
- * finish tidying up for us. We only use the n_base and n_voldir1 entries
- * and only do rmdir's.
+ * finish tidying up for us.
*/
+#ifdef AFS_NT40_ENV
static int
namei_RemoveDataDirectories(namei_t * name)
{
- char pbuf[MAXPATHLEN], *path = pbuf;
+ int code = 0;
+ char *path;
+ char tmp[256];
+ int i;
+
+ snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
+
+ path = tmp;
+ path += strlen(path);
+ *path++ = OS_DIRSEPC;
+ *(path + 1) = '\0';
+ for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
+ *path = (char)i;
+ if (rmdir(name->n_path) < 0 && errno != ENOENT)
+ code = -1;
+ }
+
+ if (!code) {
+ /* Delete the Vol_NNNNNN.data directory. */
+ path--;
+ *path = '\0';
+ if (rmdir(name->n_path) < 0 && errno != ENOENT) {
+ code = -1;
+ }
+ }
+ return code;
+}
+#else
+/*
+ * We only use the n_base and n_voldir1 entries
+ * and only do rmdir's.
+ */
+static int
+namei_RemoveDataDirectories(namei_t * name)
+{
+ int code = 0;
+ char *path;
int prefixlen = strlen(name->n_base), err = 0;
+ int vollen = strlen(name->n_voldir1);
+ char pbuf[MAXPATHLEN];
+
+ path = pbuf;
strlcpy(path, name->n_path, sizeof(pbuf));
- /* move past the prefix */
- path = path + prefixlen + 1; /* skip over the trailing / */
+ /* move past the prefix and n_voldir1 */
+ path = path + prefixlen + 1 + vollen + 1; /* skip over the trailing / */
/* now delete all dirs upto path */
- return delTree(pbuf, path, &err);
-
+ code = delTree(pbuf, path, &err);
+
+ /* We've now deleted everything under /n_base/n_voldir1/n_voldir2 that
+ * we could. Do not delete /n_base/n_voldir1, since doing such might
+ * interrupt another thread trying to create a volume. We could introduce
+ * some locking to make this safe (or only remove it for whole-partition
+ * salvages), but by not deleting it we only leave behind a maximum of
+ * 256 empty directories. So at least for now, don't bother. */
+ return code;
}
+#endif
/* Create the file in the name space.
*
* types, but if we get that far, this could should be dead by then.
*/
Inode
-namei_MakeSpecIno(int volid, int type)
+namei_MakeSpecIno(VolumeId volid, int type)
{
Inode ino;
ino = NAMEI_INODESPECIAL;
+#ifdef AFS_NT40_ENV
+ ino |= type;
+ /* tag is always 0 for special */
+#else
type &= NAMEI_TAGMASK;
ino |= ((Inode) type) << NAMEI_TAGSHIFT;
ino |= ((Inode) volid) << NAMEI_UNIQSHIFT;
+#endif
return ino;
}
-/* SetOGM - set owner group and mode bits from parm and tag
- *
+#ifdef AFS_NT40_ENV
+/* SetOGM */
+static int
+SetOGM(FD_t fd, int parm, int tag)
+{
+ return -1;
+}
+
+static int
+SetWinOGM(FD_t fd, int p1, int p2)
+{
+ BOOL code;
+ FILETIME ftime;
+
+ ftime.dwHighDateTime = p1;
+ ftime.dwLowDateTime = p2;
+
+ code = SetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
+ if (!code)
+ return -1;
+ return 0;
+}
+
+static int
+GetWinOGM(FD_t fd, int *p1, int *p2)
+{
+ BOOL code;
+ FILETIME ftime;
+
+ code = GetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
+ if (!code)
+ return -1;
+
+ *p1 = ftime.dwHighDateTime;
+ *p2 = ftime.dwLowDateTime;
+
+ return 0;
+}
+
+static int
+CheckOGM(FdHandle_t *fdP, int p1)
+{
+ int ogm_p1, ogm_p2;
+
+ if (GetWinOGM(fdP->fd_fd, &ogm_p1, &ogm_p2)) {
+ return -1;
+ }
+
+ if (ogm_p1 != p1) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+FixSpecialOGM(FdHandle_t *fdP, int check)
+{
+ Inode ino = fdP->fd_ih->ih_ino;
+ VnodeId vno = NAMEI_VNODESPECIAL, ogm_vno;
+ int ogm_volid;
+
+ if (GetWinOGM(fdP->fd_fd, &ogm_volid, &ogm_vno)) {
+ return -1;
+ }
+
+ /* the only thing we can check is the vnode number; for the volid we have
+ * nothing else to compare against */
+ if (vno != ogm_vno) {
+ if (check) {
+ return -1;
+ }
+ if (SetWinOGM(fdP->fd_fd, ogm_volid, vno)) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+#else /* AFS_NT40_ENV */
+/* SetOGM - set owner group and mode bits from parm and tag */
+static int
+SetOGM(FD_t fd, int parm, int tag)
+{
+/*
* owner - low 15 bits of parm.
* group - next 15 bits of parm.
* mode - 2 bits of parm, then lowest = 3 bits of tag.
*/
-static int
-SetOGM(int fd, int parm, int tag)
-{
int owner, group, mode;
owner = parm & 0x7fff;
mode |= tag & 0x7;
if (fchmod(fd, mode) < 0)
return -1;
-
return 0;
-
}
/* GetOGM - get parm and tag from owner, group and mode bits. */
static void
-GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
+GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
{
*parm = status->st_uid | (status->st_gid << 15);
*parm |= (status->st_mode & 0x18) << 27;
}
static int
-GetOGM(int fd, int *parm, int *tag)
+GetOGM(FdHandle_t *fdP, int *parm, int *tag)
{
- struct afs_stat status;
- if (afs_fstat(fd, &status) < 0)
+ struct afs_stat_st status;
+ if (afs_fstat(fdP->fd_fd, &status) < 0)
return -1;
-
GetOGMFromStat(&status, parm, tag);
return 0;
}
+static int
+CheckOGM(FdHandle_t *fdP, int p1)
+{
+ int parm, tag;
+
+ if (GetOGM(fdP, &parm, &tag) < 0)
+ return -1;
+ if (parm != p1)
+ return -1;
+
+ return 0;
+}
+
+static int
+FixSpecialOGM(FdHandle_t *fdP, int check)
+{
+ int inode_volid, ogm_volid;
+ int inode_type, ogm_type;
+ Inode ino = fdP->fd_ih->ih_ino;
+
+ inode_volid = ((ino >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK);
+ inode_type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
+
+ if (GetOGM(fdP, &ogm_volid, &ogm_type) < 0) {
+ Log("Error retrieving OGM info\n");
+ return -1;
+ }
+
+ if (inode_volid != ogm_volid || inode_type != ogm_type) {
+ Log("%sIncorrect OGM data (ino: vol %u type %d) (ogm: vol %u type %d)\n",
+ check?"":"Fixing ", inode_volid, inode_type, ogm_volid, ogm_type);
+
+ if (check) {
+ return -1;
+ }
+
+ if (SetOGM(fdP->fd_fd, inode_volid, inode_type) < 0) {
+ Log("Error setting OGM data\n");
+ return -1;
+ }
+ }
+ return 0;
+}
+
+#endif /* !AFS_NT40_ENV */
+
+/**
+ * Check/fix the OGM data for an inode
+ *
+ * @param[in] fdP Open file handle for the inode to check
+ * @param[in] check 1 to just check the OGM data, and return an error if it
+ * is incorrect. 0 to fix the OGM data if it is incorrect.
+ *
+ * @pre fdP must be for a special inode
+ *
+ * @return status
+ * @retval 0 success
+ * @retval -1 error
+ */
+int
+namei_FixSpecialOGM(FdHandle_t *fdP, int check)
+{
+ int vnode;
+ Inode ino = fdP->fd_ih->ih_ino;
+
+ vnode = (int)(ino & NAMEI_VNODEMASK);
+ if (vnode != NAMEI_VNODESPECIAL) {
+ Log("FixSpecialOGM: non-special vnode %u\n", vnode);
+ return -1;
+ }
+
+ return FixSpecialOGM(fdP, check);
+}
+
int big_vno = 0; /* Just in case we ever do 64 bit vnodes. */
/* Derive the name and create it O_EXCL. If that fails we have an error.
* Get the tag from a free column in the link table.
*/
+#ifdef AFS_NT40_ENV
Inode
-namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
+namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
{
namei_t name;
- int fd = -1;
+ FD_t fd = INVALID_FD;
int code = 0;
int created_dir = 0;
IHandle_t tmp;
FdHandle_t *fdP;
FdHandle_t tfd;
- int tag;
- int ogm_parm;
-
+ int type, tag;
+ int ogm_p1, ogm_p2;
+ char *p;
+ b32_string_t str1;
memset((void *)&tmp, 0, sizeof(IHandle_t));
+ memset(&tfd, 0, sizeof(FdHandle_t));
+ ih_PkgDefaults();
- tmp.ih_dev = volutil_GetPartitionID(part);
+ tmp.ih_dev = nt_DriveToDev(part);
if (tmp.ih_dev == -1) {
errno = EINVAL;
return -1;
}
- if (p2 == -1) {
+ if (p2 == INODESPECIAL) {
/* Parameters for special file:
* p1 - volume id - goes into owner/group/mode
- * p2 - vnode == -1
+ * p2 - vnode == INODESPECIAL
* p3 - type
* p4 - parent volume id
*/
- ogm_parm = p1;
- tag = p3;
-
+ ogm_p1 = p1;
+ ogm_p2 = p2;
+ type = p3;
tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
tmp.ih_ino = namei_MakeSpecIno(p1, p3);
} else {
errno = EINVAL;
return -1;
}
- /* If GetFreeTag succeeds, it atomically sets link count to 1. */
- tag = GetFreeTag(lh, p2);
- if (tag < 0)
- goto bad;
- /* name is <uniq(p3)><tag><vno(p2)> */
tmp.ih_vid = p1;
tmp.ih_ino = (Inode) p2;
- tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
- tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
-
- ogm_parm = p4;
+ ogm_p1 = p3;
+ ogm_p2 = p4;
}
namei_HandleToName(&name, &tmp);
- fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
- if (fd < 0) {
- if (errno == ENOTDIR || errno == ENOENT) {
- if (namei_CreateDataDirectories(&name, &created_dir) < 0)
- goto bad;
- fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
- 0);
- if (fd < 0)
- goto bad;
- } else {
- goto bad;
+ p = strrchr((char *)&name.n_path, '.');
+ p++;
+ for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
+ *p = *int_to_base32(str1, tag);
+ fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
+ if (fd == INVALID_FD) {
+ if (errno == ENOTDIR || errno == ENOENT) {
+ if (namei_CreateDataDirectories(&name, &created_dir) == 0)
+ fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
+ }
+ }
+
+ if (fd != INVALID_FD)
+ break;
+ if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
+ break;
+ }
+ if (fd == INVALID_FD) {
+ code = -1;
+ goto bad;
+ }
+ tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
+ tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
+
+ if (!code) {
+ if (SetWinOGM(fd, ogm_p1, ogm_p2)) {
+ errno = OS_ERROR(EBADF);
+ code = -1;
+ }
+ }
+
+ if (!code) {
+ if (p2 != INODESPECIAL) {
+ if (fd == INVALID_FD) {
+ errno = ENOENT;
+ code = nt_unlink((char *)&name.n_path);
+ code = -1;
+ goto bad;
+ }
+ fdP = IH_OPEN(lh);
+ if (fdP == NULL) {
+ code = -1;
+ goto bad;
+ }
+ code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
+ FDH_CLOSE(fdP);
+ } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
+ if (fd == INVALID_FD)
+ goto bad;
+ /* hack at tmp to setup for set link count call. */
+ tfd.fd_fd = fd;
+ code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
+ }
+ }
+
+bad:
+ if (fd != INVALID_FD)
+ OS_CLOSE(fd);
+
+ if (code || (fd == INVALID_FD)) {
+ if (p2 != INODESPECIAL) {
+ fdP = IH_OPEN(lh);
+ if (fdP) {
+ namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
+ FDH_CLOSE(fdP);
+ }
+ }
+
+ if (created_dir) {
+ int save_errno = errno;
+ namei_RemoveDataDirectories(&name);
+ errno = save_errno;
}
}
- if (SetOGM(fd, ogm_parm, tag) < 0) {
- close(fd);
- fd = -1;
- goto bad;
- }
-
- if (p2 == -1 && p3 == VI_LINKTABLE) {
- /* hack at tmp to setup for set link count call. */
- memset((void *)&tfd, 0, sizeof(FdHandle_t)); /* minimalistic still, but a little cleaner */
- tfd.fd_ih = &tmp;
- tfd.fd_fd = fd;
- code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
- }
-
- bad:
- if (fd >= 0)
- close(fd);
-
-
- if (code || (fd < 0)) {
- if (p2 != -1) {
- fdP = IH_OPEN(lh);
+ return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
+}
+#else /* !AFS_NT40_ENV */
+Inode
+icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
+ IHandle_t **a_ih)
+{
+ namei_t name;
+ int fd = INVALID_FD;
+ int code = 0;
+ int created_dir = 0;
+ IHandle_t tmp;
+ IHandle_t *realh = NULL;
+ FdHandle_t *fdP;
+ int tag;
+ int ogm_parm;
+
+ memset((void *)&tmp, 0, sizeof(IHandle_t));
+
+ ih_PkgDefaults();
+
+ tmp.ih_dev = volutil_GetPartitionID(part);
+ if (tmp.ih_dev == -1) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (p2 == -1) {
+ /* Parameters for special file:
+ * p1 - volume id - goes into owner/group/mode
+ * p2 - vnode == -1
+ * p3 - type
+ * p4 - parent volume id
+ */
+ ogm_parm = p1;
+
+ tag = p3;
+ tmp.ih_vid = p4; /* Use parent volume id, where this file will be. */
+ tmp.ih_ino = namei_MakeSpecIno(p1, p3);
+ } else {
+ int vno = p2 & NAMEI_VNODEMASK;
+ /* Parameters for regular file:
+ * p1 - volume id
+ * p2 - vnode
+ * p3 - uniq
+ * p4 - dv
+ */
+
+ if (vno != p2) {
+ big_vno++;
+ errno = EINVAL;
+ return -1;
+ }
+ /* If GetFreeTag succeeds, it atomically sets link count to 1. */
+ tag = GetFreeTag(lh, p2);
+ if (tag < 0)
+ goto bad;
+
+ tmp.ih_vid = p1;
+ tmp.ih_ino = (Inode) p2;
+ /* name is <uniq(p3)><tag><vno(p2)> */
+ tmp.ih_ino |= ((Inode) tag) << NAMEI_TAGSHIFT;
+ tmp.ih_ino |= ((Inode) p3) << NAMEI_UNIQSHIFT;
+
+ ogm_parm = p4;
+ }
+
+ namei_HandleToName(&name, &tmp);
+ fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
+ if (fd == INVALID_FD) {
+ if (errno == ENOTDIR || errno == ENOENT) {
+ if (namei_CreateDataDirectories(&name, &created_dir) < 0)
+ goto bad;
+ fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR,
+ 0);
+ if (fd == INVALID_FD)
+ goto bad;
+ } else {
+ goto bad;
+ }
+ }
+ if (SetOGM(fd, ogm_parm, tag) < 0) {
+ OS_CLOSE(fd);
+ fd = INVALID_FD;
+ goto bad;
+ }
+
+ IH_INIT(realh, tmp.ih_dev, tmp.ih_vid, tmp.ih_ino);
+ fdP = ih_attachfd(realh, fd);
+
+ /* ih_attachfd can only return NULL if we give it an invalid fd; our fd
+ * must be valid by this point. */
+ opr_Assert(fdP);
+
+ if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
+ code = namei_SetLinkCount(fdP, (Inode) 0, 1, 0);
+ }
+
+ FDH_CLOSE(fdP);
+
+ bad:
+ if (code || (fd == INVALID_FD)) {
+ if (p2 != -1) {
+ fdP = IH_OPEN(lh);
if (fdP) {
namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
FDH_CLOSE(fdP);
}
}
+ IH_RELEASE(realh);
}
- return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
+
+ *a_ih = realh;
+
+ return code;
}
+Inode
+namei_icreate(IHandle_t * lh, char *part,
+ afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+{
+ Inode ino;
+ IHandle_t *ihP = NULL;
+ int code;
+
+ code = icreate(lh, part, p1, p2, p3, p4, &ihP);
+ if (code || !ihP) {
+ opr_Assert(!ihP);
+ ino = -1;
+ } else {
+ ino = ihP->ih_ino;
+ IH_RELEASE(ihP);
+ }
+ return ino;
+}
+
+IHandle_t *
+namei_icreate_init(IHandle_t * lh, int dev, char *part,
+ afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+{
+ int code;
+ IHandle_t *ihP = NULL;
+
+ code = icreate(lh, part, p1, p2, p3, p4, &ihP);
+ if (code) {
+ opr_Assert(!ihP);
+ }
+ return ihP;
+}
+#endif
/* namei_iopen */
-int
+FD_t
namei_iopen(IHandle_t * h)
{
- int fd;
+ FD_t fd;
namei_t name;
/* Convert handle to file name. */
namei_HandleToName(&name, h);
- fd = afs_open(name.n_path, O_RDWR, 0666);
+ fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
return fd;
}
if ((ino & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
IHandle_t *tmp;
- int inode_p1, tag;
int type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
/* Verify this is the right file. */
IH_INIT(tmp, ih->ih_dev, ih->ih_vid, ino);
+ namei_HandleToName(&name, tmp);
+
fdP = IH_OPEN(tmp);
if (fdP == NULL) {
IH_RELEASE(tmp);
- errno = EINVAL;
+ errno = OS_ERROR(ENOENT);
return -1;
}
- if ((GetOGM(fdP->fd_fd, &inode_p1, &tag) < 0) || (inode_p1 != p1)) {
+ if (CheckOGM(fdP, p1) < 0) {
FDH_REALLYCLOSE(fdP);
IH_RELEASE(tmp);
- errno = EINVAL;
+ errno = OS_ERROR(EINVAL);
return -1;
}
/* If it's the link table itself, decrement the link count. */
if (type == VI_LINKTABLE) {
- if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1)) < 0) {
+ if ((count = namei_GetLinkCount(fdP, (Inode) 0, 1, 0, 1)) < 0) {
FDH_REALLYCLOSE(fdP);
IH_RELEASE(tmp);
return -1;
}
count--;
- if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
- 0) {
- FDH_REALLYCLOSE(fdP);
- IH_RELEASE(tmp);
- return -1;
- }
-
if (count > 0) {
- FDH_REALLYCLOSE(fdP);
+ /* if our count is non-zero, we just set our new linkcount and
+ * return. But if our count is 0, don't bother updating the
+ * linktable, since we're about to delete the link table,
+ * below. */
+ if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) < 0) {
+ FDH_REALLYCLOSE(fdP);
+ IH_RELEASE(tmp);
+ return -1;
+ }
+
+ FDH_CLOSE(fdP);
IH_RELEASE(tmp);
return 0;
}
+
+ namei_UnlockLinkCount(fdP, (Inode) 0);
}
- namei_HandleToName(&name, tmp);
- if ((code = unlink(name.n_path)) == 0) {
+ /* We should IH_REALLYCLOSE right before deleting the special file from
+ * disk, to ensure that somebody else cannot create a special inode,
+ * then IH_OPEN that special inode and get back a cached fd for the
+ * file we are deleting here (instead of an fd for the file they just
+ * created). */
+ IH_REALLYCLOSE(tmp);
+ FDH_REALLYCLOSE(fdP);
+ IH_RELEASE(tmp);
+
+ if ((code = OS_UNLINK(name.n_path)) == 0) {
if (type == VI_LINKTABLE) {
/* Try to remove directory. If it fails, that's ok.
* Salvage will clean up.
*/
+ char *slash = strrchr(name.n_path, OS_DIRSEPC);
+ if (slash) {
+ /* avoid an rmdir() on the file we just unlinked */
+ *slash = '\0';
+ }
(void)namei_RemoveDataDirectories(&name);
}
}
- FDH_REALLYCLOSE(fdP);
- IH_RELEASE(tmp);
} else {
/* Get a file descriptor handle for this Inode */
fdP = IH_OPEN(ih);
return -1;
}
- if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0) {
+ if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0) {
FDH_REALLYCLOSE(fdP);
return -1;
}
} else {
IHandle_t *th;
IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
- Log("Warning: Lost ref on ihandle dev %d vid %d ino %" AFS_INT64_FMT "\n",
- th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
+ Log("Warning: Lost ref on ihandle dev %d vid %" AFS_VOLID_FMT " ino %lld\n",
+ th->ih_dev, afs_printable_VolumeId_lu(th->ih_vid), (afs_int64)th->ih_ino);
IH_RELEASE(th);
-
+
/* If we're less than 0, someone presumably unlinked;
don't bother setting count to 0, but we need to drop a lock */
if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
namei_HandleToName(&name, th);
IH_RELEASE(th);
- code = unlink(name.n_path);
+ code = OS_UNLINK(name.n_path);
}
FDH_CLOSE(fdP);
}
return -1;
}
- if ((count = namei_GetLinkCount(fdP, ino, 1)) < 0)
+ if ((count = namei_GetLinkCount(fdP, ino, 1, 0, 1)) < 0)
code = -1;
else {
count++;
if (count > 7) {
- errno = EINVAL;
+ errno = OS_ERROR(EINVAL);
code = -1;
count = 7;
}
return code;
}
+#ifndef AFS_NT40_ENV
int
namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
{
afs_int32 code;
namei_t nameLink;
namei_t nameTarget;
-
+
/* Convert handle to file name. */
namei_HandleToName(&nameLink, hLink);
namei_HandleToName(&nameTarget, hTarget);
-
- unlink(nameLink.n_path);
+
+ OS_UNLINK(nameLink.n_path);
code = link(nameTarget.n_path, nameLink.n_path);
return code;
}
int
namei_copy_on_write(IHandle_t *h)
{
- afs_int32 fd, code = 0;
+ afs_int32 code = 0;
+ FD_t fd;
namei_t name;
FdHandle_t *fdP;
- struct afs_stat tstat;
-
+ struct afs_stat_st tstat;
+ afs_foff_t offset;
+
namei_HandleToName(&name, h);
- if (afs_stat(name.n_path, &tstat) < 0)
+ if (afs_stat(name.n_path, &tstat) < 0)
return EIO;
if (tstat.st_nlink > 1) { /* do a copy on write */
- char path[259];
+ char path[NAMEI_PATH_LEN + 4];
char *buf;
afs_size_t size;
ssize_t tlen;
-
+
fdP = IH_OPEN(h);
if (!fdP)
return EIO;
- afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
- fd = afs_open(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
- if (fd < 0) {
+ snprintf(path, sizeof(path), "%s-tmp", name.n_path);
+ fd = OS_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0);
+ if (fd == INVALID_FD) {
FDH_CLOSE(fdP);
return EIO;
}
buf = malloc(8192);
if (!buf) {
- close(fd);
- unlink(path);
+ OS_CLOSE(fd);
+ OS_UNLINK(path);
FDH_CLOSE(fdP);
return ENOMEM;
}
size = tstat.st_size;
- FDH_SEEK(fdP, 0, 0);
+ offset = 0;
while (size) {
tlen = size > 8192 ? 8192 : size;
- if (FDH_READ(fdP, buf, tlen) != tlen)
+ if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
break;
- if (write(fd, buf, tlen) != tlen)
+ if (OS_WRITE(fd, buf, tlen) != tlen)
break;
size -= tlen;
+ offset += tlen;
}
- close(fd);
+ OS_CLOSE(fd);
FDH_REALLYCLOSE(fdP);
free(buf);
if (size)
code = EIO;
else {
- unlink(name.n_path);
+ OS_UNLINK(name.n_path);
code = rename(path, name.n_path);
}
}
return code;
}
+#endif
/************************************************************************
* File Name Structure
*
* Using this information we can see that a layout of 256 directories, each
* with 512 subdirectories and each of those having 512 files gives us
- * 256*512*512 = 67108864 AFS files and directories.
+ * 256*512*512 = 67108864 AFS files and directories.
*
* The volume, vnode, uniquifier and data version, as well as the tag
- * are required, either for finding the file or for salvaging. It's best to
+ * are required, either for finding the file or for salvaging. It's best to
* restrict the name to something that can be mapped into 64 bits so the
* "Inode" is easily comparable (using "==") to other "Inodes". The tag
* is used to distinguish between different versions of the same file
* which are currently in the RW and clones of a volume. See "Link Table
- * Organization" below for more information on the tag. The tag is
- * required in the name of the file to ensure a unique name.
+ * Organization" below for more information on the tag. The tag is
+ * required in the name of the file to ensure a unique name.
+ *
+ * ifdef AFS_NT40_ENV
+ * The data for each volume group is in a separate directory. The name of the
+ * volume is of the form: Vol_NNNNNN.data, where NNNNNN is a base 32
+ * representation of the RW volume ID (even where the RO is the only volume
+ * on the partition). Below that are separate subdirectories for the
+ * AFS directories and special files. There are also 16 directories for files,
+ * hashed on the low 5 bits (recall bit0 is always 0) of the vnode number.
+ * These directories are named:
+ * A - P - 16 file directories.
+ * Q ----- data directory
+ * R ----- special files directory
+ *
+ * The vnode is hashed into the directory using the low bits of the
+ * vnode number.
+ *
+ * The format of a file name for a regular file is:
+ * Y:\Vol_NNNNNN.data\X\V_IIIIII.J
+ * Y - partition encoded as drive letter, starting with D
+ * NNNNNN - base 32 encoded volume number of RW volume
+ * X - hash directory, as above
+ * IIIIII - base 32 encoded vnode number
+ * J - base 32 encoded tag
+ *
+ * uniq is stored in the dwHighDateTime creation time field
+ * dv is stored in the dwLowDateTime creation time field
*
+ * Special inodes are always in the R directory, as above, and are
+ * encoded:
+ * True child volid is stored in the dwHighDateTime creation time field
+ * vnode number is always -1 (Special)
+ * type is the IIIIII part of the filename
+ * uniq is the J part of the filename
+ * parent volume id is implied in the containing directory
+ *
+ * else
* We can store data in the uid, gid and mode bits of the files, provided
* the directories have root only access. This gives us 15 bits for each
* of uid and gid (GNU chown considers 65535 to mean "don't change").
- * There are 9 available mode bits. Adn we need to store a total of
+ * There are 9 available mode bits. Adn we need to store a total of
* 32 (volume id) + 26 (vnode) + 32 (uniquifier) + 32 (data-version) + 3 (tag)
- * or 131 bits somewhere.
+ * or 131 bits somewhere.
*
* The format of a file name for a regular file is:
* /vicepX/AFSIDat/V1/V2/AA/BB/<tag><uniq><vno>
* should be formed so that the leading characters are different as quickly
* as possible, leading to faster discards of incorrect matches in the
* lookup code.
- *
+ *
+ * endif
+ *
*/
* different version of the file (or a different uniquifer) for the BU volume.
* Then one column would be holding the link count of 2 for the RW and RO
* and a different column would hold the link count of 1 for the BU volume.
+ * # ifdef AFS_NT40_ENV
+ * The column used is determined for NT by the uniquifier tag applied to
+ * generate a unique file name in the NTFS namespace. The file name is
+ * of the form "V_<vno>.<tag>" . And the <tag> is also the column number
+ * in the link table.
+ * # else
* Note that we allow only 5 volumes per file, giving 15 bits used in the
* short.
+ * # endif
*/
#define LINKTABLE_WIDTH 2
#define LINKTABLE_SHIFT 1 /* log 2 = 1 */
+/**
+ * compute namei link table file and bit offset from inode number.
+ *
+ * @param[in] ino inode number
+ * @param[out] offset link table file offset
+ * @param[out] index bit offset within 2-byte record
+ *
+ * @internal
+ */
static void
namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
{
*index = (tindex << 1) + tindex;
}
+#ifdef AFS_PTHREAD_ENV
+/* XXX do static initializers work for WINNT/pthread? */
+pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
+#define NAMEI_GLC_LOCK opr_mutex_enter(&_namei_glc_lock)
+#define NAMEI_GLC_UNLOCK opr_mutex_exit(&_namei_glc_lock)
+#else /* !AFS_PTHREAD_ENV */
+#define NAMEI_GLC_LOCK
+#define NAMEI_GLC_UNLOCK
+#endif /* !AFS_PTHREAD_ENV */
-/* namei_GetLinkCount
- * If lockit is set, lock the file and leave it locked upon a successful
- * return.
+/**
+ * get the link count of an inode.
+ *
+ * @param[in] h namei link count table file handle
+ * @param[in] ino inode number for which we are requesting a link count
+ * @param[in] lockit if asserted, return with lock held on link table file
+ * @param[in] fixup if asserted, write 1 to link count when read() returns
+ * zero (at EOF)
+ * @param[in] nowrite return success on zero byte read or ZLC
+ *
+ * @post if lockit asserted and lookup was successful, will return with write
+ * lock on link table file descriptor
+ *
+ * @return link count
+ * @retval -1 namei link table i/o error
+ *
+ * @internal
*/
-static int
-namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
+int
+namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
{
unsigned short row = 0;
afs_foff_t offset;
int index;
/* there's no linktable yet. the salvager will create one later */
- if (h->fd_fd == -1 && fixup)
+ if (h->fd_fd == INVALID_FD && fixup)
return 1;
namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
if (lockit) {
- if (flock(h->fd_fd, LOCK_EX) < 0)
+ if (FDH_LOCKFILE(h, offset) != 0)
return -1;
}
- if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
+ rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
+ if (rc == -1)
goto bad_getLinkByte;
- rc = read(h->fd_fd, (char *)&row, sizeof(row));
if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
return 1;
if (rc == 0 && fixup) {
- struct stat st;
- if (fstat(h->fd_fd, &st) || st.st_size >= offset+sizeof(row))
- goto bad_getLinkByte;
- FDH_TRUNC(h, offset+sizeof(row));
+ /*
+ * extend link table and write a link count of 1 for ino
+ *
+ * in order to make MT-safe, truncation (extension really)
+ * must happen under a mutex
+ */
+ NAMEI_GLC_LOCK;
+ if (FDH_SIZE(h) >= offset+sizeof(row)) {
+ NAMEI_GLC_UNLOCK;
+ goto bad_getLinkByte;
+ }
+ if (FDH_TRUNC(h, offset+sizeof(row))) {
+ NAMEI_GLC_UNLOCK;
+ goto bad_getLinkByte;
+ }
row = 1 << index;
- rc = write(h->fd_fd, (char *)&row, sizeof(row));
+ rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
+ NAMEI_GLC_UNLOCK;
}
if (rc != sizeof(row)) {
goto bad_getLinkByte;
}
if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
- row |= 1<<index;
- if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
- goto bad_getLinkByte;
- rc = write(h->fd_fd, (char *)&row, sizeof(row));
+ /*
+ * fix up zlc
+ *
+ * in order to make this mt-safe, we need to do the read-modify-write
+ * under a mutex. thus, we repeat the read inside the lock.
+ */
+ NAMEI_GLC_LOCK;
+ rc = FDH_PREAD(h, (char *)&row, sizeof(row), offset);
+ if (rc == sizeof(row)) {
+ row |= 1<<index;
+ rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
+ }
+ NAMEI_GLC_UNLOCK;
if (rc != sizeof(row))
goto bad_getLinkByte;
}
-
+
return (int)((row >> index) & NAMEI_TAGMASK);
bad_getLinkByte:
if (lockit)
- flock(h->fd_fd, LOCK_UN);
+ FDH_UNLOCKFILE(h, offset);
return -1;
}
-int
-namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
-{
- return namei_GetLinkCount2(h, ino, lockit, 0, 1);
-}
-
/* Return a free column index for this vnode. */
static int
GetFreeTag(IHandle_t * ih, int vno)
if (fdP == NULL)
return -1;
+ offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
+
/* Only one manipulates at a time. */
- if (flock(fdP->fd_fd, LOCK_EX) < 0) {
+ if (FDH_LOCKFILE(fdP, offset) != 0) {
FDH_REALLYCLOSE(fdP);
return -1;
}
- offset = (vno << LINKTABLE_SHIFT) + 8; /* * 2 + sizeof stamp */
- if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
- goto badGetFreeTag;
- }
-
- nBytes = read(fdP->fd_fd, (char *)&row, sizeof(row));
+ nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
if (nBytes != sizeof(row)) {
if (nBytes != 0)
goto badGetFreeTag;
coldata = 1 << (col * 3);
row |= coldata;
- if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
+ if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
goto badGetFreeTag;
}
- if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
- goto badGetFreeTag;
- }
- FDH_SYNC(fdP);
- flock(fdP->fd_fd, LOCK_UN);
- FDH_REALLYCLOSE(fdP);
- return col;;
+ (void)FDH_SYNC(fdP);
+ FDH_UNLOCKFILE(fdP, offset);
+ FDH_CLOSE(fdP);
+ return col;
badGetFreeTag:
- flock(fdP->fd_fd, LOCK_UN);
+ FDH_UNLOCKFILE(fdP, offset);
FDH_REALLYCLOSE(fdP);
return -1;
}
afs_foff_t offset;
int index;
unsigned short row;
- int junk;
+ int bytesRead;
ssize_t nBytes = -1;
namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
if (!locked) {
- if (flock(fdP->fd_fd, LOCK_EX) < 0) {
+ if (FDH_LOCKFILE(fdP, offset) != 0) {
return -1;
}
}
- if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
- errno = EBADF;
- goto bad_SetLinkCount;
- }
-
- nBytes = read(fdP->fd_fd, (char *)&row, sizeof(row));
+ nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
if (nBytes != sizeof(row)) {
if (nBytes != 0) {
- errno = EBADF;
+ errno = OS_ERROR(EBADF);
goto bad_SetLinkCount;
}
row = 0;
}
- junk = 7 << index;
+ bytesRead = 7 << index;
count <<= index;
- row &= (unsigned short)~junk;
+ row &= (unsigned short)~bytesRead;
row |= (unsigned short)count;
- if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
- errno = EBADF;
+ if (FDH_PWRITE(fdP, (char *)&row, sizeof(short), offset) != sizeof(short)) {
+ errno = OS_ERROR(EBADF);
goto bad_SetLinkCount;
}
-
- if (write(fdP->fd_fd, (char *)&row, sizeof(short)) != sizeof(short)) {
- errno = EBADF;
- goto bad_SetLinkCount;
- }
- FDH_SYNC(fdP);
+ (void)FDH_SYNC(fdP);
nBytes = 0;
bad_SetLinkCount:
- flock(fdP->fd_fd, LOCK_UN);
+ FDH_UNLOCKFILE(fdP, offset);
- return nBytes;
+ /* disallowed above 7, so... */
+ return (int)nBytes;
+}
+
+static void
+namei_UnlockLinkCount(FdHandle_t * fdP, Inode ino)
+{
+ afs_foff_t offset;
+ int index;
+
+ namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
+
+ FDH_UNLOCKFILE(fdP, offset);
}
/* ListViceInodes - write inode data to a results file. */
static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
- unsigned int volid);
-static int DecodeVolumeName(char *name, unsigned int *vid);
+ IHandle_t *myIH);
+static int DecodeVolumeName(char *name, VolumeId *vid);
static int namei_ListAFSSubDirs(IHandle_t * dirIH,
- int (*write_fun) (FILE *,
+ int (*write_fun) (FD_t,
struct ViceInodeInfo *,
- char *, char *), FILE * fp,
+ char *, char *), FD_t fp,
int (*judgeFun) (struct ViceInodeInfo *,
- afs_uint32 vid, void *),
- afs_uint32 singleVolumeNumber, void *rock);
+ VolumeId vid, void *),
+ VolumeId singleVolumeNumber, void *rock);
/* WriteInodeInfo
*
- * Write the inode data to the results file.
+ * Write the inode data to the results file.
*
* Returns -2 on error, 0 on success.
*
* can use the same inode reading code.
*/
static int
-WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
+WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
{
- int n;
- n = fwrite(info, sizeof(*info), 1, fp);
- return (n == 1) ? 0 : -2;
+ size_t n;
+ n = OS_WRITE(fp, info, sizeof(*info));
+ return (n == sizeof(*info)) ? 0 : -2;
}
int mode_errors; /* Number of errors found in mode bits on directories. */
void
-VerifyDirPerms(char *path)
+VerifyDirPerms(char *path)
+{
+ struct afs_stat_st status;
+
+ if (afs_stat(path, &status) < 0) {
+ Log("Unable to stat %s. Please manually verify mode bits for this"
+ " directory\n", path);
+ } else {
+ if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
+ mode_errors++;
+ }
+}
+
+/**
+ * Fill the results file with the requested inode information.
+ *
+ * This code optimizes single volume salvages by just looking at that one
+ * volume's directory.
+ *
+ * @param[in] devname device name string
+ * @param[in] moutnedOn vice partition mount point
+ * @param[in] resultFile result file in which to write inode
+ * metadata. If NULL, write routine is not
+ * called.
+ * @param[in] judgeInode filter function pointer. if not NULL, only
+ * inodes for which this routine returns non-
+ * zero will be written to the results file.
+ * @param[in] singleVolumeNumber volume id filter
+ * @param[out] forcep always set to 0 for namei impl
+ * @param[in] forceR not used by namei impl
+ * @param[in] wpath not used by namei impl
+ * @param[in] rock opaque pointer passed to judgeInode
+ *
+ * @return operation status
+ * @retval 0 success
+ * @retval -1 complete failure, salvage should terminate.
+ * @retval -2 not enough space on partition, salvager has error message
+ * for this.
+ */
+int
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
+ int (*judgeInode) (struct ViceInodeInfo * info, VolumeId vid, void *rock),
+ VolumeId singleVolumeNumber, int *forcep, int forceR, char *wpath,
+ void *rock)
+{
+ int ninodes;
+
+ *forcep = 0; /* no need to salvage until further notice */
+
+ /* Verify protections on directories. */
+ mode_errors = 0;
+ VerifyDirPerms(mountedOn);
+
+ ninodes =
+ namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
+ singleVolumeNumber, rock);
+
+ if (inodeFile == INVALID_FD)
+ return ninodes;
+
+ if (ninodes < 0) {
+ return ninodes;
+ }
+
+ if (OS_SYNC(inodeFile) == -1) {
+ Log("Unable to successfully fsync inode file for %s\n", mountedOn);
+ return -2;
+ }
+
+ /*
+ * Paranoia: check that the file is really the right size
+ */
+ if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
+ Log("Wrong size (%d instead of %lu) in inode file for %s\n",
+ (int) OS_SIZE(inodeFile),
+ (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
+ mountedOn);
+ return -2;
+ }
+ return 0;
+}
+
+
+/**
+ * Collect all the matching AFS files on the drive.
+ * If singleVolumeNumber is non-zero, just return files for that volume.
+ *
+ * @param[in] dev vice partition path
+ * @param[in] writeFun function pointer to a function which
+ * writes inode information to FILE fp
+ * @param[in] fp file stream where inode metadata is sent
+ * @param[in] judgeFun filter function pointer. if not NULL,
+ * only entries for which a non-zero value
+ * is returned are written to fp
+ * @param[in] singleVolumeNumber volume id filter. if nonzero, only
+ * process files for that specific volume id
+ * @param[in] rock opaque pointer passed into writeFun and
+ * judgeFun
+ *
+ * @return operation status
+ * @retval <0 error
+ * @retval >=0 number of matching files found
+ */
+int
+namei_ListAFSFiles(char *dev,
+ int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
+ char *),
+ FD_t fp,
+ int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+ VolumeId singleVolumeNumber, void *rock)
+{
+ IHandle_t ih;
+ namei_t name;
+ int ninodes = 0;
+ DIR *dirp1;
+ struct dirent *dp1;
+#ifndef AFS_NT40_ENV
+ DIR *dirp2;
+ struct dirent *dp2;
+ char path2[512];
+#endif
+#ifdef DELETE_ZLC
+ static void FreeZLCList(void);
+#endif
+
+ memset((void *)&ih, 0, sizeof(IHandle_t));
+#ifdef AFS_NT40_ENV
+ ih.ih_dev = nt_DriveToDev(dev);
+#else
+ ih.ih_dev = volutil_GetPartitionID(dev);
+#endif
+
+ if (singleVolumeNumber) {
+ ih.ih_vid = singleVolumeNumber;
+ namei_HandleToVolDir(&name, &ih);
+ ninodes =
+ namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
+ singleVolumeNumber, rock);
+ if (ninodes < 0)
+ return ninodes;
+ } else {
+ /* Find all volume data directories and descend through them. */
+ namei_HandleToInodeDir(&name, &ih);
+ ninodes = 0;
+ dirp1 = opendir(name.n_path);
+ if (!dirp1)
+ return 0;
+ while ((dp1 = readdir(dirp1))) {
+#ifdef AFS_NT40_ENV
+ /* Heirarchy is one level on Windows */
+ if (!DecodeVolumeName(dp1->d_name, &ih.ih_vid)) {
+ ninodes +=
+ namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
+ 0, rock);
+ }
+#else
+ if (*dp1->d_name == '.')
+ continue;
+ snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
+ dp1->d_name);
+ dirp2 = opendir(path2);
+ if (dirp2) {
+ while ((dp2 = readdir(dirp2))) {
+ if (*dp2->d_name == '.')
+ continue;
+ if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
+ ninodes +=
+ namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
+ 0, rock);
+ }
+ }
+ closedir(dirp2);
+ }
+#endif
+ }
+ closedir(dirp1);
+ }
+#ifdef DELETE_ZLC
+ FreeZLCList();
+#endif
+ return ninodes;
+}
+
+#ifdef DELETE_ZLC
+static void AddToZLCDeleteList(char dir, char *name);
+static void DeleteZLCFiles(char *path);
+#endif
+
+/**
+ * examine a namei volume special file.
+ *
+ * @param[in] path1 volume special directory path
+ * @param[in] dname directory entry name
+ * @param[in] myIH inode handle to volume directory
+ * @param[out] linkHandle namei link count fd handle. if
+ * the inode in question is the link
+ * table, then the FdHandle is populated
+ * @param[in] writeFun metadata write function pointer
+ * @param[in] fp file pointer where inode metadata
+ * is written by (*writeFun)()
+ * @param[in] judgeFun inode filter function pointer. if
+ * not NULL, only inodes for which this
+ * function returns non-zero are recorded
+ * into fp by writeFun
+ * @param[in] singleVolumeNumer volume id filter. if non-zero, only
+ * inodes associated with this volume id
+ * are recorded by writeFun
+ * @param[in] rock opaque pointer passed to writeFun and
+ * judgeFun
+ *
+ * @return operation status
+ * @retval 1 count this inode
+ * @retval 0 don't count this inode
+ * @retval -1 failure
+ *
+ * @internal
+ */
+static int
+_namei_examine_special(char * path1,
+ char * dname,
+ IHandle_t * myIH,
+ FdHandle_t * linkHandle,
+ int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
+ char *),
+ FD_t fp,
+ int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+ VolumeId singleVolumeNumber,
+ void *rock)
+{
+ int ret = 0;
+ struct ViceInodeInfo info;
+
+ if (DecodeInode(path1, dname, &info, myIH) < 0) {
+ ret = 0;
+ goto error;
+ }
+
+ if (info.u.param[2] != VI_LINKTABLE) {
+ info.linkCount = 1;
+ } else if (info.u.param[0] != myIH->ih_vid) {
+ /* VGID encoded in linktable filename and/or OGM data isn't
+ * consistent with VGID encoded in namei path */
+ Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
+ "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
+ "(dir_vgid=%" AFS_VOLID_FMT ", inode_vgid=%" AFS_VOLID_FMT ")\n",
+ path1, dname, afs_printable_VolumeId_lu(myIH->ih_vid),
+ afs_printable_VolumeId_lu(info.u.param[0]));
+ /* We need to set the linkCount to _something_, so linkCount
+ * doesn't just contain stack garbage. Set it to 0, so in case
+ * the salvager or whatever our caller is does try to process
+ * this like a normal file, we won't try to INC or DEC it. */
+ info.linkCount = 0;
+ } else {
+ char path2[512];
+ /* Open this handle */
+ snprintf(path2, sizeof(path2),
+ "%s" OS_DIRSEP "%s", path1, dname);
+ linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
+ info.linkCount =
+ namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
+ }
+
+ if (!judgeFun ||
+ (*judgeFun) (&info, singleVolumeNumber, rock)) {
+ ret = (*writeFun) (fp, &info, path1, dname);
+ if (ret < 0) {
+ Log("_namei_examine_special: writeFun returned %d\n", ret);
+ ret = -1;
+ } else {
+ ret = 1;
+ }
+ }
+
+ error:
+ return ret;
+}
+
+/**
+ * examine a namei file.
+ *
+ * @param[in] path3 volume special directory path
+ * @param[in] dname directory entry name
+ * @param[in] myIH inode handle to volume directory
+ * @param[in] linkHandle namei link count fd handle.
+ * @param[in] writeFun metadata write function pointer
+ * @param[in] fp file pointer where inode metadata
+ * is written by (*writeFun)()
+ * @param[in] judgeFun inode filter function pointer. if
+ * not NULL, only inodes for which this
+ * function returns non-zero are recorded
+ * into fp by writeFun
+ * @param[in] singleVolumeNumer volume id filter. if non-zero, only
+ * inodes associated with this volume id
+ * are recorded by writeFun
+ * @param[in] rock opaque pointer passed to writeFun and
+ * judgeFun
+ *
+ * @return operation status
+ * @retval 1 count this inode
+ * @retval 0 don't count this inode
+ * @retval -1 failure
+ * @retval -2 request ZLC delete
+ *
+ * @internal
+ */
+static int
+_namei_examine_reg(char * path3,
+ char * dname,
+ IHandle_t * myIH,
+ FdHandle_t * linkHandle,
+ int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
+ char *),
+ FD_t fp,
+ int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+ VolumeId singleVolumeNumber,
+ void *rock)
+{
+ int ret = 0;
+ struct ViceInodeInfo info;
+#ifdef DELETE_ZLC
+ int dirl; /* Windows-only (one level hash dir) */
+#endif
+
+ if (DecodeInode(path3, dname, &info, myIH) < 0) {
+ goto error;
+ }
+
+ info.linkCount =
+ namei_GetLinkCount(linkHandle,
+ info.inodeNumber, 1, 1, Testing);
+ if (info.linkCount == 0) {
+#ifdef DELETE_ZLC
+ Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n", path3, dname);
+#ifdef AFS_SALSRV_ENV
+ /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
+ ret = -2;
+#else /* !AFS_SALSRV_ENV */
+ dirl = path3[strlen(path3)-1];
+ AddToZLCDeleteList((char)dirl, dname);
+#endif /* !AFS_SALSRV_ENV */
+#else /* !DELETE_ZLC */
+ Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
+ dname);
+#endif
+ goto error;
+ }
+
+ if (!judgeFun ||
+ (*judgeFun) (&info, singleVolumeNumber, rock)) {
+ ret = (*writeFun) (fp, &info, path3, dname);
+ if (ret < 0) {
+ Log("_namei_examine_reg: writeFun returned %d\n", ret);
+ ret = -1;
+ } else {
+ ret = 1;
+ }
+ }
+
+ error:
+ return ret;
+}
+
+/**
+ * listsubdirs work queue node.
+ */
+struct listsubdirs_work_node {
+#ifdef AFS_SALSRV_ENV
+ int *error; /**< *error set if an error was
+ * encountered in any listsubdirs
+ * thread. */
+#endif
+
+ IHandle_t * IH; /**< volume directory handle */
+ FdHandle_t *linkHandle; /**< namei link count fd handle. when
+ * examinining the link table special
+ * inode, this will be pointed at the
+ * link table
+ */
+ FD_t fp; /**< file pointer for writeFun */
+
+ /** function which will write inode metadata to fp */
+ int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
+
+ /** inode filter function */
+ int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *);
+ VolumeId singleVolumeNumber; /**< volume id filter */
+ void * rock; /**< pointer passed to writeFun and judgeFun */
+ int code; /**< return code from examine function */
+ int special; /**< asserted when this is a volume
+ * special file */
+};
+
+/**
+ * simple wrapper around _namei_examine_special and _namei_examine_reg.
+ *
+ * @param[in] work the struct listsubdirs_work_node for the associated
+ * "list subdirs" job
+ * @param[in] dir the directory to examine
+ * @param[in] filename the filename in 'dir' to examine
+ *
+ * @return operation status
+ * @retval 1 count this inode
+ * @retval 0 don't count this inode
+ * @retval -1 failure
+ */
+static_inline int
+_namei_examine_file(const struct listsubdirs_work_node *work, char *dir,
+ char *filename)
+{
+ if (work->special) {
+ return _namei_examine_special(dir, filename, work->IH,
+ work->linkHandle, work->writeFun, work->fp,
+ work->judgeFun, work->singleVolumeNumber,
+ work->rock);
+ } else {
+ return _namei_examine_reg(dir, filename, work->IH,
+ work->linkHandle, work->writeFun, work->fp,
+ work->judgeFun, work->singleVolumeNumber,
+ work->rock);
+ }
+}
+
+
+#ifdef AFS_SALSRV_ENV
+/** @addtogroup afs_vol_salsrv_pario */
+/*@{*/
+
+/**
+ * arguments for the _namei_examine_file_cbk callback function.
+ */
+struct listsubdirs_args {
+ const struct listsubdirs_work_node *work; /**< arguments that are the same
+ * for all invocations of
+ * _namei_examine_file_cbk, in
+ * threads */
+ int *result; /**< where we can store the return code of _namei_examine_file */
+
+ char dir[512]; /**< directory to examine */
+ char filename[256]; /**< filename in 'dir' to examine */
+};
+
+/**
+ * a node in the list of results of listsubdir jobs.
+ */
+struct listsubdirs_result {
+ struct rx_queue q;
+ int inodes; /**< return value from _namei_examine_file */
+};
+
+/**
+ * clean up a list of 'struct listsubdirs_result's and interpret the results.
+ *
+ * @param[in] resultlist a list of 'struct listsubdirs_result's
+ *
+ * @return number of inodes found
+ * @retval -1 error
+ */
+static int
+_namei_listsubdirs_cleanup_results(struct rx_queue *resultlist)
+{
+ struct listsubdirs_result *res, *nres;
+ int ret = 0;
+
+ for(queue_Scan(resultlist, res, nres, listsubdirs_result)) {
+ if (ret < 0) {
+ /* noop, retain erroneous error code */
+ } else if (res->inodes < 0) {
+ ret = -1;
+ } else {
+ ret += res->inodes;
+ }
+
+ queue_Remove(res);
+ free(res);
+ res = NULL;
+ }
+
+ return ret;
+}
+
+/**
+ * wait for the spawned listsubdirs jobs to finish, and return how many inodes
+ * they found.
+ *
+ * @param[in] queue queue to wait to finish
+ * @param[in] resultlist list of 'struct listsubdirs_result's that the queued
+ * jobs are storing their results in
+ *
+ * @return number of inodes found
+ * @retval -1 error
+ */
+static int
+_namei_listsubdirs_wait(struct afs_work_queue *queue, struct rx_queue *resultlist)
+{
+ int code;
+
+ code = afs_wq_wait_all(queue);
+ if (code) {
+ return -1;
+ }
+
+ return _namei_listsubdirs_cleanup_results(resultlist);
+}
+
+/**
+ * work queue entry point for examining namei files.
+ *
+ * @param[in] queue pointer to struct Vwork_queue
+ * @param[in] node pointer to struct Vwork_queue_node
+ * @param[in] queue_rock opaque pointer to struct salsrv_pool_state
+ * @param[in] node_rock opaque pointer to struct listsubdirs_work_node
+ * @param[in] caller_rock opaque pointer to struct salsrv_worker_thread_state
+ *
+ * @return operation status
+ *
+ * @see Vwork_queue_callback_func_t
+ *
+ * @internal
+ */
+static int
+_namei_examine_file_cbk(struct afs_work_queue *queue,
+ struct afs_work_queue_node *node,
+ void *queue_rock,
+ void *node_rock,
+ void *caller_rock)
+{
+ int code;
+ struct listsubdirs_args *args = node_rock;
+ const struct listsubdirs_work_node * work = args->work;
+ char *dir = args->dir;
+ char *filename = args->filename;
+
+ code = _namei_examine_file(work, dir, filename);
+
+ *(args->result) = code;
+
+ if (code < 0) {
+ *(work->error) = 1;
+ /* we've errored, so no point in letting more jobs continue */
+ afs_wq_shutdown(queue);
+ }
+
+ return 0;
+}
+
+static pthread_once_t wq_once = PTHREAD_ONCE_INIT;
+static pthread_key_t wq_key;
+
+/**
+ * create the wq_key key for storing a work queue.
+ */
+static void
+_namei_wq_keycreate(void)
+{
+ opr_Verify(pthread_key_create(&wq_key, NULL) == 0);
+}
+
+/**
+ * set the work queue for this thread to use for backgrounding namei ops.
+ *
+ * The work queue will be used in ListAFSSubdirs (called indirectly by
+ * ListViceInodes) to examine files in parallel.
+ *
+ * @param[in] wq the work queue to use
+ */
+void
+namei_SetWorkQueue(struct afs_work_queue *wq)
{
- struct afs_stat status;
+ opr_Verify(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
- if (afs_stat(path, &status) < 0) {
- Log("Unable to stat %s. Please manually verify mode bits for this"
- " directory\n", path);
- } else {
- if (((status.st_mode & 0777) != 0700) || (status.st_uid != 0))
- mode_errors++;
- }
+ opr_Verify(pthread_setspecific(wq_key, wq) == 0);
}
-/* ListViceInodes
- * Fill the results file with the requested inode information.
+/**
+ * enqueue an examine file work unit.
*
- * Return values:
- * 0 - success
- * -1 - complete failure, salvage should terminate.
- * -2 - not enough space on partition, salvager has error message for this.
+ * @param[in] work the _namei_examine_file arguments that are common to
+ * all callers within the same ListAFSFiles operation
+ * @param[in] dir the specific directory to look at (string will be
+ * copied; can be stack/temporary memory)
+ * @param[in] filename the filename to look at (string will be copied; can be
+ * stack/temporary memory)
+ * @param[in] wq work queue to enqueue this work unit to
+ * @param[in] resultlist the list to append the 'struct listsubdirs_result' to
+ * for the enqueued work unit
*
- * This code optimizes single volume salvages by just looking at that one
- * volume's directory.
+ * @return operation status
+ * @retval 0 success
+ * @retval -1 fatal error
*
- * If the resultFile is NULL, then don't call the write routine.
+ * @note errors MUST be indicated by a -1 error code and nothing else, to be
+ * compatible with _namei_examine_reg and _namei_examine_special
+ *
+ * @internal
*/
-int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
- int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
- afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
- void *rock)
+static int
+_namei_examine_file_spawn(const struct listsubdirs_work_node *work,
+ const char *dir, const char *filename,
+ struct afs_work_queue *wq,
+ struct rx_queue *resultlist)
{
- int ninodes;
- struct afs_stat status;
-
- *forcep = 0; /* no need to salvage until further notice */
-
- /* Verify protections on directories. */
- mode_errors = 0;
- VerifyDirPerms(mountedOn);
-
- ninodes =
- namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
- singleVolumeNumber, rock);
+ int code, ret = 0;
+ struct listsubdirs_args *args = NULL;
+ struct listsubdirs_result *result = NULL;
+ struct afs_work_queue_node *node = NULL;
+ struct afs_work_queue_add_opts opts;
- if (!inodeFile)
- return ninodes;
+ args = malloc(sizeof(*args));
+ if (args == NULL) {
+ ret = -1;
+ goto error;
+ }
- if (ninodes < 0) {
- return ninodes;
+ result = malloc(sizeof(*result));
+ if (result == NULL) {
+ ret = -1;
+ goto error;
}
- if (fflush(inodeFile) == EOF) {
- Log("Unable to successfully flush inode file for %s\n", mountedOn);
- return -2;
+ code = afs_wq_node_alloc(&node);
+ if (code) {
+ ret = -1;
+ goto error;
}
- if (fsync(fileno(inodeFile)) == -1) {
- Log("Unable to successfully fsync inode file for %s\n", mountedOn);
- return -2;
+ code = afs_wq_node_set_detached(node);
+ if (code) {
+ ret = -1;
+ goto error;
}
- /*
- * Paranoia: check that the file is really the right size
- */
- if (afs_fstat(fileno(inodeFile), &status) == -1) {
- Log("Unable to successfully stat inode file for %s\n", mountedOn);
- return -2;
- }
- if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
- Log("Wrong size (%d instead of %lu) in inode file for %s\n",
- (int) status.st_size,
- (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
- mountedOn);
- return -2;
+ args->work = work;
+ args->result = &result->inodes;
+ strlcpy(args->dir, dir, sizeof(args->dir));
+ strlcpy(args->filename, filename, sizeof(args->filename));
+
+ code = afs_wq_node_set_callback(node,
+ &_namei_examine_file_cbk,
+ args, &free);
+ if (code) {
+ ret = -1;
+ goto error;
}
- return 0;
-}
+ args = NULL;
+ afs_wq_add_opts_init(&opts);
+ opts.donate = 1;
-/* namei_ListAFSFiles
- *
- * Collect all the matching AFS files on the drive.
- * If singleVolumeNumber is non-zero, just return files for that volume.
- *
- * Returns <0 on error, else number of files found to match.
- */
-int
-namei_ListAFSFiles(char *dev,
- int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
- char *), FILE * fp,
- int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
- afs_uint32 singleVolumeNumber, void *rock)
-{
- IHandle_t ih;
- namei_t name;
- int ninodes = 0;
- DIR *dirp1, *dirp2;
- struct dirent *dp1, *dp2;
- char path2[512];
-#ifdef DELETE_ZLC
- static void FreeZLCList(void);
-#endif
+ code = afs_wq_add(wq, node, &opts);
+ if (code) {
+ ret = -1;
+ goto error;
+ }
+ node = NULL;
- memset((void *)&ih, 0, sizeof(IHandle_t));
- ih.ih_dev = volutil_GetPartitionID(dev);
+ queue_Append(resultlist, result);
+ result = NULL;
- if (singleVolumeNumber) {
- ih.ih_vid = singleVolumeNumber;
- namei_HandleToVolDir(&name, &ih);
- ninodes =
- namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
- singleVolumeNumber, rock);
- if (ninodes < 0)
- return ninodes;
- } else {
- /* Find all volume data directories and descend through them. */
- namei_HandleToInodeDir(&name, &ih);
- ninodes = 0;
- dirp1 = opendir(name.n_path);
- if (!dirp1)
- return 0;
- while ((dp1 = readdir(dirp1))) {
- if (*dp1->d_name == '.')
- continue;
- afs_snprintf(path2, sizeof(path2), "%s/%s", name.n_path,
- dp1->d_name);
- dirp2 = opendir(path2);
- if (dirp2) {
- while ((dp2 = readdir(dirp2))) {
- if (*dp2->d_name == '.')
- continue;
- if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
- ninodes +=
- namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
- 0, rock);
- }
- }
- closedir(dirp2);
- }
- }
- closedir(dirp1);
+ error:
+ if (node) {
+ afs_wq_node_put(node);
+ node = NULL;
+ }
+ if (args) {
+ free(args);
+ args = NULL;
+ }
+ if (result) {
+ free(result);
+ result = NULL;
}
-#ifdef DELETE_ZLC
- FreeZLCList();
-#endif
- return ninodes;
-}
+ return ret;
+}
+/*@}*/
+#else /* !AFS_SALSRV_ENV */
+# define _namei_examine_file_spawn(work, dir, file, wq, resultlist) \
+ _namei_examine_file(work, dir, file)
+#endif /* !AFS_SALSRV_ENV */
-/* namei_ListAFSSubDirs
+/**
+ * traverse and check inodes.
+ *
+ * @param[in] dirIH volume group directory handle
+ * @param[in] writeFun function pointer which will write inode
+ * metadata to FILE stream fp
+ * @param[in] fp file stream where inode metadata gets
+ * written
+ * @param[in] judgeFun inode filter function. if not NULL, only
+ * inodes for which the filter returns non-zero
+ * will be written out by writeFun
+ * @param[in] singleVolumeNumber volume id filter. only inodes matching this
+ * filter are written out by writeFun
+ * @param[in] rock opaque pointer passed to judgeFun and writeFun
*
+ * @return operation status
+ * @retval <0 error
+ * @retval >=0 number of matching inodes found
*
- * Return values:
- * < 0 - an error
- * > = 0 - number of AFS files found.
+ * @todo the salsrv implementation may consume a lot of
+ * memory for a large volume. at some point we should
+ * probably write a background thread to asynchronously
+ * clean up the resultlist nodes to reduce memory footprint
+ *
+ * @internal
*/
static int
namei_ListAFSSubDirs(IHandle_t * dirIH,
- int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
- char *), FILE * fp,
- int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
- afs_uint32 singleVolumeNumber, void *rock)
+ int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
+ char *),
+ FD_t fp,
+ int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+ VolumeId singleVolumeNumber, void *rock)
{
+ int code = 0, ret = 0;
IHandle_t myIH = *dirIH;
namei_t name;
- char path1[512], path2[512], path3[512];
- DIR *dirp1, *dirp2, *dirp3;
- struct dirent *dp1, *dp2, *dp3;
- struct ViceInodeInfo info;
+ char path1[512], path3[512];
+ DIR *dirp1, *dirp3;
+#ifndef AFS_NT40_ENV
+ DIR *dirp2;
+ struct dirent *dp2;
+ char path2[512];
+#endif
+ struct dirent *dp1, *dp3;
FdHandle_t linkHandle;
int ninodes = 0;
-#ifdef DELETE_ZLC
- int i;
- static void AddToZLCDeleteList(char dir, char *name);
- static void DeleteZLCFiles(char *path);
+ struct listsubdirs_work_node work;
+#ifdef AFS_SALSRV_ENV
+ int error = 0;
+ struct afs_work_queue *wq;
+ int wq_up = 0;
+ struct rx_queue resultlist;
#endif
+ memset(&linkHandle, 0, sizeof(linkHandle));
+
namei_HandleToVolDir(&name, &myIH);
strlcpy(path1, name.n_path, sizeof(path1));
/* Do the directory containing the special files first to pick up link
* counts.
*/
- (void)strcat(path1, "/");
+ (void)strcat(path1, OS_DIRSEP);
(void)strcat(path1, NAMEI_SPECDIR);
- linkHandle.fd_fd = -1;
+ linkHandle.fd_fd = INVALID_FD;
+#ifdef AFS_SALSRV_ENV
+ opr_Verify(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
+
+ wq = pthread_getspecific(wq_key);
+ if (!wq) {
+ ret = -1;
+ goto error;
+ }
+ wq_up = 1;
+ queue_Init(&resultlist);
+#endif
+
+ memset(&work, 0, sizeof(work));
+ work.linkHandle = &linkHandle;
+ work.IH = &myIH;
+ work.fp = fp;
+ work.writeFun = writeFun;
+ work.judgeFun = judgeFun;
+ work.singleVolumeNumber = singleVolumeNumber;
+ work.rock = rock;
+ work.special = 1;
+#ifdef AFS_SALSRV_ENV
+ work.error = &error;
+#endif
+
dirp1 = opendir(path1);
if (dirp1) {
while ((dp1 = readdir(dirp1))) {
if (*dp1->d_name == '.')
continue;
- if (DecodeInode(path1, dp1->d_name, &info, myIH.ih_vid) < 0)
- continue;
- if (info.u.param[2] != VI_LINKTABLE) {
- info.linkCount = 1;
- } else {
- /* Open this handle */
- (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
- dp1->d_name);
- linkHandle.fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
- info.linkCount =
- namei_GetLinkCount2(&linkHandle, (Inode) 0, 1, 1, Testing);
+
+#ifdef AFS_SALSRV_ENV
+ if (error) {
+ closedir(dirp1);
+ ret = -1;
+ goto error;
}
- if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
- continue;
+#endif /* AFS_SALSRV_ENV */
- if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
- if (linkHandle.fd_fd >= 0)
- close(linkHandle.fd_fd);
+ code = _namei_examine_file_spawn(&work, path1, dp1->d_name, wq, &resultlist);
+
+ switch (code) {
+ case -1:
+ /* fatal error */
closedir(dirp1);
- return -1;
+ ret = -1;
+ goto error;
+
+ case 1:
+ /* count this inode */
+#ifndef AFS_SALSRV_ENV
+ ninodes++;
+#endif
+ break;
}
- ninodes++;
}
closedir(dirp1);
}
+#ifdef AFS_SALSRV_ENV
+ /* effectively a barrier */
+ code = _namei_listsubdirs_wait(wq, &resultlist);
+ if (code < 0 || error) {
+ ret = -1;
+ goto error;
+ }
+ error = 0;
+ ninodes += code;
+#endif
+
+ if (linkHandle.fd_fd == INVALID_FD) {
+ Log("namei_ListAFSSubDirs: warning: VG %" AFS_VOLID_FMT " does not have a link table; "
+ "salvager will recreate it.\n", afs_printable_VolumeId_lu(dirIH->ih_vid));
+ }
+
/* Now run through all the other subdirs */
namei_HandleToVolDir(&name, &myIH);
strlcpy(path1, name.n_path, sizeof(path1));
+ work.special = 0;
+
dirp1 = opendir(path1);
if (dirp1) {
while ((dp1 = readdir(dirp1))) {
+#ifndef AFS_NT40_ENV
if (*dp1->d_name == '.')
continue;
+#endif
if (!strcmp(dp1->d_name, NAMEI_SPECDIR))
continue;
+#ifndef AFS_NT40_ENV /* This level missing on Windows */
/* Now we've got a next level subdir. */
- afs_snprintf(path2, sizeof(path2), "%s/%s", path1, dp1->d_name);
+ code = snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
+ path1, dp1->d_name);
+ if (code < 0 || code >= sizeof(path2)) {
+ /* error, or truncated */
+ closedir(dirp1);
+ ret = -1;
+ goto error;
+ }
dirp2 = opendir(path2);
if (dirp2) {
while ((dp2 = readdir(dirp2))) {
continue;
/* Now we've got to the actual data */
- afs_snprintf(path3, sizeof(path3), "%s/%s", path2,
- dp2->d_name);
+ code = snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+ path2, dp2->d_name);
+#else
+ /* Now we've got to the actual data */
+ code = snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+ path1, dp1->d_name);
+#endif
+ if (code < 0 || code >= sizeof(path3)) {
+ /* error, or truncated */
+#ifndef AFS_NT40_ENV
+ closedir(dirp2);
+#endif
+ closedir(dirp1);
+ ret = -1;
+ goto error;
+ }
dirp3 = opendir(path3);
if (dirp3) {
while ((dp3 = readdir(dirp3))) {
+#ifndef AFS_NT40_ENV
if (*dp3->d_name == '.')
continue;
- if (DecodeInode
- (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
- continue;
- info.linkCount =
- namei_GetLinkCount2(&linkHandle,
- info.inodeNumber, 1, 1, Testing);
- if (info.linkCount == 0) {
-#ifdef DELETE_ZLC
- Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
- AddToZLCDeleteList((char)i, dp3->d_name);
-#else
- Log("Found 0 link count file %s/%s.\n", path3,
- dp3->d_name);
#endif
- continue;
+
+#ifdef AFS_SALSRV_ENV
+ if (error) {
+ closedir(dirp3);
+#ifndef AFS_NT40_ENV
+ closedir(dirp2);
+#endif
+ closedir(dirp1);
+ ret = -1;
+ goto error;
}
- if (judgeFun
- && !(*judgeFun) (&info, singleVolumeNumber, rock))
- continue;
+#endif /* AFS_SALSRV_ENV */
+
+ code = _namei_examine_file_spawn(&work, path3,
+ dp3->d_name, wq,
+ &resultlist);
- if ((*writeFun) (fp, &info, path3, dp3->d_name) <
- 0) {
- close(linkHandle.fd_fd);
+ switch (code) {
+ case -1:
closedir(dirp3);
+#ifndef AFS_NT40_ENV
closedir(dirp2);
+#endif
closedir(dirp1);
- return -1;
+ ret = -1;
+ goto error;
+
+ case 1:
+#ifndef AFS_SALSRV_ENV
+ ninodes++;
+#endif
+ break;
}
- ninodes++;
}
closedir(dirp3);
}
+#ifndef AFS_NT40_ENV /* This level missing on Windows */
}
closedir(dirp2);
}
+#endif
}
closedir(dirp1);
}
- if (linkHandle.fd_fd >= 0)
- close(linkHandle.fd_fd);
+#ifdef AFS_SALSRV_ENV
+ /* effectively a barrier */
+ code = _namei_listsubdirs_wait(wq, &resultlist);
+ if (code < 0 || error) {
+ ret = -1;
+ goto error;
+ }
+ error = 0;
+ ninodes += code;
+ wq_up = 0;
+#endif
+
if (!ninodes) {
/* Then why does this directory exist? Blow it away. */
namei_HandleToVolDir(&name, dirIH);
namei_RemoveDataDirectories(&name);
}
- return ninodes;
+ error:
+#ifdef AFS_SALSRV_ENV
+ if (wq_up) {
+ afs_wq_wait_all(wq);
+ }
+ _namei_listsubdirs_cleanup_results(&resultlist);
+#endif
+ if (linkHandle.fd_fd != INVALID_FD)
+ OS_CLOSE(linkHandle.fd_fd);
+
+ if (!ret) {
+ ret = ninodes;
+ }
+ return ret;
}
+/*@}*/
+
+#ifdef AFS_NT40_ENV
+static int
+DecodeVolumeName(char *name, VolumeId *vid)
+{
+ /* Name begins with "Vol_" and ends with .data. See nt_HandleToVolDir() */
+ char stmp[32];
+ size_t len;
+
+ len = strlen(name);
+ if (len <= 9)
+ return -1;
+ if (strncmp(name, "Vol_", 4))
+ return -1;
+ if (strcmp(name + len - 5, ".data"))
+ return -1;
+ strcpy(stmp, name);
+ stmp[len - 5] = '\0';
+ *vid = base32_to_int(stmp + 4);
+ return 0;
+}
+#else
static int
-DecodeVolumeName(char *name, unsigned int *vid)
+DecodeVolumeName(char *name, VolumeId *vid)
{
if (strlen(name) < 1)
return -1;
*vid = (unsigned int)flipbase64_to_int64(name);
return 0;
}
+#endif
/* DecodeInode
*
* Get the inode number from the name.
- * Get
+ *
*/
+#ifdef AFS_NT40_ENV
+static int
+DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
+ IHandle_t *myIH)
+{
+ char fpath[512];
+ int tag, vno;
+ WIN32_FIND_DATA data;
+ HANDLE dirH;
+ char *s, *t;
+ char stmp[16];
+ FdHandle_t linkHandle;
+ char dirl;
+ VolumeId volid = myIH->ih_vid;
+
+ memset(&linkHandle, 0, sizeof(linkHandle));
+
+ snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
+
+ dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
+ FindExSearchNameMatch, NULL,
+ FIND_FIRST_EX_CASE_SENSITIVE);
+ if (dirH == INVALID_HANDLE_VALUE)
+ return -1;
+
+ (void)strcpy(stmp, name);
+ s = strrchr(stmp, '_');
+ if (!s)
+ return -1;
+ s++;
+ t = strrchr(s, '.');
+ if (!t)
+ return -1;
+
+ *t = '\0';
+ vno = base32_to_int(s); /* type for special files */
+ tag = base32_to_int(t+1);
+ info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
+ info->inodeNumber |= vno;
+ info->byteCount = data.nFileSizeLow;
+
+ dirl = dpath[strlen(dpath)-1];
+ if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
+ info->inodeNumber |= NAMEI_INODESPECIAL;
+ info->u.param[0] = data.ftCreationTime.dwHighDateTime;
+ info->u.param[1] = data.ftCreationTime.dwLowDateTime;
+ info->u.param[2] = vno; /* type */
+ info->u.param[3] = volid;
+ if (vno != VI_LINKTABLE)
+ info->linkCount = 1;
+ else {
+ /* Open this handle */
+ char lpath[1024];
+ (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
+ linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
+ info->linkCount =
+ namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
+ }
+ } else {
+ info->linkCount =
+ namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
+ if (info->linkCount == 0) {
+#ifdef DELETE_ZLC
+ Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
+ fpath, data.cFileName);
+ AddToZLCDeleteList(dirl, data.cFileName);
+#else
+ Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
+ data.cFileName);
+#endif
+ } else {
+ info->u.param[2] = data.ftCreationTime.dwHighDateTime;
+ info->u.param[3] = data.ftCreationTime.dwLowDateTime;
+ info->u.param[1] = vno;
+ info->u.param[0] = volid;
+ }
+ }
+ return 0;
+}
+#else
static int
DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
- unsigned int volid)
+ IHandle_t *myIH)
{
char fpath[512];
- struct afs_stat status;
+ struct afs_stat_st status;
+ struct afs_stat_st checkstatus;
int parm, tag;
lb64_string_t check;
+ VolumeId volid = myIH->ih_vid;
+ IHandle_t tmpih;
+ namei_t nameiname;
- afs_snprintf(fpath, sizeof(fpath), "%s/%s", dpath, name);
+ snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
if (afs_stat(fpath, &status) < 0) {
return -1;
int64_to_flipbase64(check, info->inodeNumber);
if (strcmp(name, check))
return -1;
-
- GetOGMFromStat(&status, &parm, &tag);
+
+ /* Check if the _full_ path is correct, to ensure we can actually open this
+ * file later. Otherwise, the salvager can choke. */
+ memset(&tmpih, 0, sizeof(tmpih));
+ tmpih.ih_dev = myIH->ih_dev;
+ tmpih.ih_vid = myIH->ih_vid;
+ tmpih.ih_ino = info->inodeNumber;
+ namei_HandleToName(&nameiname, &tmpih);
+ if ((afs_stat(nameiname.n_path, &checkstatus) < 0) ||
+ checkstatus.st_ino != status.st_ino ||
+ checkstatus.st_size != status.st_size) {
+ static int logged;
+ /* log something for this case, since this means the filename looks
+ * like a valid inode, but it's just in the wrong place. That's pretty
+ * strange. */
+ if (!logged) {
+ logged = 1;
+ Log("Note:\n");
+ Log(" Seemingly-misplaced files have been found, which I am\n");
+ Log(" ignoring for now. If you cannot salvage the relevant volume,\n");
+ Log(" you may try manually moving them to their correct location.\n");
+ Log(" If the relevant volume seems fine, and these files do not\n");
+ Log(" appear to contain important data, you can probably manually\n");
+ Log(" delete them, or leave them alone. Contact your local OpenAFS\n");
+ Log(" expert if you are unsure.\n");
+ }
+ Log("Ignoring misplaced file in volume group %u: %s (should be %s)\n",
+ (unsigned)myIH->ih_vid, fpath, nameiname.n_path);
+ return -1;
+ }
+
if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
+ parm = ((info->inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK);
+ tag = (int)((info->inodeNumber >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
+
/* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
info->u.param[0] = parm;
info->u.param[1] = -1;
info->u.param[2] = tag;
info->u.param[3] = volid;
} else {
+ GetOGMFromStat(&status, &parm, &tag);
/* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
info->u.param[0] = volid;
info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
}
return 0;
}
+#endif
/*
* Convert the VolumeInfo file from RO to RW
#ifdef FSSYNC_BUILD_CLIENT
static afs_int32
-convertVolumeInfo(int fdr, int fdw, afs_uint32 vid)
+convertVolumeInfo(FD_t fdr, FD_t fdw, VolumeId vid)
{
struct VolumeDiskData vd;
char *p;
- if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
+ if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
sizeof(struct VolumeDiskData)) {
- Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
- afs_printable_uint32_lu(vid),
+ Log("1 convertVolumeInfo: read failed for %" AFS_VOLID_FMT " with code %d\n",
+ afs_printable_VolumeId_lu(vid),
errno);
return -1;
}
vd.inUse = 0;
vd.uniquifier += 5000; /* just in case there are still file copies from
* the old RW volume around */
+
+ /* For ROs, the copyDate contains the time that the RO volume was actually
+ * created, and the creationDate just contains the last time the RO was
+ * copied from the RW data. So, make the new RW creationDate more accurate
+ * by setting it to copyDate, if copyDate is older. Since, we know the
+ * volume is at least as old as copyDate. */
+ if (vd.copyDate < vd.creationDate) {
+ vd.creationDate = vd.copyDate;
+ } else {
+ /* If copyDate is newer, just make copyDate and creationDate the same,
+ * for consistency with other RWs */
+ vd.copyDate = vd.creationDate;
+ }
+
p = strrchr(vd.name, '.');
if (p && !strcmp(p, ".readonly")) {
memset(p, 0, 9);
}
- if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
+ if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
sizeof(struct VolumeDiskData)) {
Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
afs_printable_uint32_lu(vid),
*/
int
-namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
+namei_ConvertROtoRWvolume(char *pname, VolumeId volumeId)
{
int code = 0;
#ifdef FSSYNC_BUILD_CLIENT
char smallSeen = 0;
char largeSeen = 0;
char linkSeen = 0;
- int fd, fd2;
+ FD_t fd, fd2;
char *p;
DIR *dirp;
Inode ino;
struct DiskPartition64 *partP;
struct ViceInodeInfo info;
struct VolumeDiskHeader h;
+ char *rwpart, *rwname;
+ Error ec;
# ifdef AFS_DEMAND_ATTACH_FS
int locktype = 0;
# endif /* AFS_DEMAND_ATTACH_FS */
goto done;
}
+ /* check for existing RW on any partition on this server; */
+ /* if found, return EXDEV - invalid cross-device link */
+ VOL_LOCK;
+ VGetVolumePath(&ec, h.parent, &rwpart, &rwname);
+ if (ec == 0) {
+ Log("1 namei_ConvertROtoRWvolume: RW volume %lu already exists on server partition %s.\n",
+ afs_printable_uint32_lu(h.parent), rwpart);
+ code = EXDEV;
+ VOL_UNLOCK;
+ goto done;
+ }
+ VOL_UNLOCK;
+
FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
namei_HandleToName(&n, ih);
strlcpy(dir_name, n.n_path, sizeof(dir_name));
- p = strrchr(dir_name, '/');
+ p = strrchr(dir_name, OS_DIRSEPC);
*p = 0;
dirp = opendir(dir_name);
if (!dirp) {
while ((dp = readdir(dirp))) {
/* struct ViceInodeInfo info; */
-
+#ifndef AFS_NT40_ENV
if (*dp->d_name == '.')
continue;
- if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
- Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
+#endif
+ if (DecodeInode(dir_name, dp->d_name, &info, ih) < 0) {
+ Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
dir_name, dp->d_name);
closedir(dirp);
code = -1;
goto done;
}
if (info.u.param[1] != -1) {
- Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
+ Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s" OS_DIRSEP "%s\n", dir_name, dp->d_name);
closedir(dirp);
code = -1;
goto done;
continue;
}
}
- Log("1 namei_ConvertROtoRWvolume: found special file %s/%s"
+ Log("1 namei_ConvertROtoRWvolume: found special file %s" OS_DIRSEP "%s"
" for volume %lu\n", dir_name, dp->d_name,
afs_printable_uint32_lu(info.u.param[0]));
closedir(dirp);
largeSeen = 1;
} else {
closedir(dirp);
- Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
+ Log("1 namei_ConvertROtoRWvolume: unknown type %u of special file found : %s" OS_DIRSEP "%s\n", info.u.param[2], dir_name, dp->d_name);
code = -1;
goto done;
}
t_ih.ih_dev = ih->ih_dev;
t_ih.ih_vid = ih->ih_vid;
- (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
- fd = afs_open(oldpath, O_RDWR, 0);
- if (fd < 0) {
+ code = snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
+ infoName);
+ if (code < 0 || code >= sizeof(oldpath)) {
+ /* error, or truncated */
+ code = -1;
+ goto done;
+ }
+ fd = OS_OPEN(oldpath, O_RDWR, 0);
+ if (fd == INVALID_FD) {
Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
oldpath);
code = -1;
}
t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
namei_HandleToName(&n, &t_ih);
- fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
- if (fd2 < 0) {
+ fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
+ if (fd2 == INVALID_FD) {
Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
- close(fd);
+ OS_CLOSE(fd);
code = -1;
goto done;
}
code = convertVolumeInfo(fd, fd2, ih->ih_vid);
- close(fd);
+ OS_CLOSE(fd);
if (code) {
- close(fd2);
- unlink(n.n_path);
+ OS_CLOSE(fd2);
+ OS_UNLINK(n.n_path);
code = -1;
goto done;
}
SetOGM(fd2, ih->ih_vid, 1);
- close(fd2);
+ OS_CLOSE(fd2);
t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
namei_HandleToName(&n, &t_ih);
- (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
- fd = afs_open(newpath, O_RDWR, 0);
- if (fd < 0) {
+ code = snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+ smallName);
+ if (code < 0 || code >= sizeof(newpath)) {
+ /* error, or truncated */
+ code = -1;
+ goto done;
+ }
+ fd = OS_OPEN(newpath, O_RDWR, 0);
+ if (fd == INVALID_FD) {
Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
code = -1;
goto done;
}
SetOGM(fd, ih->ih_vid, 2);
- close(fd);
- link(newpath, n.n_path);
- unlink(newpath);
+ OS_CLOSE(fd);
+#ifdef AFS_NT40_ENV
+ MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
+#else
+ if (link(newpath, n.n_path)) {
+ Log("1 namei_ConvertROtoRWvolume: could not move SmallIndex file: %s\n", n.n_path);
+ code = -1;
+ goto done;
+ }
+ OS_UNLINK(newpath);
+#endif
t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
namei_HandleToName(&n, &t_ih);
- (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
- fd = afs_open(newpath, O_RDWR, 0);
- if (fd < 0) {
+ code = snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+ largeName);
+ if (code < 0 || code >= sizeof(newpath)) {
+ /* error, or truncated */
+ code = -1;
+ goto done;
+ }
+ fd = OS_OPEN(newpath, O_RDWR, 0);
+ if (fd == INVALID_FD) {
Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
code = -1;
goto done;
}
SetOGM(fd, ih->ih_vid, 3);
- close(fd);
- link(newpath, n.n_path);
- unlink(newpath);
+ OS_CLOSE(fd);
+#ifdef AFS_NT40_ENV
+ MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
+#else
+ if (link(newpath, n.n_path)) {
+ Log("1 namei_ConvertROtoRWvolume: could not move LargeIndex file: %s\n", n.n_path);
+ code = -1;
+ goto done;
+ }
+ OS_UNLINK(newpath);
+#endif
- unlink(oldpath);
+ OS_UNLINK(oldpath);
h.id = h.parent;
h.volumeInfo_hi = h.id;
FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
+ code = 0;
done:
# ifdef AFS_DEMAND_ATTACH_FS
if (locktype) {
if (!s)
s = result;
- (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
+ snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
return s;
}
static void
AddToZLCDeleteList(char dir, char *name)
{
- assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
+ opr_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
if (zlcCur && zlcCur->zlc_next)
zlcCur = zlcCur->zlc_next;
else {
- zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
+ zlcList_t *tmp = malloc(sizeof(zlcList_t));
if (!tmp)
return;
if (!zlcAnchor) {
}
}
- (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
+ if (dir)
+ (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
+ else
+ (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
+
zlcCur->zlc_n++;
}
for (z = zlcAnchor; z; z = z->zlc_next) {
for (i = 0; i < z->zlc_n; i++) {
- (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
+ if (path)
+ (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
+ else
+ (void)sprintf(fname, "%s", z->zlc_names[i]);
if (namei_unlink(fname) < 0) {
Log("ZLC: Can't unlink %s, dos error = %d\n", fname,
GetLastError());
}
#endif
+/**
+ * Remove empty directories associated with the volume received
+ * as an argument.
+ *
+ * @param[in] pname vice partition path
+ * @param[in] vid volume id
+ *
+ * @return 0 on success
+ */
+int
+namei_RemoveDirectories(char *pname, afs_int32 vid)
+{
+ IHandle_t dirIH;
+ namei_t name;
+
+ memset(&dirIH, 0, sizeof(dirIH));
+
+ dirIH.ih_vid = vid;
+
+#ifdef AFS_NT40_ENV
+ dirIH.ih_dev = nt_DriveToDev(pname);
+#else
+ dirIH.ih_dev = volutil_GetPartitionID(pname);
+#endif
+ if (dirIH.ih_dev == -1) {
+ return -1;
+ }
+
+ namei_HandleToVolDir(&name, &dirIH);
+
+ return namei_RemoveDataDirectories(&name);
+}
+
#endif /* AFS_NAMEI_ENV */