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>
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.
208 * target < 0: any free slot
209 * target >= 0: get slot [target]
212 find_free_fildes(oftab_t *oftab, int target)
219 for (n = 0, filp = oftab->table;
220 n < oftab->size && *filp;
224 n = target - oftab->offset;
226 if (n >= oftab->size) {
227 err = fd_grow(oftab, n);
230 filp = &oftab->table[n];
234 #ifdef HAVE_LUSTRE_HACK
235 /* FIXME sometimes we could intercept open/socket to create
236 * a fd, but missing close()? currently we have this problem
237 * with resolv lib. as a workaround simply destroy the file
240 if (oftab->table[n]) {
241 free(oftab->table[n]);
242 oftab->table[n] = NULL;
246 return oftab->offset + n;
250 * Find open file record from file descriptor.
251 * clear this entry if 'clear' is non-zero
254 __sysio_fd_get(int fd, int clear)
264 oftab = select_oftab(fd);
265 if (!oftab->table || fd >= oftab->offset + oftab->size)
268 file = oftab->table[fd - oftab->offset];
270 oftab->table[fd - oftab->offset] = NULL;
276 * Find open file record from file descriptor.
279 _sysio_fd_find(int fd)
281 return __sysio_fd_get(fd, 0);
285 * Close an open descriptor.
288 _sysio_fd_close(int fd)
292 fil = fil = __sysio_fd_get(fd, 1);
302 * Associate open file record with given file descriptor or any available
303 * file descriptor if less than zero.
306 _sysio_fd_set(struct file *fil, int fd)
314 oftab = select_oftab(fd);
317 * New fd < 0 => any available descriptor.
319 fd = find_free_fildes(oftab, fd);
323 assert(fd < oftab->offset + oftab->size);
328 ofil = __sysio_fd_get(fd, 1);
332 oftab->table[fd - oftab->offset] = fil;
338 * Duplicate old file descriptor.
340 * If the new file descriptor is less than zero, the new file descriptor
344 _sysio_fd_dup2(int oldfd, int newfd)
354 fil = _sysio_fd_find(oldfd);
358 /* old & new must belong to the same oftab */
359 if (select_oftab(oldfd) != select_oftab(newfd))
362 fd = _sysio_fd_set(fil, newfd);
369 _sysio_oftable_close_all(oftab_t *oftab)
374 for (fd = 0, filp = oftab->table;
375 (size_t )fd < oftab->size;
385 _sysio_fd_close_all()
393 * Close all open descriptors.
395 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_VIRTUAL]);
396 /* XXX see liblustre/llite_lib.c for explaination */
398 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_NATIVE]);
402 * Release current working directory.