ihandle: Refactor ih_open to split out ih_attachfd
[openafs.git] / src / vol / ihandle.c
index 58cd546..c87f43d 100644 (file)
 #include <sys/resource.h>
 #endif
 
-#include <rx/xdr.h>
+#include <afs/opr.h>
+#ifdef AFS_PTHREAD_ENV
+# include <opr/lock.h>
+#endif
 #include <afs/afsint.h>
 #include <afs/afssyscalls.h>
 #include <afs/afsutil.h>
@@ -73,7 +76,6 @@ int fdInUseCount = 0;
 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
 
 static int _ih_release_r(IHandle_t * ihP);
-void *ih_sync_thread(void *);
 
 /* start-time configurable I/O limits */
 ih_init_params vol_io_params;
@@ -95,6 +97,31 @@ void ih_PkgDefaults(void)
     /* fd cache size that will be used if/when ih_UseLargeCache()
      * is called */
     vol_io_params.fd_max_cachesize = FD_MAX_CACHESIZE;
+
+    vol_io_params.sync_behavior = IH_SYNC_ONCLOSE;
+}
+
+int
+ih_SetSyncBehavior(const char *behavior)
+{
+    int val;
+
+    if (strcmp(behavior, "always") == 0) {
+       val = IH_SYNC_ALWAYS;
+
+    } else if (strcmp(behavior, "onclose") == 0) {
+       val = IH_SYNC_ONCLOSE;
+
+    } else if (strcmp(behavior, "never") == 0) {
+       val = IH_SYNC_NEVER;
+
+    } else {
+       /* invalid behavior name */
+       return -1;
+    }
+
+    vol_io_params.sync_behavior = val;
+    return 0;
 }
 
 #ifdef AFS_PTHREAD_ENV
@@ -102,7 +129,7 @@ void ih_PkgDefaults(void)
 void
 ih_glock_init(void)
 {
-    MUTEX_INIT(&ih_glock_mutex, "ih glock", MUTEX_DEFAULT, 0);
+    opr_mutex_init(&ih_glock_mutex);
 }
 #endif /* AFS_PTHREAD_ENV */
 
