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>
28 #if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
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 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
100 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
101 DLL_INIT_LIST(fdLruHead, fdLruTail);
102 for (i = 0 ; i < I_HANDLE_HASH_SIZE ; i++) {
103 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
105 #if defined(AFS_NT40_ENV)
106 fdMaxCacheSize = FD_MAX_CACHESIZE;
107 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
110 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
111 rlim.rlim_cur = rlim.rlim_max;
112 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
113 fdMaxCacheSize = rlim.rlim_cur-FD_HANDLE_SETASIDE;
115 /* XXX this is to avoid using up all system fd netbsd is
116 * somewhat broken and have set maximum fd for a root process
117 * to the same as system fd that is avaible, so if the
118 * fileserver uses all up process fds, all system fd will be
121 * Check for this better
125 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
126 assert(fdMaxCacheSize > 0);
128 #elif defined(AFS_HPUX_ENV)
129 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
132 fdMaxCacheSize = MAX(sysconf(_SC_OPEN_MAX)-FD_HANDLE_SETASIDE, 0);
133 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
135 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
138 /* Make the file descriptor cache as big as possible. Don't this call
139 * if the program uses fopen or fdopen. */
140 void ih_UseLargeCache() {
146 fdCacheSize = fdMaxCacheSize;
151 /* Allocate a chunk of inode handles */
152 void iHandleAllocateChunk()
157 assert(ihAvailHead == NULL);
158 ihP = (IHandle_t *)malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
160 for (i = 0 ; i < I_HANDLE_MALLOCSIZE ; i++) {
161 ihP[i].ih_refcnt = 0;
162 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
166 /* Initialize an inode handle */
167 IHandle_t *ih_init(int dev, int vid, Inode ino)
169 int ihash = IH_HASH(dev, vid, ino);
178 /* Do we already have a handle for this Inode? */
179 for (ihP = ihashTable[ihash].ihash_head ; ihP ; ihP = ihP->ih_next) {
180 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
187 /* Allocate and initialize a new Inode handle */
188 if (ihAvailHead == NULL) {
189 iHandleAllocateChunk();
192 assert(ihP->ih_refcnt == 0);
193 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
199 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
200 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
201 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
206 /* Copy an inode handle */
207 IHandle_t *ih_copy(IHandle_t *ihP)
211 assert(ihP->ih_refcnt > 0);
217 /* Allocate a chunk of file descriptor handles */
218 void fdHandleAllocateChunk()
223 assert(fdAvailHead == NULL);
224 fdP = (FdHandle_t *)malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
226 for (i = 0 ; i < FD_HANDLE_MALLOCSIZE ; i++) {
227 fdP[i].fd_status = FD_HANDLE_AVAIL;
229 fdP[i].fd_fd = INVALID_FD;
230 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
234 /* Allocate a chunk of stream handles */
235 void streamHandleAllocateChunk()
238 StreamHandle_t *streamP;
240 assert(streamAvailHead == NULL);
241 streamP = (StreamHandle_t *)
242 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
243 assert(streamP != NULL);
244 for (i = 0 ; i < STREAM_HANDLE_MALLOCSIZE ; i++) {
245 streamP[i].str_fd = INVALID_FD;
246 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
252 * Get a file descriptor handle given an Inode handle
254 FdHandle_t *ih_open(IHandle_t *ihP)
266 /* Do we already have an open file handle for this Inode? */
267 for (fdP = ihP->ih_fdtail ; fdP != NULL ; fdP = fdP->fd_ihprev) {
268 if (fdP->fd_status != FD_HANDLE_INUSE) {
269 assert(fdP->fd_status == FD_HANDLE_OPEN);
270 fdP->fd_status = FD_HANDLE_INUSE;
271 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
274 FDH_SEEK(fdP, 0, SEEK_SET);
280 * Try to open the Inode, return NULL on error.
286 if (fd == INVALID_FD) {
292 /* fdCacheSize limits the size of the descriptor cache, but
293 * we permit the number of open files to exceed fdCacheSize.
294 * We only recycle open file descriptors when the number
295 * of open files reaches the size of the cache */
296 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
298 assert(fdP->fd_status == FD_HANDLE_OPEN);
299 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
300 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
301 fd_ihnext, fd_ihprev);
302 closeFd = fdP->fd_fd;
304 if (fdAvailHead == NULL) {
305 fdHandleAllocateChunk();
308 assert(fdP->fd_status == FD_HANDLE_AVAIL);
309 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
310 closeFd = INVALID_FD;
313 fdP->fd_status = FD_HANDLE_INUSE;
316 /* Add this handle to the Inode's list of open descriptors */
317 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
319 if (closeFd != INVALID_FD) {
332 * Return a file descriptor handle to the cache
334 int fd_close(FdHandle_t *fdP)
344 assert(fdInUseCount > 0);
345 assert(fdP->fd_status == FD_HANDLE_INUSE);
349 /* If a previous attempt to close ( ih_reallyclose() )
350 * all fd handles failed, then the IH_REALLY_CLOSED flag is set in
351 * the Inode handle so we call fd_reallyclose
354 if ( ihP->ih_flags & IH_REALLY_CLOSED ) {
356 return (fd_reallyclose(fdP));
359 /* If we have too many open files then close the descriptor. If we
360 * hold the last reference to the Inode handle then wait and let
361 * ih_release do the work. */
362 if (fdInUseCount > fdCacheSize && ihP->ih_refcnt > 1) {
363 assert(fdInUseCount > 0);
364 closeFd = fdP->fd_fd;
365 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
366 fd_ihnext, fd_ihprev);
367 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
368 fdP->fd_status = FD_HANDLE_AVAIL;
370 fdP->fd_fd = INVALID_FD;
380 /* Put this descriptor back into the cache */
381 fdP->fd_status = FD_HANDLE_OPEN;
382 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
384 /* If this is not the only reference to the Inode then we can decrement
385 * the reference count, otherwise we need to call ih_release. */
386 if (ihP->ih_refcnt > 1) {
398 * Return a file descriptor handle to the cache
400 int fd_reallyclose(FdHandle_t *fdP)
410 assert(fdInUseCount > 0);
411 assert(fdP->fd_status == FD_HANDLE_INUSE);
414 closeFd = fdP->fd_fd;
416 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
417 fd_ihnext, fd_ihprev);
418 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
419 fdP->fd_status = FD_HANDLE_AVAIL;
421 fdP->fd_fd = INVALID_FD;
427 /* If this is not the only reference to the Inode then we can decrement
428 * the reference count, otherwise we need to call ih_release. */
429 if (ihP->ih_refcnt > 1) {
439 /* Enable buffered I/O on a file descriptor */
440 StreamHandle_t *stream_fdopen(FD_t fd)
442 StreamHandle_t *streamP;
445 if (streamAvailHead == NULL) {
446 streamHandleAllocateChunk();
448 streamP = streamAvailHead;
449 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
452 streamP->str_fd = fd;
453 streamP->str_buflen = 0;
454 streamP->str_bufoff = 0;
455 streamP->str_error = 0;
456 streamP->str_eof = 0;
457 streamP->str_direction = STREAM_DIRECTION_NONE;
461 /* Open a file for buffered I/O */
462 StreamHandle_t *stream_open(const char *filename, const char *mode)
466 if (strcmp(mode, "r") == 0) {
467 fd = OS_OPEN(filename, O_RDONLY, 0);
468 } else if (strcmp(mode, "r+") == 0) {
469 fd = OS_OPEN(filename, O_RDWR, 0);
470 } else if (strcmp(mode, "w") == 0) {
471 fd = OS_OPEN(filename, O_WRONLY|O_TRUNC|O_CREAT, 0);
472 } else if (strcmp(mode, "w+") == 0) {
473 fd = OS_OPEN(filename, O_RDWR|O_TRUNC|O_CREAT, 0);
474 } else if (strcmp(mode, "a") == 0) {
475 fd = OS_OPEN(filename, O_WRONLY|O_APPEND|O_CREAT, 0);
476 } else if (strcmp(mode, "a+") == 0) {
477 fd = OS_OPEN(filename, O_RDWR|O_APPEND|O_CREAT, 0);
479 assert(FALSE); /* not implemented */
482 if (fd == INVALID_FD) {
485 return stream_fdopen(fd);
488 /* fread for buffered I/O handles */
489 int stream_read(void *ptr, int size, int nitems, StreamHandle_t *streamP)
491 int nbytes, bytesRead, bytesToRead;
494 /* Need to seek before changing direction */
495 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
496 streamP->str_direction = STREAM_DIRECTION_READ;
497 streamP->str_bufoff = 0;
498 streamP->str_buflen = 0;
500 assert(streamP->str_direction == STREAM_DIRECTION_READ);
504 nbytes = size * nitems;
506 while (nbytes > 0 && !streamP->str_eof) {
507 if (streamP->str_buflen == 0) {
508 streamP->str_bufoff = 0;
509 streamP->str_buflen = OS_READ(streamP->str_fd, streamP->str_buffer,
510 STREAM_HANDLE_BUFSIZE);
511 if (streamP->str_buflen < 0) {
512 streamP->str_error = errno;
513 streamP->str_buflen = 0;
516 } else if (streamP->str_buflen == 0) {
517 streamP->str_eof = 1;
522 bytesToRead = nbytes;
523 if (bytesToRead > streamP->str_buflen) {
524 bytesToRead = streamP->str_buflen;
526 memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
528 streamP->str_bufoff += bytesToRead;
529 streamP->str_buflen -= bytesToRead;
530 bytesRead += bytesToRead;
531 nbytes -= bytesToRead;
534 return (bytesRead/size);
537 /* fwrite for buffered I/O handles */
538 int stream_write(void *ptr, int size, int nitems, StreamHandle_t *streamP)
541 int rc, nbytes, bytesWritten, bytesToWrite;
543 /* Need to seek before changing direction */
544 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
545 streamP->str_direction = STREAM_DIRECTION_WRITE;
546 streamP->str_bufoff = 0;
547 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
549 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
552 nbytes = size * nitems;
556 if (streamP->str_buflen == 0) {
557 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
558 STREAM_HANDLE_BUFSIZE);
560 streamP->str_error = errno;
564 streamP->str_bufoff = 0;
565 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
568 bytesToWrite = nbytes;
569 if (bytesToWrite > streamP->str_buflen) {
570 bytesToWrite = streamP->str_buflen;
572 memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
574 streamP->str_bufoff += bytesToWrite;
575 streamP->str_buflen -= bytesToWrite;
576 bytesWritten += bytesToWrite;
577 nbytes -= bytesToWrite;
580 return (bytesWritten/size);
583 /* fseek for buffered I/O handles */
584 int stream_seek(StreamHandle_t *streamP, int offset, int whence)
589 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
590 streamP->str_bufoff > 0) {
591 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
592 streamP->str_bufoff);
594 streamP->str_error = errno;
598 streamP->str_bufoff = 0;
599 streamP->str_buflen = 0;
600 streamP->str_eof = 0;
601 streamP->str_direction = STREAM_DIRECTION_NONE;
602 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
603 streamP->str_error = errno;
609 /* fflush for buffered I/O handles */
610 int stream_flush(StreamHandle_t *streamP)
615 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
616 streamP->str_bufoff > 0) {
617 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
618 streamP->str_bufoff);
620 streamP->str_error = errno;
623 streamP->str_bufoff = 0;
624 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
630 /* Free a buffered I/O handle */
631 int stream_close(StreamHandle_t *streamP, int reallyClose)
636 assert(streamP != NULL);
637 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
638 streamP->str_bufoff > 0) {
639 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
640 streamP->str_bufoff);
646 rc = OS_CLOSE(streamP->str_fd);
651 streamP->str_fd = INVALID_FD;
654 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
661 /* Close all cached file descriptors for this inode. */
662 int ih_reallyclose(IHandle_t *ihP)
666 FdHandle_t *head, *tail;
673 assert(ihP->ih_refcnt > 0);
676 * Remove the file descriptors for this Inode from the LRU queue
677 * and put them on a temporary queue so we drop the lock before
678 * we close the files.
680 DLL_INIT_LIST(head, tail);
681 for (fdP = ihP->ih_fdhead ; fdP != NULL ; fdP = fdP->fd_ihnext) {
682 if (fdP->fd_status == FD_HANDLE_OPEN) {
683 assert(fdP->fd_ih == ihP);
684 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
685 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
687 ihP->ih_flags |= IH_REALLY_CLOSED;
692 * If we found any file descriptors in use, then we dont zero out
693 * fdhead and fdtail, since ih_reallyclose() will be called again on this
697 if ( ! (ihP->ih_flags & IH_REALLY_CLOSED) )
698 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
706 * Close the file descriptors
709 for (fdP = head ; fdP != NULL ; fdP = fdP->fd_ihnext) {
711 OS_CLOSE(fdP->fd_fd);
713 assert(fdInUseCount > 0);
715 fdP->fd_status = FD_HANDLE_AVAIL;
716 fdP->fd_fd = INVALID_FD;
722 * Append the temporary queue to the list of available descriptors
724 if (fdAvailHead == NULL) {
728 fdAvailTail->fd_next = head;
729 head->fd_prev = fdAvailTail;
737 /* Release an Inode handle. All cached file descriptors for this
738 * inode are closed when the last reference to this handle is released */
739 int ih_release(IHandle_t *ihP)
743 FdHandle_t *head, *tail;
752 * If the IH_REALLY_CLOSED flag is set then clear it here before adding
753 * the Inode handle to the available queue
755 if ( ihP->ih_flags & IH_REALLY_CLOSED )
756 ihP->ih_flags &= ~IH_REALLY_CLOSED;
759 if (ihP->ih_refcnt > 0) {
764 assert(ihP->ih_refcnt == 0);
766 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
767 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
768 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
771 * Remove the file descriptors for this Inode from the LRU queue
772 * and put them on a temporary queue so we drop the lock before
773 * we close the files.
775 DLL_INIT_LIST(head, tail);
776 for (fdP = ihP->ih_fdhead ; fdP != NULL ; fdP = fdP->fd_ihnext) {
777 assert(fdP->fd_status == FD_HANDLE_OPEN);
778 assert(fdP->fd_ih == ihP);
779 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
780 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
782 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
785 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
791 * Close the file descriptors
794 for (fdP = head ; fdP != NULL ; fdP = fdP->fd_ihnext) {
796 OS_CLOSE(fdP->fd_fd);
798 assert(fdInUseCount > 0);
800 fdP->fd_status = FD_HANDLE_AVAIL;
801 fdP->fd_fd = INVALID_FD;
807 * Append the temporary queue to the list of available descriptors
809 if (fdAvailHead == NULL) {
813 fdAvailTail->fd_next = head;
814 head->fd_prev = fdAvailTail;
817 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
823 /* Sync an inode to disk if its handle isn't NULL */
824 int ih_condsync(IHandle_t *ihP)
836 code = FDH_SYNC(fdP);
844 /*************************************************************************
845 * OS specific support routines.
846 *************************************************************************/
847 #ifndef AFS_NAMEI_ENV
848 Inode ih_icreate(IHandle_t *ih, int dev, char *part, Inode nI, int p1, int p2,
853 /* See viceinode.h */
854 if (p2 == INODESPECIAL) {
860 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
863 #endif /* AFS_NAMEI_ENV */
870 if (fstat(fd, &status)<0)
872 return status.st_size;