fileserver-largefile-support-20020107
[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 #ifdef AFS_LARGEFILE_ENV
26 #include <fcntl.h>
27 #else
28 #include <sys/file.h>
29 #endif
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #if defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
33 #ifndef AFS_LARGEFILE_ENV
34 #include <sys/fcntl.h>
35 #endif
36 #include <sys/resource.h>
37 #endif
38 #endif
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #else
42 #ifdef HAVE_STRINGS_H
43 #include <strings.h>
44 #endif
45 #endif
46 #include <rx/xdr.h>
47 #include <afs/afsint.h>
48 #include <errno.h>
49 #include <afs/afssyscalls.h>
50 #include "ihandle.h"
51 #include "nfs.h"
52 #include "viceinode.h"
53 #ifdef AFS_PTHREAD_ENV
54 #include <assert.h>
55 #else /* AFS_PTHREAD_ENV */
56 #include "afs/assert.h"
57 #endif /* AFS_PTHREAD_ENV */
58 #include <limits.h>
59
60 extern afs_int32 DErrno;
61
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 */
66
67 /* Linked list of available inode handles */
68 IHandle_t *ihAvailHead;
69 IHandle_t *ihAvailTail;
70
71 /* Linked list of available file descriptor handles */
72 FdHandle_t *fdAvailHead;
73 FdHandle_t *fdAvailTail;
74
75 /* Linked list of available stream descriptor handles */
76 StreamHandle_t *streamAvailHead;
77 StreamHandle_t *streamAvailTail;
78
79 /* LRU list for file descriptor handles */
80 FdHandle_t *fdLruHead;
81 FdHandle_t *fdLruTail;
82
83 int ih_Inited = 0;
84
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;
90 int fdCacheSize = 0;
91
92 /* Number of in use file descriptors */
93 int fdInUseCount = 0;
94
95 /* Hash table for inode handles */
96 IHashBucket_t ihashTable[I_HANDLE_HASH_SIZE];
97
98
99 #ifdef AFS_PTHREAD_ENV
100 /* Initialize the global ihandle mutex */
101 void ih_glock_init()
102 {
103     assert(pthread_mutex_init(&ih_glock_mutex, NULL) == 0);
104 }
105 #endif /* AFS_PTHREAD_ENV */
106
107 /* Initialize the file descriptor cache */
108 void ih_Initialize(void) {
109     int i;
110     assert(!ih_Inited);
111     ih_Inited = 1;
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);
117     }
118 #if defined(AFS_NT40_ENV)
119     fdMaxCacheSize = FD_MAX_CACHESIZE;
120 #elif defined(AFS_SUN5_ENV) || defined(AFS_NBSD_ENV)
121     {
122         struct rlimit rlim;
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;
127 #ifdef AFS_NBSD_ENV
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
132          * used up too !
133          *
134          * Check for this better
135          */
136         fdMaxCacheSize /= 4;
137 #endif
138         fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
139         assert(fdMaxCacheSize > 0);
140     }
141 #elif defined(AFS_HPUX_ENV)
142     /* Avoid problems with "UFSOpen: igetinode failed" panics on HPUX 11.0 */
143     fdMaxCacheSize = 0;
144 #else
145     fdMaxCacheSize = MAX(sysconf(_SC_OPEN_MAX)-FD_HANDLE_SETASIDE, 0);
146     fdMaxCacheSize = MIN(fdMaxCacheSize, FD_MAX_CACHESIZE);
147 #endif
148     fdCacheSize = MIN(fdMaxCacheSize, FD_DEFAULT_CACHESIZE);
149 }
150
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) {
154     IH_LOCK
155
156     if (!ih_Inited) {
157         ih_Initialize();
158     }
159     fdCacheSize = fdMaxCacheSize;
160
161     IH_UNLOCK
162 }
163
164 /* Allocate a chunk of inode handles */
165 void iHandleAllocateChunk(void)
166 {
167     int i;
168     IHandle_t *ihP;
169
170     assert(ihAvailHead == NULL);
171     ihP = (IHandle_t *)malloc(I_HANDLE_MALLOCSIZE * sizeof(IHandle_t));
172     assert(ihP != NULL);
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);
176     }
177 }
178
179 /* Initialize an inode handle */
180 IHandle_t *ih_init(int dev, int vid, Inode ino)
181 {
182     int ihash = IH_HASH(dev, vid, ino);
183     IHandle_t *ihP;
184
185     IH_LOCK
186
187     if (!ih_Inited) {
188         ih_Initialize();
189     }
190
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) {
194             ihP->ih_refcnt++;
195             IH_UNLOCK
196             return ihP;
197         }
198     }
199
200     /* Allocate and initialize a new Inode handle */
201     if (ihAvailHead == NULL) {
202         iHandleAllocateChunk();
203     }
204     ihP = ihAvailHead;
205     assert(ihP->ih_refcnt == 0);
206     DLL_DELETE(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
207     ihP->ih_dev = dev;
208     ihP->ih_vid = vid;
209     ihP->ih_ino = ino;
210     ihP->ih_flags = 0;
211     ihP->ih_refcnt = 1;
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);
215     IH_UNLOCK
216     return ihP;
217 }
218
219 /* Copy an inode handle */
220 IHandle_t *ih_copy(IHandle_t *ihP)
221 {
222     IH_LOCK
223     assert(ih_Inited);
224     assert(ihP->ih_refcnt > 0);
225     ihP->ih_refcnt++;
226     IH_UNLOCK
227     return ihP;
228 }
229
230 /* Allocate a chunk of file descriptor handles */
231 void fdHandleAllocateChunk(void)
232 {
233     int i;
234     FdHandle_t *fdP;
235
236     assert(fdAvailHead == NULL);
237     fdP = (FdHandle_t *)malloc(FD_HANDLE_MALLOCSIZE * sizeof(FdHandle_t));
238     assert(fdP != NULL);
239     for (i = 0 ; i < FD_HANDLE_MALLOCSIZE ; i++) {
240         fdP[i].fd_status = FD_HANDLE_AVAIL;
241         fdP[i].fd_ih = NULL;
242         fdP[i].fd_fd = INVALID_FD;
243         DLL_INSERT_TAIL(&fdP[i], fdAvailHead, fdAvailTail, fd_next, fd_prev);
244     }
245 }
246
247 /* Allocate a chunk of stream handles */
248 void streamHandleAllocateChunk(void)
249 {
250     int i;
251     StreamHandle_t *streamP;
252
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,
260                         str_next, str_prev);
261     }
262 }
263
264 /*
265  * Get a file descriptor handle given an Inode handle
266  */
267 FdHandle_t *ih_open(IHandle_t *ihP)
268 {
269     FdHandle_t *fdP;
270     FD_t fd;
271     FD_t closeFd;
272
273     if (!ihP) /* XXX should log here in the fileserver */
274         return NULL;
275
276     IH_LOCK
277
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);
284             ihP->ih_refcnt++;
285             IH_UNLOCK
286             FDH_SEEK(fdP, 0, SEEK_SET);
287             return fdP;
288         }
289     }
290
291     /*
292      * Try to open the Inode, return NULL on error.
293      */
294     fdInUseCount += 1;
295     IH_UNLOCK
296     fd = OS_IOPEN(ihP);
297     IH_LOCK
298     if (fd == INVALID_FD) {
299         fdInUseCount -= 1;
300         IH_UNLOCK
301         return NULL;
302     }
303
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) {
309         fdP = fdLruHead;
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;
315     } else {
316         if (fdAvailHead == NULL) {
317             fdHandleAllocateChunk();
318         }
319         fdP = fdAvailHead;
320         assert(fdP->fd_status == FD_HANDLE_AVAIL);
321         DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
322         closeFd = INVALID_FD;
323     }
324
325     fdP->fd_status = FD_HANDLE_INUSE;
326     fdP->fd_fd = fd;
327     fdP->fd_ih = ihP;
328
329     ihP->ih_refcnt++;
330
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);
333
334     if (closeFd != INVALID_FD) {
335         IH_UNLOCK
336         OS_CLOSE(closeFd);
337         IH_LOCK
338         fdInUseCount -= 1;
339     }
340
341     IH_UNLOCK
342     return fdP;
343 }
344
345 /*
346  * Return a file descriptor handle to the cache
347  */
348 int fd_close(FdHandle_t *fdP)
349 {
350     FD_t closeFd;
351     IHandle_t *ihP;
352
353     if (!fdP)
354         return 0;
355
356     IH_LOCK
357
358     assert(ih_Inited);
359     assert(fdInUseCount > 0);
360     assert(fdP->fd_status == FD_HANDLE_INUSE);
361
362     ihP = fdP->fd_ih;
363
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.
368      */
369     if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
370         IH_UNLOCK
371         return fd_reallyclose(fdP);
372     }
373
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);
377
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.
380      */
381     if (ihP->ih_refcnt > 1) {
382         ihP->ih_refcnt--;
383         IH_UNLOCK
384     } else {
385         IH_UNLOCK
386         ih_release(ihP);
387     }
388
389     return 0;
390 }
391
392 /*
393  * Actually close the file descriptor handle and return it to
394  * the free list.
395  */
396 int fd_reallyclose(FdHandle_t *fdP)
397 {
398     FD_t closeFd;
399     IHandle_t *ihP;
400
401     if (!fdP)
402         return 0;
403
404     IH_LOCK
405
406     assert(ih_Inited);
407     assert(fdInUseCount > 0);
408     assert(fdP->fd_status == FD_HANDLE_INUSE);
409
410     ihP = fdP->fd_ih;
411     closeFd = fdP->fd_fd;
412
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);
415
416     fdP->fd_status = FD_HANDLE_AVAIL;
417     fdP->fd_ih = NULL;
418     fdP->fd_fd = INVALID_FD;
419
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.
423      */
424     if (!ihP->ih_fdhead) {
425         ihP->ih_flags &= ~IH_REALLY_CLOSED;
426     }
427
428     IH_UNLOCK
429     OS_CLOSE(closeFd);
430     IH_LOCK
431
432     fdInUseCount -= 1;
433
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) {
437         ihP->ih_refcnt--;
438         IH_UNLOCK
439     } else {
440         IH_UNLOCK
441         ih_release(ihP);
442     }
443
444     return 0;
445 }
446
447 /* Enable buffered I/O on a file descriptor */
448 StreamHandle_t *stream_fdopen(FD_t fd)
449 {
450     StreamHandle_t *streamP;
451
452     IH_LOCK
453     if (streamAvailHead == NULL) {
454         streamHandleAllocateChunk();
455     }
456     streamP = streamAvailHead;
457     DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
458     IH_UNLOCK
459
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;
466     return streamP;
467 }
468
469 /* Open a file for buffered I/O */
470 StreamHandle_t *stream_open(const char *filename, const char *mode)
471 {
472     FD_t fd;
473
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);
486     } else {
487         assert(FALSE); /* not implemented */
488     }
489
490     if (fd == INVALID_FD) {
491         return NULL;
492     }
493     return stream_fdopen(fd);
494 }
495
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)
499 {
500     afs_size_t nbytes, bytesRead, bytesToRead;
501     char *p;
502
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;
508     } else {
509         assert(streamP->str_direction == STREAM_DIRECTION_READ);
510     }
511
512     bytesRead = 0;
513     nbytes = size * nitems;
514     p = (char *)ptr;
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;
523                 bytesRead = 0;
524                 break;
525             } else if (streamP->str_buflen == 0) {
526                 streamP->str_eof = 1;
527                 break;
528             }
529         }
530
531         bytesToRead = nbytes;
532         if (bytesToRead > streamP->str_buflen) {
533             bytesToRead = streamP->str_buflen;
534         }
535         memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
536         p += bytesToRead;
537         streamP->str_bufoff += bytesToRead;
538         streamP->str_buflen -= bytesToRead;
539         bytesRead += bytesToRead;
540         nbytes -= bytesToRead;
541     }
542
543     return (bytesRead/size);
544 }
545
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)
549 {
550     char *p;
551     afs_size_t rc, nbytes, bytesWritten, bytesToWrite;
552
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;
558     } else {
559         assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
560     }
561
562     nbytes = size * nitems;
563     bytesWritten = 0;
564     p = (char *)ptr;
565     while (nbytes > 0) {
566         if (streamP->str_buflen == 0) {
567             rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
568                           STREAM_HANDLE_BUFSIZE);
569             if (rc < 0) {
570                 streamP->str_error = errno;
571                 bytesWritten = 0;
572                 break;
573             }
574             streamP->str_bufoff = 0;
575             streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
576         }
577
578         bytesToWrite = nbytes;
579         if (bytesToWrite > streamP->str_buflen) {
580             bytesToWrite = streamP->str_buflen;
581         }
582         memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
583         p += bytesToWrite;
584         streamP->str_bufoff += bytesToWrite;
585         streamP->str_buflen -= bytesToWrite;
586         bytesWritten += bytesToWrite;
587         nbytes -= bytesToWrite;
588     }
589
590     return (bytesWritten/size);
591 }
592
593 /* fseek for buffered I/O handles */
594 int stream_seek(StreamHandle_t *streamP, afs_size_t offset, int whence)
595 {
596     int rc;
597     int retval = 0;
598
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);
603         if (rc < 0) {
604             streamP->str_error = errno;
605             retval = -1;
606         }
607     }
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;
614         retval = -1;
615     }
616     return retval;
617 }
618
619 /* fflush for buffered I/O handles */
620 int stream_flush(StreamHandle_t *streamP)
621 {
622     int rc;
623     int retval = 0;
624
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);
629         if (rc < 0) {
630             streamP->str_error = errno;
631             retval = -1;
632         }
633         streamP->str_bufoff = 0;
634         streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
635     }
636
637     return retval;
638 }
639
640 /* Free a buffered I/O handle */
641 int stream_close(StreamHandle_t *streamP, int reallyClose)
642 {
643     int rc;
644     int retval = 0;
645
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);
651         if (rc < 0) {
652             retval = -1;
653         }
654     }
655     if (reallyClose) {
656         rc = OS_CLOSE(streamP->str_fd);
657         if (rc < 0) {
658             retval = -1;
659         }
660     }
661     streamP->str_fd = INVALID_FD;
662
663     IH_LOCK
664     DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
665                     str_next, str_prev);
666     IH_UNLOCK
667
668     return retval;
669 }
670
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.
675  */
676 static int ih_fdclose(IHandle_t *ihP)
677 {
678     int closeCount, closedAll;
679     FdHandle_t *fdP, *head, *tail, *next;
680
681     assert(ihP->ih_refcnt > 0);
682
683     closedAll = 1;
684     DLL_INIT_LIST(head, tail);
685     ihP->ih_flags &= ~IH_REALLY_CLOSED;
686
687     /*
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.
691      */
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);
702         } else {
703             closedAll = 0;
704             ihP->ih_flags |= IH_REALLY_CLOSED;
705         }
706     }
707
708     /* If the ihandle reference count is 1, we should have
709      * closed all file descriptors.
710      */
711     if (ihP->ih_refcnt == 1 || closedAll) {
712         assert(closedAll);
713         assert(!ihP->ih_fdhead);
714         assert(!ihP->ih_fdtail);
715     }
716
717     if (head == NULL) {
718         return 0;       /* No file descriptors closed */
719     }
720
721     IH_UNLOCK
722
723     /*
724      * Close the file descriptors
725      */
726     closeCount = 0;
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;
731         fdP->fd_ih = NULL;
732         closeCount++;
733     }
734
735     IH_LOCK
736
737     assert(fdInUseCount >= closeCount);
738     fdInUseCount -= closeCount;
739
740     /*
741      * Append the temporary queue to the list of available descriptors
742      */
743     if (fdAvailHead == NULL) {
744         fdAvailHead = head;
745         fdAvailTail = tail;
746     } else {
747         fdAvailTail->fd_next = head;
748         head->fd_prev = fdAvailTail;
749         fdAvailTail = tail;
750     }
751
752     return 0;
753 }
754
755 /* Close all cached file descriptors for this inode. */
756 int ih_reallyclose(IHandle_t *ihP)
757 {
758     if (!ihP)
759         return 0;
760
761     IH_LOCK
762
763     assert(ihP->ih_refcnt > 0);
764     ih_fdclose(ihP);
765
766     IH_UNLOCK
767
768     return 0;
769 }
770
771 /* Release an Inode handle. All cached file descriptors for this
772  * inode are closed when the last reference to this handle is released
773  */
774 int ih_release(IHandle_t *ihP)
775 {
776     int ihash;
777
778     if (!ihP)
779         return 0;
780
781     IH_LOCK
782
783     assert(ihP->ih_refcnt > 0);
784
785     if (ihP->ih_refcnt > 1) {
786         ihP->ih_refcnt--;
787         IH_UNLOCK
788         return 0;
789     }
790
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);
794
795     ih_fdclose(ihP);
796
797     ihP->ih_refcnt--;
798
799     DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
800
801     IH_UNLOCK
802
803     return 0;
804 }
805
806 /* Sync an inode to disk if its handle isn't NULL */
807 int ih_condsync(IHandle_t *ihP)
808 {
809     int code;
810     FdHandle_t *fdP;
811
812     if (!ihP)
813         return 0;
814
815     fdP = IH_OPEN(ihP);
816     if (fdP == NULL)
817         return -1;
818
819     code = FDH_SYNC(fdP);
820     FDH_CLOSE(fdP);
821
822     return code;
823 }
824
825
826
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,
832               int p3, int p4)
833 {
834     Inode ino;
835 #ifdef  AFS_3DISPARES
836     /* See viceinode.h */
837     if (p2 == INODESPECIAL) {
838         int tp = p3;
839         p3 = p4;
840         p4 = tp;
841     }
842 #endif
843     ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
844     return ino;
845 }
846 #endif /* AFS_NAMEI_ENV */
847
848
849 #ifndef AFS_NT40_ENV
850 afs_size_t ih_size(int fd)
851 {
852 #ifdef AFS_LARGEFILE_ENV
853     struct stat64 status;
854     if (fstat64(fd, &status)<0)
855         return -1;
856 #else /* !AFS_LARGEFILE_ENV */
857     struct stat status;
858     if (fstat(fd, &status)<0)
859         return -1;
860 #endif /* !AFS_LARGEFILE_ENV */
861     return status.st_size;
862 }
863 #endif