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)
269 /* Do we already have an open file handle for this Inode? */
270 for (fdP = ihP->ih_fdtail ; fdP != NULL ; fdP = fdP->fd_ihprev) {
271 if (fdP->fd_status != FD_HANDLE_INUSE) {
272 assert(fdP->fd_status == FD_HANDLE_OPEN);
273 fdP->fd_status = FD_HANDLE_INUSE;
274 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
277 FDH_SEEK(fdP, 0, SEEK_SET);
283 * Try to open the Inode, return NULL on error.
289 if (fd == INVALID_FD) {
295 /* fdCacheSize limits the size of the descriptor cache, but
296 * we permit the number of open files to exceed fdCacheSize.
297 * We only recycle open file descriptors when the number
298 * of open files reaches the size of the cache */
299 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
301 assert(fdP->fd_status == FD_HANDLE_OPEN);
302 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
303 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
304 fd_ihnext, fd_ihprev);
305 closeFd = fdP->fd_fd;
307 if (fdAvailHead == NULL) {
308 fdHandleAllocateChunk();
311 assert(fdP->fd_status == FD_HANDLE_AVAIL);
312 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
313 closeFd = INVALID_FD;
316 fdP->fd_status = FD_HANDLE_INUSE;
322 /* Add this handle to the Inode's list of open descriptors */
323 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
325 if (closeFd != INVALID_FD) {
337 * Return a file descriptor handle to the cache
339 int fd_close(FdHandle_t *fdP)
350 assert(fdInUseCount > 0);
351 assert(fdP->fd_status == FD_HANDLE_INUSE);
355 /* Call fd_reallyclose to really close the unused file handles if
356 * the previous attempt to close (ih_reallyclose()) all file handles
357 * failed (this is determined by checking the ihandle for the flag
358 * IH_REALLY_CLOSED) or we have too many open files.
360 if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
362 return fd_reallyclose(fdP);
365 /* Put this descriptor back into the cache */
366 fdP->fd_status = FD_HANDLE_OPEN;
367 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
369 /* If this is not the only reference to the Inode then we can decrement
370 * the reference count, otherwise we need to call ih_release.
372 if (ihP->ih_refcnt > 1) {
384 * Actually close the file descriptor handle and return it to
387 int fd_reallyclose(FdHandle_t *fdP)
398 assert(fdInUseCount > 0);
399 assert(fdP->fd_status == FD_HANDLE_INUSE);
402 closeFd = fdP->fd_fd;
404 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
405 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
407 fdP->fd_status = FD_HANDLE_AVAIL;
409 fdP->fd_fd = INVALID_FD;
411 /* All the file descriptor handles have been closed; reset
412 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
413 * has completed its job.
415 if (!ihP->ih_fdhead) {
416 ihP->ih_flags &= ~IH_REALLY_CLOSED;
425 /* If this is not the only reference to the Inode then we can decrement
426 * the reference count, otherwise we need to call ih_release. */
427 if (ihP->ih_refcnt > 1) {
438 /* Enable buffered I/O on a file descriptor */
439 StreamHandle_t *stream_fdopen(FD_t fd)
441 StreamHandle_t *streamP;
444 if (streamAvailHead == NULL) {
445 streamHandleAllocateChunk();
447 streamP = streamAvailHead;
448 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
451 streamP->str_fd = fd;
452 streamP->str_buflen = 0;
453 streamP->str_bufoff = 0;
454 streamP->str_error = 0;
455 streamP->str_eof = 0;
456 streamP->str_direction = STREAM_DIRECTION_NONE;
460 /* Open a file for buffered I/O */
461 StreamHandle_t *stream_open(const char *filename, const char *mode)
465 if (strcmp(mode, "r") == 0) {
466 fd = OS_OPEN(filename, O_RDONLY, 0);
467 } else if (strcmp(mode, "r+") == 0) {
468 fd = OS_OPEN(filename, O_RDWR, 0);
469 } else if (strcmp(mode, "w") == 0) {
470 fd = OS_OPEN(filename, O_WRONLY|O_TRUNC|O_CREAT, 0);
471 } else if (strcmp(mode, "w+") == 0) {
472 fd = OS_OPEN(filename, O_RDWR|O_TRUNC|O_CREAT, 0);
473 } else if (strcmp(mode, "a") == 0) {
474 fd = OS_OPEN(filename, O_WRONLY|O_APPEND|O_CREAT, 0);
475 } else if (strcmp(mode, "a+") == 0) {
476 fd = OS_OPEN(filename, O_RDWR|O_APPEND|O_CREAT, 0);
478 assert(FALSE); /* not implemented */
481 if (fd == INVALID_FD) {
484 return stream_fdopen(fd);
487 /* fread for buffered I/O handles */
488 int stream_read(void *ptr, int size, int nitems, StreamHandle_t *streamP)
490 int nbytes, bytesRead, bytesToRead;
493 /* Need to seek before changing direction */
494 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
495 streamP->str_direction = STREAM_DIRECTION_READ;
496 streamP->str_bufoff = 0;
497 streamP->str_buflen = 0;
499 assert(streamP->str_direction == STREAM_DIRECTION_READ);
503 nbytes = size * nitems;
505 while (nbytes > 0 && !streamP->str_eof) {
506 if (streamP->str_buflen == 0) {
507 streamP->str_bufoff = 0;
508 streamP->str_buflen = OS_READ(streamP->str_fd, streamP->str_buffer,
509 STREAM_HANDLE_BUFSIZE);
510 if (streamP->str_buflen < 0) {
511 streamP->str_error = errno;
512 streamP->str_buflen = 0;
515 } else if (streamP->str_buflen == 0) {
516 streamP->str_eof = 1;
521 bytesToRead = nbytes;
522 if (bytesToRead > streamP->str_buflen) {
523 bytesToRead = streamP->str_buflen;
525 memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
527 streamP->str_bufoff += bytesToRead;
528 streamP->str_buflen -= bytesToRead;
529 bytesRead += bytesToRead;
530 nbytes -= bytesToRead;
533 return (bytesRead/size);
536 /* fwrite for buffered I/O handles */
537 int stream_write(void *ptr, int size, int nitems, StreamHandle_t *streamP)
540 int rc, nbytes, bytesWritten, bytesToWrite;
542 /* Need to seek before changing direction */
543 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
544 streamP->str_direction = STREAM_DIRECTION_WRITE;
545 streamP->str_bufoff = 0;
546 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
548 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
551 nbytes = size * nitems;
555 if (streamP->str_buflen == 0) {
556 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
557 STREAM_HANDLE_BUFSIZE);
559 streamP->str_error = errno;
563 streamP->str_bufoff = 0;
564 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
567 bytesToWrite = nbytes;
568 if (bytesToWrite > streamP->str_buflen) {
569 bytesToWrite = streamP->str_buflen;
571 memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
573 streamP->str_bufoff += bytesToWrite;
574 streamP->str_buflen -= bytesToWrite;
575 bytesWritten += bytesToWrite;
576 nbytes -= bytesToWrite;
579 return (bytesWritten/size);
582 /* fseek for buffered I/O handles */
583 int stream_seek(StreamHandle_t *streamP, int offset, int whence)
588 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
589 streamP->str_bufoff > 0) {
590 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
591 streamP->str_bufoff);
593 streamP->str_error = errno;
597 streamP->str_bufoff = 0;
598 streamP->str_buflen = 0;
599 streamP->str_eof = 0;
600 streamP->str_direction = STREAM_DIRECTION_NONE;
601 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
602 streamP->str_error = errno;
608 /* fflush for buffered I/O handles */
609 int stream_flush(StreamHandle_t *streamP)
614 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
615 streamP->str_bufoff > 0) {
616 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
617 streamP->str_bufoff);
619 streamP->str_error = errno;
622 streamP->str_bufoff = 0;
623 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
629 /* Free a buffered I/O handle */
630 int stream_close(StreamHandle_t *streamP, int reallyClose)
635 assert(streamP != NULL);
636 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
637 streamP->str_bufoff > 0) {
638 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
639 streamP->str_bufoff);
645 rc = OS_CLOSE(streamP->str_fd);
650 streamP->str_fd = INVALID_FD;
653 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
660 /* Close all unused file descriptors associated with the inode
661 * handle. Called with IH_LOCK held. May drop and reacquire
662 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
663 * if it fails to close all file handles.
665 static int ih_fdclose(IHandle_t *ihP)
667 int closeCount, closedAll;
668 FdHandle_t *fdP, *head, *tail, *next;
670 assert(ihP->ih_refcnt > 0);
673 DLL_INIT_LIST(head, tail);
674 ihP->ih_flags &= ~IH_REALLY_CLOSED;
677 * Remove the file descriptors for this Inode from the LRU queue
678 * and the IHandle queue and put them on a temporary queue so we
679 * can drop the lock before we close the files.
681 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
682 next = fdP->fd_ihnext;
683 assert(fdP->fd_ih == ihP);
684 assert(fdP->fd_status == FD_HANDLE_OPEN ||
685 fdP->fd_status == FD_HANDLE_INUSE);
686 if (fdP->fd_status == FD_HANDLE_OPEN) {
687 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail,
688 fd_ihnext, fd_ihprev);
689 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
690 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
693 ihP->ih_flags |= IH_REALLY_CLOSED;
697 /* If the ihandle reference count is 1, we should have
698 * closed all file descriptors.
700 if (ihP->ih_refcnt == 1 || closedAll) {
702 assert(!ihP->ih_fdhead);
703 assert(!ihP->ih_fdtail);
707 return 0; /* No file descriptors closed */
713 * Close the file descriptors
716 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
717 OS_CLOSE(fdP->fd_fd);
718 fdP->fd_status = FD_HANDLE_AVAIL;
719 fdP->fd_fd = INVALID_FD;
726 assert(fdInUseCount >= closeCount);
727 fdInUseCount -= closeCount;
730 * Append the temporary queue to the list of available descriptors
732 if (fdAvailHead == NULL) {
736 fdAvailTail->fd_next = head;
737 head->fd_prev = fdAvailTail;
744 /* Close all cached file descriptors for this inode. */
745 int ih_reallyclose(IHandle_t *ihP)
752 assert(ihP->ih_refcnt > 0);
760 /* Release an Inode handle. All cached file descriptors for this
761 * inode are closed when the last reference to this handle is released
763 int ih_release(IHandle_t *ihP)
772 assert(ihP->ih_refcnt > 0);
774 if (ihP->ih_refcnt > 1) {
780 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
781 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
782 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
788 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
795 /* Sync an inode to disk if its handle isn't NULL */
796 int ih_condsync(IHandle_t *ihP)
808 code = FDH_SYNC(fdP);
816 /*************************************************************************
817 * OS specific support routines.
818 *************************************************************************/
819 #ifndef AFS_NAMEI_ENV
820 Inode ih_icreate(IHandle_t *ih, int dev, char *part, Inode nI, int p1, int p2,
825 /* See viceinode.h */
826 if (p2 == INODESPECIAL) {
832 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
835 #endif /* AFS_NAMEI_ENV */
842 if (fstat(fd, &status)<0)
844 return status.st_size;