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 extern afs_int32 DErrno;
67 #ifdef AFS_PTHREAD_ENV
68 pthread_once_t ih_glock_once = PTHREAD_ONCE_INIT;
69 pthread_mutex_t ih_glock_mutex;
70 #endif /* AFS_PTHREAD_ENV */
72 /* Linked list of available inode handles */
73 IHandle_t *ihAvailHead;
74 IHandle_t *ihAvailTail;
76 /* Linked list of available file descriptor handles */
77 FdHandle_t *fdAvailHead;
78 FdHandle_t *fdAvailTail;
80 /* Linked list of available stream descriptor handles */
81 StreamHandle_t *streamAvailHead;
82 StreamHandle_t *streamAvailTail;
84 /* LRU list for file descriptor handles */
85 FdHandle_t *fdLruHead;
86 FdHandle_t *fdLruTail;
90 /* Most of the servers use fopen/fdopen. Since the FILE structure
91 * only has eight bits for the file descriptor, the cache size
92 * has to be less than 256. The cache can be made larger as long
93 * as you are sure you don't need fopen/fdopen. */
94 int fdMaxCacheSize = 0;
97 /* Number of in use file descriptors */
100 /* Hash table for inode handles */
101 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
104 #ifdef AFS_PTHREAD_ENV
105 /* Initialize the global ihandle mutex */
109 assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
111 #endif /* AFS_PTHREAD_ENV */
113 /* Initialize the file descriptor cache */
120 DLL_INIT_LIST(ihAvailHead, ihAvailTail);
121 DLL_INIT_LIST(fdAvailHead, fdAvailTail);
122 DLL_INIT_LIST(fdLruHead, fdLruTail);
123 for (i = 0; i < I_HANDLE_HASH_SIZE; i++) {
124 DLL_INIT_LIST(ihashTable[i].ihash_head, ihashTable[i].ihash_tail);
126 #if defined(AFS_NT40_ENV)
127 fdMaxCacheSize = FD_MAX_CACHESIZE;
128 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
131 assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
132 rlim.rlim_cur = rlim.rlim_max;
133 assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
134 fdMaxCacheSize = rlim.rlim_cur - FD_HANDLE_SETASIDE;
136 /* XXX this is to avoid using up all system fd netbsd is
137 * somewhat broken and have set maximum fd for a root process
138 * to the same as system fd that is avaible, so if the
139 * fileserver uses all up process fds, all system fd will be
142 * Check for this better
146 fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
147 assert(fdMaxCacheSize > 0);
149 #elif defined(AFS_HPUX_ENV)
150 /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
154 long fdMax = MAX(sysconf(_SC_OPEN_MAX) - FD_HANDLE_SETASIDE, 0);
155 fdMaxCacheSize = (int)MIN(fdMax, FD_MAX_CACHESIZE);
158 fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
161 /* Make the file descriptor cache as big as possible. Don't this call
162 * if the program uses fopen or fdopen. */
164 ih_UseLargeCache(void)
166 IH_LOCK if (!ih_Inited) {
169 fdCacheSize = fdMaxCacheSize;
173 /* Allocate a chunk of inode handles */
175 iHandleAllocateChunk(void)
180 assert(ihAvailHead == NULL);
181 ihP = (IHandle_t *) malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
183 for (i = 0; i < I_HANDLE_MALLOCSIZE; i++) {
184 ihP[i].ih_refcnt = 0;
185 DLL_INSERT_TAIL(&ihP[i], ihAvailHead, ihAvailTail, ih_next, ih_prev);
189 /* Initialize an inode handle */
191 ih_init(int dev, int vid, Inode ino)
193 int ihash = IH_HASH(dev, vid, ino);
196 IH_LOCK if (!ih_Inited) {
200 /* Do we already have a handle for this Inode? */
201 for (ihP = ihashTable[ihash].ihash_head; ihP; ihP = ihP->ih_next) {
202 if (ihP->ih_ino == ino && ihP->ih_vid == vid && ihP->ih_dev == dev) {
204 IH_UNLOCK return ihP;
208 /* Allocate and initialize a new Inode handle */
209 if (ihAvailHead == NULL) {
210 iHandleAllocateChunk();
213 assert(ihP->ih_refcnt == 0);
214 DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
220 DLL_INIT_LIST(ihP->ih_fdhead, ihP->ih_fdtail);
221 DLL_INSERT_TAIL(ihP, ihashTable[ihash].ihash_head,
222 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
223 IH_UNLOCK return ihP;
226 /* Copy an inode handle */
228 ih_copy(IHandle_t * ihP)
230 IH_LOCK assert(ih_Inited);
231 assert(ihP->ih_refcnt > 0);
233 IH_UNLOCK return ihP;
236 /* Allocate a chunk of file descriptor handles */
238 fdHandleAllocateChunk(void)
243 assert(fdAvailHead == NULL);
244 fdP = (FdHandle_t *) malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
246 for (i = 0; i < FD_HANDLE_MALLOCSIZE; i++) {
247 fdP[i].fd_status = FD_HANDLE_AVAIL;
249 fdP[i].fd_fd = INVALID_FD;
250 DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
254 /* Allocate a chunk of stream handles */
256 streamHandleAllocateChunk(void)
259 StreamHandle_t *streamP;
261 assert(streamAvailHead == NULL);
262 streamP = (StreamHandle_t *)
263 malloc(STREAM_HANDLE_MALLOCSIZE * sizeof(StreamHandle_t));
264 assert(streamP != NULL);
265 for (i = 0; i < STREAM_HANDLE_MALLOCSIZE; i++) {
266 streamP[i].str_fd = INVALID_FD;
267 DLL_INSERT_TAIL(&streamP[i], streamAvailHead, streamAvailTail,
273 * Get a file descriptor handle given an Inode handle
276 ih_open(IHandle_t * ihP)
282 if (!ihP) /* XXX should log here in the fileserver */
286 /* Do we already have an open file handle for this Inode? */
287 for (fdP = ihP->ih_fdtail; fdP != NULL; fdP = fdP->fd_ihprev) {
288 if (fdP->fd_status != FD_HANDLE_INUSE) {
289 assert(fdP->fd_status == FD_HANDLE_OPEN);
290 fdP->fd_status = FD_HANDLE_INUSE;
291 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
293 IH_UNLOCK(void) FDH_SEEK(fdP, 0, SEEK_SET);
299 * Try to open the Inode, return NULL on error.
302 IH_UNLOCK fd = OS_IOPEN(ihP);
303 IH_LOCK if (fd == INVALID_FD) {
305 IH_UNLOCK return NULL;
308 /* fdCacheSize limits the size of the descriptor cache, but
309 * we permit the number of open files to exceed fdCacheSize.
310 * We only recycle open file descriptors when the number
311 * of open files reaches the size of the cache */
312 if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
314 assert(fdP->fd_status == FD_HANDLE_OPEN);
315 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
316 DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
317 fd_ihnext, fd_ihprev);
318 closeFd = fdP->fd_fd;
320 if (fdAvailHead == NULL) {
321 fdHandleAllocateChunk();
324 assert(fdP->fd_status == FD_HANDLE_AVAIL);
325 DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
326 closeFd = INVALID_FD;
329 fdP->fd_status = FD_HANDLE_INUSE;
335 /* Add this handle to the Inode's list of open descriptors */
336 DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
339 if (closeFd != INVALID_FD) {
340 IH_UNLOCK OS_CLOSE(closeFd);
341 IH_LOCK fdInUseCount -= 1;
344 IH_UNLOCK return fdP;
348 * Return a file descriptor handle to the cache
351 fd_close(FdHandle_t * fdP)
358 IH_LOCK assert(ih_Inited);
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) {
370 IH_UNLOCK return fd_reallyclose(fdP);
373 /* Put this descriptor back into the cache */
374 fdP->fd_status = FD_HANDLE_OPEN;
375 DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
377 /* If this is not the only reference to the Inode then we can decrement
378 * the reference count, otherwise we need to call ih_release.
380 if (ihP->ih_refcnt > 1) {
383 IH_UNLOCK ih_release(ihP);
390 * Actually close the file descriptor handle and return it to
394 fd_reallyclose(FdHandle_t * fdP)
402 IH_LOCK assert(ih_Inited);
403 assert(fdInUseCount > 0);
404 assert(fdP->fd_status == FD_HANDLE_INUSE);
407 closeFd = fdP->fd_fd;
409 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
410 DLL_INSERT_TAIL(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
412 fdP->fd_status = FD_HANDLE_AVAIL;
414 fdP->fd_fd = INVALID_FD;
416 /* All the file descriptor handles have been closed; reset
417 * the IH_REALLY_CLOSED flag indicating that ih_reallyclose
418 * has completed its job.
420 if (!ihP->ih_fdhead) {
421 ihP->ih_flags &= ~IH_REALLY_CLOSED;
424 IH_UNLOCK OS_CLOSE(closeFd);
425 IH_LOCK fdInUseCount -= 1;
427 /* If this is not the only reference to the Inode then we can decrement
428 * the reference count, otherwise we need to call ih_release. */
429 if (ihP->ih_refcnt > 1) {
432 IH_UNLOCK ih_release(ihP);
438 /* Enable buffered I/O on a file descriptor */
440 stream_fdopen(FD_t fd)
442 StreamHandle_t *streamP;
444 IH_LOCK if (streamAvailHead == NULL) {
445 streamHandleAllocateChunk();
447 streamP = streamAvailHead;
448 DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
449 IH_UNLOCK streamP->str_fd = fd;
450 streamP->str_buflen = 0;
451 streamP->str_bufoff = 0;
452 streamP->str_error = 0;
453 streamP->str_eof = 0;
454 streamP->str_direction = STREAM_DIRECTION_NONE;
458 /* Open a file for buffered I/O */
460 stream_open(const char *filename, const char *mode)
464 if (strcmp(mode, "r") == 0) {
465 fd = OS_OPEN(filename, O_RDONLY, 0);
466 } else if (strcmp(mode, "r+") == 0) {
467 fd = OS_OPEN(filename, O_RDWR, 0);
468 } else if (strcmp(mode, "w") == 0) {
469 fd = OS_OPEN(filename, O_WRONLY | O_TRUNC | O_CREAT, 0);
470 } else if (strcmp(mode, "w+") == 0) {
471 fd = OS_OPEN(filename, O_RDWR | O_TRUNC | O_CREAT, 0);
472 } else if (strcmp(mode, "a") == 0) {
473 fd = OS_OPEN(filename, O_WRONLY | O_APPEND | O_CREAT, 0);
474 } else if (strcmp(mode, "a+") == 0) {
475 fd = OS_OPEN(filename, O_RDWR | O_APPEND | O_CREAT, 0);
477 assert(FALSE); /* not implemented */
480 if (fd == INVALID_FD) {
483 return stream_fdopen(fd);
486 /* fread for buffered I/O handles */
488 stream_read(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
489 StreamHandle_t * streamP)
491 afs_fsize_t nbytes, bytesRead, bytesToRead;
494 /* Need to seek before changing direction */
495 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
496 streamP->str_direction = STREAM_DIRECTION_READ;
497 streamP->str_bufoff = 0;
498 streamP->str_buflen = 0;
500 assert(streamP->str_direction == STREAM_DIRECTION_READ);
504 nbytes = size * nitems;
506 while (nbytes > 0 && !streamP->str_eof) {
507 if (streamP->str_buflen == 0) {
508 streamP->str_bufoff = 0;
509 streamP->str_buflen =
510 OS_READ(streamP->str_fd, streamP->str_buffer,
511 STREAM_HANDLE_BUFSIZE);
512 if (streamP->str_buflen < 0) {
513 streamP->str_error = errno;
514 streamP->str_buflen = 0;
517 } else if (streamP->str_buflen == 0) {
518 streamP->str_eof = 1;
523 bytesToRead = nbytes;
524 if (bytesToRead > streamP->str_buflen) {
525 bytesToRead = streamP->str_buflen;
527 memcpy(p, streamP->str_buffer + streamP->str_bufoff, bytesToRead);
529 streamP->str_bufoff += bytesToRead;
530 streamP->str_buflen -= bytesToRead;
531 bytesRead += bytesToRead;
532 nbytes -= bytesToRead;
535 return (bytesRead / size);
538 /* fwrite for buffered I/O handles */
540 stream_write(void *ptr, afs_fsize_t size, afs_fsize_t nitems,
541 StreamHandle_t * streamP)
545 afs_fsize_t nbytes, bytesWritten, bytesToWrite;
547 /* Need to seek before changing direction */
548 if (streamP->str_direction == STREAM_DIRECTION_NONE) {
549 streamP->str_direction = STREAM_DIRECTION_WRITE;
550 streamP->str_bufoff = 0;
551 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
553 assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
556 nbytes = size * nitems;
560 if (streamP->str_buflen == 0) {
561 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
562 STREAM_HANDLE_BUFSIZE);
564 streamP->str_error = errno;
568 streamP->str_bufoff = 0;
569 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
572 bytesToWrite = nbytes;
573 if (bytesToWrite > streamP->str_buflen) {
574 bytesToWrite = streamP->str_buflen;
576 memcpy(streamP->str_buffer + streamP->str_bufoff, p, bytesToWrite);
578 streamP->str_bufoff += bytesToWrite;
579 streamP->str_buflen -= bytesToWrite;
580 bytesWritten += bytesToWrite;
581 nbytes -= bytesToWrite;
584 return (bytesWritten / size);
587 /* fseek for buffered I/O handles */
589 stream_seek(StreamHandle_t * streamP, afs_foff_t offset, int whence)
594 if (streamP->str_direction == STREAM_DIRECTION_WRITE
595 && streamP->str_bufoff > 0) {
596 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
597 streamP->str_bufoff);
599 streamP->str_error = errno;
603 streamP->str_bufoff = 0;
604 streamP->str_buflen = 0;
605 streamP->str_eof = 0;
606 streamP->str_direction = STREAM_DIRECTION_NONE;
607 if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
608 streamP->str_error = errno;
614 /* fflush for buffered I/O handles */
616 stream_flush(StreamHandle_t * streamP)
621 if (streamP->str_direction == STREAM_DIRECTION_WRITE
622 && streamP->str_bufoff > 0) {
623 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
624 streamP->str_bufoff);
626 streamP->str_error = errno;
629 streamP->str_bufoff = 0;
630 streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
636 /* Free a buffered I/O handle */
638 stream_close(StreamHandle_t * streamP, int reallyClose)
643 assert(streamP != NULL);
644 if (streamP->str_direction == STREAM_DIRECTION_WRITE
645 && streamP->str_bufoff > 0) {
646 rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
647 streamP->str_bufoff);
653 rc = OS_CLOSE(streamP->str_fd);
658 streamP->str_fd = INVALID_FD;
660 IH_LOCK DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
662 IH_UNLOCK return retval;
665 /* Close all unused file descriptors associated with the inode
666 * handle. Called with IH_LOCK held. May drop and reacquire
667 * IH_LOCK. Sets the IH_REALLY_CLOSED flag in the inode handle
668 * if it fails to close all file handles.
671 ih_fdclose(IHandle_t * ihP)
673 int closeCount, closedAll;
674 FdHandle_t *fdP, *head, *tail, *next;
676 assert(ihP->ih_refcnt > 0);
679 DLL_INIT_LIST(head, tail);
680 ihP->ih_flags &= ~IH_REALLY_CLOSED;
683 * Remove the file descriptors for this Inode from the LRU queue
684 * and the IHandle queue and put them on a temporary queue so we
685 * can drop the lock before we close the files.
687 for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
688 next = fdP->fd_ihnext;
689 assert(fdP->fd_ih == ihP);
690 assert(fdP->fd_status == FD_HANDLE_OPEN
691 || fdP->fd_status == FD_HANDLE_INUSE);
692 if (fdP->fd_status == FD_HANDLE_OPEN) {
693 DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext,
695 DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
696 DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
699 ihP->ih_flags |= IH_REALLY_CLOSED;
703 /* If the ihandle reference count is 1, we should have
704 * closed all file descriptors.
706 if (ihP->ih_refcnt == 1 || closedAll) {
708 assert(!ihP->ih_fdhead);
709 assert(!ihP->ih_fdtail);
713 return 0; /* No file descriptors closed */
718 * Close the file descriptors
721 for (fdP = head; fdP != NULL; fdP = fdP->fd_next) {
722 OS_CLOSE(fdP->fd_fd);
723 fdP->fd_status = FD_HANDLE_AVAIL;
724 fdP->fd_fd = INVALID_FD;
729 IH_LOCK 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. */
749 ih_reallyclose(IHandle_t * ihP)
754 IH_LOCK 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
764 ih_release(IHandle_t * ihP)
771 IH_LOCK assert(ihP->ih_refcnt > 0);
773 if (ihP->ih_refcnt > 1) {
778 ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
779 DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
780 ihashTable[ihash].ihash_tail, ih_next, ih_prev);
786 DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
791 /* Sync an inode to disk if its handle isn't NULL */
793 ih_condsync(IHandle_t * ihP)
805 code = FDH_SYNC(fdP);
813 /*************************************************************************
814 * OS specific support routines.
815 *************************************************************************/
816 #ifndef AFS_NAMEI_ENV
818 ih_icreate(IHandle_t * ih, int dev, char *part, Inode nI, int p1, int p2,
823 /* See viceinode.h */
824 if (p2 == INODESPECIAL) {
830 ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
833 #endif /* AFS_NAMEI_ENV */
840 struct afs_stat status;
841 if (afs_fstat(fd, &status) < 0)
843 return status.st_size;