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>
41 #include <afs/afsint.h>
43 #include <afs/afssyscalls.h>
46 #include "viceinode.h"
47 #ifdef AFS_PTHREAD_ENV
49 #else /* AFS_PTHREAD_ENV */
50 #include "afs/assert.h"
51 #endif /* AFS_PTHREAD_ENV */
54 extern afs_int32 DErrno;
56 #ifdef AFS_PTHREAD_ENV
57 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
58 pthread_mutex_t ih_glock_mutex;
59 #endif /* AFS_PTHREAD_ENV */
61 /* Linked list of available inode handles */
62 IHandle_t *ihAvailHead;
63 IHandle_t *ihAvailTail;
65 /* Linked list of available file descriptor handles */
66 FdHandle_t *fdAvailHead;
67 FdHandle_t *fdAvailTail;
69 /* Linked list of available stream descriptor handles */
70 StreamHandle_t *streamAvailHead;
71 StreamHandle_t *streamAvailTail;
73 /* LRU list for file descriptor handles */
74 FdHandle_t *fdLruHead;
75 FdHandle_t *fdLruTail;
79 /* Most of the servers use fopen/fdopen. Since the FILE structure
80 * only has eight bits for the file descriptor, the cache size
81 * has to be less than 256. The cache can be made larger as long
82 * as you are sure you don't need fopen/fdopen. */
83 int fdMaxCacheSize = 0;
86 /* Number of in use file descriptors */
89 /* Hash table for inode handles */
90 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
93 #ifdef AFS_PTHREAD_ENV
94 /* Initialize the global ihandle mutex */
97 assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
99 #endif /* AFS_PTHREAD_ENV */
101 /* Initialize the file descriptor cache */
102 void ih_Initialize() {
106 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
107 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
108 DLL_INIT_LIST(fdLruHead, fdLruTail);
109 for (i = 0 ; i < I_HANDLE_HASH_SIZE ; i++) {
110 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
112 #if defined(AFS_NT40_ENV)
113 fdMaxCacheSize = FD_MAX_CACHESIZE;
114 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
117 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
118 rlim.rlim_cur = rlim.rlim_max;
119 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
120 fdMaxCacheSize = rlim.rlim_cur-FD_HANDLE_SETASIDE;
122 /* XXX this is to avoid using up all system fd netbsd is
123 * somewhat broken and have set maximum fd for a root process
124 * to the same as system fd that is avaible, so if the
125 * fileserver uses all up process fds, all system fd will be
128 * Check for this better
132 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
133 assert(fdMaxCacheSize > 0);
135 #elif defined(AFS_HPUX_ENV)
136 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
139 fdMaxCacheSize = MAX(sysconf(_SC_OPEN_MAX)-FD_HANDLE_SETASIDE, 0);
140 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
142 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
145 /* Make the file descriptor cache as big as possible. Don't this call
146 * if the program uses fopen or fdopen. */
147 void ih_UseLargeCache() {
153 fdCacheSize = fdMaxCacheSize;
158 /* Allocate a chunk of inode handles */
159 void iHandleAllocateChunk()
164 assert(ihAvailHead == NULL);
165 ihP = (IHandle_t *)malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
167 for (i = 0 ; i < I_HANDLE_MALLOCSIZE ; i++) {
168 ihP[i].ih_refcnt = 0;
169 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
173 /* Initialize an inode handle */
174 IHandle_t *ih_init(int dev, int vid, Inode ino)
176 int ihash = IH_HASH(dev, vid, ino);
185 /* Do we already have a handle for this Inode? */
186 for (ihP = ihashTable[ihash].ihash_head ; ihP ; ihP = ihP->ih_next) {
187 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
194 /* Allocate and initialize a new Inode handle */
195 if (ihAvailHead == NULL) {
196 iHandleAllocateChunk();
199 assert(ihP->ih_refcnt == 0);
200 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
206 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
207 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
208 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
213 /* Copy an inode handle */
214 IHandle_t *ih_copy(IHandle_t *ihP)
218 assert(ihP->ih_refcnt > 0);
224 /* Allocate a chunk of file descriptor handles */
225 void fdHandleAllocateChunk()
230 assert(fdAvailHead == NULL);
231 fdP = (FdHandle_t *)malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
233 for (i = 0 ; i < FD_HANDLE_MALLOCSIZE ; i++) {
234 fdP[i].fd_status = FD_HANDLE_AVAIL;
236 fdP[i].fd_fd = INVALID_FD;
237 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
241 /* Allocate a chunk of stream handles */
242 void streamHandleAllocateChunk()
245 StreamHandle_t *streamP;
247 assert(streamAvailHead == NULL);
248 streamP = (StreamHandle_t *)
249 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
250 assert(streamP != NULL);
251 for (i = 0 ; i < STREAM_HANDLE_MALLOCSIZE ; i++) {
252 streamP[i].str_fd = INVALID_FD;
253 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
259 * Get a file descriptor handle given an Inode handle
261 FdHandle_t *ih_open(IHandle_t *ihP)
267 if (!ihP) /* XXX should log here in the fileserver */
272 /* Do we already have an open file handle for this Inode? */
273 for (fdP = ihP->ih_fdtail ; fdP != NULL ; fdP = fdP->fd_ihprev) {
274 if (fdP->fd_status != FD_HANDLE_INUSE) {
275 assert(fdP->fd_status == FD_HANDLE_OPEN);
276 fdP->fd_status = FD_HANDLE_INUSE;
277 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
280 FDH_SEEK(fdP, 0, SEEK_SET);
286 * Try to open the Inode, return NULL on error.
292 if (fd == INVALID_FD) {
298 /* fdCacheSize limits the size of the descriptor cache, but
299 * we permit the number of open files to exceed fdCacheSize.
300 * We only recycle open file descriptors when the number
301 * of open files reaches the size of the cache */
302 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
304 assert(fdP->fd_status == FD_HANDLE_OPEN);
305 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
306 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
307 fd_ihnext, fd_ihprev);
308 closeFd = fdP->fd_fd;
310 if (fdAvailHead == NULL) {
311 fdHandleAllocateChunk();
314 assert(fdP->fd_status == FD_HANDLE_AVAIL);
315 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
316 closeFd = INVALID_FD;
319 fdP->fd_status = FD_HANDLE_INUSE;
325 /* Add this handle to the Inode's list of open descriptors */
326 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
328 if (closeFd != INVALID_FD) {
340 * Return a file descriptor handle to the cache
342 int fd_close(FdHandle_t *fdP)
353 assert(fdInUseCount > 0);
354 assert(fdP->fd_status == FD_HANDLE_INUSE);
358 /* Call fd_reallyclose to really close the unused file handles if
359 * the previous attempt to close (ih_reallyclose()) all file handles
360 * failed (this is determined by checking the ihandle for the flag
361 * IH_REALLY_CLOSED) or we have too many open files.
363 if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
365 return fd_reallyclose(fdP);
368 /* Put this descriptor back into the cache */
369 fdP->fd_status = FD_HANDLE_OPEN;
370 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
372 /* If this is not the only reference to the Inode then we can decrement
373 * the reference count, otherwise we need to call ih_release.
375 if (ihP->ih_refcnt > 1) {
387 * Actually close the file descriptor handle and return it to
390 int fd_reallyclose(FdHandle_t *fdP)
401 assert(fdInUseCount > 0);
402 assert(fdP->fd_status == FD_HANDLE_INUSE);
405 closeFd = fdP->fd_fd;
407 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
408 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
410 fdP->fd_status = FD_HANDLE_AVAIL;
412 fdP->fd_fd = INVALID_FD;
414 /* All the file descriptor handles have been closed; reset
415 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
416 * has completed its job.
418 if (!ihP->ih_fdhead) {
419 ihP->ih_flags &= ~IH_REALLY_CLOSED;
428 /* If this is not the only reference to the Inode then we can decrement
429 * the reference count, otherwise we need to call ih_release. */
430 if (ihP->ih_refcnt > 1) {
441 /* Enable buffered I/O on a file descriptor */
442 StreamHandle_t *stream_fdopen(FD_t fd)
444 StreamHandle_t *streamP;
447 if (streamAvailHead == NULL) {
448 streamHandleAllocateChunk();
450 streamP = streamAvailHead;
451 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
454 streamP->str_fd = fd;
455 streamP->str_buflen = 0;
456 streamP->str_bufoff = 0;
457 streamP->str_error = 0;
458 streamP->str_eof = 0;
459 streamP->str_direction = STREAM_DIRECTION_NONE;
463 /* Open a file for buffered I/O */
464 StreamHandle_t *stream_open(const char *filename, const char *mode)
468 if (strcmp(mode, "r") == 0) {
469 fd = OS_OPEN(filename, O_RDONLY, 0);
470 } else if (strcmp(mode, "r+") == 0) {
471 fd = OS_OPEN(filename, O_RDWR, 0);
472 } else if (strcmp(mode, "w") == 0) {
473 fd = OS_OPEN(filename, O_WRONLY|O_TRUNC|O_CREAT, 0);
474 } else if (strcmp(mode, "w+") == 0) {
475 fd = OS_OPEN(filename, O_RDWR|O_TRUNC|O_CREAT, 0);
476 } else if (strcmp(mode, "a") == 0) {
477 fd = OS_OPEN(filename, O_WRONLY|O_APPEND|O_CREAT, 0);
478 } else if (strcmp(mode, "a+") == 0) {
479 fd = OS_OPEN(filename, O_RDWR|O_APPEND|O_CREAT, 0);
481 assert(FALSE); /* not implemented */
484 if (fd == INVALID_FD) {
487 return stream_fdopen(fd);
490 /* fread for buffered I/O handles */
491 int stream_read(void *ptr, int size, int nitems, StreamHandle_t *streamP)
493 int nbytes, bytesRead, bytesToRead;
496 /* Need to seek before changing direction */
497 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
498 streamP->str_direction = STREAM_DIRECTION_READ;
499 streamP->str_bufoff = 0;
500 streamP->str_buflen = 0;
502 assert(streamP->str_direction == STREAM_DIRECTION_READ);
506 nbytes = size * nitems;
508 while (nbytes > 0 && !streamP->str_eof) {
509 if (streamP->str_buflen == 0) {
510 streamP->str_bufoff = 0;
511 streamP->str_buflen = OS_READ(streamP->str_fd, streamP->str_buffer,
512 STREAM_HANDLE_BUFSIZE);
513 if (streamP->str_buflen < 0) {
514 streamP->str_error = errno;
515 streamP->str_buflen = 0;
518 } else if (streamP->str_buflen == 0) {
519 streamP->str_eof = 1;
524 bytesToRead = nbytes;
525 if (bytesToRead > streamP->str_buflen) {
526 bytesToRead = streamP->str_buflen;
528 memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
530 streamP->str_bufoff += bytesToRead;
531 streamP->str_buflen -= bytesToRead;
532 bytesRead += bytesToRead;
533 nbytes -= bytesToRead;
536 return (bytesRead/size);
539 /* fwrite for buffered I/O handles */
540 int stream_write(void *ptr, int size, int nitems, StreamHandle_t *streamP)
543 int rc, nbytes, bytesWritten, bytesToWrite;
545 /* Need to seek before changing direction */
546 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
547 streamP->str_direction = STREAM_DIRECTION_WRITE;
548 streamP->str_bufoff = 0;
549 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
551 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
554 nbytes = size * nitems;
558 if (streamP->str_buflen == 0) {
559 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
560 STREAM_HANDLE_BUFSIZE);
562 streamP->str_error = errno;
566 streamP->str_bufoff = 0;
567 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
570 bytesToWrite = nbytes;
571 if (bytesToWrite > streamP->str_buflen) {
572 bytesToWrite = streamP->str_buflen;
574 memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
576 streamP->str_bufoff += bytesToWrite;
577 streamP->str_buflen -= bytesToWrite;
578 bytesWritten += bytesToWrite;
579 nbytes -= bytesToWrite;
582 return (bytesWritten/size);
585 /* fseek for buffered I/O handles */
586 int stream_seek(StreamHandle_t *streamP, int offset, int whence)
591 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
592 streamP->str_bufoff > 0) {
593 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
594 streamP->str_bufoff);
596 streamP->str_error = errno;
600 streamP->str_bufoff = 0;
601 streamP->str_buflen = 0;
602 streamP->str_eof = 0;
603 streamP->str_direction = STREAM_DIRECTION_NONE;
604 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
605 streamP->str_error = errno;
611 /* fflush for buffered I/O handles */
612 int stream_flush(StreamHandle_t *streamP)
617 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
618 streamP->str_bufoff > 0) {
619 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
620 streamP->str_bufoff);
622 streamP->str_error = errno;
625 streamP->str_bufoff = 0;
626 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
632 /* Free a buffered I/O handle */
633 int stream_close(StreamHandle_t *streamP, int reallyClose)
638 assert(streamP != NULL);
639 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
640 streamP->str_bufoff > 0) {
641 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
642 streamP->str_bufoff);
648 rc = OS_CLOSE(streamP->str_fd);
653 streamP->str_fd = INVALID_FD;
656 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
663 /* Close all unused file descriptors associated with the inode
664 * handle. Called with IH_LOCK held. May drop and reacquire
665 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
666 * if it fails to close all file handles.
668 static int ih_fdclose(IHandle_t *ihP)
670 int closeCount, closedAll;
671 FdHandle_t *fdP, *head, *tail, *next;
673 assert(ihP->ih_refcnt > 0);
676 DLL_INIT_LIST(head, tail);
677 ihP->ih_flags &= ~IH_REALLY_CLOSED;
680 * Remove the file descriptors for this Inode from the LRU queue
681 * and the IHandle queue and put them on a temporary queue so we
682 * can drop the lock before we close the files.
684 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
685 next = fdP->fd_ihnext;
686 assert(fdP->fd_ih == ihP);
687 assert(fdP->fd_status == FD_HANDLE_OPEN ||
688 fdP->fd_status == FD_HANDLE_INUSE);
689 if (fdP->fd_status == FD_HANDLE_OPEN) {
690 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail,
691 fd_ihnext, fd_ihprev);
692 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
693 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
696 ihP->ih_flags |= IH_REALLY_CLOSED;
700 /* If the ihandle reference count is 1, we should have
701 * closed all file descriptors.
703 if (ihP->ih_refcnt == 1 || closedAll) {
705 assert(!ihP->ih_fdhead);
706 assert(!ihP->ih_fdtail);
710 return 0; /* No file descriptors closed */
716 * Close the file descriptors
719 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
720 OS_CLOSE(fdP->fd_fd);
721 fdP->fd_status = FD_HANDLE_AVAIL;
722 fdP->fd_fd = INVALID_FD;
729 assert(fdInUseCount >= closeCount);
730 fdInUseCount -= closeCount;
733 * Append the temporary queue to the list of available descriptors
735 if (fdAvailHead == NULL) {
739 fdAvailTail->fd_next = head;
740 head->fd_prev = fdAvailTail;
747 /* Close all cached file descriptors for this inode. */
748 int ih_reallyclose(IHandle_t *ihP)
755 assert(ihP->ih_refcnt > 0);
763 /* Release an Inode handle. All cached file descriptors for this
764 * inode are closed when the last reference to this handle is released
766 int ih_release(IHandle_t *ihP)
775 assert(ihP->ih_refcnt > 0);
777 if (ihP->ih_refcnt > 1) {
783 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
784 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
785 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
791 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
798 /* Sync an inode to disk if its handle isn't NULL */
799 int ih_condsync(IHandle_t *ihP)
811 code = FDH_SYNC(fdP);
819 /*************************************************************************
820 * OS specific support routines.
821 *************************************************************************/
822 #ifndef AFS_NAMEI_ENV
823 Inode ih_icreate(IHandle_t *ih, int dev, char *part, Inode nI, int p1, int p2,
828 /* See viceinode.h */
829 if (p2 == INODESPECIAL) {
835 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
838 #endif /* AFS_NAMEI_ENV */
845 if (fstat(fd, &status)<0)
847 return status.st_size;