ubik: make uphys_close static
[openafs.git] / src / ubik / phys.c
index 2611056..5ef33be 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * 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>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 #include <sys/types.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
 #include <io.h>
@@ -23,16 +26,23 @@ RCSID
 #include <netinet/in.h>
 #endif
 #include <sys/stat.h>
+
+/* #ifdef AFS_PTHREAD_ENV */
+#if 0   /* temporary hack - klm */
+/* nothing */
+#else
 #include <lwp.h>
+#endif
+
 #include <lock.h>
-#include <errno.h>
-#include <string.h>
+#include <afs/afsutil.h>
 
 #define        UBIK_INTERNALS 1
 #include "ubik.h"
 
-/* these routines are called via the proc ptr in the ubik_dbase structure.  They provide access to
- * the physical disk, by converting the file numbers being processed (>= 0 for user data space, < 0
+/*! \file
+ * These routines are called via the proc ptr in the ubik_dbase structure.  They provide access to
+ * the physical disk, by converting the file numbers being processed ( >= 0 for user data space, < 0
  * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
  * etc.
  */
@@ -46,15 +56,16 @@ static struct fdcache {
 
 static char pbuffer[1024];
 
-/* beware, when using this function, of the header in front of most files */
+/*!
+ * \warning Beware, when using this function, of the header in front of most files.
+ */
 static int
-uphys_open(register struct ubik_dbase *adbase, afs_int32 afid)
+uphys_open(struct ubik_dbase *adbase, afs_int32 afid)
 {
-    char temp[20];
-    register int fd;
+    int fd;
     static int initd;
-    register int i;
-    register struct fdcache *tfd;
+    int i;
+    struct fdcache *tfd;
     struct fdcache *bestfd;
 
     /* initialize package */
@@ -78,15 +89,8 @@ uphys_open(register struct ubik_dbase *adbase, afs_int32 afid)
     }
 
     /* not found, open it and try to enter in cache */
-    strcpy(pbuffer, adbase->pathName);
-    strcat(pbuffer, ".DB");
-    if (afid < 0) {
-       i = -afid;
-       strcat(pbuffer, "SYS");
-    } else
-       i = afid;
-    sprintf(temp, "%d", i);
-    strcat(pbuffer, temp);
+    afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d", adbase->pathName,
+                (afid<0)?"SYS":"", (afid<0)?-afid:afid);
     fd = open(pbuffer, O_CREAT | O_RDWR, 0600);
     if (fd < 0) {
        /* try opening read-only */
@@ -126,20 +130,35 @@ uphys_open(register struct ubik_dbase *adbase, afs_int32 afid)
     return fd;
 }
 
