2 * This Cplant(TM) source code is the property of Sandia National
5 * This Cplant(TM) source code is copyrighted by Sandia National
8 * The redistribution of this Cplant(TM) source code is subject to the
9 * terms of the GNU Lesser General Public License
10 * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
12 * Cplant(TM) Copyright 1998-2003 Sandia Corporation.
13 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14 * license for use of this work by or on behalf of the US Government.
15 * Export of this program may require a license from the United States
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Questions or comments about this library should be sent to:
37 * Sandia National Laboratories, New Mexico
39 * Albuquerque, NM 87185-1110
48 #include <sys/types.h>
49 #include <sys/queue.h>
56 * Support for file IO.
60 * The open files table
62 typedef struct oftab {
63 struct file **table; /* table array */
64 size_t size; /* current table size */
65 int offset; /* base fd number */
66 int max; /* max size */
69 #define OFTAB_NATIVE (0)
70 #define OFTAB_VIRTUAL (1)
72 static oftab_t _sysio_oftab[2] = {
74 {NULL, 0, 0, 1024*1024},
77 static int native_max_fds = 0;
79 static inline void init_oftab()
81 if (!native_max_fds) {
82 native_max_fds = sysconf(_SC_OPEN_MAX);
83 if (native_max_fds <= 0)
85 _sysio_oftab[OFTAB_NATIVE].max = native_max_fds - 1;
86 _sysio_oftab[OFTAB_VIRTUAL].offset = native_max_fds;
90 static inline oftab_t *select_oftab(int fd)
92 return & _sysio_oftab[fd >= native_max_fds || fd < 0];
96 * Create and initialize open file record.
99 _sysio_fnew(struct inode *ino, int flags)
103 fil = malloc(sizeof(struct file));
107 _SYSIO_FINIT(fil, ino, flags);
114 * Destroy open file record.
117 _sysio_fgone(struct file *fil)
123 err = (*fil->f_ino->i_ops.inop_close)(fil->f_ino);
129 * IO operation completion handler.
132 _sysio_fcompletio(struct ioctx *ioctx, struct file *fil)
136 if (ioctx->ioctx_cc <= 0)
139 assert(ioctx->ioctx_ino == fil->f_ino);
140 off = fil->f_pos + ioctx->ioctx_cc;
141 if (fil->f_pos && off <= fil->f_pos)
147 * Grow (or truncate) the file descriptor table.
150 fd_grow(oftab_t *oftab, size_t n)
154 struct file **noftab, **filp;
157 * Sanity check the new size.
160 if ((size_t )fd != n)
163 n++; /* index -> size */
164 assert(n > oftab->size);
171 if (n - oftab->size < oftab->size)
173 noftab = realloc(oftab->table, n * sizeof(struct file *));
176 oftab->table = noftab;
181 filp = oftab->table + count;
189 static void free_oftab(oftab_t *ot)
200 free_oftab(&_sysio_oftab[OFTAB_NATIVE]);
201 free_oftab(&_sysio_oftab[OFTAB_VIRTUAL]);
206 * Find a free slot in the open files table which >= @low
210 find_free_fildes(oftab_t *oftab, int low)
219 n = low - oftab->offset;
223 for (filp = oftab->table + n;
224 n < oftab->size && *filp;
228 if (n >= oftab->size) {
229 err = fd_grow(oftab, n);
232 filp = &oftab->table[n];
236 return oftab->offset + n;
240 * Find open file record from file descriptor.
241 * clear this entry if 'clear' is non-zero
244 __sysio_fd_get(int fd, int clear)
254 oftab = select_oftab(fd);
255 if (!oftab->table || fd >= oftab->offset + oftab->size)
258 file = oftab->table[fd - oftab->offset];
260 oftab->table[fd - oftab->offset] = NULL;
266 * Find open file record from file descriptor.
269 _sysio_fd_find(int fd)
271 return __sysio_fd_get(fd, 0);
275 * Close an open descriptor.
278 _sysio_fd_close(int fd)
282 fil = fil = __sysio_fd_get(fd, 1);
292 * Associate open file record with given file descriptor (if forced), or any
293 * available file descriptor if less than zero, or any available descriptor
294 * greater than or equal to the given one if not forced.
297 _sysio_fd_set(struct file *fil, int fd, int force)
308 oftab = select_oftab(fd);
311 * Search for a free descriptor if needed.
314 fd = find_free_fildes(oftab, fd);
319 if (fd - oftab->offset >= oftab->size) {
320 err = fd_grow(oftab, fd - oftab->offset);
328 ofil = __sysio_fd_get(fd, 1);
330 /* FIXME sometimes we could intercept open/socket to create
331 * a fd, but missing close()? currently we have this problem
332 * with resolv lib. as a workaround simply destroy the file
333 * struct here. And this hack will break the behavior of
336 if (fd >= 0 && oftab == &_sysio_oftab[0])
342 oftab->table[fd - oftab->offset] = fil;
348 * Duplicate old file descriptor.
350 * If the new file descriptor is less than zero, the new file descriptor
351 * is chosen freely. Otherwise, choose an available descriptor greater
352 * than or equal to the new, if not forced. Otherwise, if forced, (re)use
356 _sysio_fd_dup(int oldfd, int newfd, int force)
366 fil = _sysio_fd_find(oldfd);
370 /* old & new must belong to the same oftab */
371 if (select_oftab(oldfd) != select_oftab(newfd))
374 fd = _sysio_fd_set(fil, newfd, force);
381 _sysio_oftable_close_all(oftab_t *oftab)
386 for (fd = 0, filp = oftab->table;
387 (size_t )fd < oftab->size;
397 _sysio_fd_close_all()
405 * Close all open descriptors.
407 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_VIRTUAL]);
408 /* XXX see liblustre/llite_lib.c for explaination */
410 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_NATIVE]);
414 * Release current working directory.