Windows: define AFS_IHANDLE_PIO_ENV for ihandle pio
[openafs.git] / src / vol / ihandle.c
index d903c01..c32ae75 100644 (file)
@@ -319,13 +319,107 @@ streamHandleAllocateChunk(void)
 
 /*
  * Get a file descriptor handle given an Inode handle
+ * Takes the given file descriptor, and creates a new FdHandle_t for it,
+ * attached to the given IHandle_t. If fdLruHead is not NULL, fd can be
+ * INVALID_FD, indicating that the caller failed to open the relevant file
+ * because we had too many FDs open; ih_attachfd_r will then just evict/close
+ * an existing fd in the cache, and return NULL. You must not call this
+ * function with an invalid fd while fdLruHead is NULL; instead, error out.
+ */
+static FdHandle_t *
+ih_attachfd_r(IHandle_t *ihP, FD_t fd)
+{
+    FD_t closeFd;
+    FdHandle_t *fdP;
+
+    /* If the given fd is invalid, we must have an available fd to close.
+     * Otherwise, the caller must have realized this before calling
+     * ih_attachfd_r and yielded an error before getting here. */
+    opr_Assert(fd != INVALID_FD || fdLruHead != NULL);
+
+    /* fdCacheSize limits the size of the descriptor cache, but
+     * we permit the number of open files to exceed fdCacheSize.
+     * We only recycle open file descriptors when the number
+     * of open files reaches the size of the cache */
+    if ((fdInUseCount > fdCacheSize || fd == INVALID_FD)  && fdLruHead != NULL) {
+       fdP = fdLruHead;
+       opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
+       DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
+       DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
+                  fd_ihnext, fd_ihprev);
+       closeFd = fdP->fd_fd;
+       if (fd == INVALID_FD) {
+           fdCacheSize--;          /* reduce in order to not run into here too often */
+           DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
+           fdP->fd_status = FD_HANDLE_AVAIL;
+           fdP->fd_ih = NULL;
+           fdP->fd_fd = INVALID_FD;
+           IH_UNLOCK;
+           OS_CLOSE(closeFd);
+           IH_LOCK;
+           fdInUseCount -= 1;
+           return NULL;
+       }
+    } else {
+       if (fdAvailHead == NULL) {
+           fdHandleAllocateChunk();
+       }
+       fdP = fdAvailHead;
+       opr_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
+       DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
+       closeFd = INVALID_FD;
+    }
+
+    fdP->fd_status = FD_HANDLE_INUSE;
+    fdP->fd_fd = fd;
+    fdP->fd_ih = ihP;
+    fdP->fd_refcnt++;
+
+    ihP->ih_refcnt++;
+
+    /* Add this handle to the Inode's list of open descriptors */
+    DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
+                   fd_ihprev);
+
+    if (closeFd != INVALID_FD) {
+       IH_UNLOCK;
+       OS_CLOSE(closeFd);
+       IH_LOCK;
+       fdInUseCount -= 1;
+    }
+
+    return fdP;
+}
+
+FdHandle_t *
+ih_attachfd(IHandle_t *ihP, FD_t fd)
+{
+    FdHandle_t *fdP;
+
+    if (fd == INVALID_FD) {
+       return NULL;
+    }
+
+    IH_LOCK;
+
+    fdInUseCount += 1;
+
+    fdP = ih_attachfd_r(ihP, fd);
+    opr_Assert(fdP);
+
+    IH_UNLOCK;
+
+    return fdP;
+}
+
+/*
+ * Get a file descriptor handle given an Inode handle
  */
 FdHandle_t *
 ih_open(IHandle_t * ihP)
 {
     FdHandle_t *fdP;
     FD_t fd;
-    FD_t closeFd;
 
     if (!ihP)                  /* XXX should log here in the fileserver */
        return NULL;
@@ -339,7 +433,7 @@ ih_open(IHandle_t * ihP)
             * cannot reuse it; it will be closed soon. */
            continue;
        }
