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 #include <sys/types.h>
30 #if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
31 #include <sys/fcntl.h>
32 #include <sys/resource.h>
37 #include <afs/afsint.h>
39 #include <afs/afssyscalls.h>
42 #include "viceinode.h"
43 #ifdef AFS_PTHREAD_ENV
45 #else /* AFS_PTHREAD_ENV */
46 #include "afs/assert.h"
47 #endif /* AFS_PTHREAD_ENV */
52 #define afs_stat stat64
53 #define afs_fstat fstat64
54 #else /* !O_LARGEFILE */
56 #define afs_fstat fstat
57 #endif /* !O_LARGEFILE */
58 #endif /* AFS_NT40_ENV */
60 #ifdef AFS_PTHREAD_ENV
61 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
62 pthread_mutex_t ih_glock_mutex;
63 #endif /* AFS_PTHREAD_ENV */
65 /* Linked list of available inode handles */
66 IHandle_t *ihAvailHead;
67 IHandle_t *ihAvailTail;
69 /* Linked list of available file descriptor handles */
70 FdHandle_t *fdAvailHead;
71 FdHandle_t *fdAvailTail;
73 /* Linked list of available stream descriptor handles */
74 StreamHandle_t *streamAvailHead;
75 StreamHandle_t *streamAvailTail;
77 /* LRU list for file descriptor handles */
78 FdHandle_t *fdLruHead;
79 FdHandle_t *fdLruTail;
83 /* Most of the servers use fopen/fdopen. Since the FILE structure
84 * only has eight bits for the file descriptor, the cache size
85 * has to be less than 256. The cache can be made larger as long
86 * as you are sure you don't need fopen/fdopen. */
87 int fdMaxCacheSize = 0;
90 /* Number of in use file descriptors */
93 /* Hash table for inode handles */
94 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
96 void *ih_sync_thread(void *);
98 #ifdef AFS_PTHREAD_ENV
99 /* Initialize the global ihandle mutex */
103 assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
105 #endif /* AFS_PTHREAD_ENV */
107 /* Initialize the file descriptor cache */
114 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
115 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
116 DLL_INIT_LIST(fdLruHead, fdLruTail);
117 for (i = 0; i < I_HANDLE_HASH_SIZE; i++) {
118 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
120 #if defined(AFS_NT40_ENV)
121 fdMaxCacheSize = FD_MAX_CACHESIZE;
122 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
125 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
126 rlim.rlim_cur = rlim.rlim_max;
127 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
128 fdMaxCacheSize = rlim.rlim_cur - FD_HANDLE_SETASIDE;
130 /* XXX this is to avoid using up all system fd netbsd is
131 * somewhat broken and have set maximum fd for a root process
132 * to the same as system fd that is avaible, so if the
133 * fileserver uses all up process fds, all system fd will be
136 * Check for this better
140 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
141 assert(fdMaxCacheSize > 0);
143 #elif defined(AFS_HPUX_ENV)
144 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
148 long fdMax = MAX(sysconf(_SC_OPEN_MAX) - FD_HANDLE_SETASIDE, 0);
149 fdMaxCacheSize = (int)MIN(fdMax, FD_MAX_CACHESIZE);
152 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
155 #ifdef AFS_PTHREAD_ENV
157 pthread_attr_t tattr;
159 pthread_attr_init(&tattr);
160 pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_DETACHED);
162 pthread_create(&syncer, &tattr, ih_sync_thread, NULL);
163 #else /* AFS_PTHREAD_ENV */
165 LWP_CreateProcess(ih_sync_thread, 16*1024, LWP_MAX_PRIORITY - 2,
166 NULL, "ih_syncer", &syncer);
167 #endif /* AFS_PTHREAD_ENV */
172 /* Make the file descriptor cache as big as possible. Don't this call
173 * if the program uses fopen or fdopen. */
175 ih_UseLargeCache(void)
181 fdCacheSize = fdMaxCacheSize;
186 /* Allocate a chunk of inode handles */
188 iHandleAllocateChunk(void)
193 assert(ihAvailHead == NULL);
194 ihP = (IHandle_t *) malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
196 for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
197 ihP[i].ih_refcnt = 0;
198 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
202 /* Initialize an inode handle */
204 ih_init(int dev, int vid, Inode ino)
206 int ihash = IH_HASH(dev, vid, ino);
214 /* Do we already have a handle for this Inode? */
215 for (ihP = ihashTable[ihash].ihash_head; ihP; ihP = ihP->ih_next) {
216 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
223 /* Allocate and initialize a new Inode handle */
224 if (ihAvailHead == NULL) {
225 iHandleAllocateChunk();
228 assert(ihP->ih_refcnt == 0);
229 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
235 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
236 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
237 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
242 /* Copy an inode handle */
244 ih_copy(IHandle_t * ihP)
248 assert(ihP->ih_refcnt > 0);
254 /* Allocate a chunk of file descriptor handles */
256 fdHandleAllocateChunk(void)
261 assert(fdAvailHead == NULL);
262 fdP = (FdHandle_t *) malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
264 for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
265 fdP[i].fd_status = FD_HANDLE_AVAIL;
267 fdP[i].fd_fd = INVALID_FD;
268 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
272 /* Allocate a chunk of stream handles */
274 streamHandleAllocateChunk(void)
277 StreamHandle_t *streamP;
279 assert(streamAvailHead == NULL);
280 streamP = (StreamHandle_t *)
281 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
282 assert(streamP != NULL);
283 for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
284 streamP[i].str_fd = INVALID_FD;
285 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
291 * Get a file descriptor handle given an Inode handle
294 ih_open(IHandle_t * ihP)
300 if (!ihP) /* XXX should log here in the fileserver */
305 /* Do we already have an open file handle for this Inode? */
306 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
307 if (fdP->fd_status != FD_HANDLE_INUSE) {
308 assert(fdP->fd_status == FD_HANDLE_OPEN);
309 fdP->fd_status = FD_HANDLE_INUSE;
310 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
313 (void)FDH_SEEK(fdP, 0, SEEK_SET);
319 * Try to open the Inode, return NULL on error.
326 if (fd == INVALID_FD && (errno != EMFILE || fdLruHead == NULL) ) {
332 /* fdCacheSize limits the size of the descriptor cache, but
333 * we permit the number of open files to exceed fdCacheSize.
334 * We only recycle open file descriptors when the number
335 * of open files reaches the size of the cache */
336 if ((fdInUseCount > fdCacheSize || fd == INVALID_FD) && fdLruHead != NULL) {
338 assert(fdP->fd_status == FD_HANDLE_OPEN);
339 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
340 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
341 fd_ihnext, fd_ihprev);
342 closeFd = fdP->fd_fd;
343 if (fd == INVALID_FD) {
344 fdCacheSize--; /* reduce in order to not run into here too often */
345 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
346 fdP->fd_status = FD_HANDLE_AVAIL;
348 fdP->fd_fd = INVALID_FD;
354 if (fdAvailHead == NULL) {
355 fdHandleAllocateChunk();
358 assert(fdP->fd_status == FD_HANDLE_AVAIL);
359 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
360 closeFd = INVALID_FD;
363 fdP->fd_status = FD_HANDLE_INUSE;
369 /* Add this handle to the Inode's list of open descriptors */
370 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
373 if (closeFd != INVALID_FD) {
385 * Return a file descriptor handle to the cache
388 fd_close(FdHandle_t * fdP)
397 assert(fdInUseCount > 0);
398 assert(fdP->fd_status == FD_HANDLE_INUSE);
402 /* Call fd_reallyclose to really close the unused file handles if
403 * the previous attempt to close (ih_reallyclose()) all file handles
404 * failed (this is determined by checking the ihandle for the flag
405 * IH_REALLY_CLOSED) or we have too many open files.
407 if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
409 return fd_reallyclose(fdP);
412 /* Put this descriptor back into the cache */
413 fdP->fd_status = FD_HANDLE_OPEN;
414 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
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.
419 if (ihP->ih_refcnt > 1) {
431 * Actually close the file descriptor handle and return it to
435 fd_reallyclose(FdHandle_t * fdP)
445 assert(fdInUseCount > 0);
446 assert(fdP->fd_status == FD_HANDLE_INUSE);
449 closeFd = fdP->fd_fd;
451 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
452 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
454 fdP->fd_status = FD_HANDLE_AVAIL;
456 fdP->fd_fd = INVALID_FD;
458 /* All the file descriptor handles have been closed; reset
459 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
460 * has completed its job.
462 if (!ihP->ih_fdhead) {
463 ihP->ih_flags &= ~IH_REALLY_CLOSED;
471 /* If this is not the only reference to the Inode then we can decrement
472 * the reference count, otherwise we need to call ih_release. */
473 if (ihP->ih_refcnt > 1) {
484 /* Enable buffered I/O on a file descriptor */
486 stream_fdopen(FD_t fd)
488 StreamHandle_t *streamP;
491 if (streamAvailHead == NULL) {
492 streamHandleAllocateChunk();
494 streamP = streamAvailHead;
495 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
497 streamP->str_fd = fd;
498 streamP->str_buflen = 0;
499 streamP->str_bufoff = 0;
500 streamP->str_error = 0;
501 streamP->str_eof = 0;
502 streamP->str_direction = STREAM_DIRECTION_NONE;
506 /* Open a file for buffered I/O */
508 stream_open(const char *filename, const char *mode)
510 FD_t fd = INVALID_FD;
512 if (strcmp(mode, "r") == 0) {
513 fd = OS_OPEN(filename, O_RDONLY, 0);
514 } else if (strcmp(mode, "r+") == 0) {
515 fd = OS_OPEN(filename, O_RDWR, 0);
516 } else if (strcmp(mode, "w") == 0) {
517 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
518 } else if (strcmp(mode, "w+") == 0) {
519 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
520 } else if (strcmp(mode, "a") == 0) {
521 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
522 } else if (strcmp(mode, "a+") == 0) {
523 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
525 assert(FALSE); /* not implemented */
528 if (fd == INVALID_FD) {
531 return stream_fdopen(fd);
534 /* fread for buffered I/O handles */
536 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
537 StreamHandle_t * streamP)
539 afs_fsize_t nbytes, bytesRead, bytesToRead;
542 /* Need to seek before changing direction */
543 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
544 streamP->str_direction = STREAM_DIRECTION_READ;
545 streamP->str_bufoff = 0;
546 streamP->str_buflen = 0;
548 assert(streamP->str_direction == STREAM_DIRECTION_READ);
552 nbytes = size * nitems;
554 while (nbytes > 0 && !streamP->str_eof) {
555 if (streamP->str_buflen == 0) {
556 streamP->str_bufoff = 0;
557 streamP->str_buflen =
558 OS_READ(streamP->str_fd, streamP->str_buffer,
559 STREAM_HANDLE_BUFSIZE);
560 if (streamP->str_buflen < 0) {
561 streamP->str_error = errno;
562 streamP->str_buflen = 0;
565 } else if (streamP->str_buflen == 0) {
566 streamP->str_eof = 1;
571 bytesToRead = nbytes;
572 if (bytesToRead > streamP->str_buflen) {
573 bytesToRead = streamP->str_buflen;
575 memcpy(p, streamP->str_buffer + streamP->str_bufoff, bytesToRead);
577 streamP->str_bufoff += bytesToRead;
578 streamP->str_buflen -= bytesToRead;
579 bytesRead += bytesToRead;
580 nbytes -= bytesToRead;
583 return (bytesRead / size);
586 /* fwrite for buffered I/O handles */
588 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
589 StreamHandle_t * streamP)
593 afs_fsize_t nbytes, bytesWritten, bytesToWrite;
595 /* Need to seek before changing direction */
596 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
597 streamP->str_direction = STREAM_DIRECTION_WRITE;
598 streamP->str_bufoff = 0;
599 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
601 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
604 nbytes = size * nitems;
608 if (streamP->str_buflen == 0) {
609 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
610 STREAM_HANDLE_BUFSIZE);
612 streamP->str_error = errno;
616 streamP->str_bufoff = 0;
617 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
620 bytesToWrite = nbytes;
621 if (bytesToWrite > streamP->str_buflen) {
622 bytesToWrite = streamP->str_buflen;
624 memcpy(streamP->str_buffer + streamP->str_bufoff, p, bytesToWrite);
626 streamP->str_bufoff += bytesToWrite;
627 streamP->str_buflen -= bytesToWrite;
628 bytesWritten += bytesToWrite;
629 nbytes -= bytesToWrite;
632 return (bytesWritten / size);
635 /* fseek for buffered I/O handles */
637 stream_seek(StreamHandle_t * streamP, afs_foff_t offset, int whence)
642 if (streamP->str_direction == STREAM_DIRECTION_WRITE
643 && streamP->str_bufoff > 0) {
644 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
645 streamP->str_bufoff);
647 streamP->str_error = errno;
651 streamP->str_bufoff = 0;
652 streamP->str_buflen = 0;
653 streamP->str_eof = 0;
654 streamP->str_direction = STREAM_DIRECTION_NONE;
655 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
656 streamP->str_error = errno;
662 /* fflush for buffered I/O handles */
664 stream_flush(StreamHandle_t * streamP)
669 if (streamP->str_direction == STREAM_DIRECTION_WRITE
670 && streamP->str_bufoff > 0) {
671 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
672 streamP->str_bufoff);
674 streamP->str_error = errno;
677 streamP->str_bufoff = 0;
678 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
684 /* Free a buffered I/O handle */
686 stream_close(StreamHandle_t * streamP, int reallyClose)
691 assert(streamP != NULL);
692 if (streamP->str_direction == STREAM_DIRECTION_WRITE
693 && streamP->str_bufoff > 0) {
694 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
695 streamP->str_bufoff);
701 rc = OS_CLOSE(streamP->str_fd);
706 streamP->str_fd = INVALID_FD;
709 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
715 /* Close all unused file descriptors associated with the inode
716 * handle. Called with IH_LOCK held. May drop and reacquire
717 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
718 * if it fails to close all file handles.
721 ih_fdclose(IHandle_t * ihP)
723 int closeCount, closedAll;
724 FdHandle_t *fdP, *head, *tail, *next;
726 assert(ihP->ih_refcnt > 0);
729 DLL_INIT_LIST(head, tail);
730 ihP->ih_flags &= ~IH_REALLY_CLOSED;
733 * Remove the file descriptors for this Inode from the LRU queue
734 * and the IHandle queue and put them on a temporary queue so we
735 * can drop the lock before we close the files.
737 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
738 next = fdP->fd_ihnext;
739 assert(fdP->fd_ih == ihP);
740 assert(fdP->fd_status == FD_HANDLE_OPEN
741 || fdP->fd_status == FD_HANDLE_INUSE);
742 if (fdP->fd_status == FD_HANDLE_OPEN) {
743 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
745 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
746 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
749 ihP->ih_flags |= IH_REALLY_CLOSED;
753 /* If the ihandle reference count is 1, we should have
754 * closed all file descriptors.
756 if (ihP->ih_refcnt == 1 || closedAll) {
758 assert(!ihP->ih_fdhead);
759 assert(!ihP->ih_fdtail);
763 return 0; /* No file descriptors closed */
768 * Close the file descriptors
771 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
772 OS_CLOSE(fdP->fd_fd);
773 fdP->fd_status = FD_HANDLE_AVAIL;
774 fdP->fd_fd = INVALID_FD;
780 assert(fdInUseCount >= closeCount);
781 fdInUseCount -= closeCount;
784 * Append the temporary queue to the list of available descriptors
786 if (fdAvailHead == NULL) {
790 fdAvailTail->fd_next = head;
791 head->fd_prev = fdAvailTail;
798 /* Close all cached file descriptors for this inode. */
800 ih_reallyclose(IHandle_t * ihP)
806 assert(ihP->ih_refcnt > 0);
813 /* Release an Inode handle. All cached file descriptors for this
814 * inode are closed when the last reference to this handle is released
817 ih_release(IHandle_t * ihP)
825 assert(ihP->ih_refcnt > 0);
827 if (ihP->ih_refcnt > 1) {
833 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
834 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
835 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
841 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
847 /* Sync an inode to disk if its handle isn't NULL */
849 ih_condsync(IHandle_t * ihP)
861 code = FDH_SYNC(fdP);
873 for (ihash = 0; ihash < I_HANDLE_HASH_SIZE; ihash++) {
874 IHandle_t *ihP, *ihPnext;
876 ihP = ihashTable[ihash].ihash_head;
878 ihP->ih_refcnt++; /* must not disappear over unlock */
879 for (; ihP; ihP = ihPnext) {
881 if (ihP->ih_synced) {
888 if (fdP) OS_SYNC(fdP->fd_fd);
894 /* when decrementing the refcount, the ihandle might disappear
895 and we might not even be able to proceed to the next one.
896 Hence the gymnastics putting a hold on the next one already */
897 ihPnext = ihP->ih_next;
898 if (ihPnext) ihPnext->ih_refcnt++;
900 if (ihP->ih_refcnt > 1) {
914 ih_sync_thread(void *dummy) {
917 #ifdef AFS_PTHREAD_ENV
919 #else /* AFS_PTHREAD_ENV */
921 #endif /* AFS_PTHREAD_ENV */
932 /*************************************************************************
933 * OS specific support routines.
934 *************************************************************************/
935 #ifndef AFS_NAMEI_ENV
937 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
942 /* See viceinode.h */
943 if (p2 == INODESPECIAL) {
949 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
952 #endif /* AFS_NAMEI_ENV */
959 struct afs_stat status;
960 if (afs_fstat(fd, &status) < 0)
962 return status.st_size;