2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* ihandle.c - file descriptor cacheing for Inode handles. */
12 /************************************************************************/
14 #include <afsconfig.h>
15 #include <afs/param.h>
21 #ifdef HAVE_SYS_RESOURCE_H
22 #include <sys/resource.h>
26 #ifdef AFS_PTHREAD_ENV
27 # include <opr/lock.h>
29 #include <afs/afsint.h>
30 #include <afs/afssyscalls.h>
31 #include <afs/afsutil.h>
35 #include "viceinode.h"
37 #ifdef AFS_PTHREAD_ENV
38 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
39 pthread_mutex_t ih_glock_mutex;
40 #endif /* AFS_PTHREAD_ENV */
42 /* Linked list of available inode handles */
43 IHandle_t *ihAvailHead;
44 IHandle_t *ihAvailTail;
46 /* Linked list of available file descriptor handles */
47 FdHandle_t *fdAvailHead;
48 FdHandle_t *fdAvailTail;
50 /* Linked list of available stream descriptor handles */
51 StreamHandle_t *streamAvailHead;
52 StreamHandle_t *streamAvailTail;
54 /* LRU list for file descriptor handles */
55 FdHandle_t *fdLruHead;
56 FdHandle_t *fdLruTail;
59 int ih_PkgDefaultsSet = 0;
61 /* Most of the servers use fopen/fdopen. Since the FILE structure
62 * only has eight bits for the file descriptor, the cache size
63 * has to be less than 256. The cache can be made larger as long
64 * as you are sure you don't need fopen/fdopen. */
66 /* As noted in ihandle.h, the fileno member of FILE on most platforms
67 * in 2008 is a 16- or 32-bit signed int. -Matt
69 int fdMaxCacheSize = 0;
72 /* Number of in use file descriptors */
75 /* Hash table for inode handles */
76 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
78 static int _ih_release_r(IHandle_t * ihP);
80 /* start-time configurable I/O limits */
81 ih_init_params vol_io_params;
83 void ih_PkgDefaults(void)
86 ih_PkgDefaultsSet = 1;
88 /* default to well-known values */
89 vol_io_params.fd_handle_setaside = FD_HANDLE_SETASIDE;
91 /* initial fd cachesize. the only one that will be used if
92 * the application does not call ih_UseLargeCache(). set this
93 * to a value representable in fileno member of the system's
94 * FILE structure (or equivalent). */
95 vol_io_params.fd_initial_cachesize = FD_DEFAULT_CACHESIZE;
97 /* fd cache size that will be used if/when ih_UseLargeCache()
99 vol_io_params.fd_max_cachesize = FD_MAX_CACHESIZE;
101 vol_io_params.sync_behavior = IH_SYNC_ONCLOSE;
105 ih_SetSyncBehavior(const char *behavior)
109 if (strcmp(behavior, "always") == 0) {
110 val = IH_SYNC_ALWAYS;
112 } else if (strcmp(behavior, "onclose") == 0) {
113 val = IH_SYNC_ONCLOSE;
115 } else if (strcmp(behavior, "never") == 0) {
119 /* invalid behavior name */
123 vol_io_params.sync_behavior = val;
127 #ifdef AFS_PTHREAD_ENV
128 /* Initialize the global ihandle mutex */
132 opr_mutex_init(&ih_glock_mutex);
134 #endif /* AFS_PTHREAD_ENV */
136 /* Initialize the file descriptor cache */
141 opr_Assert(!ih_Inited);
143 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
144 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
145 DLL_INIT_LIST(fdLruHead, fdLruTail);
146 for (i = 0; i < I_HANDLE_HASH_SIZE; i++) {
147 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
149 #if defined(AFS_NT40_ENV)
150 fdMaxCacheSize = vol_io_params.fd_max_cachesize;
151 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
154 opr_Verify(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
155 rlim.rlim_cur = rlim.rlim_max;
156 opr_Verify(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
157 fdMaxCacheSize = rlim.rlim_cur - vol_io_params.fd_handle_setaside;
159 /* XXX this is to avoid using up all system fd netbsd is
160 * somewhat broken and have set maximum fd for a root process
161 * to the same as system fd that is avaible, so if the
162 * fileserver uses all up process fds, all system fd will be
165 * Check for this better
169 fdMaxCacheSize = min(fdMaxCacheSize, vol_io_params.fd_max_cachesize);
170 opr_Assert(fdMaxCacheSize > 0);
172 #elif defined(AFS_HPUX_ENV)
173 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
177 long fdMax = max(sysconf(_SC_OPEN_MAX) - vol_io_params.fd_handle_setaside,
179 fdMaxCacheSize = (int)min(fdMax, vol_io_params.fd_max_cachesize);
182 fdCacheSize = min(fdMaxCacheSize, vol_io_params.fd_initial_cachesize);
185 /* Make the file descriptor cache as big as possible. Don't this call
186 * if the program uses fopen or fdopen, if fd_max_cachesize cannot be
187 * represented in the fileno member of the system FILE structure (or
191 ih_UseLargeCache(void)
195 if (!ih_PkgDefaultsSet) {
203 fdCacheSize = fdMaxCacheSize;
208 /* Allocate a chunk of inode handles */
210 iHandleAllocateChunk(void)
215 opr_Assert(ihAvailHead == NULL);
216 ihP = malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
217 opr_Assert(ihP != NULL);
218 for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
219 ihP[i].ih_refcnt = 0;
220 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
224 /* Initialize an inode handle */
226 ih_init(int dev, int vid, Inode ino)
228 int ihash = IH_HASH(dev, vid, ino);
231 if (!ih_PkgDefaultsSet) {
240 /* Do we already have a handle for this Inode? */
241 for (ihP = ihashTable[ihash].ihash_head; ihP; ihP = ihP->ih_next) {
242 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
249 /* Allocate and initialize a new Inode handle */
250 if (ihAvailHead == NULL) {
251 iHandleAllocateChunk();
254 opr_Assert(ihP->ih_refcnt == 0);
255 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
262 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
263 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
264 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
269 /* Copy an inode handle */
271 ih_copy(IHandle_t * ihP)
274 opr_Assert(ih_Inited);
275 opr_Assert(ihP->ih_refcnt > 0);
281 /* Allocate a chunk of file descriptor handles */
283 fdHandleAllocateChunk(void)
288 opr_Assert(fdAvailHead == NULL);
289 fdP = malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
290 opr_Assert(fdP != NULL);
291 for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
292 fdP[i].fd_status = FD_HANDLE_AVAIL;
293 fdP[i].fd_refcnt = 0;
295 fdP[i].fd_fd = INVALID_FD;
296 fdP[i].fd_ihnext = NULL;
297 fdP[i].fd_ihprev = NULL;
298 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
302 /* Allocate a chunk of stream handles */
304 streamHandleAllocateChunk(void)
307 StreamHandle_t *streamP;
309 opr_Assert(streamAvailHead == NULL);
310 streamP = (StreamHandle_t *)
311 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
312 opr_Assert(streamP != NULL);
313 for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
314 streamP[i].str_fd = INVALID_FD;
315 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
321 * Get a file descriptor handle given an Inode handle
322 * Takes the given file descriptor, and creates a new FdHandle_t for it,
323 * attached to the given IHandle_t. fd can be INVALID_FD, indicating that the
324 * caller failed to open the relevant file because we had too many FDs open;
325 * ih_attachfd_r will then just evict/close an existing fd in the cache, and
329 ih_attachfd_r(IHandle_t *ihP, FD_t fd)
334 /* fdCacheSize limits the size of the descriptor cache, but
335 * we permit the number of open files to exceed fdCacheSize.
336 * We only recycle open file descriptors when the number
337 * of open files reaches the size of the cache */
338 if ((fdInUseCount > fdCacheSize || fd == INVALID_FD) && fdLruHead != NULL) {
340 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
341 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
342 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
343 fd_ihnext, fd_ihprev);
344 closeFd = fdP->fd_fd;
345 if (fd == INVALID_FD) {
346 fdCacheSize--; /* reduce in order to not run into here too often */
347 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
348 fdP->fd_status = FD_HANDLE_AVAIL;
350 fdP->fd_fd = INVALID_FD;
358 if (fdAvailHead == NULL) {
359 fdHandleAllocateChunk();
362 opr_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
363 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
364 closeFd = INVALID_FD;
367 fdP->fd_status = FD_HANDLE_INUSE;
374 /* Add this handle to the Inode's list of open descriptors */
375 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
378 if (closeFd != INVALID_FD) {
389 ih_attachfd(IHandle_t *ihP, FD_t fd)
397 fdP = ih_attachfd_r(ihP, fd);
408 * Get a file descriptor handle given an Inode handle
411 ih_open(IHandle_t * ihP)
416 if (!ihP) /* XXX should log here in the fileserver */
421 /* Do we already have an open file handle for this Inode? */
422 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
423 if (fdP->fd_status == FD_HANDLE_CLOSING) {
424 /* The handle was open when an IH_REALLYCLOSE was issued, so we
425 * cannot reuse it; it will be closed soon. */
430 * If we don't have positional i/o, don't try to share fds, since
431 * we can't do so in a threadsafe way.
433 if (fdP->fd_status == FD_HANDLE_INUSE) {
436 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
438 opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
439 #endif /* HAVE_PIO */
442 if (fdP->fd_status == FD_HANDLE_OPEN) {
443 fdP->fd_status = FD_HANDLE_INUSE;
444 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
452 * Try to open the Inode, return NULL on error.
459 if (fd == INVALID_FD && (errno != EMFILE || fdLruHead == NULL) ) {
465 fdP = ih_attachfd_r(ihP, fd);
467 opr_Assert(fd == INVALID_FD);
478 * Return a file descriptor handle to the cache
481 fd_close(FdHandle_t * fdP)
489 opr_Assert(ih_Inited);
490 opr_Assert(fdInUseCount > 0);
491 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
492 fdP->fd_status == FD_HANDLE_CLOSING);
496 /* Call fd_reallyclose to really close the unused file handles if
497 * the previous attempt to close (ih_reallyclose()) all file handles
498 * failed (this is determined by checking the ihandle for the flag
499 * IH_REALLY_CLOSED) or we have too many open files.
501 if (fdP->fd_status == FD_HANDLE_CLOSING ||
502 ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
504 return fd_reallyclose(fdP);
508 if (fdP->fd_refcnt == 0) {
509 /* Put this descriptor back into the cache */
510 fdP->fd_status = FD_HANDLE_OPEN;
511 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
514 /* If this is not the only reference to the Inode then we can decrement
515 * the reference count, otherwise we need to call ih_release.
517 if (ihP->ih_refcnt > 1)
528 * Actually close the file descriptor handle and return it to
532 fd_reallyclose(FdHandle_t * fdP)
541 opr_Assert(ih_Inited);
542 opr_Assert(fdInUseCount > 0);
543 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
544 fdP->fd_status == FD_HANDLE_CLOSING);
547 closeFd = fdP->fd_fd;
550 if (fdP->fd_refcnt == 0) {
551 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
552 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
554 fdP->fd_status = FD_HANDLE_AVAIL;
557 fdP->fd_fd = INVALID_FD;
560 /* All the file descriptor handles have been closed; reset
561 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
562 * has completed its job.
564 if (!ihP->ih_fdhead) {
565 ihP->ih_flags &= ~IH_REALLY_CLOSED;
567 FdHandle_t *lfdP, *next;
569 for (lfdP = ihP->ih_fdhead; lfdP != NULL; lfdP = next) {
570 next = lfdP->fd_ihnext;
571 osi_Assert(lfdP->fd_ih == ihP);
572 if (lfdP->fd_status != FD_HANDLE_CLOSING) {
577 /* no *future* fd should be subjected to this */
579 ihP->ih_flags &= ~IH_REALLY_CLOSED;
582 if (fdP->fd_refcnt == 0) {
589 /* If this is not the only reference to the Inode then we can decrement
590 * the reference count, otherwise we need to call ih_release. */
591 if (ihP->ih_refcnt > 1)
601 /* Enable buffered I/O on a file descriptor */
603 stream_fdopen(FD_t fd)
605 StreamHandle_t *streamP;
608 if (streamAvailHead == NULL) {
609 streamHandleAllocateChunk();
611 streamP = streamAvailHead;
612 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
614 streamP->str_fd = fd;
615 streamP->str_buflen = 0;
616 streamP->str_bufoff = 0;
617 streamP->str_fdoff = 0;
618 streamP->str_error = 0;
619 streamP->str_eof = 0;
620 streamP->str_direction = STREAM_DIRECTION_NONE;
624 /* Open a file for buffered I/O */
626 stream_open(const char *filename, const char *mode)
628 FD_t fd = INVALID_FD;
630 if (strcmp(mode, "r") == 0) {
631 fd = OS_OPEN(filename, O_RDONLY, 0);
632 } else if (strcmp(mode, "r+") == 0) {
633 fd = OS_OPEN(filename, O_RDWR, 0);
634 } else if (strcmp(mode, "w") == 0) {
635 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
636 } else if (strcmp(mode, "w+") == 0) {
637 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
638 } else if (strcmp(mode, "a") == 0) {
639 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
640 } else if (strcmp(mode, "a+") == 0) {
641 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
643 opr_abort(); /* not implemented */
646 if (fd == INVALID_FD) {
649 return stream_fdopen(fd);
652 /* fread for buffered I/O handles */
654 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
655 StreamHandle_t * streamP)
657 afs_fsize_t nbytes, bytesRead, bytesToRead;
660 /* Need to seek before changing direction */
661 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
662 streamP->str_direction = STREAM_DIRECTION_READ;
663 streamP->str_bufoff = 0;
664 streamP->str_buflen = 0;
666 opr_Assert(streamP->str_direction == STREAM_DIRECTION_READ);
670 nbytes = size * nitems;
672 while (nbytes > 0 && !streamP->str_eof) {
673 if (streamP->str_buflen == 0) {
674 streamP->str_bufoff = 0;
675 streamP->str_buflen =
676 OS_PREAD(streamP->str_fd, streamP->str_buffer,
677 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
678 if (streamP->str_buflen < 0) {
679 streamP->str_error = errno;
680 streamP->str_buflen = 0;
683 } else if (streamP->str_buflen == 0) {
684 streamP->str_eof = 1;
687 streamP->str_fdoff += streamP->str_buflen;
690 bytesToRead = nbytes;
691 if (bytesToRead > streamP->str_buflen) {
692 bytesToRead = streamP->str_buflen;
694 memcpy(p, streamP->str_buffer + streamP->str_bufoff, bytesToRead);
696 streamP->str_bufoff += bytesToRead;
697 streamP->str_buflen -= bytesToRead;
698 bytesRead += bytesToRead;
699 nbytes -= bytesToRead;
702 return (bytesRead / size);
705 /* fwrite for buffered I/O handles */
707 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
708 StreamHandle_t * streamP)
712 afs_fsize_t nbytes, bytesWritten, bytesToWrite;
714 /* Need to seek before changing direction */
715 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
716 streamP->str_direction = STREAM_DIRECTION_WRITE;
717 streamP->str_bufoff = 0;
718 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
720 opr_Assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
723 nbytes = size * nitems;
727 if (streamP->str_buflen == 0) {
728 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
729 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
731 streamP->str_error = errno;
735 streamP->str_fdoff += rc;
736 streamP->str_bufoff = 0;
737 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
740 bytesToWrite = nbytes;
741 if (bytesToWrite > streamP->str_buflen) {
742 bytesToWrite = streamP->str_buflen;
744 memcpy(streamP->str_buffer + streamP->str_bufoff, p, bytesToWrite);
746 streamP->str_bufoff += bytesToWrite;
747 streamP->str_buflen -= bytesToWrite;
748 bytesWritten += bytesToWrite;
749 nbytes -= bytesToWrite;
752 return (bytesWritten / size);
755 /* fseek for buffered I/O handles */
757 stream_aseek(StreamHandle_t * streamP, afs_foff_t offset)
762 if (streamP->str_direction == STREAM_DIRECTION_WRITE
763 && streamP->str_bufoff > 0) {
764 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
765 streamP->str_bufoff, streamP->str_fdoff);
767 streamP->str_error = errno;
771 streamP->str_fdoff = offset;
772 streamP->str_bufoff = 0;
773 streamP->str_buflen = 0;
774 streamP->str_eof = 0;
775 streamP->str_direction = STREAM_DIRECTION_NONE;
779 /* fflush for buffered I/O handles */
781 stream_flush(StreamHandle_t * streamP)
786 if (streamP->str_direction == STREAM_DIRECTION_WRITE
787 && streamP->str_bufoff > 0) {
788 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
789 streamP->str_bufoff, streamP->str_fdoff);
791 streamP->str_error = errno;
794 streamP->str_fdoff += rc;
796 streamP->str_bufoff = 0;
797 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
803 /* Free a buffered I/O handle */
805 stream_close(StreamHandle_t * streamP, int reallyClose)
810 opr_Assert(streamP != NULL);
811 if (streamP->str_direction == STREAM_DIRECTION_WRITE
812 && streamP->str_bufoff > 0) {
813 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
814 streamP->str_bufoff, streamP->str_fdoff);
818 streamP->str_fdoff += rc;
822 rc = OS_CLOSE(streamP->str_fd);
827 streamP->str_fd = INVALID_FD;
830 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
836 /* Close all unused file descriptors associated with the inode
837 * handle. Called with IH_LOCK held. May drop and reacquire
838 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
839 * if it fails to close all file handles.
842 ih_fdclose(IHandle_t * ihP)
844 int closeCount, closedAll;
845 FdHandle_t *fdP, *head, *tail, *next;
847 opr_Assert(ihP->ih_refcnt > 0);
850 DLL_INIT_LIST(head, tail);
851 ihP->ih_flags &= ~IH_REALLY_CLOSED;
854 * Remove the file descriptors for this Inode from the LRU queue
855 * and the IHandle queue and put them on a temporary queue so we
856 * can drop the lock before we close the files.
858 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
859 next = fdP->fd_ihnext;
860 opr_Assert(fdP->fd_ih == ihP);
861 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN
862 || fdP->fd_status == FD_HANDLE_INUSE
863 || fdP->fd_status == FD_HANDLE_CLOSING);
864 if (fdP->fd_status == FD_HANDLE_OPEN) {
865 /* Note that FdHandle_t's do not count against the parent
866 * IHandle_t ref count when they are FD_HANDLE_OPEN. So, we don't
867 * need to dec the parent IHandle_t ref count for each one we pull
869 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
871 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
872 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
875 fdP->fd_status = FD_HANDLE_CLOSING;
876 ihP->ih_flags |= IH_REALLY_CLOSED;
880 /* If the ihandle reference count is 1, we should have
881 * closed all file descriptors.
883 if (ihP->ih_refcnt == 1 || closedAll) {
884 opr_Assert(closedAll);
885 opr_Assert(!ihP->ih_fdhead);
886 opr_Assert(!ihP->ih_fdtail);
890 return 0; /* No file descriptors closed */
895 * Close the file descriptors
898 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
899 OS_CLOSE(fdP->fd_fd);
900 fdP->fd_status = FD_HANDLE_AVAIL;
902 fdP->fd_fd = INVALID_FD;
908 opr_Assert(fdInUseCount >= closeCount);
909 fdInUseCount -= closeCount;
912 * Append the temporary queue to the list of available descriptors
914 if (fdAvailHead == NULL) {
918 fdAvailTail->fd_next = head;
919 head->fd_prev = fdAvailTail;
926 /* Close all cached file descriptors for this inode. */
928 ih_reallyclose(IHandle_t * ihP)
934 ihP->ih_refcnt++; /* must not disappear over unlock */
935 if (ihP->ih_synced) {
937 opr_Assert(vol_io_params.sync_behavior != IH_SYNC_ALWAYS);
938 opr_Assert(vol_io_params.sync_behavior != IH_SYNC_NEVER);
951 opr_Assert(ihP->ih_refcnt > 0);
955 if (ihP->ih_refcnt > 1)
964 /* Release an Inode handle. All cached file descriptors for this
965 * inode are closed when the last reference to this handle is released
968 _ih_release_r(IHandle_t * ihP)
975 opr_Assert(ihP->ih_refcnt > 0);
977 if (ihP->ih_refcnt > 1) {
982 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
983 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
984 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
990 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
995 /* Release an Inode handle. All cached file descriptors for this
996 * inode are closed when the last reference to this handle is released
999 ih_release(IHandle_t * ihP)
1007 ret = _ih_release_r(ihP);
1012 /* Sync an inode to disk if its handle isn't NULL */
1014 ih_condsync(IHandle_t * ihP)
1026 code = FDH_SYNC(fdP);
1032 /*************************************************************************
1033 * OS specific support routines.
1034 *************************************************************************/
1035 #ifndef AFS_NAMEI_ENV
1037 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
1041 #ifdef AFS_3DISPARES
1042 /* See viceinode.h */
1043 if (p2 == INODESPECIAL) {
1049 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
1052 #endif /* AFS_NAMEI_ENV */
1059 if (!GetFileSizeEx(fd, &size))
1061 return size.QuadPart;
1063 struct afs_stat_st status;
1064 if (afs_fstat(fd, &status) < 0)
1066 return status.st_size;
1072 ih_pread(int fd, void * buf, size_t count, afs_foff_t offset)
1075 code = OS_SEEK(fd, offset, 0);
1078 return OS_READ(fd, buf, count);
1082 ih_pwrite(int fd, const void * buf, size_t count, afs_foff_t offset)
1085 code = OS_SEEK(fd, offset, 0);
1088 return OS_WRITE(fd, buf, count);
1090 #endif /* !HAVE_PIO */
1092 #ifndef AFS_NT40_ENV
1094 ih_isunlinked(int fd)
1096 struct afs_stat_st status;
1097 if (afs_fstat(fd, &status) < 0) {
1100 if (status.st_nlink < 1) {
1105 #endif /* !AFS_NT40_ENV */
1108 ih_fdsync(FdHandle_t *fdP)
1110 switch (vol_io_params.sync_behavior) {
1111 case IH_SYNC_ALWAYS:
1112 return OS_SYNC(fdP->fd_fd);
1113 case IH_SYNC_ONCLOSE:
1115 fdP->fd_ih->ih_synced = 1;