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>
20 #include <sys/types.h>
29 #include <sys/fcntl.h>
30 #include <sys/resource.h>
34 #include <afs/afsint.h>
36 #include <afs/afssyscalls.h>
39 #include "viceinode.h"
40 #ifdef AFS_PTHREAD_ENV
42 #else /* AFS_PTHREAD_ENV */
43 #include "afs/assert.h"
44 #endif /* AFS_PTHREAD_ENV */
47 extern afs_int32 DErrno;
49 #ifdef AFS_PTHREAD_ENV
50 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
51 pthread_mutex_t ih_glock_mutex;
52 #endif /* AFS_PTHREAD_ENV */
54 /* Linked list of available inode handles */
55 IHandle_t *ihAvailHead;
56 IHandle_t *ihAvailTail;
58 /* Linked list of available file descriptor handles */
59 FdHandle_t *fdAvailHead;
60 FdHandle_t *fdAvailTail;
62 /* Linked list of available stream descriptor handles */
63 StreamHandle_t *streamAvailHead;
64 StreamHandle_t *streamAvailTail;
66 /* LRU list for file descriptor handles */
67 FdHandle_t *fdLruHead;
68 FdHandle_t *fdLruTail;
72 /* Most of the servers use fopen/fdopen. Since the FILE structure
73 * only has eight bits for the file descriptor, the cache size
74 * has to be less than 256. The cache can be made larger as long
75 * as you are sure you don't need fopen/fdopen. */
76 int fdMaxCacheSize = 0;
79 /* Number of in use file descriptors */
82 /* Hash table for inode handles */
83 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
86 #ifdef AFS_PTHREAD_ENV
87 /* Initialize the global ihandle mutex */
90 assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
92 #endif /* AFS_PTHREAD_ENV */
94 /* Initialize the file descriptor cache */
95 void ih_Initialize() {
99 #endif /* AFS_SUN5_ENV */
102 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
103 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
104 DLL_INIT_LIST(fdLruHead, fdLruTail);
105 for (i = 0 ; i < I_HANDLE_HASH_SIZE ; i++) {
106 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
108 #if defined(AFS_NT40_ENV)
109 fdMaxCacheSize = FD_MAX_CACHESIZE;
110 #elif defined(AFS_SUN5_ENV)
111 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
112 rlim.rlim_cur = rlim.rlim_max;
113 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
114 fdMaxCacheSize = rlim.rlim_cur-FD_HANDLE_SETASIDE;
115 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
116 assert(fdMaxCacheSize > 0);
117 #elif defined(AFS_HPUX_ENV)
118 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
121 fdMaxCacheSize = MAX(sysconf(_SC_OPEN_MAX)-FD_HANDLE_SETASIDE, 0);
122 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
124 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
127 /* Make the file descriptor cache as big as possible. Don't this call
128 * if the program uses fopen or fdopen. */
129 void ih_UseLargeCache() {
135 fdCacheSize = fdMaxCacheSize;
140 /* Allocate a chunk of inode handles */
141 void iHandleAllocateChunk()
146 assert(ihAvailHead == NULL);
147 ihP = (IHandle_t *)malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
149 for (i = 0 ; i < I_HANDLE_MALLOCSIZE ; i++) {
150 ihP[i].ih_refcnt = 0;
151 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
155 /* Initialize an inode handle */
156 IHandle_t *ih_init(int dev, int vid, Inode ino)
158 int ihash = IH_HASH(dev, vid, ino);
167 /* Do we already have a handle for this Inode? */
168 for (ihP = ihashTable[ihash].ihash_head ; ihP ; ihP = ihP->ih_next) {
169 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
176 /* Allocate and initialize a new Inode handle */
177 if (ihAvailHead == NULL) {
178 iHandleAllocateChunk();
181 assert(ihP->ih_refcnt == 0);
182 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
188 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
189 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
190 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
195 /* Copy an inode handle */
196 IHandle_t *ih_copy(IHandle_t *ihP)
200 assert(ihP->ih_refcnt > 0);
206 /* Allocate a chunk of file descriptor handles */
207 void fdHandleAllocateChunk()
212 assert(fdAvailHead == NULL);
213 fdP = (FdHandle_t *)malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
215 for (i = 0 ; i < FD_HANDLE_MALLOCSIZE ; i++) {
216 fdP[i].fd_status = FD_HANDLE_AVAIL;
218 fdP[i].fd_fd = INVALID_FD;
219 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
223 /* Allocate a chunk of stream handles */
224 void streamHandleAllocateChunk()
227 StreamHandle_t *streamP;
229 assert(streamAvailHead == NULL);
230 streamP = (StreamHandle_t *)
231 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
232 assert(streamP != NULL);
233 for (i = 0 ; i < STREAM_HANDLE_MALLOCSIZE ; i++) {
234 streamP[i].str_fd = INVALID_FD;
235 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
241 * Get a file descriptor handle given an Inode handle
243 FdHandle_t *ih_open(IHandle_t *ihP)
255 /* Do we already have an open file handle for this Inode? */
256 for (fdP = ihP->ih_fdtail ; fdP != NULL ; fdP = fdP->fd_ihprev) {
257 if (fdP->fd_status != FD_HANDLE_INUSE) {
258 assert(fdP->fd_status == FD_HANDLE_OPEN);
259 fdP->fd_status = FD_HANDLE_INUSE;
260 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
263 FDH_SEEK(fdP, 0, SEEK_SET);
269 * Try to open the Inode, return NULL on error.
275 if (fd == INVALID_FD) {
281 /* fdCacheSize limits the size of the descriptor cache, but
282 * we permit the number of open files to exceed fdCacheSize.
283 * We only recycle open file descriptors when the number
284 * of open files reaches the size of the cache */
285 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
287 assert(fdP->fd_status == FD_HANDLE_OPEN);
288 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
289 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
290 fd_ihnext, fd_ihprev);
291 closeFd = fdP->fd_fd;
293 if (fdAvailHead == NULL) {
294 fdHandleAllocateChunk();
297 assert(fdP->fd_status == FD_HANDLE_AVAIL);
298 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
299 closeFd = INVALID_FD;
302 fdP->fd_status = FD_HANDLE_INUSE;
305 /* Add this handle to the Inode's list of open descriptors */
306 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
308 if (closeFd != INVALID_FD) {
321 * Return a file descriptor handle to the cache
323 int fd_close(FdHandle_t *fdP)
333 assert(fdInUseCount > 0);
334 assert(fdP->fd_status == FD_HANDLE_INUSE);
338 /* If a previous attempt to close ( ih_reallyclose() )
339 * all fd handles failed, then the IH_REALLY_CLOSED flag is set in
340 * the Inode handle so we call fd_reallyclose
343 if ( ihP->ih_flags & IH_REALLY_CLOSED ) {
345 return (fd_reallyclose(fdP));
348 /* If we have too many open files then close the descriptor. If we
349 * hold the last reference to the Inode handle then wait and let
350 * ih_release do the work. */
351 if (fdInUseCount > fdCacheSize && ihP->ih_refcnt > 1) {
352 assert(fdInUseCount > 0);
353 closeFd = fdP->fd_fd;
354 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
355 fd_ihnext, fd_ihprev);
356 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
357 fdP->fd_status = FD_HANDLE_AVAIL;
359 fdP->fd_fd = INVALID_FD;
369 /* Put this descriptor back into the cache */
370 fdP->fd_status = FD_HANDLE_OPEN;
371 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
373 /* If this is not the only reference to the Inode then we can decrement
374 * the reference count, otherwise we need to call ih_release. */
375 if (ihP->ih_refcnt > 1) {
387 * Return a file descriptor handle to the cache
389 int fd_reallyclose(FdHandle_t *fdP)
399 assert(fdInUseCount > 0);
400 assert(fdP->fd_status == FD_HANDLE_INUSE);
403 closeFd = fdP->fd_fd;
405 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
406 fd_ihnext, fd_ihprev);
407 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
408 fdP->fd_status = FD_HANDLE_AVAIL;
410 fdP->fd_fd = INVALID_FD;
416 /* If this is not the only reference to the Inode then we can decrement
417 * the reference count, otherwise we need to call ih_release. */
418 if (ihP->ih_refcnt > 1) {
428 /* Enable buffered I/O on a file descriptor */
429 StreamHandle_t *stream_fdopen(FD_t fd)
431 StreamHandle_t *streamP;
434 if (streamAvailHead == NULL) {
435 streamHandleAllocateChunk();
437 streamP = streamAvailHead;
438 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
441 streamP->str_fd = fd;
442 streamP->str_buflen = 0;
443 streamP->str_bufoff = 0;
444 streamP->str_error = 0;
445 streamP->str_eof = 0;
446 streamP->str_direction = STREAM_DIRECTION_NONE;
450 /* Open a file for buffered I/O */
451 StreamHandle_t *stream_open(char *filename, char *mode)
455 if (strcmp(mode, "r") == 0) {
456 fd = OS_OPEN(filename, O_RDONLY, 0);
457 } else if (strcmp(mode, "r+") == 0) {
458 fd = OS_OPEN(filename, O_RDWR, 0);
459 } else if (strcmp(mode, "w") == 0) {
460 fd = OS_OPEN(filename, O_WRONLY|O_TRUNC|O_CREAT, 0);
461 } else if (strcmp(mode, "w+") == 0) {
462 fd = OS_OPEN(filename, O_RDWR|O_TRUNC|O_CREAT, 0);
463 } else if (strcmp(mode, "a") == 0) {
464 fd = OS_OPEN(filename, O_WRONLY|O_APPEND|O_CREAT, 0);
465 } else if (strcmp(mode, "a+") == 0) {
466 fd = OS_OPEN(filename, O_RDWR|O_APPEND|O_CREAT, 0);
468 assert(FALSE); /* not implemented */
471 if (fd == INVALID_FD) {
474 return stream_fdopen(fd);
477 /* fread for buffered I/O handles */
478 int stream_read(void *ptr, int size, int nitems, StreamHandle_t *streamP)
480 int nbytes, bytesRead, bytesToRead;
483 /* Need to seek before changing direction */
484 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
485 streamP->str_direction = STREAM_DIRECTION_READ;
486 streamP->str_bufoff = 0;
487 streamP->str_buflen = 0;
489 assert(streamP->str_direction == STREAM_DIRECTION_READ);
493 nbytes = size * nitems;
495 while (nbytes > 0 && !streamP->str_eof) {
496 if (streamP->str_buflen == 0) {
497 streamP->str_bufoff = 0;
498 streamP->str_buflen = OS_READ(streamP->str_fd, streamP->str_buffer,
499 STREAM_HANDLE_BUFSIZE);
500 if (streamP->str_buflen < 0) {
501 streamP->str_error = errno;
502 streamP->str_buflen = 0;
505 } else if (streamP->str_buflen == 0) {
506 streamP->str_eof = 1;
511 bytesToRead = nbytes;
512 if (bytesToRead > streamP->str_buflen) {
513 bytesToRead = streamP->str_buflen;
515 memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
517 streamP->str_bufoff += bytesToRead;
518 streamP->str_buflen -= bytesToRead;
519 bytesRead += bytesToRead;
520 nbytes -= bytesToRead;
523 return (bytesRead/size);
526 /* fwrite for buffered I/O handles */
527 int stream_write(void *ptr, int size, int nitems, StreamHandle_t *streamP)
530 int rc, nbytes, bytesWritten, bytesToWrite;
532 /* Need to seek before changing direction */
533 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
534 streamP->str_direction = STREAM_DIRECTION_WRITE;
535 streamP->str_bufoff = 0;
536 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
538 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
541 nbytes = size * nitems;
545 if (streamP->str_buflen == 0) {
546 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
547 STREAM_HANDLE_BUFSIZE);
549 streamP->str_error = errno;
553 streamP->str_bufoff = 0;
554 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
557 bytesToWrite = nbytes;
558 if (bytesToWrite > streamP->str_buflen) {
559 bytesToWrite = streamP->str_buflen;
561 memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
563 streamP->str_bufoff += bytesToWrite;
564 streamP->str_buflen -= bytesToWrite;
565 bytesWritten += bytesToWrite;
566 nbytes -= bytesToWrite;
569 return (bytesWritten/size);
572 /* fseek for buffered I/O handles */
573 int stream_seek(StreamHandle_t *streamP, int offset, int whence)
578 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
579 streamP->str_bufoff > 0) {
580 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
581 streamP->str_bufoff);
583 streamP->str_error = errno;
587 streamP->str_bufoff = 0;
588 streamP->str_buflen = 0;
589 streamP->str_eof = 0;
590 streamP->str_direction = STREAM_DIRECTION_NONE;
591 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
592 streamP->str_error = errno;
598 /* fflush for buffered I/O handles */
599 int stream_flush(StreamHandle_t *streamP)
604 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
605 streamP->str_bufoff > 0) {
606 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
607 streamP->str_bufoff);
609 streamP->str_error = errno;
612 streamP->str_bufoff = 0;
613 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
619 /* Free a buffered I/O handle */
620 int stream_close(StreamHandle_t *streamP, int reallyClose)
625 assert(streamP != NULL);
626 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
627 streamP->str_bufoff > 0) {
628 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
629 streamP->str_bufoff);
635 rc = OS_CLOSE(streamP->str_fd);
640 streamP->str_fd = INVALID_FD;
643 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
650 /* Close all cached file descriptors for this inode. */
651 int ih_reallyclose(IHandle_t *ihP)
655 FdHandle_t *head, *tail;
662 assert(ihP->ih_refcnt > 0);
665 * Remove the file descriptors for this Inode from the LRU queue
666 * and put them on a temporary queue so we drop the lock before
667 * we close the files.
669 DLL_INIT_LIST(head, tail);
670 for (fdP = ihP->ih_fdhead ; fdP != NULL ; fdP = fdP->fd_ihnext) {
671 if (fdP->fd_status == FD_HANDLE_OPEN) {
672 assert(fdP->fd_ih == ihP);
673 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
674 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
676 ihP->ih_flags |= IH_REALLY_CLOSED;
681 * If we found any file descriptors in use, then we dont zero out
682 * fdhead and fdtail, since ih_reallyclose() will be called again on this
686 if ( ! (ihP->ih_flags & IH_REALLY_CLOSED) )
687 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
695 * Close the file descriptors
698 for (fdP = head ; fdP != NULL ; fdP = fdP->fd_ihnext) {
700 OS_CLOSE(fdP->fd_fd);
702 assert(fdInUseCount > 0);
704 fdP->fd_status = FD_HANDLE_AVAIL;
705 fdP->fd_fd = INVALID_FD;
711 * Append the temporary queue to the list of available descriptors
713 if (fdAvailHead == NULL) {
717 fdAvailTail->fd_next = head;
718 head->fd_prev = fdAvailTail;
726 /* Release an Inode handle. All cached file descriptors for this
727 * inode are closed when the last reference to this handle is released */
728 int ih_release(IHandle_t *ihP)
732 FdHandle_t *head, *tail;
741 * If the IH_REALLY_CLOSED flag is set then clear it here before adding
742 * the Inode handle to the available queue
744 if ( ihP->ih_flags & IH_REALLY_CLOSED )
745 ihP->ih_flags &= ~IH_REALLY_CLOSED;
748 if (ihP->ih_refcnt > 0) {
753 assert(ihP->ih_refcnt == 0);
755 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
756 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
757 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
760 * Remove the file descriptors for this Inode from the LRU queue
761 * and put them on a temporary queue so we drop the lock before
762 * we close the files.
764 DLL_INIT_LIST(head, tail);
765 for (fdP = ihP->ih_fdhead ; fdP != NULL ; fdP = fdP->fd_ihnext) {
766 assert(fdP->fd_status == FD_HANDLE_OPEN);
767 assert(fdP->fd_ih == ihP);
768 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
769 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
771 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
774 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
780 * Close the file descriptors
783 for (fdP = head ; fdP != NULL ; fdP = fdP->fd_ihnext) {
785 OS_CLOSE(fdP->fd_fd);
787 assert(fdInUseCount > 0);
789 fdP->fd_status = FD_HANDLE_AVAIL;
790 fdP->fd_fd = INVALID_FD;
796 * Append the temporary queue to the list of available descriptors
798 if (fdAvailHead == NULL) {
802 fdAvailTail->fd_next = head;
803 head->fd_prev = fdAvailTail;
806 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
812 /* Sync an inode to disk if its handle isn't NULL */
813 int ih_condsync(IHandle_t *ihP)
825 code = FDH_SYNC(fdP);
833 /*************************************************************************
834 * OS specific support routines.
835 *************************************************************************/
836 #ifndef AFS_NAMEI_ENV
837 Inode ih_icreate(IHandle_t *ih, int dev, char *part, Inode nI, int p1, int p2,
842 /* See viceinode.h */
843 if (p2 == INODESPECIAL) {
849 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
852 #endif /* AFS_NAMEI_ENV */
859 if (fstat(fd, &status)<0)
861 return status.st_size;