@@ -111,7 +138,7 @@ void
 ih_Initialize(void)
 {
     int i;
-    osi_Assert(!ih_Inited);
+    opr_Assert(!ih_Inited);
     ih_Inited = 1;
     DLL_INIT_LIST(ihAvailHead, ihAvailTail);
     DLL_INIT_LIST(fdAvailHead, fdAvailTail);
@@ -124,9 +151,9 @@ ih_Initialize(void)
 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
     {
        struct rlimit rlim;
-       osi_Assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
+       opr_Verify(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
        rlim.rlim_cur = rlim.rlim_max;
-       osi_Assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
+       opr_Verify(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
        fdMaxCacheSize = rlim.rlim_cur - vol_io_params.fd_handle_setaside;
 #ifdef AFS_NBSD_ENV
        /* XXX this is to avoid using up all system fd netbsd is
@@ -140,7 +167,7 @@ ih_Initialize(void)
        fdMaxCacheSize /= 4;
 #endif
        fdMaxCacheSize = min(fdMaxCacheSize, vol_io_params.fd_max_cachesize);
-       osi_Assert(fdMaxCacheSize > 0);
+       opr_Assert(fdMaxCacheSize > 0);
     }
 #elif defined(AFS_HPUX_ENV)
     /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
@@ -153,23 +180,6 @@ ih_Initialize(void)
     }
 #endif
     fdCacheSize = min(fdMaxCacheSize, vol_io_params.fd_initial_cachesize);
-
-    {
-#ifdef AFS_PTHREAD_ENV
-       pthread_t syncer;
-       pthread_attr_t tattr;
-
-       pthread_attr_init(&tattr);
-       pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
-
-       pthread_create(&syncer, &tattr, ih_sync_thread, NULL);
-#else /* AFS_PTHREAD_ENV */
-       PROCESS syncer;
-       LWP_CreateProcess(ih_sync_thread, 16*1024, LWP_MAX_PRIORITY - 2,
-           NULL, "ih_syncer", &syncer);
-#endif /* AFS_PTHREAD_ENV */
-    }
-
 }
 
 /* Make the file descriptor cache as big as possible. Don't this call
@@ -202,9 +212,9 @@ iHandleAllocateChunk(void)
     int i;
     IHandle_t *ihP;
 
-    osi_Assert(ihAvailHead == NULL);
-    ihP = (IHandle_t *) malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
-    osi_Assert(ihP != NULL);
+    opr_Assert(ihAvailHead == NULL);
+    ihP = malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
+    opr_Assert(ihP != NULL);
     for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
        ihP[i].ih_refcnt = 0;
        DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
@@ -241,7 +251,7 @@ ih_init(int dev, int vid, Inode ino)
        iHandleAllocateChunk();
     }
     ihP = ihAvailHead;
-    osi_Assert(ihP->ih_refcnt == 0);
+    opr_Assert(ihP->ih_refcnt == 0);
     DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
     ihP->ih_dev = dev;
     ihP->ih_vid = vid;
@@ -261,8 +271,8 @@ IHandle_t *
 ih_copy(IHandle_t * ihP)
 {
     IH_LOCK;
-    osi_Assert(ih_Inited);
-    osi_Assert(ihP->ih_refcnt > 0);
+    opr_Assert(ih_Inited);
+    opr_Assert(ihP->ih_refcnt > 0);
     ihP->ih_refcnt++;
     IH_UNLOCK;
     return ihP;
@@ -275,9 +285,9 @@ fdHandleAllocateChunk(void)
     int i;
     FdHandle_t *fdP;
 
-    osi_Assert(fdAvailHead == NULL);
-    fdP = (FdHandle_t *) malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
-    osi_Assert(fdP != NULL);
+    opr_Assert(fdAvailHead == NULL);
+    fdP = malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
+    opr_Assert(fdP != NULL);
     for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
        fdP[i].fd_status = FD_HANDLE_AVAIL;
        fdP[i].fd_refcnt = 0;
@@ -296,10 +306,10 @@ streamHandleAllocateChunk(void)
     int i;
     StreamHandle_t *streamP;
 
-    osi_Assert(streamAvailHead == NULL);
+    opr_Assert(streamAvailHead == NULL);
     streamP = (StreamHandle_t *)
        malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
-    osi_Assert(streamP != NULL);
+    opr_Assert(streamP != NULL);
     for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
        streamP[i].str_fd = INVALID_FD;
        DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
@@ -309,13 +319,99 @@ 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. 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.
+ */
+static FdHandle_t *
+ih_attachfd_r(IHandle_t *ihP, FD_t fd)
+{
+    FD_t closeFd;
+    FdHandle_t *fdP;
+
+    /* 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;
+
+    IH_LOCK;
+
+    fdInUseCount += 1;
+
+    fdP = ih_attachfd_r(ihP, fd);
+    if (!fdP) {
+       fdInUseCount -= 1;
+    }
+
+    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;
@@ -337,9 +433,9 @@ ih_open(IHandle_t * ihP)
        if (fdP->fd_status == FD_HANDLE_INUSE) {
            continue;
        }
-       osi_Assert(fdP->fd_status == FD_HANDLE_OPEN);
+       opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
 #else /* HAVE_PIO */
-       osi_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
+       opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
 #endif /* HAVE_PIO */
 
        fdP->fd_refcnt++;
@@ -366,56 +462,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;
-       osi_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;
-       osi_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;
 }
 
@@ -431,9 +486,9 @@ fd_close(FdHandle_t * fdP)
        return 0;
 
     IH_LOCK;
-    osi_Assert(ih_Inited);
-    osi_Assert(fdInUseCount > 0);
-    osi_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
+    opr_Assert(ih_Inited);
+    opr_Assert(fdInUseCount > 0);
+    opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
                fdP->fd_status == FD_HANDLE_CLOSING);
 
     ihP = fdP->fd_ih;
@@ -483,9 +538,9 @@ fd_reallyclose(FdHandle_t * fdP)
        return 0;
 
     IH_LOCK;
-    osi_Assert(ih_Inited);
-    osi_Assert(fdInUseCount > 0);
-    osi_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
+    opr_Assert(ih_Inited);
+    opr_Assert(fdInUseCount > 0);
+    opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
                fdP->fd_status == FD_HANDLE_CLOSING);
 
     ihP = fdP->fd_ih;
