pull-prototypes-to-head-20020821
[openafs.git] / src / vol / ihandle.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*  ihandle.c   - file descriptor cacheing for Inode handles.           */
11 /*                                                                      */
12 /************************************************************************/
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID("$Header$");
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <errno.h>
22 #ifdef AFS_NT40_ENV
23 #include <fcntl.h>
24 #else
25 #include <sys/file.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
28 #if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
29 #include <sys/fcntl.h>
30 #include <sys/resource.h>
31 #endif
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #else
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39 #endif
40 #include <rx/xdr.h>
41 #include <afs/afsint.h>
42 #include <errno.h>
43 #include <afs/afssyscalls.h>
44 #include "ihandle.h"
45 #include "nfs.h"
46 #include "viceinode.h"
47 #ifdef AFS_PTHREAD_ENV
48 #include <assert.h>
49 #else /* AFS_PTHREAD_ENV */
50 #include "afs/assert.h"
51 #endif /* AFS_PTHREAD_ENV */
52 #include <limits.h>
53
54 extern afs_int32 DErrno;
55
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 */
60
61 /* Linked list of available inode handles */
62 IHandle_t *ihAvailHead;
63 IHandle_t *ihAvailTail;
64
65 /* Linked list of available file descriptor handles */
66 FdHandle_t *fdAvailHead;
67 FdHandle_t *fdAvailTail;
68
69 /* Linked list of available stream descriptor handles */
70 StreamHandle_t *streamAvailHead;
71 StreamHandle_t *streamAvailTail;
72
73 /* LRU list for file descriptor handles */
74 FdHandle_t *fdLruHead;
75 FdHandle_t *fdLruTail;
76
77 int ih_Inited = 0;
78
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;
84 int fdCacheSize = 0;
85
86 /* Number of in use file descriptors */
87 int fdInUseCount = 0;
88
89 /* Hash table for inode handles */
90 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
91
92
93 #ifdef AFS_PTHREAD_ENV
94 /* Initialize the global ihandle mutex */
95 void ih_glock_init()
96 {
97     assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
98 }
99 #endif /* AFS_PTHREAD_ENV */
100
101 /* Initialize the file descriptor cache */
102 void ih_Initialize() {
103     int i;
104     assert(!ih_Inited);
105     ih_Inited = 1;
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);
111     }
112 #if defined(AFS_NT40_ENV)
113     fdMaxCacheSize = FD_MAX_CACHESIZE;
114 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
115     {
116         struct rlimit rlim;
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;
121 #ifdef AFS_NBSD_ENV
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
126          * used up too !
127          *
128          * Check for this better
129          */
130         fdMaxCacheSize /= 4;
131 #endif
132         fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
133         assert(fdMaxCacheSize > 0);
134     }
135 #elif defined(AFS_HPUX_ENV)
136     /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
137     fdMaxCacheSize = 0;
138 #else
139     fdMaxCacheSize = MAX(sysconf(_SC_OPEN_MAX)-FD_HANDLE_SETASIDE, 0);
140     fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
141 #endif
142     fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
143 }
144
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() {
148     IH_LOCK
149
150     if (!ih_Inited) {
151         ih_Initialize();
152     }
153     fdCacheSize = fdMaxCacheSize;
154
155     IH_UNLOCK
156 }
157
158 /* Allocate a chunk of inode handles */
159 void iHandleAllocateChunk()
160 {
161     int i;
162     IHandle_t *ihP;
163
164     assert(ihAvailHead == NULL);
165     ihP = (IHandle_t *)malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
166     assert(ihP != NULL);
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);
170     }
171 }
172
173 /* Initialize an inode handle */
174 IHandle_t *ih_init(int dev, int vid, Inode ino)
175 {
176     int ihash = IH_HASH(dev, vid, ino);
177     IHandle_t *ihP;
178
179     IH_LOCK
180
181     if (!ih_Inited) {
182         ih_Initialize();
183     }
184
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) {
188             ihP->ih_refcnt++;
189             IH_UNLOCK
190             return ihP;
191         }
192     }
193
194     /* Allocate and initialize a new Inode handle */
195     if (ihAvailHead == NULL) {
196         iHandleAllocateChunk();
197     }
198     ihP = ihAvailHead;
199     assert(ihP->ih_refcnt == 0);
200     DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
201     ihP->ih_dev = dev;
202     ihP->ih_vid = vid;
203     ihP->ih_ino = ino;
204     ihP->ih_flags = 0;
205     ihP->ih_refcnt = 1;
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);
209     IH_UNLOCK
210     return ihP;
211 }
212
213 /* Copy an inode handle */
214 IHandle_t *ih_copy(IHandle_t *ihP)
215 {
216     IH_LOCK
217     assert(ih_Inited);
218     assert(ihP->ih_refcnt > 0);
219     ihP->ih_refcnt++;
220     IH_UNLOCK
221     return ihP;
222 }
223
224 /* Allocate a chunk of file descriptor handles */
225 void fdHandleAllocateChunk()
226 {
227     int i;
228     FdHandle_t *fdP;
229
230     assert(fdAvailHead == NULL);
231     fdP = (FdHandle_t *)malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
232     assert(fdP != NULL);
233     for (i = 0 ; i < FD_HANDLE_MALLOCSIZE ; i++) {
234         fdP[i].fd_status = FD_HANDLE_AVAIL;
235         fdP[i].fd_ih = NULL;
236         fdP[i].fd_fd = INVALID_FD;
237         DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
238     }
239 }
240
241 /* Allocate a chunk of stream handles */
242 void streamHandleAllocateChunk()
243 {
244     int i;
245     StreamHandle_t *streamP;
246
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,
254                         str_next, str_prev);
255     }
256 }
257
258 /*
259  * Get a file descriptor handle given an Inode handle
260  */
261 FdHandle_t *ih_open(IHandle_t *ihP)
262 {
263     FdHandle_t *fdP;
264     FD_t fd;
265     FD_t closeFd;
266
267     IH_LOCK
268
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);
275             ihP->ih_refcnt++;
276             IH_UNLOCK
277             FDH_SEEK(fdP, 0, SEEK_SET);
278             return fdP;
279         }
280     }
281
282     /*
283      * Try to open the Inode, return NULL on error.
284      */
285     fdInUseCount += 1;
286     IH_UNLOCK
287     fd = OS_IOPEN(ihP);
288     IH_LOCK
289     if (fd == INVALID_FD) {
290         fdInUseCount -= 1;
291         IH_UNLOCK
292         return NULL;
293     }
294
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) {
300         fdP = fdLruHead;
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;
306     } else {
307         if (fdAvailHead == NULL) {
308             fdHandleAllocateChunk();
309         }
310         fdP = fdAvailHead;
311         assert(fdP->fd_status == FD_HANDLE_AVAIL);
312         DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
313         closeFd = INVALID_FD;
314     }
315
316     fdP->fd_status = FD_HANDLE_INUSE;
317     fdP->fd_fd = fd;
318     fdP->fd_ih = ihP;
319
320     ihP->ih_refcnt++;
321
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);
324
325     if (closeFd != INVALID_FD) {
326         IH_UNLOCK
327         OS_CLOSE(closeFd);
328         IH_LOCK
329         fdInUseCount -= 1;
330     }
331
332     IH_UNLOCK
333     return fdP;
334 }
335
336 /*
337  * Return a file descriptor handle to the cache
338  */
339 int fd_close(FdHandle_t *fdP)
340 {
341     FD_t closeFd;
342     IHandle_t *ihP;
343
344     if (!fdP)
345         return 0;
346
347     IH_LOCK
348
349     assert(ih_Inited);
350     assert(fdInUseCount > 0);
351     assert(fdP->fd_status == FD_HANDLE_INUSE);
352
353     ihP = fdP->fd_ih;
354
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.
359      */
360     if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
361         IH_UNLOCK
362         return fd_reallyclose(fdP);
363     }
364
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);
368
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.
371      */
372     if (ihP->ih_refcnt > 1) {
373         ihP->ih_refcnt--;
374         IH_UNLOCK
375     } else {
376         IH_UNLOCK
377         ih_release(ihP);
378     }
379
380     return 0;
381 }
382
383 /*
384  * Actually close the file descriptor handle and return it to
385  * the free list.
386  */
387 int fd_reallyclose(FdHandle_t *fdP)
388 {
389     FD_t closeFd;
390     IHandle_t *ihP;
391
392     if (!fdP)
393         return 0;
394
395     IH_LOCK
396
397     assert(ih_Inited);
398     assert(fdInUseCount > 0);
399     assert(fdP->fd_status == FD_HANDLE_INUSE);
400
401     ihP = fdP->fd_ih;
402     closeFd = fdP->fd_fd;
403
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);
406
407     fdP->fd_status = FD_HANDLE_AVAIL;
408     fdP->fd_ih = NULL;
409     fdP->fd_fd = INVALID_FD;
410
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.
414      */
415     if (!ihP->ih_fdhead) {
416         ihP->ih_flags &= ~IH_REALLY_CLOSED;
417     }
418
419     IH_UNLOCK
420     OS_CLOSE(closeFd);
421     IH_LOCK
422
423     fdInUseCount -= 1;
424
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) {
428         ihP->ih_refcnt--;
429         IH_UNLOCK
430     } else {
431         IH_UNLOCK
432         ih_release(ihP);
433     }
434
435     return 0;
436 }
437
438 /* Enable buffered I/O on a file descriptor */
439 StreamHandle_t *stream_fdopen(FD_t fd)
440 {
441     StreamHandle_t *streamP;
442
443     IH_LOCK
444     if (streamAvailHead == NULL) {
445         streamHandleAllocateChunk();
446     }
447     streamP = streamAvailHead;
448     DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
449     IH_UNLOCK
450
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;
457     return streamP;
458 }
459
460 /* Open a file for buffered I/O */
461 StreamHandle_t *stream_open(const char *filename, const char *mode)
462 {
463     FD_t fd;
464
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);
477     } else {
478         assert(FALSE); /* not implemented */
479     }
480
481     if (fd == INVALID_FD) {
482         return NULL;
483     }
484     return stream_fdopen(fd);
485 }
486
487 /* fread for buffered I/O handles */
488 int stream_read(void *ptr, int size, int nitems, StreamHandle_t *streamP)
489 {
490     int nbytes, bytesRead, bytesToRead;
491     char *p;
492
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;
498     } else {
499         assert(streamP->str_direction == STREAM_DIRECTION_READ);
500     }
501
502     bytesRead = 0;
503     nbytes = size * nitems;
504     p = (char *)ptr;
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;
513                 bytesRead = 0;
514                 break;
515             } else if (streamP->str_buflen == 0) {
516                 streamP->str_eof = 1;
517                 break;
518             }
519         }
520
521         bytesToRead = nbytes;
522         if (bytesToRead > streamP->str_buflen) {
523             bytesToRead = streamP->str_buflen;
524         }
525         memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
526         p += bytesToRead;
527         streamP->str_bufoff += bytesToRead;
528         streamP->str_buflen -= bytesToRead;
529         bytesRead += bytesToRead;
530         nbytes -= bytesToRead;
531     }
532
533     return (bytesRead/size);
534 }
535
536 /* fwrite for buffered I/O handles */
537 int stream_write(void *ptr, int size, int nitems, StreamHandle_t *streamP)
538 {
539     char *p;
540     int rc, nbytes, bytesWritten, bytesToWrite;
541
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;
547     } else {
548         assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
549     }
550
551     nbytes = size * nitems;
552     bytesWritten = 0;
553     p = (char *)ptr;
554     while (nbytes > 0) {
555         if (streamP->str_buflen == 0) {
556             rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
557                           STREAM_HANDLE_BUFSIZE);
558             if (rc < 0) {
559                 streamP->str_error = errno;
560                 bytesWritten = 0;
561                 break;
562             }
563             streamP->str_bufoff = 0;
564             streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
565         }
566
567         bytesToWrite = nbytes;
568         if (bytesToWrite > streamP->str_buflen) {
569             bytesToWrite = streamP->str_buflen;
570         }
571         memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
572         p += bytesToWrite;
573         streamP->str_bufoff += bytesToWrite;
574         streamP->str_buflen -= bytesToWrite;
575         bytesWritten += bytesToWrite;
576         nbytes -= bytesToWrite;
577     }
578
579     return (bytesWritten/size);
580 }
581
582 /* fseek for buffered I/O handles */
583 int stream_seek(StreamHandle_t *streamP, int offset, int whence)
584 {
585     int rc;
586     int retval = 0;
587
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);
592         if (rc < 0) {
593             streamP->str_error = errno;
594             retval = -1;
595         }
596     }
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;
603         retval = -1;
604     }
605     return retval;
606 }
607
608 /* fflush for buffered I/O handles */
609 int stream_flush(StreamHandle_t *streamP)
610 {
611     int rc;
612     int retval = 0;
613
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);
618         if (rc < 0) {
619             streamP->str_error = errno;
620             retval = -1;
621         }
622         streamP->str_bufoff = 0;
623         streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
624     }
625
626     return retval;
627 }
628
629 /* Free a buffered I/O handle */
630 int stream_close(StreamHandle_t *streamP, int reallyClose)
631 {
632     int rc;
633     int retval = 0;
634
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);
640         if (rc < 0) {
641             retval = -1;
642         }
643     }
644     if (reallyClose) {
645         rc = OS_CLOSE(streamP->str_fd);
646         if (rc < 0) {
647             retval = -1;
648         }
649     }
650     streamP->str_fd = INVALID_FD;
651
652     IH_LOCK
653     DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
654                     str_next, str_prev);
655     IH_UNLOCK
656
657     return retval;
658 }
659
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.
664  */
665 static int ih_fdclose(IHandle_t *ihP)
666 {
667     int closeCount, closedAll;
668     FdHandle_t *fdP, *head, *tail, *next;
669
670     assert(ihP->ih_refcnt > 0);
671
672     closedAll = 1;
673     DLL_INIT_LIST(head, tail);
674     ihP->ih_flags &= ~IH_REALLY_CLOSED;
675
676     /*
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.
680      */
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);
691         } else {
692             closedAll = 0;
693             ihP->ih_flags |= IH_REALLY_CLOSED;
694         }
695     }
696
697     /* If the ihandle reference count is 1, we should have
698      * closed all file descriptors.
699      */
700     if (ihP->ih_refcnt == 1 || closedAll) {
701         assert(closedAll);
702         assert(!ihP->ih_fdhead);
703         assert(!ihP->ih_fdtail);
704     }
705
706     if (head == NULL) {
707         return 0;       /* No file descriptors closed */
708     }
709
710     IH_UNLOCK
711
712     /*
713      * Close the file descriptors
714      */
715     closeCount = 0;
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;
720         fdP->fd_ih = NULL;
721         closeCount++;
722     }
723
724     IH_LOCK
725
726     assert(fdInUseCount >= closeCount);
727     fdInUseCount -= closeCount;
728
729     /*
730      * Append the temporary queue to the list of available descriptors
731      */
732     if (fdAvailHead == NULL) {
733         fdAvailHead = head;
734         fdAvailTail = tail;
735     } else {
736         fdAvailTail->fd_next = head;
737         head->fd_prev = fdAvailTail;
738         fdAvailTail = tail;
739     }
740
741     return 0;
742 }
743
744 /* Close all cached file descriptors for this inode. */
745 int ih_reallyclose(IHandle_t *ihP)
746 {
747     if (!ihP)
748         return 0;
749
750     IH_LOCK
751
752     assert(ihP->ih_refcnt > 0);
753     ih_fdclose(ihP);
754
755     IH_UNLOCK
756
757     return 0;
758 }
759
760 /* Release an Inode handle. All cached file descriptors for this
761  * inode are closed when the last reference to this handle is released
762  */
763 int ih_release(IHandle_t *ihP)
764 {
765     int ihash;
766
767     if (!ihP)
768         return 0;
769
770     IH_LOCK
771
772     assert(ihP->ih_refcnt > 0);
773
774     if (ihP->ih_refcnt > 1) {
775         ihP->ih_refcnt--;
776         IH_UNLOCK
777         return 0;
778     }
779
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);
783
784     ih_fdclose(ihP);
785
786     ihP->ih_refcnt--;
787
788     DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
789
790     IH_UNLOCK
791
792     return 0;
793 }
794
795 /* Sync an inode to disk if its handle isn't NULL */
796 int ih_condsync(IHandle_t *ihP)
797 {
798     int code;
799     FdHandle_t *fdP;
800
801     if (!ihP)
802         return 0;
803
804     fdP = IH_OPEN(ihP);
805     if (fdP == NULL)
806         return -1;
807
808     code = FDH_SYNC(fdP);
809     FDH_CLOSE(fdP);
810
811     return code;
812 }
813
814
815
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,
821               int p3, int p4)
822 {
823     Inode ino;
824 #ifdef  AFS_3DISPARES
825     /* See viceinode.h */
826     if (p2 == INODESPECIAL) {
827         int tp = p3;
828         p3 = p4;
829         p4 = tp;
830     }
831 #endif
832     ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
833     return ino;
834 }
835 #endif /* AFS_NAMEI_ENV */
836
837
838 #ifndef AFS_NT40_ENV
839 int ih_size(int fd)
840 {
841     struct stat status;
842     if (fstat(fd, &status)<0)
843         return -1;
844     return status.st_size;
845 }
846 #endif