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);
79 void *ih_sync_thread(void *);
81 /* start-time configurable I/O limits */
82 ih_init_params vol_io_params;
84 void ih_PkgDefaults(void)
87 ih_PkgDefaultsSet = 1;
89 /* default to well-known values */
90 vol_io_params.fd_handle_setaside = FD_HANDLE_SETASIDE;
92 /* initial fd cachesize. the only one that will be used if
93 * the application does not call ih_UseLargeCache(). set this
94 * to a value representable in fileno member of the system's
95 * FILE structure (or equivalent). */
96 vol_io_params.fd_initial_cachesize = FD_DEFAULT_CACHESIZE;
98 /* fd cache size that will be used if/when ih_UseLargeCache()
100 vol_io_params.fd_max_cachesize = FD_MAX_CACHESIZE;
103 #ifdef AFS_PTHREAD_ENV
104 /* Initialize the global ihandle mutex */
108 opr_mutex_init(&ih_glock_mutex);
110 #endif /* AFS_PTHREAD_ENV */
112 /* Initialize the file descriptor cache */
117 opr_Assert(!ih_Inited);
119 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
120 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
121 DLL_INIT_LIST(fdLruHead, fdLruTail);
122 for (i = 0; i < I_HANDLE_HASH_SIZE; i++) {
123 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
125 #if defined(AFS_NT40_ENV)
126 fdMaxCacheSize = vol_io_params.fd_max_cachesize;
127 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
130 opr_Verify(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
131 rlim.rlim_cur = rlim.rlim_max;
132 opr_Verify(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
133 fdMaxCacheSize = rlim.rlim_cur - vol_io_params.fd_handle_setaside;
135 /* XXX this is to avoid using up all system fd netbsd is
136 * somewhat broken and have set maximum fd for a root process
137 * to the same as system fd that is avaible, so if the
138 * fileserver uses all up process fds, all system fd will be
141 * Check for this better
145 fdMaxCacheSize = min(fdMaxCacheSize, vol_io_params.fd_max_cachesize);
146 opr_Assert(fdMaxCacheSize > 0);
148 #elif defined(AFS_HPUX_ENV)
149 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
153 long fdMax = max(sysconf(_SC_OPEN_MAX) - vol_io_params.fd_handle_setaside,
155 fdMaxCacheSize = (int)min(fdMax, vol_io_params.fd_max_cachesize);
158 fdCacheSize = min(fdMaxCacheSize, vol_io_params.fd_initial_cachesize);
161 #ifdef AFS_PTHREAD_ENV
163 pthread_attr_t tattr;
165 pthread_attr_init(&tattr);
166 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
168 pthread_create(&syncer, &tattr, ih_sync_thread, NULL);
169 #else /* AFS_PTHREAD_ENV */
171 LWP_CreateProcess(ih_sync_thread, 16*1024, LWP_MAX_PRIORITY - 2,
172 NULL, "ih_syncer", &syncer);
173 #endif /* AFS_PTHREAD_ENV */
178 /* Make the file descriptor cache as big as possible. Don't this call
179 * if the program uses fopen or fdopen, if fd_max_cachesize cannot be
180 * represented in the fileno member of the system FILE structure (or
184 ih_UseLargeCache(void)
188 if (!ih_PkgDefaultsSet) {
196 fdCacheSize = fdMaxCacheSize;
201 /* Allocate a chunk of inode handles */
203 iHandleAllocateChunk(void)
208 opr_Assert(ihAvailHead == NULL);
209 ihP = malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
210 opr_Assert(ihP != NULL);
211 for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
212 ihP[i].ih_refcnt = 0;
213 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
217 /* Initialize an inode handle */
219 ih_init(int dev, int vid, Inode ino)
221 int ihash = IH_HASH(dev, vid, ino);
224 if (!ih_PkgDefaultsSet) {
233 /* Do we already have a handle for this Inode? */
234 for (ihP = ihashTable[ihash].ihash_head; ihP; ihP = ihP->ih_next) {
235 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
242 /* Allocate and initialize a new Inode handle */
243 if (ihAvailHead == NULL) {
244 iHandleAllocateChunk();
247 opr_Assert(ihP->ih_refcnt == 0);
248 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
255 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
256 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
257 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
262 /* Copy an inode handle */
264 ih_copy(IHandle_t * ihP)
267 opr_Assert(ih_Inited);
268 opr_Assert(ihP->ih_refcnt > 0);
274 /* Allocate a chunk of file descriptor handles */
276 fdHandleAllocateChunk(void)
281 opr_Assert(fdAvailHead == NULL);
282 fdP = malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
283 opr_Assert(fdP != NULL);
284 for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
285 fdP[i].fd_status = FD_HANDLE_AVAIL;
286 fdP[i].fd_refcnt = 0;
288 fdP[i].fd_fd = INVALID_FD;
289 fdP[i].fd_ihnext = NULL;
290 fdP[i].fd_ihprev = NULL;
291 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
295 /* Allocate a chunk of stream handles */
297 streamHandleAllocateChunk(void)
300 StreamHandle_t *streamP;
302 opr_Assert(streamAvailHead == NULL);
303 streamP = (StreamHandle_t *)
304 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
305 opr_Assert(streamP != NULL);
306 for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
307 streamP[i].str_fd = INVALID_FD;
308 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
314 * Get a file descriptor handle given an Inode handle
317 ih_open(IHandle_t * ihP)
323 if (!ihP) /* XXX should log here in the fileserver */
328 /* Do we already have an open file handle for this Inode? */
329 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
330 if (fdP->fd_status == FD_HANDLE_CLOSING) {
331 /* The handle was open when an IH_REALLYCLOSE was issued, so we
332 * cannot reuse it; it will be closed soon. */
337 * If we don't have positional i/o, don't try to share fds, since
338 * we can't do so in a threadsafe way.
340 if (fdP->fd_status == FD_HANDLE_INUSE) {
343 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
345 opr_Assert(fdP->fd_status != FD_HANDLE_AVAIL);
346 #endif /* HAVE_PIO */
349 if (fdP->fd_status == FD_HANDLE_OPEN) {
350 fdP->fd_status = FD_HANDLE_INUSE;
351 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
359 * Try to open the Inode, return NULL on error.
366 if (fd == INVALID_FD && (errno != EMFILE || fdLruHead == NULL) ) {
372 /* fdCacheSize limits the size of the descriptor cache, but
373 * we permit the number of open files to exceed fdCacheSize.
374 * We only recycle open file descriptors when the number
375 * of open files reaches the size of the cache */
376 if ((fdInUseCount > fdCacheSize || fd == INVALID_FD) && fdLruHead != NULL) {
378 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN);
379 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
380 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
381 fd_ihnext, fd_ihprev);
382 closeFd = fdP->fd_fd;
383 if (fd == INVALID_FD) {
384 fdCacheSize--; /* reduce in order to not run into here too often */
385 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
386 fdP->fd_status = FD_HANDLE_AVAIL;
388 fdP->fd_fd = INVALID_FD;
394 if (fdAvailHead == NULL) {
395 fdHandleAllocateChunk();
398 opr_Assert(fdP->fd_status == FD_HANDLE_AVAIL);
399 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
400 closeFd = INVALID_FD;
403 fdP->fd_status = FD_HANDLE_INUSE;
410 /* Add this handle to the Inode's list of open descriptors */
411 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
414 if (closeFd != INVALID_FD) {
426 * Return a file descriptor handle to the cache
429 fd_close(FdHandle_t * fdP)
437 opr_Assert(ih_Inited);
438 opr_Assert(fdInUseCount > 0);
439 opr_Assert(fdP->fd_status == FD_HANDLE_INUSE ||
440 fdP->fd_status == FD_HANDLE_CLOSING);
444 /* Call fd_reallyclose to really close the unused file handles if
445 * the previous attempt to close (ih_reallyclose()) all file handles
446 * failed (this is determined by checking the ihandle for the flag
447 * IH_REALLY_CLOSED) or we have too many open files.
449 if (fdP->fd_status == FD_HANDLE_CLOSING ||
450 ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
452 return fd_reallyclose(fdP);
456 if (fdP->fd_refcnt == 0) {
457 /* Put this descriptor back into the cache */
458 fdP->fd_status = FD_HANDLE_OPEN;
459 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
462 /* If this is not the only reference to the Inode then we can decrement
463 * the reference count, otherwise we need to call ih_release.
465 if (ihP->ih_refcnt > 1)
476 * Actually close the file descriptor handle and return it to
480 fd_reallyclose(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);
495 closeFd = fdP->fd_fd;
498 if (fdP->fd_refcnt == 0) {
499 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
500 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
502 fdP->fd_status = FD_HANDLE_AVAIL;
505 fdP->fd_fd = INVALID_FD;
508 /* All the file descriptor handles have been closed; reset
509 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
510 * has completed its job.
512 if (!ihP->ih_fdhead) {
513 ihP->ih_flags &= ~IH_REALLY_CLOSED;
515 FdHandle_t *lfdP, *next;
517 for (lfdP = ihP->ih_fdhead; lfdP != NULL; lfdP = next) {
518 next = lfdP->fd_ihnext;
519 osi_Assert(lfdP->fd_ih == ihP);
520 if (lfdP->fd_status != FD_HANDLE_CLOSING) {
525 /* no *future* fd should be subjected to this */
527 ihP->ih_flags &= ~IH_REALLY_CLOSED;
530 if (fdP->fd_refcnt == 0) {
537 /* If this is not the only reference to the Inode then we can decrement
538 * the reference count, otherwise we need to call ih_release. */
539 if (ihP->ih_refcnt > 1)
549 /* Enable buffered I/O on a file descriptor */
551 stream_fdopen(FD_t fd)
553 StreamHandle_t *streamP;
556 if (streamAvailHead == NULL) {
557 streamHandleAllocateChunk();
559 streamP = streamAvailHead;
560 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
562 streamP->str_fd = fd;
563 streamP->str_buflen = 0;
564 streamP->str_bufoff = 0;
565 streamP->str_fdoff = 0;
566 streamP->str_error = 0;
567 streamP->str_eof = 0;
568 streamP->str_direction = STREAM_DIRECTION_NONE;
572 /* Open a file for buffered I/O */
574 stream_open(const char *filename, const char *mode)
576 FD_t fd = INVALID_FD;
578 if (strcmp(mode, "r") == 0) {
579 fd = OS_OPEN(filename, O_RDONLY, 0);
580 } else if (strcmp(mode, "r+") == 0) {
581 fd = OS_OPEN(filename, O_RDWR, 0);
582 } else if (strcmp(mode, "w") == 0) {
583 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
584 } else if (strcmp(mode, "w+") == 0) {
585 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
586 } else if (strcmp(mode, "a") == 0) {
587 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
588 } else if (strcmp(mode, "a+") == 0) {
589 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
591 opr_abort(); /* not implemented */
594 if (fd == INVALID_FD) {
597 return stream_fdopen(fd);
600 /* fread for buffered I/O handles */
602 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
603 StreamHandle_t * streamP)
605 afs_fsize_t nbytes, bytesRead, bytesToRead;
608 /* Need to seek before changing direction */
609 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
610 streamP->str_direction = STREAM_DIRECTION_READ;
611 streamP->str_bufoff = 0;
612 streamP->str_buflen = 0;
614 opr_Assert(streamP->str_direction == STREAM_DIRECTION_READ);
618 nbytes = size * nitems;
620 while (nbytes > 0 && !streamP->str_eof) {
621 if (streamP->str_buflen == 0) {
622 streamP->str_bufoff = 0;
623 streamP->str_buflen =
624 OS_PREAD(streamP->str_fd, streamP->str_buffer,
625 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
626 if (streamP->str_buflen < 0) {
627 streamP->str_error = errno;
628 streamP->str_buflen = 0;
631 } else if (streamP->str_buflen == 0) {
632 streamP->str_eof = 1;
635 streamP->str_fdoff += streamP->str_buflen;
638 bytesToRead = nbytes;
639 if (bytesToRead > streamP->str_buflen) {
640 bytesToRead = streamP->str_buflen;
642 memcpy(p, streamP->str_buffer + streamP->str_bufoff, bytesToRead);
644 streamP->str_bufoff += bytesToRead;
645 streamP->str_buflen -= bytesToRead;
646 bytesRead += bytesToRead;
647 nbytes -= bytesToRead;
650 return (bytesRead / size);
653 /* fwrite for buffered I/O handles */
655 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
656 StreamHandle_t * streamP)
660 afs_fsize_t nbytes, bytesWritten, bytesToWrite;
662 /* Need to seek before changing direction */
663 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
664 streamP->str_direction = STREAM_DIRECTION_WRITE;
665 streamP->str_bufoff = 0;
666 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
668 opr_Assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
671 nbytes = size * nitems;
675 if (streamP->str_buflen == 0) {
676 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
677 STREAM_HANDLE_BUFSIZE, streamP->str_fdoff);
679 streamP->str_error = errno;
683 streamP->str_fdoff += rc;
684 streamP->str_bufoff = 0;
685 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
688 bytesToWrite = nbytes;
689 if (bytesToWrite > streamP->str_buflen) {
690 bytesToWrite = streamP->str_buflen;
692 memcpy(streamP->str_buffer + streamP->str_bufoff, p, bytesToWrite);
694 streamP->str_bufoff += bytesToWrite;
695 streamP->str_buflen -= bytesToWrite;
696 bytesWritten += bytesToWrite;
697 nbytes -= bytesToWrite;
700 return (bytesWritten / size);
703 /* fseek for buffered I/O handles */
705 stream_aseek(StreamHandle_t * streamP, afs_foff_t offset)
710 if (streamP->str_direction == STREAM_DIRECTION_WRITE
711 && streamP->str_bufoff > 0) {
712 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
713 streamP->str_bufoff, streamP->str_fdoff);
715 streamP->str_error = errno;
719 streamP->str_fdoff = offset;
720 streamP->str_bufoff = 0;
721 streamP->str_buflen = 0;
722 streamP->str_eof = 0;
723 streamP->str_direction = STREAM_DIRECTION_NONE;
727 /* fflush for buffered I/O handles */
729 stream_flush(StreamHandle_t * streamP)
734 if (streamP->str_direction == STREAM_DIRECTION_WRITE
735 && streamP->str_bufoff > 0) {
736 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
737 streamP->str_bufoff, streamP->str_fdoff);
739 streamP->str_error = errno;
742 streamP->str_fdoff += rc;
744 streamP->str_bufoff = 0;
745 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
751 /* Free a buffered I/O handle */
753 stream_close(StreamHandle_t * streamP, int reallyClose)
758 opr_Assert(streamP != NULL);
759 if (streamP->str_direction == STREAM_DIRECTION_WRITE
760 && streamP->str_bufoff > 0) {
761 rc = OS_PWRITE(streamP->str_fd, streamP->str_buffer,
762 streamP->str_bufoff, streamP->str_fdoff);
766 streamP->str_fdoff += rc;
770 rc = OS_CLOSE(streamP->str_fd);
775 streamP->str_fd = INVALID_FD;
778 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
784 /* Close all unused file descriptors associated with the inode
785 * handle. Called with IH_LOCK held. May drop and reacquire
786 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
787 * if it fails to close all file handles.
790 ih_fdclose(IHandle_t * ihP)
792 int closeCount, closedAll;
793 FdHandle_t *fdP, *head, *tail, *next;
795 opr_Assert(ihP->ih_refcnt > 0);
798 DLL_INIT_LIST(head, tail);
799 ihP->ih_flags &= ~IH_REALLY_CLOSED;
802 * Remove the file descriptors for this Inode from the LRU queue
803 * and the IHandle queue and put them on a temporary queue so we
804 * can drop the lock before we close the files.
806 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
807 next = fdP->fd_ihnext;
808 opr_Assert(fdP->fd_ih == ihP);
809 opr_Assert(fdP->fd_status == FD_HANDLE_OPEN
810 || fdP->fd_status == FD_HANDLE_INUSE
811 || fdP->fd_status == FD_HANDLE_CLOSING);
812 if (fdP->fd_status == FD_HANDLE_OPEN) {
813 /* Note that FdHandle_t's do not count against the parent
814 * IHandle_t ref count when they are FD_HANDLE_OPEN. So, we don't
815 * need to dec the parent IHandle_t ref count for each one we pull
817 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
819 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
820 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
823 fdP->fd_status = FD_HANDLE_CLOSING;
824 ihP->ih_flags |= IH_REALLY_CLOSED;
828 /* If the ihandle reference count is 1, we should have
829 * closed all file descriptors.
831 if (ihP->ih_refcnt == 1 || closedAll) {
832 opr_Assert(closedAll);
833 opr_Assert(!ihP->ih_fdhead);
834 opr_Assert(!ihP->ih_fdtail);
838 return 0; /* No file descriptors closed */
843 * Close the file descriptors
846 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
847 OS_CLOSE(fdP->fd_fd);
848 fdP->fd_status = FD_HANDLE_AVAIL;
850 fdP->fd_fd = INVALID_FD;
856 opr_Assert(fdInUseCount >= closeCount);
857 fdInUseCount -= closeCount;
860 * Append the temporary queue to the list of available descriptors
862 if (fdAvailHead == NULL) {
866 fdAvailTail->fd_next = head;
867 head->fd_prev = fdAvailTail;
874 /* Close all cached file descriptors for this inode. */
876 ih_reallyclose(IHandle_t * ihP)
882 ihP->ih_refcnt++; /* must not disappear over unlock */
883 if (ihP->ih_synced) {
897 opr_Assert(ihP->ih_refcnt > 0);
901 if (ihP->ih_refcnt > 1)
910 /* Release an Inode handle. All cached file descriptors for this
911 * inode are closed when the last reference to this handle is released
914 _ih_release_r(IHandle_t * ihP)
921 opr_Assert(ihP->ih_refcnt > 0);
923 if (ihP->ih_refcnt > 1) {
928 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
929 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
930 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
936 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
941 /* Release an Inode handle. All cached file descriptors for this
942 * inode are closed when the last reference to this handle is released
945 ih_release(IHandle_t * ihP)
953 ret = _ih_release_r(ihP);
958 /* Sync an inode to disk if its handle isn't NULL */
960 ih_condsync(IHandle_t * ihP)
972 code = FDH_SYNC(fdP);
984 for (ihash = 0; ihash < I_HANDLE_HASH_SIZE; ihash++) {
985 IHandle_t *ihP, *ihPnext;
987 ihP = ihashTable[ihash].ihash_head;
989 ihP->ih_refcnt++; /* must not disappear over unlock */
990 for (; ihP; ihP = ihPnext) {
992 if (ihP->ih_synced) {
1000 OS_SYNC(fdP->fd_fd);
1007 /* when decrementing the refcount, the ihandle might disappear
1008 and we might not even be able to proceed to the next one.
1009 Hence the gymnastics putting a hold on the next one already */
1010 ihPnext = ihP->ih_next;
1011 if (ihPnext) ihPnext->ih_refcnt++;
1013 if (ihP->ih_refcnt > 1)
1023 ih_sync_thread(void *dummy) {
1024 afs_pthread_setname_self("ih_syncer");
1027 #ifdef AFS_PTHREAD_ENV
1029 #else /* AFS_PTHREAD_ENV */
1031 #endif /* AFS_PTHREAD_ENV */
1039 /*************************************************************************
1040 * OS specific support routines.
1041 *************************************************************************/
1042 #ifndef AFS_NAMEI_ENV
1044 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
1048 #ifdef AFS_3DISPARES
1049 /* See viceinode.h */
1050 if (p2 == INODESPECIAL) {
1056 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
1059 #endif /* AFS_NAMEI_ENV */
1066 if (!GetFileSizeEx(fd, &size))
1068 return size.QuadPart;
1070 struct afs_stat_st status;
1071 if (afs_fstat(fd, &status) < 0)
1073 return status.st_size;
1079 ih_pread(int fd, void * buf, size_t count, afs_foff_t offset)
1082 code = OS_SEEK(fd, offset, 0);
1085 return OS_READ(fd, buf, count);
1089 ih_pwrite(int fd, const void * buf, size_t count, afs_foff_t offset)
1092 code = OS_SEEK(fd, offset, 0);
1095 return OS_WRITE(fd, buf, count);
1097 #endif /* !HAVE_PIO */
1099 #ifndef AFS_NT40_ENV
1101 ih_isunlinked(int fd)
1103 struct afs_stat_st status;
1104 if (afs_fstat(fd, &status) < 0) {
1107 if (status.st_nlink < 1) {
1112 #endif /* !AFS_NT40_ENV */