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>
29 #if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
30 #include <sys/fcntl.h>
31 #include <sys/resource.h>
42 #include <afs/afsint.h>
44 #include <afs/afssyscalls.h>
47 #include "viceinode.h"
48 #ifdef AFS_PTHREAD_ENV
50 #else /* AFS_PTHREAD_ENV */
51 #include "afs/assert.h"
52 #endif /* AFS_PTHREAD_ENV */
57 #define afs_stat stat64
58 #define afs_fstat fstat64
59 #else /* !O_LARGEFILE */
61 #define afs_fstat fstat
62 #endif /* !O_LARGEFILE */
63 #endif /* AFS_NT40_ENV */
65 #ifdef AFS_PTHREAD_ENV
66 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
67 pthread_mutex_t ih_glock_mutex;
68 #endif /* AFS_PTHREAD_ENV */
70 /* Linked list of available inode handles */
71 IHandle_t *ihAvailHead;
72 IHandle_t *ihAvailTail;
74 /* Linked list of available file descriptor handles */
75 FdHandle_t *fdAvailHead;
76 FdHandle_t *fdAvailTail;
78 /* Linked list of available stream descriptor handles */
79 StreamHandle_t *streamAvailHead;
80 StreamHandle_t *streamAvailTail;
82 /* LRU list for file descriptor handles */
83 FdHandle_t *fdLruHead;
84 FdHandle_t *fdLruTail;
88 /* Most of the servers use fopen/fdopen. Since the FILE structure
89 * only has eight bits for the file descriptor, the cache size
90 * has to be less than 256. The cache can be made larger as long
91 * as you are sure you don't need fopen/fdopen. */
92 int fdMaxCacheSize = 0;
95 /* Number of in use file descriptors */
98 /* Hash table for inode handles */
99 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
102 #ifdef AFS_PTHREAD_ENV
103 /* Initialize the global ihandle mutex */
107 assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
109 #endif /* AFS_PTHREAD_ENV */
111 /* Initialize the file descriptor cache */
118 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
119 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
120 DLL_INIT_LIST(fdLruHead, fdLruTail);
121 for (i = 0; i < I_HANDLE_HASH_SIZE; i++) {
122 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
124 #if defined(AFS_NT40_ENV)
125 fdMaxCacheSize = FD_MAX_CACHESIZE;
126 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
129 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
130 rlim.rlim_cur = rlim.rlim_max;
131 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
132 fdMaxCacheSize = rlim.rlim_cur - FD_HANDLE_SETASIDE;
134 /* XXX this is to avoid using up all system fd netbsd is
135 * somewhat broken and have set maximum fd for a root process
136 * to the same as system fd that is avaible, so if the
137 * fileserver uses all up process fds, all system fd will be
140 * Check for this better
144 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
145 assert(fdMaxCacheSize > 0);
147 #elif defined(AFS_HPUX_ENV)
148 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
152 long fdMax = MAX(sysconf(_SC_OPEN_MAX) - FD_HANDLE_SETASIDE, 0);
153 fdMaxCacheSize = (int)MIN(fdMax, FD_MAX_CACHESIZE);
156 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
159 /* Make the file descriptor cache as big as possible. Don't this call
160 * if the program uses fopen or fdopen. */
162 ih_UseLargeCache(void)
164 IH_LOCK if (!ih_Inited) {
167 fdCacheSize = fdMaxCacheSize;
171 /* Allocate a chunk of inode handles */
173 iHandleAllocateChunk(void)
178 assert(ihAvailHead == NULL);
179 ihP = (IHandle_t *) malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
181 for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
182 ihP[i].ih_refcnt = 0;
183 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
187 /* Initialize an inode handle */
189 ih_init(int dev, int vid, Inode ino)
191 int ihash = IH_HASH(dev, vid, ino);
194 IH_LOCK if (!ih_Inited) {
198 /* Do we already have a handle for this Inode? */
199 for (ihP = ihashTable[ihash].ihash_head; ihP; ihP = ihP->ih_next) {
200 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
202 IH_UNLOCK return ihP;
206 /* Allocate and initialize a new Inode handle */
207 if (ihAvailHead == NULL) {
208 iHandleAllocateChunk();
211 assert(ihP->ih_refcnt == 0);
212 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
218 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
219 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
220 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
221 IH_UNLOCK return ihP;
224 /* Copy an inode handle */
226 ih_copy(IHandle_t * ihP)
228 IH_LOCK assert(ih_Inited);
229 assert(ihP->ih_refcnt > 0);
231 IH_UNLOCK return ihP;
234 /* Allocate a chunk of file descriptor handles */
236 fdHandleAllocateChunk(void)
241 assert(fdAvailHead == NULL);
242 fdP = (FdHandle_t *) malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
244 for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
245 fdP[i].fd_status = FD_HANDLE_AVAIL;
247 fdP[i].fd_fd = INVALID_FD;
248 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
252 /* Allocate a chunk of stream handles */
254 streamHandleAllocateChunk(void)
257 StreamHandle_t *streamP;
259 assert(streamAvailHead == NULL);
260 streamP = (StreamHandle_t *)
261 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
262 assert(streamP != NULL);
263 for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
264 streamP[i].str_fd = INVALID_FD;
265 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
271 * Get a file descriptor handle given an Inode handle
274 ih_open(IHandle_t * ihP)
280 if (!ihP) /* XXX should log here in the fileserver */
284 /* Do we already have an open file handle for this Inode? */
285 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
286 if (fdP->fd_status != FD_HANDLE_INUSE) {
287 assert(fdP->fd_status == FD_HANDLE_OPEN);
288 fdP->fd_status = FD_HANDLE_INUSE;
289 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
291 IH_UNLOCK(void) FDH_SEEK(fdP, 0, SEEK_SET);
297 * Try to open the Inode, return NULL on error.
300 IH_UNLOCK fd = OS_IOPEN(ihP);
301 IH_LOCK if (fd == INVALID_FD) {
303 IH_UNLOCK return NULL;
306 /* fdCacheSize limits the size of the descriptor cache, but
307 * we permit the number of open files to exceed fdCacheSize.
308 * We only recycle open file descriptors when the number
309 * of open files reaches the size of the cache */
310 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
312 assert(fdP->fd_status == FD_HANDLE_OPEN);
313 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
314 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
315 fd_ihnext, fd_ihprev);
316 closeFd = fdP->fd_fd;
318 if (fdAvailHead == NULL) {
319 fdHandleAllocateChunk();
322 assert(fdP->fd_status == FD_HANDLE_AVAIL);
323 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
324 closeFd = INVALID_FD;
327 fdP->fd_status = FD_HANDLE_INUSE;
333 /* Add this handle to the Inode's list of open descriptors */
334 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
337 if (closeFd != INVALID_FD) {
338 IH_UNLOCK OS_CLOSE(closeFd);
339 IH_LOCK fdInUseCount -= 1;
342 IH_UNLOCK return fdP;
346 * Return a file descriptor handle to the cache
349 fd_close(FdHandle_t * fdP)
356 IH_LOCK assert(ih_Inited);
357 assert(fdInUseCount > 0);
358 assert(fdP->fd_status == FD_HANDLE_INUSE);
362 /* Call fd_reallyclose to really close the unused file handles if
363 * the previous attempt to close (ih_reallyclose()) all file handles
364 * failed (this is determined by checking the ihandle for the flag
365 * IH_REALLY_CLOSED) or we have too many open files.
367 if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
368 IH_UNLOCK return fd_reallyclose(fdP);
371 /* Put this descriptor back into the cache */
372 fdP->fd_status = FD_HANDLE_OPEN;
373 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
375 /* If this is not the only reference to the Inode then we can decrement
376 * the reference count, otherwise we need to call ih_release.
378 if (ihP->ih_refcnt > 1) {
381 IH_UNLOCK ih_release(ihP);
388 * Actually close the file descriptor handle and return it to
392 fd_reallyclose(FdHandle_t * fdP)
400 IH_LOCK assert(ih_Inited);
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;
422 IH_UNLOCK OS_CLOSE(closeFd);
423 IH_LOCK fdInUseCount -= 1;
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) {
430 IH_UNLOCK ih_release(ihP);
436 /* Enable buffered I/O on a file descriptor */
438 stream_fdopen(FD_t fd)
440 StreamHandle_t *streamP;
442 IH_LOCK if (streamAvailHead == NULL) {
443 streamHandleAllocateChunk();
445 streamP = streamAvailHead;
446 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
447 IH_UNLOCK streamP->str_fd = fd;
448 streamP->str_buflen = 0;
449 streamP->str_bufoff = 0;
450 streamP->str_error = 0;
451 streamP->str_eof = 0;
452 streamP->str_direction = STREAM_DIRECTION_NONE;
456 /* Open a file for buffered I/O */
458 stream_open(const char *filename, const char *mode)
462 if (strcmp(mode, "r") == 0) {
463 fd = OS_OPEN(filename, O_RDONLY, 0);
464 } else if (strcmp(mode, "r+") == 0) {
465 fd = OS_OPEN(filename, O_RDWR, 0);
466 } else if (strcmp(mode, "w") == 0) {
467 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
468 } else if (strcmp(mode, "w+") == 0) {
469 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
470 } else if (strcmp(mode, "a") == 0) {
471 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
472 } else if (strcmp(mode, "a+") == 0) {
473 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
475 assert(FALSE); /* not implemented */
478 if (fd == INVALID_FD) {
481 return stream_fdopen(fd);
484 /* fread for buffered I/O handles */
486 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
487 StreamHandle_t * streamP)
489 afs_fsize_t nbytes, bytesRead, bytesToRead;
492 /* Need to seek before changing direction */
493 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
494 streamP->str_direction = STREAM_DIRECTION_READ;
495 streamP->str_bufoff = 0;
496 streamP->str_buflen = 0;
498 assert(streamP->str_direction == STREAM_DIRECTION_READ);
502 nbytes = size * nitems;
504 while (nbytes > 0 && !streamP->str_eof) {
505 if (streamP->str_buflen == 0) {
506 streamP->str_bufoff = 0;
507 streamP->str_buflen =
508 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 */
538 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
539 StreamHandle_t * streamP)
543 afs_fsize_t 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 */
587 stream_seek(StreamHandle_t * streamP, afs_foff_t offset, int whence)
592 if (streamP->str_direction == STREAM_DIRECTION_WRITE
593 && streamP->str_bufoff > 0) {
594 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
595 streamP->str_bufoff);
597 streamP->str_error = errno;
601 streamP->str_bufoff = 0;
602 streamP->str_buflen = 0;
603 streamP->str_eof = 0;
604 streamP->str_direction = STREAM_DIRECTION_NONE;
605 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
606 streamP->str_error = errno;
612 /* fflush for buffered I/O handles */
614 stream_flush(StreamHandle_t * streamP)
619 if (streamP->str_direction == STREAM_DIRECTION_WRITE
620 && streamP->str_bufoff > 0) {
621 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
622 streamP->str_bufoff);
624 streamP->str_error = errno;
627 streamP->str_bufoff = 0;
628 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
634 /* Free a buffered I/O handle */
636 stream_close(StreamHandle_t * streamP, int reallyClose)
641 assert(streamP != NULL);
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);
651 rc = OS_CLOSE(streamP->str_fd);
656 streamP->str_fd = INVALID_FD;
658 IH_LOCK DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
660 IH_UNLOCK return retval;
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.
669 ih_fdclose(IHandle_t * ihP)
671 int closeCount, closedAll;
672 FdHandle_t *fdP, *head, *tail, *next;
674 assert(ihP->ih_refcnt > 0);
677 DLL_INIT_LIST(head, tail);
678 ihP->ih_flags &= ~IH_REALLY_CLOSED;
681 * Remove the file descriptors for this Inode from the LRU queue
682 * and the IHandle queue and put them on a temporary queue so we
683 * can drop the lock before we close the files.
685 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
686 next = fdP->fd_ihnext;
687 assert(fdP->fd_ih == ihP);
688 assert(fdP->fd_status == FD_HANDLE_OPEN
689 || fdP->fd_status == FD_HANDLE_INUSE);
690 if (fdP->fd_status == FD_HANDLE_OPEN) {
691 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
693 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
694 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
697 ihP->ih_flags |= IH_REALLY_CLOSED;
701 /* If the ihandle reference count is 1, we should have
702 * closed all file descriptors.
704 if (ihP->ih_refcnt == 1 || closedAll) {
706 assert(!ihP->ih_fdhead);
707 assert(!ihP->ih_fdtail);
711 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;
727 IH_LOCK assert(fdInUseCount >= closeCount);
728 fdInUseCount -= closeCount;
731 * Append the temporary queue to the list of available descriptors
733 if (fdAvailHead == NULL) {
737 fdAvailTail->fd_next = head;
738 head->fd_prev = fdAvailTail;
745 /* Close all cached file descriptors for this inode. */
747 ih_reallyclose(IHandle_t * ihP)
752 IH_LOCK assert(ihP->ih_refcnt > 0);
758 /* Release an Inode handle. All cached file descriptors for this
759 * inode are closed when the last reference to this handle is released
762 ih_release(IHandle_t * ihP)
769 IH_LOCK assert(ihP->ih_refcnt > 0);
771 if (ihP->ih_refcnt > 1) {
776 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
777 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
778 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
784 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
789 /* Sync an inode to disk if its handle isn't NULL */
791 ih_condsync(IHandle_t * ihP)
803 code = FDH_SYNC(fdP);
811 /*************************************************************************
812 * OS specific support routines.
813 *************************************************************************/
814 #ifndef AFS_NAMEI_ENV
816 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
821 /* See viceinode.h */
822 if (p2 == INODESPECIAL) {
828 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
831 #endif /* AFS_NAMEI_ENV */
838 struct afs_stat status;
839 if (afs_fstat(fd, &status) < 0)
841 return status.st_size;