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>
25 #ifdef AFS_LARGEFILE_ENV
32 #if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
33 #ifndef AFS_LARGEFILE_ENV
34 #include <sys/fcntl.h>
36 #include <sys/resource.h>
47 #include <afs/afsint.h>
49 #include <afs/afssyscalls.h>
52 #include "viceinode.h"
53 #ifdef AFS_PTHREAD_ENV
55 #else /* AFS_PTHREAD_ENV */
56 #include "afs/assert.h"
57 #endif /* AFS_PTHREAD_ENV */
60 extern afs_int32 DErrno;
62 #ifdef AFS_PTHREAD_ENV
63 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
64 pthread_mutex_t ih_glock_mutex;
65 #endif /* AFS_PTHREAD_ENV */
67 /* Linked list of available inode handles */
68 IHandle_t *ihAvailHead;
69 IHandle_t *ihAvailTail;
71 /* Linked list of available file descriptor handles */
72 FdHandle_t *fdAvailHead;
73 FdHandle_t *fdAvailTail;
75 /* Linked list of available stream descriptor handles */
76 StreamHandle_t *streamAvailHead;
77 StreamHandle_t *streamAvailTail;
79 /* LRU list for file descriptor handles */
80 FdHandle_t *fdLruHead;
81 FdHandle_t *fdLruTail;
85 /* Most of the servers use fopen/fdopen. Since the FILE structure
86 * only has eight bits for the file descriptor, the cache size
87 * has to be less than 256. The cache can be made larger as long
88 * as you are sure you don't need fopen/fdopen. */
89 int fdMaxCacheSize = 0;
92 /* Number of in use file descriptors */
95 /* Hash table for inode handles */
96 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
99 #ifdef AFS_PTHREAD_ENV
100 /* 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 */
108 void ih_Initialize(void) {
112 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
113 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
114 DLL_INIT_LIST(fdLruHead, fdLruTail);
115 for (i = 0 ; i < I_HANDLE_HASH_SIZE ; i++) {
116 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
118 #if defined(AFS_NT40_ENV)
119 fdMaxCacheSize = FD_MAX_CACHESIZE;
120 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
123 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
124 rlim.rlim_cur = rlim.rlim_max;
125 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
126 fdMaxCacheSize = rlim.rlim_cur-FD_HANDLE_SETASIDE;
128 /* XXX this is to avoid using up all system fd netbsd is
129 * somewhat broken and have set maximum fd for a root process
130 * to the same as system fd that is avaible, so if the
131 * fileserver uses all up process fds, all system fd will be
134 * Check for this better
138 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
139 assert(fdMaxCacheSize > 0);
141 #elif defined(AFS_HPUX_ENV)
142 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
145 fdMaxCacheSize = MAX(sysconf(_SC_OPEN_MAX)-FD_HANDLE_SETASIDE, 0);
146 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
148 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
151 /* Make the file descriptor cache as big as possible. Don't this call
152 * if the program uses fopen or fdopen. */
153 void ih_UseLargeCache(void) {
159 fdCacheSize = fdMaxCacheSize;
164 /* Allocate a chunk of inode handles */
165 void iHandleAllocateChunk(void)
170 assert(ihAvailHead == NULL);
171 ihP = (IHandle_t *)malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
173 for (i = 0 ; i < I_HANDLE_MALLOCSIZE ; i++) {
174 ihP[i].ih_refcnt = 0;
175 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
179 /* Initialize an inode handle */
180 IHandle_t *ih_init(int dev, int vid, Inode ino)
182 int ihash = IH_HASH(dev, vid, ino);
191 /* Do we already have a handle for this Inode? */
192 for (ihP = ihashTable[ihash].ihash_head ; ihP ; ihP = ihP->ih_next) {
193 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
200 /* Allocate and initialize a new Inode handle */
201 if (ihAvailHead == NULL) {
202 iHandleAllocateChunk();
205 assert(ihP->ih_refcnt == 0);
206 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
212 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
213 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
214 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
219 /* Copy an inode handle */
220 IHandle_t *ih_copy(IHandle_t *ihP)
224 assert(ihP->ih_refcnt > 0);
230 /* Allocate a chunk of file descriptor handles */
231 void fdHandleAllocateChunk(void)
236 assert(fdAvailHead == NULL);
237 fdP = (FdHandle_t *)malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
239 for (i = 0 ; i < FD_HANDLE_MALLOCSIZE ; i++) {
240 fdP[i].fd_status = FD_HANDLE_AVAIL;
242 fdP[i].fd_fd = INVALID_FD;
243 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
247 /* Allocate a chunk of stream handles */
248 void streamHandleAllocateChunk(void)
251 StreamHandle_t *streamP;
253 assert(streamAvailHead == NULL);
254 streamP = (StreamHandle_t *)
255 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
256 assert(streamP != NULL);
257 for (i = 0 ; i < STREAM_HANDLE_MALLOCSIZE ; i++) {
258 streamP[i].str_fd = INVALID_FD;
259 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
265 * Get a file descriptor handle given an Inode handle
267 FdHandle_t *ih_open(IHandle_t *ihP)
273 if (!ihP) /* XXX should log here in the fileserver */
278 /* Do we already have an open file handle for this Inode? */
279 for (fdP = ihP->ih_fdtail ; fdP != NULL ; fdP = fdP->fd_ihprev) {
280 if (fdP->fd_status != FD_HANDLE_INUSE) {
281 assert(fdP->fd_status == FD_HANDLE_OPEN);
282 fdP->fd_status = FD_HANDLE_INUSE;
283 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
286 FDH_SEEK(fdP, 0, SEEK_SET);
292 * Try to open the Inode, return NULL on error.
298 if (fd == INVALID_FD) {
304 /* fdCacheSize limits the size of the descriptor cache, but
305 * we permit the number of open files to exceed fdCacheSize.
306 * We only recycle open file descriptors when the number
307 * of open files reaches the size of the cache */
308 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
310 assert(fdP->fd_status == FD_HANDLE_OPEN);
311 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
312 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
313 fd_ihnext, fd_ihprev);
314 closeFd = fdP->fd_fd;
316 if (fdAvailHead == NULL) {
317 fdHandleAllocateChunk();
320 assert(fdP->fd_status == FD_HANDLE_AVAIL);
321 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
322 closeFd = INVALID_FD;
325 fdP->fd_status = FD_HANDLE_INUSE;
331 /* Add this handle to the Inode's list of open descriptors */
332 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
334 if (closeFd != INVALID_FD) {
346 * Return a file descriptor handle to the cache
348 int fd_close(FdHandle_t *fdP)
359 assert(fdInUseCount > 0);
360 assert(fdP->fd_status == FD_HANDLE_INUSE);
364 /* Call fd_reallyclose to really close the unused file handles if
365 * the previous attempt to close (ih_reallyclose()) all file handles
366 * failed (this is determined by checking the ihandle for the flag
367 * IH_REALLY_CLOSED) or we have too many open files.
369 if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
371 return fd_reallyclose(fdP);
374 /* Put this descriptor back into the cache */
375 fdP->fd_status = FD_HANDLE_OPEN;
376 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
378 /* If this is not the only reference to the Inode then we can decrement
379 * the reference count, otherwise we need to call ih_release.
381 if (ihP->ih_refcnt > 1) {
393 * Actually close the file descriptor handle and return it to
396 int fd_reallyclose(FdHandle_t *fdP)
407 assert(fdInUseCount > 0);
408 assert(fdP->fd_status == FD_HANDLE_INUSE);
411 closeFd = fdP->fd_fd;
413 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
414 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
416 fdP->fd_status = FD_HANDLE_AVAIL;
418 fdP->fd_fd = INVALID_FD;
420 /* All the file descriptor handles have been closed; reset
421 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
422 * has completed its job.
424 if (!ihP->ih_fdhead) {
425 ihP->ih_flags &= ~IH_REALLY_CLOSED;
434 /* If this is not the only reference to the Inode then we can decrement
435 * the reference count, otherwise we need to call ih_release. */
436 if (ihP->ih_refcnt > 1) {
447 /* Enable buffered I/O on a file descriptor */
448 StreamHandle_t *stream_fdopen(FD_t fd)
450 StreamHandle_t *streamP;
453 if (streamAvailHead == NULL) {
454 streamHandleAllocateChunk();
456 streamP = streamAvailHead;
457 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
460 streamP->str_fd = fd;
461 streamP->str_buflen = 0;
462 streamP->str_bufoff = 0;
463 streamP->str_error = 0;
464 streamP->str_eof = 0;
465 streamP->str_direction = STREAM_DIRECTION_NONE;
469 /* Open a file for buffered I/O */
470 StreamHandle_t *stream_open(const char *filename, const char *mode)
474 if (strcmp(mode, "r") == 0) {
475 fd = OS_OPEN(filename, O_RDONLY, 0);
476 } else if (strcmp(mode, "r+") == 0) {
477 fd = OS_OPEN(filename, O_RDWR, 0);
478 } else if (strcmp(mode, "w") == 0) {
479 fd = OS_OPEN(filename, O_WRONLY|O_TRUNC|O_CREAT, 0);
480 } else if (strcmp(mode, "w+") == 0) {
481 fd = OS_OPEN(filename, O_RDWR|O_TRUNC|O_CREAT, 0);
482 } else if (strcmp(mode, "a") == 0) {
483 fd = OS_OPEN(filename, O_WRONLY|O_APPEND|O_CREAT, 0);
484 } else if (strcmp(mode, "a+") == 0) {
485 fd = OS_OPEN(filename, O_RDWR|O_APPEND|O_CREAT, 0);
487 assert(FALSE); /* not implemented */
490 if (fd == INVALID_FD) {
493 return stream_fdopen(fd);
496 /* fread for buffered I/O handles */
497 afs_size_t stream_read(void *ptr, afs_size_t size, afs_size_t nitems,
498 StreamHandle_t *streamP)
500 afs_size_t nbytes, bytesRead, bytesToRead;
503 /* Need to seek before changing direction */
504 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
505 streamP->str_direction = STREAM_DIRECTION_READ;
506 streamP->str_bufoff = 0;
507 streamP->str_buflen = 0;
509 assert(streamP->str_direction == STREAM_DIRECTION_READ);
513 nbytes = size * nitems;
515 while (nbytes > 0 && !streamP->str_eof) {
516 if (streamP->str_buflen == 0) {
517 streamP->str_bufoff = 0;
518 streamP->str_buflen = OS_READ(streamP->str_fd, streamP->str_buffer,
519 STREAM_HANDLE_BUFSIZE);
520 if (streamP->str_buflen < 0) {
521 streamP->str_error = errno;
522 streamP->str_buflen = 0;
525 } else if (streamP->str_buflen == 0) {
526 streamP->str_eof = 1;
531 bytesToRead = nbytes;
532 if (bytesToRead > streamP->str_buflen) {
533 bytesToRead = streamP->str_buflen;
535 memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
537 streamP->str_bufoff += bytesToRead;
538 streamP->str_buflen -= bytesToRead;
539 bytesRead += bytesToRead;
540 nbytes -= bytesToRead;
543 return (bytesRead/size);
546 /* fwrite for buffered I/O handles */
547 afs_size_t stream_write(void *ptr, afs_size_t size, afs_size_t nitems,
548 StreamHandle_t *streamP)
551 afs_size_t rc, nbytes, bytesWritten, bytesToWrite;
553 /* Need to seek before changing direction */
554 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
555 streamP->str_direction = STREAM_DIRECTION_WRITE;
556 streamP->str_bufoff = 0;
557 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
559 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
562 nbytes = size * nitems;
566 if (streamP->str_buflen == 0) {
567 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
568 STREAM_HANDLE_BUFSIZE);
570 streamP->str_error = errno;
574 streamP->str_bufoff = 0;
575 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
578 bytesToWrite = nbytes;
579 if (bytesToWrite > streamP->str_buflen) {
580 bytesToWrite = streamP->str_buflen;
582 memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
584 streamP->str_bufoff += bytesToWrite;
585 streamP->str_buflen -= bytesToWrite;
586 bytesWritten += bytesToWrite;
587 nbytes -= bytesToWrite;
590 return (bytesWritten/size);
593 /* fseek for buffered I/O handles */
594 int stream_seek(StreamHandle_t *streamP, afs_size_t offset, int whence)
599 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
600 streamP->str_bufoff > 0) {
601 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
602 streamP->str_bufoff);
604 streamP->str_error = errno;
608 streamP->str_bufoff = 0;
609 streamP->str_buflen = 0;
610 streamP->str_eof = 0;
611 streamP->str_direction = STREAM_DIRECTION_NONE;
612 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
613 streamP->str_error = errno;
619 /* fflush for buffered I/O handles */
620 int stream_flush(StreamHandle_t *streamP)
625 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
626 streamP->str_bufoff > 0) {
627 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
628 streamP->str_bufoff);
630 streamP->str_error = errno;
633 streamP->str_bufoff = 0;
634 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
640 /* Free a buffered I/O handle */
641 int stream_close(StreamHandle_t *streamP, int reallyClose)
646 assert(streamP != NULL);
647 if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
648 streamP->str_bufoff > 0) {
649 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
650 streamP->str_bufoff);
656 rc = OS_CLOSE(streamP->str_fd);
661 streamP->str_fd = INVALID_FD;
664 DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
671 /* Close all unused file descriptors associated with the inode
672 * handle. Called with IH_LOCK held. May drop and reacquire
673 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
674 * if it fails to close all file handles.
676 static int ih_fdclose(IHandle_t *ihP)
678 int closeCount, closedAll;
679 FdHandle_t *fdP, *head, *tail, *next;
681 assert(ihP->ih_refcnt > 0);
684 DLL_INIT_LIST(head, tail);
685 ihP->ih_flags &= ~IH_REALLY_CLOSED;
688 * Remove the file descriptors for this Inode from the LRU queue
689 * and the IHandle queue and put them on a temporary queue so we
690 * can drop the lock before we close the files.
692 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
693 next = fdP->fd_ihnext;
694 assert(fdP->fd_ih == ihP);
695 assert(fdP->fd_status == FD_HANDLE_OPEN ||
696 fdP->fd_status == FD_HANDLE_INUSE);
697 if (fdP->fd_status == FD_HANDLE_OPEN) {
698 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail,
699 fd_ihnext, fd_ihprev);
700 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
701 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
704 ihP->ih_flags |= IH_REALLY_CLOSED;
708 /* If the ihandle reference count is 1, we should have
709 * closed all file descriptors.
711 if (ihP->ih_refcnt == 1 || closedAll) {
713 assert(!ihP->ih_fdhead);
714 assert(!ihP->ih_fdtail);
718 return 0; /* No file descriptors closed */
724 * Close the file descriptors
727 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
728 OS_CLOSE(fdP->fd_fd);
729 fdP->fd_status = FD_HANDLE_AVAIL;
730 fdP->fd_fd = INVALID_FD;
737 assert(fdInUseCount >= closeCount);
738 fdInUseCount -= closeCount;
741 * Append the temporary queue to the list of available descriptors
743 if (fdAvailHead == NULL) {
747 fdAvailTail->fd_next = head;
748 head->fd_prev = fdAvailTail;
755 /* Close all cached file descriptors for this inode. */
756 int ih_reallyclose(IHandle_t *ihP)
763 assert(ihP->ih_refcnt > 0);
771 /* Release an Inode handle. All cached file descriptors for this
772 * inode are closed when the last reference to this handle is released
774 int ih_release(IHandle_t *ihP)
783 assert(ihP->ih_refcnt > 0);
785 if (ihP->ih_refcnt > 1) {
791 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
792 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
793 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
799 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
806 /* Sync an inode to disk if its handle isn't NULL */
807 int ih_condsync(IHandle_t *ihP)
819 code = FDH_SYNC(fdP);
827 /*************************************************************************
828 * OS specific support routines.
829 *************************************************************************/
830 #ifndef AFS_NAMEI_ENV
831 Inode ih_icreate(IHandle_t *ih, int dev, char *part, Inode nI, int p1, int p2,
836 /* See viceinode.h */
837 if (p2 == INODESPECIAL) {
843 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
846 #endif /* AFS_NAMEI_ENV */
850 afs_size_t ih_size(int fd)
852 #ifdef AFS_LARGEFILE_ENV
853 struct stat64 status;
854 if (fstat64(fd, &status)<0)
856 #else /* !AFS_LARGEFILE_ENV */
858 if (fstat(fd, &status)<0)
860 #endif /* !AFS_LARGEFILE_ENV */
861 return status.st_size;