#include <roken.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#else
-#include <sys/file.h>
-#include <unistd.h>
-#include <sys/stat.h>
-#if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
-#include <sys/fcntl.h>
+#include <limits.h>
+
+#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
-#endif
-#include <rx/xdr.h>
+#include <afs/opr.h>
+#ifdef AFS_PTHREAD_ENV
+# include <opr/lock.h>
+#endif
#include <afs/afsint.h>
-#include <errno.h>
#include <afs/afssyscalls.h>
+#include <afs/afsutil.h>
+
#include "nfs.h"
#include "ihandle.h"
#include "viceinode.h"
-#include "afs/afs_assert.h"
-#include <limits.h>
#ifdef AFS_PTHREAD_ENV
pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
/* Hash table for inode handles */
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 */
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 */
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);
#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
*/
fdMaxCacheSize /= 4;
#endif
- fdMaxCacheSize = MIN(fdMaxCacheSize, vol_io_params.fd_max_cachesize);
- osi_Assert(fdMaxCacheSize > 0);
+ fdMaxCacheSize = min(fdMaxCacheSize, vol_io_params.fd_max_cachesize);
+ opr_Assert(fdMaxCacheSize > 0);
}
#elif defined(AFS_HPUX_ENV)
/* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
fdMaxCacheSize = 0;
#else
{
- long fdMax = MAX(sysconf(_SC_OPEN_MAX) - vol_io_params.fd_handle_setaside,
+ long fdMax = max(sysconf(_SC_OPEN_MAX) - vol_io_params.fd_handle_setaside,
0);
- fdMaxCacheSize = (int)MIN(fdMax, vol_io_params.fd_max_cachesize);
+ fdMaxCacheSize = (int)min(fdMax, vol_io_params.fd_max_cachesize);
}
#endif
- fdCacheSize = MIN(fdMaxCacheSize, vol_io_params.fd_initial_cachesize);
+ fdCacheSize = min(fdMaxCacheSize, vol_io_params.fd_initial_cachesize);
{
#ifdef AFS_PTHREAD_ENV
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);
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;
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;
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;
fdP[i].fd_fd = INVALID_FD;
fdP[i].fd_ihnext = NULL;
fdP[i].fd_ihprev = NULL;
- fdP[i].fd_needs_rclose = 0;
DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
}
}
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,
/* Do we already have an open file handle for this Inode? */
for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
+ if (fdP->fd_status == FD_HANDLE_CLOSING) {
+ /* The handle was open when an IH_REALLYCLOSE was issued, so we
+ * cannot reuse it; it will be closed soon. */
+ continue;
+ }
#ifndef HAVE_PIO
/*
* If we don't have positional i/o, don't try to share fds, since
* we can't do so in a threadsafe way.
*/
- if (fdP->fd_status != FD_HANDLE_INUSE) {
- osi_Assert(fdP->fd_status == FD_HANDLE_OPEN);
+ if (fdP->fd_status == FD_HANDLE_INUSE) {
+ continue;
+ }
+ opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
#else /* HAVE_PIO */
- if (fdP->fd_status != FD_HANDLE_AVAIL) {
+ opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
#endif /* HAVE_PIO */
- fdP->fd_refcnt++;
- if (fdP->fd_status == FD_HANDLE_OPEN) {
+
+ fdP->fd_refcnt++;
+ if (fdP->fd_status == FD_HANDLE_OPEN) {
fdP->fd_status = FD_HANDLE_INUSE;
DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
- }
- ihP->ih_refcnt++;
- IH_UNLOCK;
- return fdP;
}
+ ihP->ih_refcnt++;
+ IH_UNLOCK;
+ return fdP;
}
/*
* 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);
+ 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);
fdP->fd_status = FD_HANDLE_AVAIL;
fdP->fd_ih = NULL;
fdP->fd_fd = INVALID_FD;
- fdP->fd_needs_rclose = 0;
IH_UNLOCK;
OS_CLOSE(closeFd);
goto ih_open_retry;
fdHandleAllocateChunk();
}
fdP = fdAvailHead;
- osi_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
+ 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_needs_rclose = 0;
fdP->fd_refcnt++;
ihP->ih_refcnt++;
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;
* failed (this is determined by checking the ihandle for the flag
* IH_REALLY_CLOSED) or we have too many open files.
*/
- if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize ||
- fdP->fd_needs_rclose) {
-
+ if (fdP->fd_status == FD_HANDLE_CLOSING ||
+ ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
IH_UNLOCK;
return fd_reallyclose(fdP);
}
/* If this is not the only reference to the Inode then we can decrement
* the reference count, otherwise we need to call ih_release.
*/
- if (ihP->ih_refcnt > 1) {
+ if (ihP->ih_refcnt > 1)
ihP->ih_refcnt--;
- IH_UNLOCK;
- } else {
- IH_UNLOCK;
- ih_release(ihP);
- }
+ else
+ _ih_release_r(ihP);
+
+ IH_UNLOCK;
return 0;
}
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;
closeFd = fdP->fd_fd;
fdP->fd_refcnt = 0;
fdP->fd_ih = NULL;
fdP->fd_fd = INVALID_FD;
- fdP->fd_needs_rclose = 0;
- } else {
- fdP->fd_needs_rclose = 1;
}
/* All the file descriptor handles have been closed; reset
*/
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) {
/* If this is not the only reference to the Inode then we can decrement
* the reference count, otherwise we need to call ih_release. */
- if (ihP->ih_refcnt > 1) {
+ if (ihP->ih_refcnt > 1)
ihP->ih_refcnt--;
- IH_UNLOCK;
- } else {
- IH_UNLOCK;
- ih_release(ihP);
- }
+ else
+ _ih_release_r(ihP);
+
+ IH_UNLOCK;
return 0;
}
} 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) {
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;
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;
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,
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);
*/
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
- || fdP->fd_status == FD_HANDLE_INUSE);
+ 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) {
+ /* Note that FdHandle_t's do not count against the parent
+ * IHandle_t ref count when they are FD_HANDLE_OPEN. So, we don't
+ * need to dec the parent IHandle_t ref count for each one we pull
+ * off here. */
DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
fd_ihprev);
DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
} else {
closedAll = 0;
+ fdP->fd_status = FD_HANDLE_CLOSING;
ihP->ih_flags |= IH_REALLY_CLOSED;
}
}
* 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) {
fdP->fd_refcnt = 0;
fdP->fd_fd = INVALID_FD;
fdP->fd_ih = NULL;
- fdP->fd_needs_rclose = 0;
closeCount++;
}
IH_LOCK;
- osi_Assert(fdInUseCount >= closeCount);
+ opr_Assert(fdInUseCount >= closeCount);
fdInUseCount -= closeCount;
/*
IH_LOCK;
}
- osi_Assert(ihP->ih_refcnt > 0);
+ opr_Assert(ihP->ih_refcnt > 0);
ih_fdclose(ihP);
- if (ihP->ih_refcnt > 1) {
+ if (ihP->ih_refcnt > 1)
ihP->ih_refcnt--;
- IH_UNLOCK;
- } else {
- IH_UNLOCK;
- ih_release(ihP);
- }
+ else
+ _ih_release_r(ihP);
+
+ IH_UNLOCK;
return 0;
}
/* Release an Inode handle. All cached file descriptors for this
* inode are closed when the last reference to this handle is released
*/
-int
-ih_release(IHandle_t * ihP)
+static int
+_ih_release_r(IHandle_t * ihP)
{
int ihash;
if (!ihP)
return 0;
- IH_LOCK;
- osi_Assert(ihP->ih_refcnt > 0);
+ opr_Assert(ihP->ih_refcnt > 0);
if (ihP->ih_refcnt > 1) {
ihP->ih_refcnt--;
- IH_UNLOCK;
return 0;
}
DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
- IH_UNLOCK;
return 0;
}
+/* Release an Inode handle. All cached file descriptors for this
+ * inode are closed when the last reference to this handle is released
+ */
+int
+ih_release(IHandle_t * ihP)
+{
+ int ret;
+
+ if (!ihP)
+ return 0;
+
+ IH_LOCK;
+ ret = _ih_release_r(ihP);
+ IH_UNLOCK;
+ return ret;
+}
+
/* Sync an inode to disk if its handle isn't NULL */
int
ih_condsync(IHandle_t * ihP)
ihPnext = ihP->ih_next;
if (ihPnext) ihPnext->ih_refcnt++;
- if (ihP->ih_refcnt > 1) {
+ if (ihP->ih_refcnt > 1)
ihP->ih_refcnt--;
- } else {
- IH_UNLOCK;
- ih_release(ihP);
- IH_LOCK;
- }
-
+ 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
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 */