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);
116 * Destroy open file record.
119 _sysio_fgone(struct file *fil)
125 err = (*fil->f_ino->i_ops.inop_close)(fil->f_ino);
132 * IO operation completion handler.
135 _sysio_fcompletio(struct ioctx *ioctx, struct file *fil)
139 if (ioctx->ioctx_cc <= 0)
142 assert(ioctx->ioctx_ino == fil->f_ino);
143 off = fil->f_pos + ioctx->ioctx_cc;
144 if (fil->f_pos && off <= fil->f_pos)
150 * Grow (or truncate) the file descriptor table.
153 fd_grow(oftab_t *oftab, size_t n)
157 struct file **noftab, **filp;
160 * Sanity check the new size.
163 if ((size_t )fd != n)
166 n++; /* index -> size */
167 assert(n > oftab->size);
174 if (n - oftab->size < oftab->size)
176 noftab = realloc(oftab->table, n * sizeof(struct file *));
179 oftab->table = noftab;
184 filp = oftab->table + count;
191 #ifdef ZERO_SUM_MEMORY
192 static void free_oftab(oftab_t *ot)
203 free_oftab(&_sysio_oftab[OFTAB_NATIVE]);
204 free_oftab(&_sysio_oftab[OFTAB_VIRTUAL]);
209 * Find a free slot in the open files table which >= @low
213 find_free_fildes(oftab_t *oftab, int low)
222 n = low - oftab->offset;
226 for (filp = oftab->table + n;
227 n < oftab->size && *filp;
231 if (n >= oftab->size) {
232 err = fd_grow(oftab, n);
235 filp = &oftab->table[n];
239 return oftab->offset + n;
243 * Find open file record from file descriptor.
244 * clear this entry if 'clear' is non-zero
247 __sysio_fd_get(int fd, int clear)
257 oftab = select_oftab(fd);
258 if (!oftab->table || fd >= oftab->offset + oftab->size)
261 file = oftab->table[fd - oftab->offset];
263 oftab->table[fd - oftab->offset] = NULL;
269 * Find open file record from file descriptor.
272 _sysio_fd_find(int fd)
274 return __sysio_fd_get(fd, 0);
278 * Close an open descriptor.
281 _sysio_fd_close(int fd)
285 fil = __sysio_fd_get(fd, 1);
295 * Associate open file record with given file descriptor (if forced), or any
296 * available file descriptor if less than zero, or any available descriptor
297 * greater than or equal to the given one if not forced.
300 _sysio_fd_set(struct file *fil, int fd, int force)
311 oftab = select_oftab(fd);
314 * Search for a free descriptor if needed.
317 fd = find_free_fildes(oftab, fd);
322 if (fd - oftab->offset >= oftab->size) {
323 err = fd_grow(oftab, fd - oftab->offset);
331 ofil = __sysio_fd_get(fd, 1);
333 /* FIXME sometimes we could intercept open/socket to create
334 * a fd, but missing close()? currently we have this problem
335 * with resolv lib. as a workaround simply destroy the file
336 * struct here. And this hack will break the behavior of
339 if (fd >= 0 && oftab == &_sysio_oftab[0])
345 oftab->table[fd - oftab->offset] = fil;
351 * Duplicate old file descriptor.
353 * If the new file descriptor is less than zero, the new file descriptor
354 * is chosen freely. Otherwise, choose an available descriptor greater
355 * than or equal to the new, if not forced. Otherwise, if forced, (re)use
359 _sysio_fd_dup(int oldfd, int newfd, int force)
366 if (oldfd == newfd && oldfd >= 0)
369 fil = _sysio_fd_find(oldfd);
373 /* old & new must belong to the same oftab */
374 if (select_oftab(oldfd) != select_oftab(newfd))
377 fd = _sysio_fd_set(fil, newfd, force);
384 _sysio_oftable_close_all(oftab_t *oftab)
389 for (fd = 0, filp = oftab->table;
390 (size_t )fd < oftab->size;
400 _sysio_fd_close_all()
402 /* Close all open descriptors */
403 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_VIRTUAL]);
404 /* FIXME: libsysio does not currently perform enough cleanup of
405 * open files to allow __liblustre_cleanup_() to safely call
406 * unmount(). See the related FIXME comment in that function
407 * for details. The following disabled code is left in place to
408 * document the solution that was originally under consideration
409 * but never fully implemented. */
411 _sysio_oftable_close_all(&_sysio_oftab[OFTAB_NATIVE]);
414 /* Release current working directory */