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 #include <afs/afsint.h>
27 #include <afs/afssyscalls.h>
28 #include <afs/afsutil.h>
32 #include "viceinode.h"
34 #ifdef AFS_PTHREAD_ENV
35 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
36 pthread_mutex_t ih_glock_mutex;
37 #endif /* AFS_PTHREAD_ENV */
39 /* Linked list of available inode handles */
40 IHandle_t *ihAvailHead;
41 IHandle_t *ihAvailTail;
43 /* Linked list of available file descriptor handles */
44 FdHandle_t *fdAvailHead;
45 FdHandle_t *fdAvailTail;
47 /* Linked list of available stream descriptor handles */
48 StreamHandle_t *streamAvailHead;
49 StreamHandle_t *streamAvailTail;
51 /* LRU list for file descriptor handles */
52 FdHandle_t *fdLruHead;
53 FdHandle_t *fdLruTail;
56 int ih_PkgDefaultsSet = 0;
58 /* Most of the servers use fopen/fdopen. Since the FILE structure
59 * only has eight bits for the file descriptor, the cache size
60 * has to be less than 256. The cache can be made larger as long
61 * as you are sure you don't need fopen/fdopen. */
63 /* As noted in ihandle.h, the fileno member of FILE on most platforms
64 * in 2008 is a 16- or 32-bit signed int. -Matt
66 int fdMaxCacheSize = 0;
69 /* Number of in use file descriptors */
72 /* Hash table for inode handles */
73 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
75 static int _ih_release_r(IHandle_t * ihP);
76 void *ih_sync_thread(void *);
78 /* start-time configurable I/O limits */
79 ih_init_params vol_io_params;
81 void ih_PkgDefaults(void)
84 ih_PkgDefaultsSet = 1;
86 /* default to well-known values */
87 vol_io_params.fd_handle_setaside = FD_HANDLE_SETASIDE;
89 /* initial fd cachesize. the only one that will be used if
90 * the application does not call ih_UseLargeCache(). set this
91 * to a value representable in fileno member of the system's
92 * FILE structure (or equivalent). */
93 vol_io_params.fd_initial_cachesize = FD_DEFAULT_CACHESIZE;
95 /* fd cache size that will be used if/when ih_UseLargeCache()
97 vol_io_params.fd_max_cachesize = FD_MAX_CACHESIZE;
100 #ifdef AFS_PTHREAD_ENV
101 /* Initialize the global ihandle mutex */
105 MUTEX_INIT(&ih_glock_mutex, "ih glock", MUTEX_DEFAULT, 0);
107 #endif /* AFS_PTHREAD_ENV */
109 /* Initialize the file descriptor cache */
114 opr_Assert(!ih_Inited);
116 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
117 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
118 DLL_INIT_LIST(fdLruHead, fdLruTail);
119 for (i = 0; i < I_HANDLE_HASH_SIZE; i++) {
120 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
122 #if defined(AFS_NT40_ENV)
123 fdMaxCacheSize = vol_io_params.fd_max_cachesize;
124 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
127 opr_Verify(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
128 rlim.rlim_cur = rlim.rlim_max;
129 opr_Verify(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
130 fdMaxCacheSize = rlim.rlim_cur - vol_io_params.fd_handle_setaside;
132 /* XXX this is to avoid using up all system fd netbsd is
133 * somewhat broken and have set maximum fd for a root process
134 * to the same as system fd that is avaible, so if the
135 * fileserver uses all up process fds, all system fd will be
138 * Check for this better
142 fdMaxCacheSize = min(fdMaxCacheSize, vol_io_params.fd_max_cachesize);
143 opr_Assert(fdMaxCacheSize > 0);
145 #elif defined(AFS_HPUX_ENV)
146 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
150 long fdMax = max(sysconf(_SC_OPEN_MAX) - vol_io_params.fd_handle_setaside,
152 fdMaxCacheSize = (int)min(fdMax, vol_io_params.fd_max_cachesize);
155 fdCacheSize = min(fdMaxCacheSize, vol_io_params.fd_initial_cachesize);
158 #ifdef AFS_PTHREAD_ENV
160 pthread_attr_t tattr;
162 pthread_attr_init(&tattr);
163 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
165 pthread_create(&syncer, &tattr, ih_sync_thread, NULL);
166 #else /* AFS_PTHREAD_ENV */
168 LWP_CreateProcess(ih_sync_thread, 16*1024, LWP_MAX_PRIORITY - 2,
169 NULL, "ih_syncer", &syncer);
170 #endif /* AFS_PTHREAD_ENV */
175 /* Make the file descriptor cache as big as possible. Don't this call
176 * if the program uses fopen or fdopen, if fd_max_cachesize cannot be
177 * represented in the fileno member of the system FILE structure (or
181 ih_UseLargeCache(void)
185 if (!ih_PkgDefaultsSet) {
193 fdCacheSize = fdMaxCacheSize;
198 /* Allocate a chunk of inode handles */
200 iHandleAllocateChunk(void)
205 opr_Assert(ihAvailHead == NULL);
206 ihP = malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
207 opr_Assert(ihP != NULL);
208 for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
209 ihP[i].ih_refcnt = 0;
210 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
214 /* Initialize an inode handle */
216 ih_init(int dev, int vid, Inode ino)
218 int ihash = IH_HASH(dev, vid, ino);
221 if (!ih_PkgDefaultsSet) {
230 /* Do we already have a handle for this Inode? */
231 for (ihP = ihashTable[ihash].ihash_head; ihP; ihP = ihP->ih_next) {
232 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
239 /* Allocate and initialize a new Inode handle */
240 if (ihAvailHead == NULL) {
241 iHandleAllocateChunk();
244 opr_Assert(ihP->ih_refcnt == 0);
245 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
252 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
253 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
254 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
259 /* Copy an inode handle */
261 ih_copy(IHandle_t * ihP)
264 opr_Assert(ih_Inited);
265 opr_Assert(ihP->ih_refcnt > 0);
271 /* Allocate a chunk of file descriptor handles */
273 fdHandleAllocateChunk(void)
278 opr_Assert(fdAvailHead == NULL);
279 fdP = malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
280 opr_Assert(fdP != NULL);
281 for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
282 fdP[i].fd_status = FD_HANDLE_AVAIL;
283 fdP[i].fd_refcnt = 0;
285 fdP[i].fd_fd = INVALID_FD;
286 fdP[i].fd_ihnext = NULL;
287 fdP[i].fd_ihprev = NULL;
288 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
292 /* Allocate a chunk of stream handles */
294 streamHandleAllocateChunk(void)
297 StreamHandle_t *streamP;
299 opr_Assert(streamAvailHead == NULL);
300 streamP = (StreamHandle_t *)
301 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
302 opr_Assert(streamP != NULL);
303 for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
304 streamP[i].str_fd = INVALID_FD;
305 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
311 * Get a file descriptor handle given an Inode handle
314 ih_open(IHandle_t * ihP)
320 if (!ihP) /* XXX should log here in the fileserver */
325 /* Do we already have an open file handle for this Inode? */
326 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
327 if (fdP->fd_status == FD_HANDLE_CLOSING) {
328 /* The handle was open when an IH_REALLYCLOSE was issued, so we
329 * cannot reuse it; it will be closed soon. */
334 * If we don't have positional i/o, don't try to share fds, since
335 * we can't do so in a threadsafe way.
337 if (fdP->fd_status == FD_HANDLE_INUSE) {
340 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
342 opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
343 #endif /* HAVE_PIO */
346 if (fdP->fd_status == FD_HANDLE_OPEN) {
347 fdP->fd_status = FD_HANDLE_INUSE;
348 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
356 * Try to open the Inode, return NULL on error.
363 if (fd == INVALID_FD && (errno != EMFILE || fdLruHead == NULL) ) {
369 /* fdCacheSize limits the size of the descriptor cache, but
370 * we permit the number of open files to exceed fdCacheSize.
371 * We only recycle open file descriptors when the number
372 * of open files reaches the size of the cache */
373 if ((fdInUseCount > fdCacheSize || fd == INVALID_FD) && fdLruHead != NULL) {
375 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
376 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
377 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
378 fd_ihnext, fd_ihprev);
379 closeFd = fdP->fd_fd;
380 if (fd == INVALID_FD) {
381 fdCacheSize--; /* reduce in order to not run into here too often */
382 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
383 fdP->fd_status = FD_HANDLE_AVAIL;
385 fdP->fd_fd = INVALID_FD;
391 if (fdAvailHead == NULL) {
392 fdHandleAllocateChunk();
395 opr_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
396 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
397 closeFd = INVALID_FD;
400 fdP->fd_status = FD_HANDLE_INUSE;
407 /* Add this handle to the Inode's list of open descriptors */
408 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
411 if (closeFd != INVALID_FD) {
423 * Return a file descriptor handle to the cache
426 fd_close(FdHandle_t * fdP)
434 opr_Assert(ih_Inited);
435 opr_Assert(fdInUseCount > 0);
436 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
437 fdP->fd_status == FD_HANDLE_CLOSING);
441 /* Call fd_reallyclose to really close the unused file handles if
442 * the previous attempt to close (ih_reallyclose()) all file handles
443 * failed (this is determined by checking the ihandle for the flag
444 * IH_REALLY_CLOSED) or we have too many open files.
446 if (fdP->fd_status == FD_HANDLE_CLOSING ||
447 ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
449 return fd_reallyclose(fdP);
453 if (fdP->fd_refcnt == 0) {
454 /* Put this descriptor back into the cache */
455 fdP->fd_status = FD_HANDLE_OPEN;
456 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
459 /* If this is not the only reference to the Inode then we can decrement
460 * the reference count, otherwise we need to call ih_release.
462 if (ihP->ih_refcnt > 1)
473 * Actually close the file descriptor handle and return it to
477 fd_reallyclose(FdHandle_t * fdP)
486 opr_Assert(ih_Inited);
487 opr_Assert(fdInUseCount > 0);
488 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
489 fdP->fd_status == FD_HANDLE_CLOSING);
492 closeFd = fdP->fd_fd;
495 if (fdP->fd_refcnt == 0) {
496 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
497 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
499 fdP->fd_status = FD_HANDLE_AVAIL;
502 fdP->fd_fd = INVALID_FD;
505 /* All the file descriptor handles have been closed; reset
506 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
507 * has completed its job.
509 if (!ihP->ih_fdhead) {
510 ihP->ih_flags &= ~IH_REALLY_CLOSED;
512 FdHandle_t *lfdP, *next;
514 for (lfdP = ihP->ih_fdhead; lfdP != NULL; lfdP = next) {
515 next = lfdP->fd_ihnext;
516 osi_Assert(lfdP->fd_ih == ihP);
517 if (lfdP->fd_status != FD_HANDLE_CLOSING) {
522 /* no *future* fd should be subjected to this */
524 ihP->ih_flags &= ~IH_REALLY_CLOSED;
527 if (fdP->fd_refcnt == 0) {
534 /* If this is not the only reference to the Inode then we can decrement
535 * the reference count, otherwise we need to call ih_release. */
536 if (ihP->ih_refcnt > 1)
546 /* Enable buffered I/O on a file descriptor */
548 stream_fdopen(FD_t fd)
550 StreamHandle_t *streamP;
553 if (streamAvailHead == NULL) {
554 streamHandleAllocateChunk();
556 streamP = streamAvailHead;
557 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
559 streamP->str_fd = fd;
560 streamP->str_buflen = 0;
561 streamP->str_bufoff = 0;
562 streamP->str_fdoff = 0;
563 streamP->str_error = 0;
564 streamP->str_eof = 0;
565 streamP->str_direction = STREAM_DIRECTION_NONE;
569 /* Open a file for buffered I/O */
571 stream_open(const char *filename, const char *mode)
573 FD_t fd = INVALID_FD;
575 if (strcmp(mode, "r") == 0) {
576 fd = OS_OPEN(filename, O_RDONLY, 0);
577 } else if (strcmp(mode, "r+") == 0) {
578 fd = OS_OPEN(filename, O_RDWR, 0);
579 } else if (strcmp(mode, "w") == 0) {
580 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
581 } else if (strcmp(mode, "w+") == 0) {
582 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
583 } else if (strcmp(mode, "a") == 0) {
584 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
585 } else if (strcmp(mode, "a+") == 0) {
586 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
588 opr_abort(); /* not implemented */
591 if (fd == INVALID_FD) {
594 return stream_fdopen(fd);
597 /* fread for buffered I/O handles */
599 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
600 StreamHandle_t * streamP)
602 afs_fsize_t nbytes, bytesRead, bytesToRead;
605 /* Need to seek before changing direction */
606 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
607 streamP->str_direction = STREAM_DIRECTION_READ;
608 streamP->str_bufoff = 0;
609 streamP->str_buflen = 0;
611 opr_Assert(streamP->str_direction == STREAM_DIRECTION_READ);
615 nbytes = size * nitems;
617 while (nbytes > 0 && !streamP->str_eof) {
618 if (streamP->str_buflen == 0) {
619 streamP->str_bufoff = 0;
620 streamP->str_buflen =
621 OS_PREAD(streamP->str_fd, streamP->str_buffer,
622 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
623 if (streamP->str_buflen < 0) {
624 streamP->str_error = errno;
625 streamP->str_buflen = 0;
628 } else if (streamP->str_buflen == 0) {
629 streamP->str_eof = 1;
632 streamP->str_fdoff += streamP->str_buflen;
635 bytesToRead = nbytes;
636 if (bytesToRead > streamP->str_buflen) {
637 bytesToRead = streamP->str_buflen;
639 memcpy(p, streamP->str_buffer + streamP->str_bufoff, bytesToRead);
641 streamP->str_bufoff += bytesToRead;
642 streamP->str_buflen -= bytesToRead;
643 bytesRead += bytesToRead;
644 nbytes -= bytesToRead;
647 return (bytesRead / size);
650 /* fwrite for buffered I/O handles */
652 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
653 StreamHandle_t * streamP)
657 afs_fsize_t nbytes, bytesWritten, bytesToWrite;
659 /* Need to seek before changing direction */
660 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
661 streamP->str_direction = STREAM_DIRECTION_WRITE;
662 streamP->str_bufoff = 0;
663 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
665 opr_Assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
668 nbytes = size * nitems;
672 if (streamP->str_buflen == 0) {
673 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
674 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
676 streamP->str_error = errno;
680 streamP->str_fdoff += rc;
681 streamP->str_bufoff = 0;
682 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
685 bytesToWrite = nbytes;
686 if (bytesToWrite > streamP->str_buflen) {
687 bytesToWrite = streamP->str_buflen;
689 memcpy(streamP->str_buffer + streamP->str_bufoff, p, bytesToWrite);
691 streamP->str_bufoff += bytesToWrite;
692 streamP->str_buflen -= bytesToWrite;
693 bytesWritten += bytesToWrite;
694 nbytes -= bytesToWrite;
697 return (bytesWritten / size);
700 /* fseek for buffered I/O handles */
702 stream_aseek(StreamHandle_t * streamP, afs_foff_t offset)
707 if (streamP->str_direction == STREAM_DIRECTION_WRITE
708 && streamP->str_bufoff > 0) {
709 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
710 streamP->str_bufoff, streamP->str_fdoff);
712 streamP->str_error = errno;
716 streamP->str_fdoff = offset;
717 streamP->str_bufoff = 0;
718 streamP->str_buflen = 0;
719 streamP->str_eof = 0;
720 streamP->str_direction = STREAM_DIRECTION_NONE;
724 /* fflush for buffered I/O handles */
726 stream_flush(StreamHandle_t * streamP)
731 if (streamP->str_direction == STREAM_DIRECTION_WRITE
732 && streamP->str_bufoff > 0) {
733 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
734 streamP->str_bufoff, streamP->str_fdoff);
736 streamP->str_error = errno;
739 streamP->str_fdoff += rc;
741 streamP->str_bufoff = 0;
742 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
748 /* Free a buffered I/O handle */
750 stream_close(StreamHandle_t * streamP, int reallyClose)
755 opr_Assert(streamP != NULL);
756 if (streamP->str_direction == STREAM_DIRECTION_WRITE
757 && streamP->str_bufoff > 0) {
758 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
759 streamP->str_bufoff, streamP->str_fdoff);
763 streamP->str_fdoff += rc;
767 rc = OS_CLOSE(streamP->str_fd);
772 streamP->str_fd = INVALID_FD;
775 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
781 /* Close all unused file descriptors associated with the inode
782 * handle. Called with IH_LOCK held. May drop and reacquire
783 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
784 * if it fails to close all file handles.
787 ih_fdclose(IHandle_t * ihP)
789 int closeCount, closedAll;
790 FdHandle_t *fdP, *head, *tail, *next;
792 opr_Assert(ihP->ih_refcnt > 0);
795 DLL_INIT_LIST(head, tail);
796 ihP->ih_flags &= ~IH_REALLY_CLOSED;
799 * Remove the file descriptors for this Inode from the LRU queue
800 * and the IHandle queue and put them on a temporary queue so we
801 * can drop the lock before we close the files.
803 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
804 next = fdP->fd_ihnext;
805 opr_Assert(fdP->fd_ih == ihP);
806 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN
807 || fdP->fd_status == FD_HANDLE_INUSE
808 || fdP->fd_status == FD_HANDLE_CLOSING);
809 if (fdP->fd_status == FD_HANDLE_OPEN) {
810 /* Note that FdHandle_t's do not count against the parent
811 * IHandle_t ref count when they are FD_HANDLE_OPEN. So, we don't
812 * need to dec the parent IHandle_t ref count for each one we pull
814 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
816 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
817 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
820 fdP->fd_status = FD_HANDLE_CLOSING;
821 ihP->ih_flags |= IH_REALLY_CLOSED;
825 /* If the ihandle reference count is 1, we should have
826 * closed all file descriptors.
828 if (ihP->ih_refcnt == 1 || closedAll) {
829 opr_Assert(closedAll);
830 opr_Assert(!ihP->ih_fdhead);
831 opr_Assert(!ihP->ih_fdtail);
835 return 0; /* No file descriptors closed */
840 * Close the file descriptors
843 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
844 OS_CLOSE(fdP->fd_fd);
845 fdP->fd_status = FD_HANDLE_AVAIL;
847 fdP->fd_fd = INVALID_FD;
853 opr_Assert(fdInUseCount >= closeCount);
854 fdInUseCount -= closeCount;
857 * Append the temporary queue to the list of available descriptors
859 if (fdAvailHead == NULL) {
863 fdAvailTail->fd_next = head;
864 head->fd_prev = fdAvailTail;
871 /* Close all cached file descriptors for this inode. */
873 ih_reallyclose(IHandle_t * ihP)
879 ihP->ih_refcnt++; /* must not disappear over unlock */
880 if (ihP->ih_synced) {
894 opr_Assert(ihP->ih_refcnt > 0);
898 if (ihP->ih_refcnt > 1)
907 /* Release an Inode handle. All cached file descriptors for this
908 * inode are closed when the last reference to this handle is released
911 _ih_release_r(IHandle_t * ihP)
918 opr_Assert(ihP->ih_refcnt > 0);
920 if (ihP->ih_refcnt > 1) {
925 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
926 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
927 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
933 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
938 /* Release an Inode handle. All cached file descriptors for this
939 * inode are closed when the last reference to this handle is released
942 ih_release(IHandle_t * ihP)
950 ret = _ih_release_r(ihP);
955 /* Sync an inode to disk if its handle isn't NULL */
957 ih_condsync(IHandle_t * ihP)
969 code = FDH_SYNC(fdP);
981 for (ihash = 0; ihash < I_HANDLE_HASH_SIZE; ihash++) {
982 IHandle_t *ihP, *ihPnext;
984 ihP = ihashTable[ihash].ihash_head;
986 ihP->ih_refcnt++; /* must not disappear over unlock */
987 for (; ihP; ihP = ihPnext) {
989 if (ihP->ih_synced) {
1004 /* when decrementing the refcount, the ihandle might disappear
1005 and we might not even be able to proceed to the next one.
1006 Hence the gymnastics putting a hold on the next one already */
1007 ihPnext = ihP->ih_next;
1008 if (ihPnext) ihPnext->ih_refcnt++;
1010 if (ihP->ih_refcnt > 1)
1020 ih_sync_thread(void *dummy) {
1021 afs_pthread_setname_self("ih_syncer");
1024 #ifdef AFS_PTHREAD_ENV
1026 #else /* AFS_PTHREAD_ENV */
1028 #endif /* AFS_PTHREAD_ENV */
1036 /*************************************************************************
1037 * OS specific support routines.
1038 *************************************************************************/
1039 #ifndef AFS_NAMEI_ENV
1041 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
1045 #ifdef AFS_3DISPARES
1046 /* See viceinode.h */
1047 if (p2 == INODESPECIAL) {
1053 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
1056 #endif /* AFS_NAMEI_ENV */
1063 if (!GetFileSizeEx(fd, &size))
1065 return size.QuadPart;
1067 struct afs_stat_st status;
1068 if (afs_fstat(fd, &status) < 0)
1070 return status.st_size;
1076 ih_pread(int fd, void * buf, size_t count, afs_foff_t offset)
1079 code = OS_SEEK(fd, offset, 0);
1082 return OS_READ(fd, buf, count);
1086 ih_pwrite(int fd, const void * buf, size_t count, afs_foff_t offset)
1089 code = OS_SEEK(fd, offset, 0);
1092 return OS_WRITE(fd, buf, count);
1094 #endif /* !HAVE_PIO */