@@ -508,6 +563,20 @@ fd_reallyclose(FdHandle_t * fdP)
      */
     if (!ihP->ih_fdhead) {
        ihP->ih_flags &= ~IH_REALLY_CLOSED;
+    } else {
+       FdHandle_t *lfdP, *next;
+       int clear = 1;
+       for (lfdP = ihP->ih_fdhead; lfdP != NULL; lfdP = next) {
+           next = lfdP->fd_ihnext;
+           osi_Assert(lfdP->fd_ih == ihP);
+           if (lfdP->fd_status != FD_HANDLE_CLOSING) {
+               clear = 0;
+               break;
+           }
+       }
+       /* no *future* fd should be subjected to this */
+       if (clear)
+           ihP->ih_flags &= ~IH_REALLY_CLOSED;
     }
 
     if (fdP->fd_refcnt == 0) {
@@ -571,7 +640,7 @@ stream_open(const char *filename, const char *mode)
     } else if (strcmp(mode, "a+") == 0) {
        fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
     } else {
-       osi_Assert(FALSE);              /* not implemented */
+       opr_abort();            /* not implemented */
     }
 
     if (fd == INVALID_FD) {
@@ -594,7 +663,7 @@ stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
        streamP->str_bufoff = 0;
        streamP->str_buflen = 0;
     } else {
-       osi_Assert(streamP->str_direction == STREAM_DIRECTION_READ);
+       opr_Assert(streamP->str_direction == STREAM_DIRECTION_READ);
     }
 
     bytesRead = 0;
@@ -648,7 +717,7 @@ stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
        streamP->str_bufoff = 0;
        streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
     } else {
-       osi_Assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
+       opr_Assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
     }
 
     nbytes = size * nitems;
@@ -738,7 +807,7 @@ stream_close(StreamHandle_t * streamP, int reallyClose)
     ssize_t rc;
     int retval = 0;
 