-#ifndef HAVE_PIO
+#ifndef AFS_IHANDLE_PIO_ENV
        /*
         * If we don't have positional i/o, don't try to share fds, since
         * we can't do so in a threadsafe way.
@@ -348,9 +442,9 @@ ih_open(IHandle_t * ihP)
            continue;
        }
        opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
-#else /* HAVE_PIO */
+#else /* AFS_IHANDLE_PIO_ENV */
        opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
-#endif /* HAVE_PIO */
+#endif /* AFS_IHANDLE_PIO_ENV */
 
        fdP->fd_refcnt++;
        if (fdP->fd_status == FD_HANDLE_OPEN) {
@@ -376,56 +470,15 @@ ih_open_retry:
        return NULL;
     }
 
-    /* fdCacheSize limits the size of the descriptor cache, but
-     * we permit the number of open files to exceed fdCacheSize.
-     * We only recycle open file descriptors when the number
-     * of open files reaches the size of the cache */
-    if ((fdInUseCount > fdCacheSize || fd == INVALID_FD)  && fdLruHead != NULL) {
-       fdP = fdLruHead;
-       opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
-       DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
-       DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
-                  fd_ihnext, fd_ihprev);
-       closeFd = fdP->fd_fd;
-       if (fd == INVALID_FD) {
-           fdCacheSize--;          /* reduce in order to not run into here too often */
-           DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
-           fdP->fd_status = FD_HANDLE_AVAIL;
-           fdP->fd_ih = NULL;
-           fdP->fd_fd = INVALID_FD;
-           IH_UNLOCK;
-           OS_CLOSE(closeFd);
-           goto ih_open_retry;
-       }
-    } else {
-       if (fdAvailHead == NULL) {
-           fdHandleAllocateChunk();
-       }
-       fdP = fdAvailHead;
-       opr_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
-       DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
-       closeFd = INVALID_FD;
-    }
-
-    fdP->fd_status = FD_HANDLE_INUSE;
-    fdP->fd_fd = fd;
-    fdP->fd_ih = ihP;
-    fdP->fd_refcnt++;
-
-    ihP->ih_refcnt++;
-
-    /* Add this handle to the Inode's list of open descriptors */
-    DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
-                   fd_ihprev);
-
-    if (closeFd != INVALID_FD) {
+    fdP = ih_attachfd_r(ihP, fd);
+    if (!fdP) {
+       opr_Assert(fd == INVALID_FD);
        IH_UNLOCK;
-       OS_CLOSE(closeFd);
-       IH_LOCK;
-       fdInUseCount -= 1;
+       goto ih_open_retry;
     }
 
     IH_UNLOCK;
+
     return fdP;
 }
 
@@ -1006,6 +1059,23 @@ ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
 }
 #endif /* AFS_NAMEI_ENV */
 
+#if defined(AFS_NT40_ENV) || !defined(AFS_NAMEI_ENV)
+/* Unix namei implements its own more efficient IH_CREATE_INIT; this wrapper
+ * is for everyone else */
+IHandle_t *
+ih_icreate_init(IHandle_t *lh, int dev, char *part, Inode nearInode,
+                afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+{
+    IHandle_t *ihP;
+    Inode ino = IH_CREATE(lh, dev, part, nearInode, p1, p2, p3, p4);
+    if (!VALID_INO(ino)) {
+        return NULL;
+    }
+    IH_INIT(ihP, dev, p1, ino);
+    return ihP;
+}
+#endif
+
 afs_sfsize_t
 ih_size(FD_t fd)
 {
@@ -1022,7 +1092,7 @@ ih_size(FD_t fd)
 #endif
 }
 
-#ifndef HAVE_PIO
+#ifndef AFS_IHANDLE_PIO_ENV
 ssize_t
 ih_pread(int fd, void * buf, size_t count, afs_foff_t offset)
 {
@@ -1042,7 +1112,7 @@ ih_pwrite(int fd, const void * buf, size_t count, afs_foff_t offset)
            return code;
        return OS_WRITE(fd, buf, count);
 }
-#endif /* !HAVE_PIO */
+#endif /* !AFS_IHANDLE_PIO_ENV */
 
 #ifndef AFS_NT40_ENV
 int