-/* close the file, maintaining ref count in cache structure */
-int
-uphys_close(register int afd)
+/*!
+ * \brief Close the file, maintaining ref count in cache structure.
+ */
+static int
+uphys_close(int afd)
 {
-    register int i;
-    register struct fdcache *tfd;
+    int i;
+    struct fdcache *tfd;
 
     if (afd < 0)
        return EBADF;
     tfd = fdcache;
     for (i = 0; i < MAXFDCACHE; i++, tfd++) {
        if (tfd->fd == afd) {
-           tfd->refCount--;
-           return 0;
+           if (tfd->fileID != -10000) {
+               tfd->refCount--;
+               return 0;
+           } else {
+               if (tfd->refCount > 0) {
+                   tfd->refCount--;
+                   if (tfd->refCount == 0) {
+                       close(tfd->fd);
+                       tfd->fd = -1;
+                   }
+                   return 0;
+               }
+               tfd->fd = -1;
+               break;
+           }
        }
     }
     return close(afd);
@@ -148,9 +167,9 @@ uphys_close(register int afd)
 int
 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
 {
-    register int fd;
+    int fd;
     struct stat tstat;
-    register afs_int32 code;
+    afs_int32 code;
 
     fd = uphys_open(adbase, afid);
     if (fd < 0)
@@ -170,11 +189,11 @@ uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
 }
 
 int
-uphys_read(register struct ubik_dbase *adbase, afs_int32 afile,
-          register char *abuffer, afs_int32 apos, afs_int32 alength)
+uphys_read(struct ubik_dbase *adbase, afs_int32 afile,
+          void *abuffer, afs_int32 apos, afs_int32 alength)
 {
-    register int fd;
-    register afs_int32 code;
+    int fd;
+    afs_int32 code;
 
     fd = uphys_open(adbase, afile);
     if (fd < 0)
@@ -190,11 +209,11 @@ uphys_read(register struct ubik_dbase *adbase, afs_int32 afile,
 }
 
 int
-uphys_write(register struct ubik_dbase *adbase, afs_int32 afile,
-           register char *abuffer, afs_int32 apos, afs_int32 alength)
+uphys_write(struct ubik_dbase *adbase, afs_int32 afile,
+           void *abuffer, afs_int32 apos, afs_int32 alength)
 {
-    register int fd;
-    register afs_int32 code;
+    int fd;
+    afs_int32 code;
     afs_int32 length;
 
     fd = uphys_open(adbase, afile);
@@ -214,10 +233,10 @@ uphys_write(register struct ubik_dbase *adbase, afs_int32 afile,
 }
 
 int
-uphys_truncate(register struct ubik_dbase *adbase, afs_int32 afile,
+uphys_truncate(struct ubik_dbase *adbase, afs_int32 afile,
               afs_int32 asize)
 {
-    register afs_int32 code, fd;
+    afs_int32 code, fd;
     fd = uphys_open(adbase, afile);
     if (fd < 0)
        return UNOENT;
@@ -226,21 +245,27 @@ uphys_truncate(register struct ubik_dbase *adbase, afs_int32 afile,
     return code;
 }
 
-/* get number of dbase files */
+/*!
+ * \brief Get number of dbase files.
+ *
+ * \todo Really should scan dir for data.
+ */
 int
-uphys_getnfiles(register struct ubik_dbase *adbase)
+uphys_getnfiles(struct ubik_dbase *adbase)
 {
     /* really should scan dir for data */
     return 1;
 }
 
-/* get database label, with aversion in host order */
+/*!
+ * \brief Get database label, with \p aversion in host order.
+ */
 int
-uphys_getlabel(register struct ubik_dbase *adbase, afs_int32 afile,
+uphys_getlabel(struct ubik_dbase *adbase, afs_int32 afile,
               struct ubik_version *aversion)
 {
     struct ubik_hdr thdr;
-    register afs_int32 code, fd;
+    afs_int32 code, fd;
 
     fd = uphys_open(adbase, afile);
     if (fd < 0)
@@ -256,13 +281,15 @@ uphys_getlabel(register struct ubik_dbase *adbase, afs_int32 afile,
     return 0;
 }
 
-/* label database, with aversion in host order */
+/*!
+ * \brief Label database, with \p aversion in host order.
+ */
 int
-uphys_setlabel(register struct ubik_dbase *adbase, afs_int32 afile,
+uphys_setlabel(struct ubik_dbase *adbase, afs_int32 afile,
               struct ubik_version *aversion)
 {
     struct ubik_hdr thdr;
-    register afs_int32 code, fd;
+    afs_int32 code, fd;
 
     fd = uphys_open(adbase, afile);
     if (fd < 0)
@@ -281,11 +308,30 @@ uphys_setlabel(register struct ubik_dbase *adbase, afs_int32 afile,
 }
 
 int
-uphys_sync(register struct ubik_dbase *adbase, afs_int32 afile)
+uphys_sync(struct ubik_dbase *adbase, afs_int32 afile)
 {
-    register afs_int32 code, fd;
+    afs_int32 code, fd;
     fd = uphys_open(adbase, afile);
     code = fsync(fd);
     uphys_close(fd);
     return code;
 }
+
+void
+uphys_invalidate(struct ubik_dbase *adbase, afs_int32 afid)
+{
+    int i;
+    struct fdcache *tfd;
+
+    /* scan file descr cache */
+    for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
+       if (afid == tfd->fileID) {
+           tfd->fileID = -10000;
+           if (tfd->fd >= 0 && tfd->refCount == 0) {
+               close(tfd->fd);
+               tfd->fd = -1;
+           }
+           return;
+       }
+    }
+}