-    osi_Assert(streamP != NULL);
+    opr_Assert(streamP != NULL);
     if (streamP->str_direction == STREAM_DIRECTION_WRITE
        && streamP->str_bufoff > 0) {
        rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
@@ -775,7 +844,7 @@ ih_fdclose(IHandle_t * ihP)
     int closeCount, closedAll;
     FdHandle_t *fdP, *head, *tail, *next;
 
-    osi_Assert(ihP->ih_refcnt > 0);
+    opr_Assert(ihP->ih_refcnt > 0);
 
     closedAll = 1;
     DLL_INIT_LIST(head, tail);
@@ -788,8 +857,8 @@ ih_fdclose(IHandle_t * ihP)
      */
     for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
        next = fdP->fd_ihnext;
-       osi_Assert(fdP->fd_ih == ihP);
-       osi_Assert(fdP->fd_status == FD_HANDLE_OPEN
+       opr_Assert(fdP->fd_ih == ihP);
+       opr_Assert(fdP->fd_status == FD_HANDLE_OPEN
               || fdP->fd_status == FD_HANDLE_INUSE
               || fdP->fd_status == FD_HANDLE_CLOSING);
        if (fdP->fd_status == FD_HANDLE_OPEN) {
@@ -812,9 +881,9 @@ ih_fdclose(IHandle_t * ihP)
      * closed all file descriptors.
      */
     if (ihP->ih_refcnt == 1 || closedAll) {
-       osi_Assert(closedAll);
-       osi_Assert(!ihP->ih_fdhead);
-       osi_Assert(!ihP->ih_fdtail);
+       opr_Assert(closedAll);
+       opr_Assert(!ihP->ih_fdhead);
+       opr_Assert(!ihP->ih_fdtail);
     }
 
     if (head == NULL) {
@@ -836,7 +905,7 @@ ih_fdclose(IHandle_t * ihP)
     }
 
     IH_LOCK;
-    osi_Assert(fdInUseCount >= closeCount);
+    opr_Assert(fdInUseCount >= closeCount);
     fdInUseCount -= closeCount;
 
     /*
@@ -865,6 +934,8 @@ ih_reallyclose(IHandle_t * ihP)
     ihP->ih_refcnt++;   /* must not disappear over unlock */
     if (ihP->ih_synced) {
        FdHandle_t *fdP;
+       opr_Assert(vol_io_params.sync_behavior != IH_SYNC_ALWAYS);
+       opr_Assert(vol_io_params.sync_behavior != IH_SYNC_NEVER);
         ihP->ih_synced = 0;
        IH_UNLOCK;
 
@@ -877,7 +948,7 @@ ih_reallyclose(IHandle_t * ihP)
        IH_LOCK;
     }
 
-    osi_Assert(ihP->ih_refcnt > 0);
+    opr_Assert(ihP->ih_refcnt > 0);
 
     ih_fdclose(ihP);
 
@@ -901,7 +972,7 @@ _ih_release_r(IHandle_t * ihP)
     if (!ihP)
        return 0;
 
-    osi_Assert(ihP->ih_refcnt > 0);
+    opr_Assert(ihP->ih_refcnt > 0);
 
     if (ihP->ih_refcnt > 1) {
        ihP->ih_refcnt--;
@@ -958,67 +1029,6 @@ ih_condsync(IHandle_t * ihP)
     return code;
 }
 
-void
-ih_sync_all(void) {
-
-    int ihash;
-
-    IH_LOCK;
-    for (ihash = 0; ihash < I_HANDLE_HASH_SIZE; ihash++) {
-       IHandle_t *ihP, *ihPnext;
-
-       ihP = ihashTable[ihash].ihash_head;
-       if (ihP)
-           ihP->ih_refcnt++;   /* must not disappear over unlock */
-       for (; ihP; ihP = ihPnext) {
-
-           if (ihP->ih_synced) {
-               FdHandle_t *fdP;
-
-               ihP->ih_synced = 0;
-               IH_UNLOCK;
-
-               fdP = IH_OPEN(ihP);
-               if (fdP) {
-                   OS_SYNC(fdP->fd_fd);
-                   FDH_CLOSE(fdP);
-               }
-
-               IH_LOCK;
-           }
-
-           /* when decrementing the refcount, the ihandle might disappear
-              and we might not even be able to proceed to the next one.
-              Hence the gymnastics putting a hold on the next one already */
-           ihPnext = ihP->ih_next;
-           if (ihPnext) ihPnext->ih_refcnt++;
-
-           if (ihP->ih_refcnt > 1)
-               ihP->ih_refcnt--;
-           else
-               _ih_release_r(ihP);
-       }
-    }
-    IH_UNLOCK;
-}
-
-void *
-ih_sync_thread(void *dummy) {
-    afs_pthread_setname_self("ih_syncer");
-    while(1) {
-
-#ifdef AFS_PTHREAD_ENV
-       sleep(10);
-#else /* AFS_PTHREAD_ENV */
-       IOMGR_Sleep(60);
-#endif /* AFS_PTHREAD_ENV */
-
-        ih_sync_all();
-    }
-    return NULL;
-}
-
-
 /*************************************************************************
  * OS specific support routines.
  *************************************************************************/
@@ -1078,3 +1088,37 @@ ih_pwrite(int fd, const void * buf, size_t count, afs_foff_t offset)
        return OS_WRITE(fd, buf, count);
 }
 #endif /* !HAVE_PIO */
+
+#ifndef AFS_NT40_ENV
+int
+ih_isunlinked(int fd)
+{
+    struct afs_stat_st status;
+    if (afs_fstat(fd, &status) < 0) {
+       return -1;
+    }
+    if (status.st_nlink < 1) {
+       return 1;
+    }
+    return 0;
+}
+#endif /* !AFS_NT40_ENV */
+
+int
+ih_fdsync(FdHandle_t *fdP)
+{
+    switch (vol_io_params.sync_behavior) {
+    case IH_SYNC_ALWAYS:
+       return OS_SYNC(fdP->fd_fd);
+    case IH_SYNC_ONCLOSE:
+       if (fdP->fd_ih) {
+           fdP->fd_ih->ih_synced = 1;
+           return 0;
+       }
+       return 1;
+    case IH_SYNC_NEVER:
+       return 0;
+    default:
+       opr_Assert(0);
+    }
+}