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>
50 #include <sys/queue.h>
57 * Support for file IO.
61 * The open files table
63 typedef struct oftab {
64 struct file **table; /* table array */
65 size_t size; /* current table size */
66 int offset; /* base fd number */
67 int max; /* max size */
70 #define OFTAB_NATIVE (0)
71 #define OFTAB_VIRTUAL (1)
73 static oftab_t _sysio_oftab[2] = {
75 {NULL, 0, 0, 1024*1024},
78 static int native_max_fds = 0;
80 static inline void init_oftab()
82 if (!native_max_fds) {
83 native_max_fds = sysconf(_SC_OPEN_MAX);
84 if (native_max_fds <= 0)
86 _sysio_oftab[OFTAB_NATIVE].max = native_max_fds - 1;
87 _sysio_oftab[OFTAB_VIRTUAL].offset = native_max_fds;
91 static inline oftab_t *select_oftab(int fd)
93 return & _sysio_oftab[fd >= native_max_fds || fd < 0];
97 * Create and initialize open file record.
100 _sysio_fnew(struct inode *ino, int flags)
104 fil = malloc(sizeof(struct file));
108 _SYSIO_FINIT(fil, ino, flags);
115 * Destroy open file record.
118 _sysio_fgone(struct file *fil)
124 err = (*fil->f_ino->i_ops.inop_close)(fil->f_ino);
130 * IO operation completion handler.
133 _sysio_fcompletio(struct ioctx *ioctx, struct file *fil)
137 if (ioctx->ioctx_cc <= 0)
140 assert(ioctx->ioctx_ino == fil->f_ino);
141 off = fil->f_pos + ioctx->ioctx_cc;
142 if (fil->f_pos && off <= fil->f_pos)
148 * Grow (or truncate) the file descriptor table.
151 fd_grow(oftab_t *oftab, size_t n)
155 struct file **noftab, **filp;
158 * Sanity check the new size.
161 if ((size_t )fd != n)
164 n++; /* index -> size */
165 assert(n > oftab->size);
172 if (n - oftab->size < oftab->size)
174 noftab = realloc(oftab->table, n * sizeof(struct file *));
177 oftab->table = noftab;
182 filp = oftab->table + count;
190 static void free_oftab(oftab_t *ot)
201 free_oftab(&_sysio_oftab[OFTAB_NATIVE]);
202 free_oftab(&_sysio_oftab[OFTAB_VIRTUAL]);
207 * Find a free slot in the open files table which >= @low
211 find_free_fildes(oftab_t *oftab, int low)
220 n = low - oftab->offset;
224 for (filp = oftab->table + n;
225 n < oftab->size && *filp;
229 if (n >= oftab->size) {
230 err = fd_grow(oftab, n);
233 filp = &oftab->table[n];
237 return oftab->offset + n;
241 * Find open file record from file descriptor.
242 * clear this entry if 'clear' is non-zero
245 __sysio_fd_get(int fd, int clear)
255 oftab = select_oftab(fd);
256 if (!oftab->table || fd >= oftab->offset + oftab->size)
259 file = oftab->table[fd - oftab->offset];
261 oftab->table[fd - oftab->offset] = NULL;
267 * Find open file record from file descriptor.
270 _sysio_fd_find(int fd)
272 return __sysio_fd_get(fd, 0);
276 * Close an open descriptor.
279 _sysio_fd_close(int fd)
283 fil = fil = __sysio_fd_get(fd, 1);
293 * Associate open file record with given file descriptor (if forced), or any
294 * available file descriptor if less than zero, or any available descriptor
295 * greater than or equal to the given one if not forced.
298 _sysio_fd_set(struct file *fil, int fd, int force)
309 oftab = select_oftab(fd);
312 * Search for a free descriptor if needed.
315 fd = find_free_fildes(oftab, fd);
320 if (fd - oftab->offset >= oftab->size) {
321 err = fd_grow(oftab, fd - oftab->offset);
329 ofil = __sysio_fd_get(fd, 1);
331 /* FIXME sometimes we could intercept open/socket to create
332 * a fd, but missing close()? currently we have this problem
333 * with resolv lib. as a workaround simply destroy the file
334 * struct here. And this hack will break the behavior of
337 if (fd >= 0 && oftab == &_sysio_oftab[0])
343 oftab->table[fd - oftab->offset] = fil;
349 * Duplicate old file descriptor.
351 * If the new file descriptor is less than zero, the new file descriptor
352 * is chosen freely. Otherwise, choose an available descriptor greater
353 * than or equal to the new, if not forced. Otherwise, if forced, (re)use
357 _sysio_fd_dup(int oldfd, int newfd, int force)
367 fil = _sysio_fd_find(oldfd);
371 /* old & new must belong to the same oftab */
372 if (select_oftab(oldfd) != select_oftab(newfd))
375 fd = _sysio_fd_set(fil, newfd, force);
382 _sysio_oftable_close_all(oftab_t *oftab)
387 for (fd = 0, filp = oftab->table;
388 (size_t )fd < oftab->size;
398 _sysio_fd_close_all()
406 * Close all open descriptors.
408 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_VIRTUAL]);
409 /* XXX see liblustre/llite_lib.c for explaination */
411 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_NATIVE]);
415 * Release current working directory.