vol: Don't always FDH_REALLYCLOSE on linktable ops
[openafs.git] / src / vol / namei_ops.c
index cb69e91..62191d9 100644 (file)
 
 #include <roken.h>
 
+
 #ifdef AFS_NAMEI_ENV
-#include <stdio.h>
-#include <stdlib.h>
-#ifndef AFS_NT40_ENV
-#include <unistd.h>
-#else
+
+#ifdef HAVE_SYS_FILE_H
+# include <sys/file.h>
+#endif
+
+#ifdef AFS_NT40_ENV
 #define DELETE_ZLC
-#include <io.h>
 #include <windows.h>
 #include <winnt.h>
 #include <winbase.h>
-#include <winsock2.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#endif
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#ifdef AFS_NT40_ENV
 #include <direct.h>
-#else
-#include <sys/file.h>
-#include <sys/param.h>
 #endif
-#include <dirent.h>
+
 #include <afs/afs_assert.h>
-#include <string.h>
 #include <lock.h>
 #include <afs/afsutil.h>
 #include <lwp.h>
@@ -56,6 +45,7 @@
 #include "volume_inline.h"
 #include "common.h"
 #include <afs/errors.h>
+
 #ifdef AFS_NT40_ENV
 #include <afs/errmap_nt.h>
 #endif
 #include <vol/vol-salvage.h>
 #endif
 
-#if !defined(HAVE_FLOCK) && !defined(AFS_NT40_ENV)
-#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)
-#endif
-
 int Testing=0;
 
 
@@ -353,12 +318,13 @@ namei_ViceREADME(char *partition)
     int fd;
 
     /* Create the inode directory if we're starting for the first time */
-    (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
-                      INODEDIR);
+    snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
+            INODEDIR);
     mkdir(filename, 0700);
 
-    (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
-                       partition, INODEDIR);
+    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) {
        (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
@@ -382,7 +348,7 @@ namei_CreateDataDirectories(namei_t * name, int *created)
     int i;
 
     *created = 0;
-    afs_snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
+    snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
 
     if (mkdir(tmp) < 0) {
         if (errno != EEXIST)
@@ -540,7 +506,7 @@ namei_RemoveDataDirectories(namei_t * name)
     char tmp[256];
     int i;
 
-    afs_snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
+    snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
 
     path = tmp;
     path += strlen(path);
@@ -1030,7 +996,7 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
            }
 
            if (count > 0) {
-               FDH_REALLYCLOSE(fdP);
+               FDH_CLOSE(fdP);
                IH_RELEASE(tmp);
                return 0;
            }
@@ -1067,7 +1033,7 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
        } 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",
+           Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
                th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
            IH_RELEASE(th);
 
@@ -1171,7 +1137,7 @@ namei_copy_on_write(IHandle_t *h)
        fdP = IH_OPEN(h);
        if (!fdP)
            return EIO;
-       afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
+       snprintf(path, sizeof(path), "%s-tmp", name.n_path);
        fd = OS_OPEN(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
        if (fd == INVALID_FD) {
            FDH_CLOSE(fdP);
@@ -1506,7 +1472,7 @@ GetFreeTag(IHandle_t * ih, int vno)
     }
     FDH_SYNC(fdP);
     FDH_UNLOCKFILE(fdP, offset);
-    FDH_REALLYCLOSE(fdP);
+    FDH_CLOSE(fdP);
     return col;
 
   badGetFreeTag:
@@ -1596,7 +1562,7 @@ WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
 {
     size_t n;
     n = OS_WRITE(fp, info, sizeof(*info));
-    return (n == 1) ? 0 : -2;
+    return (n == sizeof(*info)) ? 0 : -2;
 }
 
 
@@ -1674,7 +1640,7 @@ ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
     /*
      * Paranoia:  check that the file is really the right size
      */
-    if (OS_SIZE(inodeFile) * sizeof(struct ViceInodeInfo)) {
+    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),
@@ -1760,8 +1726,8 @@ namei_ListAFSFiles(char *dev,
 #else
            if (*dp1->d_name == '.')
                continue;
-           afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
-                        dp1->d_name);
+           snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
+                    dp1->d_name);
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -1861,8 +1827,8 @@ _namei_examine_special(char * path1,
     } else {
        char path2[512];
        /* Open this handle */
-       (void)afs_snprintf(path2, sizeof(path2),
-                          "%s" OS_DIRSEP "%s", path1, dname);
+       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);
@@ -1870,9 +1836,12 @@ _namei_examine_special(char * path1,
 
     if (!judgeFun ||
        (*judgeFun) (&info, singleVolumeNumber, rock)) {
-       ret = 1;
-        if ((*writeFun) (fp, &info, path1, dname) < 0) {
+       ret = (*writeFun) (fp, &info, path1, dname);
+       if (ret < 0) {
+           Log("_namei_examine_special: writeFun returned %d\n", ret);
            ret = -1;
+       } else {
+           ret = 1;
        }
     }
 
@@ -1952,9 +1921,12 @@ _namei_examine_reg(char * path3,
 
     if (!judgeFun ||
        (*judgeFun) (&info, singleVolumeNumber, rock)) {
-       ret = 1;
-        if ((*writeFun) (fp, &info, path3, dname) < 0) {
+       ret = (*writeFun) (fp, &info, path3, dname);
+       if (ret < 0) {
+           Log("_namei_examine_reg: writeFun returned %d\n", ret);
            ret = -1;
+       } else {
+           ret = 1;
        }
     }
 
@@ -2438,7 +2410,8 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 
 #ifndef AFS_NT40_ENV /* This level missing on Windows */
            /* Now we've got a next level subdir. */
-           afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", path1, dp1->d_name);
+           snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
+                    path1, dp1->d_name);
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -2446,12 +2419,12 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                        continue;
 
                    /* Now we've got to the actual data */
-                   afs_snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s", path2,
-                                dp2->d_name);
+                   snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+                            path2, dp2->d_name);
 #else
                    /* Now we've got to the actual data */
-                   afs_snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s", path1,
-                                dp1->d_name);
+                   snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+                            path1, dp1->d_name);
 #endif
                    dirp3 = opendir(path3);
                    if (dirp3) {
@@ -2592,7 +2565,7 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     FdHandle_t linkHandle;
     char dirl;
 
-    afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
+    snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
 
     dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
                           FindExSearchNameMatch, NULL,
@@ -2664,7 +2637,7 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     int parm, tag;
     lb64_string_t check;
 
-    afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
+    snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
 
     if (afs_stat(fpath, &status) < 0) {
        return -1;
@@ -2907,8 +2880,8 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     t_ih.ih_dev = ih->ih_dev;
     t_ih.ih_vid = ih->ih_vid;
 
-    (void)afs_snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
-                      infoName);
+    snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
+            infoName);
     fd = OS_OPEN(oldpath, O_RDWR, 0);
     if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
@@ -2938,8 +2911,8 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
     namei_HandleToName(&n, &t_ih);
-    (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
-                      smallName);
+    snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+            smallName);
     fd = OS_OPEN(newpath, O_RDWR, 0);
     if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
@@ -2957,8 +2930,8 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
     namei_HandleToName(&n, &t_ih);
-    (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
-                      largeName);
+    snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+            largeName);
     fd = OS_OPEN(newpath, O_RDWR, 0);
     if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
@@ -3019,7 +2992,7 @@ PrintInode(char *s, Inode ino)
     if (!s)
        s = result;
 
-    (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%" AFS_UINT64_FMT, (afs_uintmax_t) ino);
+    snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
 
     return s;
 }