revert-large-file-support-20030328
[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     if (!ihP) /* XXX should log here in the fileserver */
268         return NULL;
269
270     IH_LOCK
271
272     /* Do we already have an open file handle for this Inode? */
273     for (fdP = ihP->ih_fdtail ; fdP != NULL ; fdP = fdP->fd_ihprev) {
274         if (fdP->fd_status != FD_HANDLE_INUSE) {
275             assert(fdP->fd_status == FD_HANDLE_OPEN);
276             fdP->fd_status = FD_HANDLE_INUSE;
277             DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
278             ihP->ih_refcnt++;
279             IH_UNLOCK
280             FDH_SEEK(fdP, 0, SEEK_SET);
281             return fdP;
282         }
283     }
284
285     /*
286      * Try to open the Inode, return NULL on error.
287      */
288     fdInUseCount += 1;
289     IH_UNLOCK
290     fd = OS_IOPEN(ihP);
291     IH_LOCK
292     if (fd == INVALID_FD) {
293         fdInUseCount -= 1;
294         IH_UNLOCK
295         return NULL;
296     }
297
298     /* fdCacheSize limits the size of the descriptor cache, but
299      * we permit the number of open files to exceed fdCacheSize.
300      * We only recycle open file descriptors when the number
301      * of open files reaches the size of the cache */
302     if (fdInUseCount > fdCacheSize && fdLruHead != NULL) {
303         fdP = fdLruHead;
304         assert(fdP->fd_status == FD_HANDLE_OPEN);
305         DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
306         DLL_DELETE(fdP, fdP->fd_ih->ih_fdhead, fdP->fd_ih->ih_fdtail,
307             fd_ihnext, fd_ihprev);
308         closeFd = fdP->fd_fd;
309     } else {
310         if (fdAvailHead == NULL) {
311             fdHandleAllocateChunk();
312         }
313         fdP = fdAvailHead;
314         assert(fdP->fd_status == FD_HANDLE_AVAIL);
315         DLL_DELETE(fdP, fdAvailHead, fdAvailTail, fd_next, fd_prev);
316         closeFd = INVALID_FD;
317     }
318
319     fdP->fd_status = FD_HANDLE_INUSE;
320     fdP->fd_fd = fd;
321     fdP->fd_ih = ihP;
322
323     ihP->ih_refcnt++;
324
325     /* Add this handle to the Inode's list of open descriptors */
326     DLL_INSERT_TAIL(fdP, ihP->ih_fdhead, ihP->ih_fdtail, fd_ihnext, fd_ihprev);
327
328     if (closeFd != INVALID_FD) {
329         IH_UNLOCK
330         OS_CLOSE(closeFd);
331         IH_LOCK
332         fdInUseCount -= 1;
333     }
334
335     IH_UNLOCK
336     return fdP;
337 }
338
339 /*
340  * Return a file descriptor handle to the cache
341  */
342 int fd_close(FdHandle_t *fdP)
343 {
344     FD_t closeFd;
345     IHandle_t *ihP;
346
347     if (!fdP)
348         return 0;
349
350     IH_LOCK
351
352     assert(ih_Inited);
353     assert(fdInUseCount > 0);
354     assert(fdP->fd_status == FD_HANDLE_INUSE);
355
356     ihP = fdP->fd_ih;
357
358     /* Call fd_reallyclose to really close the unused file handles if
359      * the previous attempt to close (ih_reallyclose()) all file handles
360      * failed (this is determined by checking the ihandle for the flag
361      * IH_REALLY_CLOSED) or we have too many open files.
362      */
363     if (ihP->ih_flags & IH_REALLY_CLOSED || fdInUseCount > fdCacheSize) {
364         IH_UNLOCK
365         return fd_reallyclose(fdP);
366     }
367
368     /* Put this descriptor back into the cache */
369     fdP->fd_status = FD_HANDLE_OPEN;
370     DLL_INSERT_TAIL(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
371
372     /* If this is not the only reference to the Inode then we can decrement
373      * the reference count, otherwise we need to call ih_release.
374      */
375     if (ihP->ih_refcnt > 1) {
376         ihP->ih_refcnt--;
377         IH_UNLOCK
378     } else {
379         IH_UNLOCK
380         ih_release(ihP);
381     }
382
383     return 0;
384 }
385
386 /*
387  * Actually close the file descriptor handle and return it to
388  * the free list.
389  */
390 int fd_reallyclose(FdHandle_t *fdP)
391 {
392     FD_t closeFd;
393     IHandle_t *ihP;
394
395     if (!fdP)
396         return 0;
397
398     IH_LOCK
399
400     assert(ih_Inited);
401     assert(fdInUseCount > 0);
402     assert(fdP->fd_status == FD_HANDLE_INUSE);
403
404     ihP = fdP->fd_ih;
405     closeFd = fdP->fd_fd;
406
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);
409
410     fdP->fd_status = FD_HANDLE_AVAIL;
411     fdP->fd_ih = NULL;
412     fdP->fd_fd = INVALID_FD;
413
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.
417      */
418     if (!ihP->ih_fdhead) {
419         ihP->ih_flags &= ~IH_REALLY_CLOSED;
420     }
421
422     IH_UNLOCK
423     OS_CLOSE(closeFd);
424     IH_LOCK
425
426     fdInUseCount -= 1;
427
428     /* If this is not the only reference to the Inode then we can decrement
429      * the reference count, otherwise we need to call ih_release. */
430     if (ihP->ih_refcnt > 1) {
431         ihP->ih_refcnt--;
432         IH_UNLOCK
433     } else {
434         IH_UNLOCK
435         ih_release(ihP);
436     }
437
438     return 0;
439 }
440
441 /* Enable buffered I/O on a file descriptor */
442 StreamHandle_t *stream_fdopen(FD_t fd)
443 {
444     StreamHandle_t *streamP;
445
446     IH_LOCK
447     if (streamAvailHead == NULL) {
448         streamHandleAllocateChunk();
449     }
450     streamP = streamAvailHead;
451     DLL_DELETE(streamP, streamAvailHead, streamAvailTail, str_next, str_prev);
452     IH_UNLOCK
453
454     streamP->str_fd = fd;
455     streamP->str_buflen = 0;
456     streamP->str_bufoff = 0;
457     streamP->str_error = 0;
458     streamP->str_eof = 0;
459     streamP->str_direction = STREAM_DIRECTION_NONE;
460     return streamP;
461 }
462
463 /* Open a file for buffered I/O */
464 StreamHandle_t *stream_open(const char *filename, const char *mode)
465 {
466     FD_t fd;
467
468     if (strcmp(mode, "r") == 0) {
469         fd = OS_OPEN(filename, O_RDONLY, 0);
470     } else if (strcmp(mode, "r+") == 0) {
471         fd = OS_OPEN(filename, O_RDWR, 0);
472     } else if (strcmp(mode, "w") == 0) {
473         fd = OS_OPEN(filename, O_WRONLY|O_TRUNC|O_CREAT, 0);
474     } else if (strcmp(mode, "w+") == 0) {
475         fd = OS_OPEN(filename, O_RDWR|O_TRUNC|O_CREAT, 0);
476     } else if (strcmp(mode, "a") == 0) {
477         fd = OS_OPEN(filename, O_WRONLY|O_APPEND|O_CREAT, 0);
478     } else if (strcmp(mode, "a+") == 0) {
479         fd = OS_OPEN(filename, O_RDWR|O_APPEND|O_CREAT, 0);
480     } else {
481         assert(FALSE); /* not implemented */
482     }
483
484     if (fd == INVALID_FD) {
485         return NULL;
486     }
487     return stream_fdopen(fd);
488 }
489
490 /* fread for buffered I/O handles */
491 int stream_read(void *ptr, int size, int nitems, StreamHandle_t *streamP)
492 {
493     int nbytes, bytesRead, bytesToRead;
494     char *p;
495
496     /* Need to seek before changing direction */
497     if (streamP->str_direction == STREAM_DIRECTION_NONE) {
498         streamP->str_direction = STREAM_DIRECTION_READ;
499         streamP->str_bufoff = 0;
500         streamP->str_buflen = 0;
501     } else {
502         assert(streamP->str_direction == STREAM_DIRECTION_READ);
503     }
504
505     bytesRead = 0;
506     nbytes = size * nitems;
507     p = (char *)ptr;
508     while (nbytes > 0 && !streamP->str_eof) {
509         if (streamP->str_buflen == 0) {
510             streamP->str_bufoff = 0;
511             streamP->str_buflen = OS_READ(streamP->str_fd, streamP->str_buffer,
512                                           STREAM_HANDLE_BUFSIZE);
513             if (streamP->str_buflen < 0) {
514                 streamP->str_error = errno;
515                 streamP->str_buflen = 0;
516                 bytesRead = 0;
517                 break;
518             } else if (streamP->str_buflen == 0) {
519                 streamP->str_eof = 1;
520                 break;
521             }
522         }
523
524         bytesToRead = nbytes;
525         if (bytesToRead > streamP->str_buflen) {
526             bytesToRead = streamP->str_buflen;
527         }
528         memcpy(p, streamP->str_buffer+streamP->str_bufoff, bytesToRead);
529         p += bytesToRead;
530         streamP->str_bufoff += bytesToRead;
531         streamP->str_buflen -= bytesToRead;
532         bytesRead += bytesToRead;
533         nbytes -= bytesToRead;
534     }
535
536     return (bytesRead/size);
537 }
538
539 /* fwrite for buffered I/O handles */
540 int stream_write(void *ptr, int size, int nitems, StreamHandle_t *streamP)
541 {
542     char *p;
543     int rc, nbytes, bytesWritten, bytesToWrite;
544
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;
550     } else {
551         assert(streamP->str_direction == STREAM_DIRECTION_WRITE);
552     }
553
554     nbytes = size * nitems;
555     bytesWritten = 0;
556     p = (char *)ptr;
557     while (nbytes > 0) {
558         if (streamP->str_buflen == 0) {
559             rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
560                           STREAM_HANDLE_BUFSIZE);
561             if (rc < 0) {
562                 streamP->str_error = errno;
563                 bytesWritten = 0;
564                 break;
565             }
566             streamP->str_bufoff = 0;
567             streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
568         }
569
570         bytesToWrite = nbytes;
571         if (bytesToWrite > streamP->str_buflen) {
572             bytesToWrite = streamP->str_buflen;
573         }
574         memcpy(streamP->str_buffer+streamP->str_bufoff, p, bytesToWrite);
575         p += bytesToWrite;
576         streamP->str_bufoff += bytesToWrite;
577         streamP->str_buflen -= bytesToWrite;
578         bytesWritten += bytesToWrite;
579         nbytes -= bytesToWrite;
580     }
581
582     return (bytesWritten/size);
583 }
584
585 /* fseek for buffered I/O handles */
586 int stream_seek(StreamHandle_t *streamP, int offset, int whence)
587 {
588     int rc;
589     int retval = 0;
590
591     if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
592         streamP->str_bufoff > 0) {
593         rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
594                       streamP->str_bufoff);
595         if (rc < 0) {
596             streamP->str_error = errno;
597             retval = -1;
598         }
599     }
600     streamP->str_bufoff = 0;
601     streamP->str_buflen = 0;
602     streamP->str_eof = 0;
603     streamP->str_direction = STREAM_DIRECTION_NONE;
604     if (OS_SEEK(streamP->str_fd, offset, whence) < 0) {
605         streamP->str_error = errno;
606         retval = -1;
607     }
608     return retval;
609 }
610
611 /* fflush for buffered I/O handles */
612 int stream_flush(StreamHandle_t *streamP)
613 {
614     int rc;
615     int retval = 0;
616
617     if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
618         streamP->str_bufoff > 0) {
619         rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
620                       streamP->str_bufoff);
621         if (rc < 0) {
622             streamP->str_error = errno;
623             retval = -1;
624         }
625         streamP->str_bufoff = 0;
626         streamP->str_buflen = STREAM_HANDLE_BUFSIZE;
627     }
628
629     return retval;
630 }
631
632 /* Free a buffered I/O handle */
633 int stream_close(StreamHandle_t *streamP, int reallyClose)
634 {
635     int rc;
636     int retval = 0;
637
638     assert(streamP != NULL);
639     if (streamP->str_direction == STREAM_DIRECTION_WRITE &&
640         streamP->str_bufoff > 0) {
641         rc = OS_WRITE(streamP->str_fd, streamP->str_buffer,
642                       streamP->str_bufoff);
643         if (rc < 0) {
644             retval = -1;
645         }
646     }
647     if (reallyClose) {
648         rc = OS_CLOSE(streamP->str_fd);
649         if (rc < 0) {
650             retval = -1;
651         }
652     }
653     streamP->str_fd = INVALID_FD;
654
655     IH_LOCK
656     DLL_INSERT_TAIL(streamP, streamAvailHead, streamAvailTail,
657                     str_next, str_prev);
658     IH_UNLOCK
659
660     return retval;
661 }
662
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.
667  */
668 static int ih_fdclose(IHandle_t *ihP)
669 {
670     int closeCount, closedAll;
671     FdHandle_t *fdP, *head, *tail, *next;
672
673     assert(ihP->ih_refcnt > 0);
674
675     closedAll = 1;
676     DLL_INIT_LIST(head, tail);
677     ihP->ih_flags &= ~IH_REALLY_CLOSED;
678
679     /*
680      * Remove the file descriptors for this Inode from the LRU queue
681      * and the IHandle queue and put them on a temporary queue so we
682      * can drop the lock before we close the files.
683      */
684     for (fdP = ihP->ih_fdhead; fdP != NULL; fdP = next) {
685         next = fdP->fd_ihnext;
686         assert(fdP->fd_ih == ihP);
687         assert(fdP->fd_status == FD_HANDLE_OPEN ||
688                fdP->fd_status == FD_HANDLE_INUSE);
689         if (fdP->fd_status == FD_HANDLE_OPEN) {
690             DLL_DELETE(fdP, ihP->ih_fdhead, ihP->ih_fdtail,
691                 fd_ihnext, fd_ihprev);
692             DLL_DELETE(fdP, fdLruHead, fdLruTail, fd_next, fd_prev);
693             DLL_INSERT_TAIL(fdP, head, tail, fd_next, fd_prev);
694         } else {
695             closedAll = 0;
696             ihP->ih_flags |= IH_REALLY_CLOSED;
697         }
698     }
699
700     /* If the ihandle reference count is 1, we should have
701      * closed all file descriptors.
702      */
703     if (ihP->ih_refcnt == 1 || closedAll) {
704         assert(closedAll);
705         assert(!ihP->ih_fdhead);
706         assert(!ihP->ih_fdtail);
707     }
708
709     if (head == NULL) {
710         return 0;       /* No file descriptors closed */
711     }
712
713     IH_UNLOCK
714
715     /*
716      * Close the file descriptors
717      */
718     closeCount = 0;
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;
723         fdP->fd_ih = NULL;
724         closeCount++;
725     }
726
727     IH_LOCK
728
729     assert(fdInUseCount >= closeCount);
730     fdInUseCount -= closeCount;
731
732     /*
733      * Append the temporary queue to the list of available descriptors
734      */
735     if (fdAvailHead == NULL) {
736         fdAvailHead = head;
737         fdAvailTail = tail;
738     } else {
739         fdAvailTail->fd_next = head;
740         head->fd_prev = fdAvailTail;
741         fdAvailTail = tail;
742     }
743
744     return 0;
745 }
746
747 /* Close all cached file descriptors for this inode. */
748 int ih_reallyclose(IHandle_t *ihP)
749 {
750     if (!ihP)
751         return 0;
752
753     IH_LOCK
754
755     assert(ihP->ih_refcnt > 0);
756     ih_fdclose(ihP);
757
758     IH_UNLOCK
759
760     return 0;
761 }
762
763 /* Release an Inode handle. All cached file descriptors for this
764  * inode are closed when the last reference to this handle is released
765  */
766 int ih_release(IHandle_t *ihP)
767 {
768     int ihash;
769
770     if (!ihP)
771         return 0;
772
773     IH_LOCK
774
775     assert(ihP->ih_refcnt > 0);
776
777     if (ihP->ih_refcnt > 1) {
778         ihP->ih_refcnt--;
779         IH_UNLOCK
780         return 0;
781     }
782
783     ihash = IH_HASH(ihP->ih_dev, ihP->ih_vid, ihP->ih_ino);
784     DLL_DELETE(ihP, ihashTable[ihash].ihash_head,
785         ihashTable[ihash].ihash_tail, ih_next, ih_prev);
786
787     ih_fdclose(ihP);
788
789     ihP->ih_refcnt--;
790
791     DLL_INSERT_TAIL(ihP, ihAvailHead, ihAvailTail, ih_next, ih_prev);
792
793     IH_UNLOCK
794
795     return 0;
796 }
797
798 /* Sync an inode to disk if its handle isn't NULL */
799 int ih_condsync(IHandle_t *ihP)
800 {
801     int code;
802     FdHandle_t *fdP;
803
804     if (!ihP)
805         return 0;
806
807     fdP = IH_OPEN(ihP);
808     if (fdP == NULL)
809         return -1;
810
811     code = FDH_SYNC(fdP);
812     FDH_CLOSE(fdP);
813
814     return code;
815 }
816
817
818
819 /*************************************************************************
820  * OS specific support routines.
821  *************************************************************************/
822 #ifndef AFS_NAMEI_ENV
823 Inode ih_icreate(IHandle_t *ih, int dev, char *part, Inode nI, int p1, int p2,
824               int p3, int p4)
825 {
826     Inode ino;
827 #ifdef  AFS_3DISPARES
828     /* See viceinode.h */
829     if (p2 == INODESPECIAL) {
830         int tp = p3;
831         p3 = p4;
832         p4 = tp;
833     }
834 #endif
835     ino = ICREATE(dev, part, nI, p1, p2, p3, p4);
836     return ino;
837 }
838 #endif /* AFS_NAMEI_ENV */
839
840
841 #ifndef AFS_NT40_ENV
842 int ih_size(int fd)
843 {
844     struct stat status;
845     if (fstat(fd, &status)<0)
846         return -1;
847     return status.st_size;
848 }
849 #endif