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. If fdLruHead is not NULL, fd can be
324 * INVALID_FD, indicating that the caller failed to open the relevant file
325 * because we had too many FDs open; ih_attachfd_r will then just evict/close
326 * an existing fd in the cache, and return NULL. You must not call this
327 * function with an invalid fd while fdLruHead is NULL; instead, error out.
330 ih_attachfd_r(IHandle_t *ihP, FD_t fd)
335 /* If the given fd is invalid, we must have an available fd to close.
336 * Otherwise, the caller must have realized this before calling
337 * ih_attachfd_r and yielded an error before getting here. */
338 opr_Assert(fd != INVALID_FD || fdLruHead != NULL);
340 /* fdCacheSize limits the size of the descriptor cache, but
341 * we permit the number of open files to exceed fdCacheSize.
342 * We only recycle open file descriptors when the number
343 * of open files reaches the size of the cache */
344 if ((fdInUseCount > fdCacheSize || fd == INVALID_FD) && fdLruHead != NULL) {
346 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
347 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
348 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
349 fd_ihnext, fd_ihprev);
350 closeFd = fdP->fd_fd;
351 if (fd == INVALID_FD) {
352 fdCacheSize--; /* reduce in order to not run into here too often */
353 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
354 fdP->fd_status = FD_HANDLE_AVAIL;
356 fdP->fd_fd = INVALID_FD;
364 if (fdAvailHead == NULL) {
365 fdHandleAllocateChunk();
368 opr_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
369 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
370 closeFd = INVALID_FD;
373 fdP->fd_status = FD_HANDLE_INUSE;
380 /* Add this handle to the Inode's list of open descriptors */
381 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
384 if (closeFd != INVALID_FD) {
395 ih_attachfd(IHandle_t *ihP, FD_t fd)
399 if (fd == INVALID_FD) {
407 fdP = ih_attachfd_r(ihP, fd);
416 * Get a file descriptor handle given an Inode handle
419 ih_open(IHandle_t * ihP)
424 if (!ihP) /* XXX should log here in the fileserver */
429 /* Do we already have an open file handle for this Inode? */
430 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
431 if (fdP->fd_status == FD_HANDLE_CLOSING) {
432 /* The handle was open when an IH_REALLYCLOSE was issued, so we
433 * cannot reuse it; it will be closed soon. */
438 * If we don't have positional i/o, don't try to share fds, since
439 * we can't do so in a threadsafe way.
441 if (fdP->fd_status == FD_HANDLE_INUSE) {
444 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
446 opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
447 #endif /* HAVE_PIO */
450 if (fdP->fd_status == FD_HANDLE_OPEN) {
451 fdP->fd_status = FD_HANDLE_INUSE;
452 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
460 * Try to open the Inode, return NULL on error.
467 if (fd == INVALID_FD && (errno != EMFILE || fdLruHead == NULL) ) {
473 fdP = ih_attachfd_r(ihP, fd);
475 opr_Assert(fd == INVALID_FD);
486 * Return a file descriptor handle to the cache
489 fd_close(FdHandle_t * fdP)
497 opr_Assert(ih_Inited);
498 opr_Assert(fdInUseCount > 0);
499 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
500 fdP->fd_status == FD_HANDLE_CLOSING);
504 /* Call fd_reallyclose to really close the unused file handles if
505 * the previous attempt to close (ih_reallyclose()) all file handles
506 * failed (this is determined by checking the ihandle for the flag
507 * IH_REALLY_CLOSED) or we have too many open files.
509 if (fdP->fd_status == FD_HANDLE_CLOSING ||
510 ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
512 return fd_reallyclose(fdP);
516 if (fdP->fd_refcnt == 0) {
517 /* Put this descriptor back into the cache */
518 fdP->fd_status = FD_HANDLE_OPEN;
519 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
522 /* If this is not the only reference to the Inode then we can decrement
523 * the reference count, otherwise we need to call ih_release.
525 if (ihP->ih_refcnt > 1)
536 * Actually close the file descriptor handle and return it to
540 fd_reallyclose(FdHandle_t * fdP)
549 opr_Assert(ih_Inited);
550 opr_Assert(fdInUseCount > 0);
551 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
552 fdP->fd_status == FD_HANDLE_CLOSING);
555 closeFd = fdP->fd_fd;
558 if (fdP->fd_refcnt == 0) {
559 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
560 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
562 fdP->fd_status = FD_HANDLE_AVAIL;
565 fdP->fd_fd = INVALID_FD;
568 /* All the file descriptor handles have been closed; reset
569 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
570 * has completed its job.
572 if (!ihP->ih_fdhead) {
573 ihP->ih_flags &= ~IH_REALLY_CLOSED;
575 FdHandle_t *lfdP, *next;
577 for (lfdP = ihP->ih_fdhead; lfdP != NULL; lfdP = next) {
578 next = lfdP->fd_ihnext;
579 osi_Assert(lfdP->fd_ih == ihP);
580 if (lfdP->fd_status != FD_HANDLE_CLOSING) {
585 /* no *future* fd should be subjected to this */
587 ihP->ih_flags &= ~IH_REALLY_CLOSED;
590 if (fdP->fd_refcnt == 0) {
597 /* If this is not the only reference to the Inode then we can decrement
598 * the reference count, otherwise we need to call ih_release. */
599 if (ihP->ih_refcnt > 1)
609 /* Enable buffered I/O on a file descriptor */
611 stream_fdopen(FD_t fd)
613 StreamHandle_t *streamP;
616 if (streamAvailHead == NULL) {
617 streamHandleAllocateChunk();
619 streamP = streamAvailHead;
620 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
622 streamP->str_fd = fd;
623 streamP->str_buflen = 0;
624 streamP->str_bufoff = 0;
625 streamP->str_fdoff = 0;
626 streamP->str_error = 0;
627 streamP->str_eof = 0;
628 streamP->str_direction = STREAM_DIRECTION_NONE;
632 /* Open a file for buffered I/O */
634 stream_open(const char *filename, const char *mode)
636 FD_t fd = INVALID_FD;
638 if (strcmp(mode, "r") == 0) {
639 fd = OS_OPEN(filename, O_RDONLY, 0);
640 } else if (strcmp(mode, "r+") == 0) {
641 fd = OS_OPEN(filename, O_RDWR, 0);
642 } else if (strcmp(mode, "w") == 0) {
643 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
644 } else if (strcmp(mode, "w+") == 0) {
645 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
646 } else if (strcmp(mode, "a") == 0) {
647 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
648 } else if (strcmp(mode, "a+") == 0) {
649 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
651 opr_abort(); /* not implemented */
654 if (fd == INVALID_FD) {
657 return stream_fdopen(fd);
660 /* fread for buffered I/O handles */
662 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
663 StreamHandle_t * streamP)
665 afs_fsize_t nbytes, bytesRead, bytesToRead;
668 /* Need to seek before changing direction */
669 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
670 streamP->str_direction = STREAM_DIRECTION_READ;
671 streamP->str_bufoff = 0;
672 streamP->str_buflen = 0;
674 opr_Assert(streamP->str_direction == STREAM_DIRECTION_READ);
678 nbytes = size * nitems;
680 while (nbytes > 0 && !streamP->str_eof) {
681 if (streamP->str_buflen == 0) {
682 streamP->str_bufoff = 0;
683 streamP->str_buflen =
684 OS_PREAD(streamP->str_fd, streamP->str_buffer,
685 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
686 if (streamP->str_buflen < 0) {
687 streamP->str_error = errno;
688 streamP->str_buflen = 0;
691 } else if (streamP->str_buflen == 0) {
692 streamP->str_eof = 1;
695 streamP->str_fdoff += streamP->str_buflen;
698 bytesToRead = nbytes;
699 if (bytesToRead > streamP->str_buflen) {
700 bytesToRead = streamP->str_buflen;
702 memcpy(p, streamP->str_buffer + streamP->str_bufoff, bytesToRead);
704 streamP->str_bufoff += bytesToRead;
705 streamP->str_buflen -= bytesToRead;
706 bytesRead += bytesToRead;
707 nbytes -= bytesToRead;
710 return (bytesRead / size);
713 /* fwrite for buffered I/O handles */
715 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
716 StreamHandle_t * streamP)
720 afs_fsize_t nbytes, bytesWritten, bytesToWrite;
722 /* Need to seek before changing direction */
723 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
724 streamP->str_direction = STREAM_DIRECTION_WRITE;
725 streamP->str_bufoff = 0;
726 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
728 opr_Assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
731 nbytes = size * nitems;
735 if (streamP->str_buflen == 0) {
736 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
737 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
739 streamP->str_error = errno;
743 streamP->str_fdoff += rc;
744 streamP->str_bufoff = 0;
745 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
748 bytesToWrite = nbytes;
749 if (bytesToWrite > streamP->str_buflen) {
750 bytesToWrite = streamP->str_buflen;
752 memcpy(streamP->str_buffer + streamP->str_bufoff, p, bytesToWrite);
754 streamP->str_bufoff += bytesToWrite;
755 streamP->str_buflen -= bytesToWrite;
756 bytesWritten += bytesToWrite;
757 nbytes -= bytesToWrite;
760 return (bytesWritten / size);
763 /* fseek for buffered I/O handles */
765 stream_aseek(StreamHandle_t * streamP, afs_foff_t offset)
770 if (streamP->str_direction == STREAM_DIRECTION_WRITE
771 && streamP->str_bufoff > 0) {
772 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
773 streamP->str_bufoff, streamP->str_fdoff);
775 streamP->str_error = errno;
779 streamP->str_fdoff = offset;
780 streamP->str_bufoff = 0;
781 streamP->str_buflen = 0;
782 streamP->str_eof = 0;
783 streamP->str_direction = STREAM_DIRECTION_NONE;
787 /* fflush for buffered I/O handles */
789 stream_flush(StreamHandle_t * streamP)
794 if (streamP->str_direction == STREAM_DIRECTION_WRITE
795 && streamP->str_bufoff > 0) {
796 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
797 streamP->str_bufoff, streamP->str_fdoff);
799 streamP->str_error = errno;
802 streamP->str_fdoff += rc;
804 streamP->str_bufoff = 0;
805 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
811 /* Free a buffered I/O handle */
813 stream_close(StreamHandle_t * streamP, int reallyClose)
818 opr_Assert(streamP != NULL);
819 if (streamP->str_direction == STREAM_DIRECTION_WRITE
820 && streamP->str_bufoff > 0) {
821 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
822 streamP->str_bufoff, streamP->str_fdoff);
826 streamP->str_fdoff += rc;
830 rc = OS_CLOSE(streamP->str_fd);
835 streamP->str_fd = INVALID_FD;
838 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
844 /* Close all unused file descriptors associated with the inode
845 * handle. Called with IH_LOCK held. May drop and reacquire
846 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
847 * if it fails to close all file handles.
850 ih_fdclose(IHandle_t * ihP)
852 int closeCount, closedAll;
853 FdHandle_t *fdP, *head, *tail, *next;
855 opr_Assert(ihP->ih_refcnt > 0);
858 DLL_INIT_LIST(head, tail);
859 ihP->ih_flags &= ~IH_REALLY_CLOSED;
862 * Remove the file descriptors for this Inode from the LRU queue
863 * and the IHandle queue and put them on a temporary queue so we
864 * can drop the lock before we close the files.
866 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
867 next = fdP->fd_ihnext;
868 opr_Assert(fdP->fd_ih == ihP);
869 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN
870 || fdP->fd_status == FD_HANDLE_INUSE
871 || fdP->fd_status == FD_HANDLE_CLOSING);
872 if (fdP->fd_status == FD_HANDLE_OPEN) {
873 /* Note that FdHandle_t's do not count against the parent
874 * IHandle_t ref count when they are FD_HANDLE_OPEN. So, we don't
875 * need to dec the parent IHandle_t ref count for each one we pull
877 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
879 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
880 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
883 fdP->fd_status = FD_HANDLE_CLOSING;
884 ihP->ih_flags |= IH_REALLY_CLOSED;
888 /* If the ihandle reference count is 1, we should have
889 * closed all file descriptors.
891 if (ihP->ih_refcnt == 1 || closedAll) {
892 opr_Assert(closedAll);
893 opr_Assert(!ihP->ih_fdhead);
894 opr_Assert(!ihP->ih_fdtail);
898 return 0; /* No file descriptors closed */
903 * Close the file descriptors
906 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
907 OS_CLOSE(fdP->fd_fd);
908 fdP->fd_status = FD_HANDLE_AVAIL;
910 fdP->fd_fd = INVALID_FD;
916 opr_Assert(fdInUseCount >= closeCount);
917 fdInUseCount -= closeCount;
920 * Append the temporary queue to the list of available descriptors
922 if (fdAvailHead == NULL) {
926 fdAvailTail->fd_next = head;
927 head->fd_prev = fdAvailTail;
934 /* Close all cached file descriptors for this inode. */
936 ih_reallyclose(IHandle_t * ihP)
942 ihP->ih_refcnt++; /* must not disappear over unlock */
943 if (ihP->ih_synced) {
945 opr_Assert(vol_io_params.sync_behavior != IH_SYNC_ALWAYS);
946 opr_Assert(vol_io_params.sync_behavior != IH_SYNC_NEVER);
959 opr_Assert(ihP->ih_refcnt > 0);
963 if (ihP->ih_refcnt > 1)
972 /* Release an Inode handle. All cached file descriptors for this
973 * inode are closed when the last reference to this handle is released
976 _ih_release_r(IHandle_t * ihP)
983 opr_Assert(ihP->ih_refcnt > 0);
985 if (ihP->ih_refcnt > 1) {
990 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
991 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
992 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
998 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
1003 /* Release an Inode handle. All cached file descriptors for this
1004 * inode are closed when the last reference to this handle is released
1007 ih_release(IHandle_t * ihP)
1015 ret = _ih_release_r(ihP);
1020 /* Sync an inode to disk if its handle isn't NULL */
1022 ih_condsync(IHandle_t * ihP)
1034 code = FDH_SYNC(fdP);
1040 /*************************************************************************
1041 * OS specific support routines.
1042 *************************************************************************/
1043 #ifndef AFS_NAMEI_ENV
1045 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
1049 #ifdef AFS_3DISPARES
1050 /* See viceinode.h */
1051 if (p2 == INODESPECIAL) {
1057 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
1060 #endif /* AFS_NAMEI_ENV */
1062 #if defined(AFS_NT40_ENV) || !defined(AFS_NAMEI_ENV)
1063 /* Unix namei implements its own more efficient IH_CREATE_INIT; this wrapper
1064 * is for everyone else */
1066 ih_icreate_init(IHandle_t *lh, int dev, char *part, Inode nearInode,
1067 afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
1070 Inode ino = IH_CREATE(lh, dev, part, nearInode, p1, p2, p3, p4);
1071 if (!VALID_INO(ino)) {
1074 IH_INIT(ihP, dev, p1, ino);
1084 if (!GetFileSizeEx(fd, &size))
1086 return size.QuadPart;
1088 struct afs_stat_st status;
1089 if (afs_fstat(fd, &status) < 0)
1091 return status.st_size;
1097 ih_pread(int fd, void * buf, size_t count, afs_foff_t offset)
1100 code = OS_SEEK(fd, offset, 0);
1103 return OS_READ(fd, buf, count);
1107 ih_pwrite(int fd, const void * buf, size_t count, afs_foff_t offset)
1110 code = OS_SEEK(fd, offset, 0);
1113 return OS_WRITE(fd, buf, count);
1115 #endif /* !HAVE_PIO */
1117 #ifndef AFS_NT40_ENV
1119 ih_isunlinked(int fd)
1121 struct afs_stat_st status;
1122 if (afs_fstat(fd, &status) < 0) {
1125 if (status.st_nlink < 1) {
1130 #endif /* !AFS_NT40_ENV */
1133 ih_fdsync(FdHandle_t *fdP)
1135 switch (vol_io_params.sync_behavior) {
1136 case IH_SYNC_ALWAYS:
1137 return OS_SYNC(fdP->fd_fd);
1138 case IH_SYNC_ONCLOSE:
1140 fdP->fd_ih->ih_synced = 1;