[Libsysio-commit] HEAD: libsysio/src file_hack.c stdlib.c fcntl.c module.mk open.c
Brought to you by:
lward
From: Lee W. <lw...@us...> - 2004-05-20 19:31:58
|
Update of /cvsroot/libsysio/libsysio/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19480/src Modified Files: fcntl.c module.mk open.c Added Files: file_hack.c stdlib.c Log Message: Integrated CFS changes from Eric's patch dated 5-19-2004. --- NEW FILE --- /* * This Cplant(TM) source code is the property of Sandia National * Laboratories. * * This Cplant(TM) source code is copyrighted by Sandia National * Laboratories. * * The redistribution of this Cplant(TM) source code is subject to the * terms of the GNU Lesser General Public License * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html) * * Cplant(TM) Copyright 1998-2003 Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the US Government. * Export of this program may require a license from the United States * Government. */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Questions or comments about this library should be sent to: * * Lee Ward * Sandia National Laboratories, New Mexico * P.O. Box 5800 * Albuquerque, NM 87185-1110 * * le...@sa... */ #include <unistd.h> #include <stdlib.h> #include <assert.h> #include <errno.h> #include <sys/types.h> #include <sys/queue.h> #include "sysio.h" #include "file.h" #include "inode.h" #include "xtio.h" /* * Support for file IO. */ /* * The open files table */ typedef struct oftab { struct file **table; /* table array */ size_t size; /* current table size */ int offset; /* base fd number */ int max; /* max size */ } oftab_t; #define OFTAB_NATIVE (0) #define OFTAB_VIRTUAL (1) static oftab_t _sysio_oftab[2] = { {NULL, 0, 0, 0}, {NULL, 0, 0, 1024*1024}, }; static int native_max_fds = 0; static inline void init_oftab() { if (!native_max_fds) { native_max_fds = sysconf(_SC_OPEN_MAX); if (native_max_fds <= 0) abort(); _sysio_oftab[OFTAB_NATIVE].max = native_max_fds - 1; _sysio_oftab[OFTAB_VIRTUAL].offset = native_max_fds; } } static inline oftab_t *select_oftab(int fd) { return & _sysio_oftab[fd >= native_max_fds || fd < 0]; } /* * Create and initialize open file record. */ struct file * _sysio_fnew(struct inode *ino, int flags) { struct file *fil; fil = malloc(sizeof(struct file)); if (!fil) return NULL; _SYSIO_FINIT(fil, ino, flags); I_REF(ino); return fil; } /* * Destroy open file record. */ void _sysio_fgone(struct file *fil) { int err; assert(!fil->f_ref); assert(fil->f_ino); err = (*fil->f_ino->i_ops.inop_close)(fil->f_ino); assert(!err); free(fil); } /* * IO operation completion handler. */ void _sysio_fcompletio(struct ioctx *ioctx, struct file *fil) { _SYSIO_OFF_T off; if (ioctx->ioctx_cc <= 0) return; assert(ioctx->ioctx_ino == fil->f_ino); off = fil->f_pos + ioctx->ioctx_cc; if (fil->f_pos && off <= fil->f_pos) abort(); fil->f_pos = off; } /* * Grow (or truncate) the file descriptor table. */ static int fd_grow(oftab_t *oftab, size_t n) { int fd; size_t count; struct file **noftab, **filp; /* * Sanity check the new size. */ fd = (int )n; if ((size_t )fd != n) return -EMFILE; n++; /* index -> size */ assert(n > oftab->size); if (n > oftab->max) return -ERANGE; if (n < 8) n = 8; if (n - oftab->size < oftab->size) n = (n + 1) * 2; noftab = realloc(oftab->table, n * sizeof(struct file *)); if (!noftab) return -ENOMEM; oftab->table = noftab; count = oftab->size; oftab->size = n; if (n < count) return 0; filp = oftab->table + count; n -= count; while (n--) *filp++ = NULL; return 0; } #if ZERO_SUM_MEMORY static void free_oftab(oftab_t *ot) { if (ot->table) { free(ot->table); ot->size = 0; } } void _sysio_fd_shutdown() { free_oftab(&_sysio_oftab[OFTAB_NATIVE]); free_oftab(&_sysio_oftab[OFTAB_VIRTUAL]); } #endif /* * Find a free slot in the open files table. * target < 0: any free slot * target >= 0: get slot [target] */ static int find_free_fildes(oftab_t *oftab, int target) { int n; int err; struct file **filp; if (target < 0) { for (n = 0, filp = oftab->table; n < oftab->size && *filp; n++, filp++) ; } else n = target - oftab->offset; if (n >= oftab->size) { err = fd_grow(oftab, n); if (err) return err; filp = &oftab->table[n]; assert(!*filp); } #ifdef HAVE_LUSTRE_HACK /* FIXME sometimes we could intercept open/socket to create * a fd, but missing close()? currently we have this problem * with resolv lib. as a workaround simply destroy the file * struct here. */ if (oftab->table[n]) { free(oftab->table[n]); oftab->table[n] = NULL; } #endif return oftab->offset + n; } /* * Find open file record from file descriptor. * clear this entry if 'clear' is non-zero */ static struct file * __sysio_fd_get(int fd, int clear) { oftab_t *oftab; struct file *file; init_oftab(); if (fd < 0) return NULL; oftab = select_oftab(fd); if (!oftab->table || fd >= oftab->offset + oftab->size) return NULL; file = oftab->table[fd - oftab->offset]; if (clear) oftab->table[fd - oftab->offset] = NULL; return file; } /* * Find open file record from file descriptor. */ struct file * _sysio_fd_find(int fd) { return __sysio_fd_get(fd, 0); } /* * Close an open descriptor. */ int _sysio_fd_close(int fd) { struct file *fil; fil = fil = __sysio_fd_get(fd, 1); if (!fil) return -EBADF; F_RELE(fil); return 0; } /* * Associate open file record with given file descriptor or any available * file descriptor if less than zero. */ int _sysio_fd_set(struct file *fil, int fd) { int err; struct file *ofil; oftab_t *oftab; init_oftab(); oftab = select_oftab(fd); /* * New fd < 0 => any available descriptor. */ fd = find_free_fildes(oftab, fd); if (fd < 0) return fd; assert(fd < oftab->offset + oftab->size); /* * Remember old. */ ofil = __sysio_fd_get(fd, 1); if (ofil) F_RELE(ofil); oftab->table[fd - oftab->offset] = fil; return fd; } /* * Duplicate old file descriptor. * * If the new file descriptor is less than zero, the new file descriptor * is chosen freely. */ int _sysio_fd_dup2(int oldfd, int newfd) { struct file *fil; int fd; init_oftab(); if (oldfd == newfd) return 0; fil = _sysio_fd_find(oldfd); if (!fil) return -EBADF; /* old & new must belong to the same oftab */ if (select_oftab(oldfd) != select_oftab(newfd)) return -EINVAL; fd = _sysio_fd_set(fil, newfd); if (fd >= 0) F_REF(fil); return fd; } void _sysio_oftable_close_all(oftab_t *oftab) { struct file **filp; int fd; for (fd = 0, filp = oftab->table; (size_t )fd < oftab->size; fd++, filp++) { if (!*filp) continue; F_RELE(*filp); *filp = NULL; } } int _sysio_fd_close_all() { int fd; struct file **filp; oftab_t *oftab; int i; /* * Close all open descriptors. */ _sysio_oftable_close_all(&_sysio_oftab[OFTAB_VIRTUAL]); _sysio_oftable_close_all(&_sysio_oftab[OFTAB_NATIVE]); /* * Release current working directory. */ if (_sysio_cwd) { P_RELE(_sysio_cwd); _sysio_cwd = NULL; } return 0; } Index: fcntl.c =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/fcntl.c,v retrieving revision 1.11 retrieving revision 1.12 diff -u -w -b -B -p -r1.11 -r1.12 --- fcntl.c 30 Apr 2004 19:24:46 -0000 1.11 +++ fcntl.c 20 May 2004 19:31:48 -0000 1.12 @@ -53,6 +53,36 @@ #include "sysio-symbols.h" +#ifdef HAVE_LUSTRE_HACK +#include <syscall.h> + +static int +_sysio_fcntl(int fd, int cmd, va_list ap) +{ + int err; + long arg; + + switch (cmd) { + case F_GETFD: + case F_GETFL: + case F_GETOWN: + return syscall(SYS_fcntl, fd, cmd); + case F_DUPFD: + case F_SETFD: + case F_SETFL: + case F_GETLK: + case F_SETLK: + case F_SETLKW: + case F_SETOWN: + arg = va_arg(ap, long); + return syscall(SYS_fcntl, fd, cmd, arg); + } + + errno = ENOSYS; + return -1; +} +#endif + int SYSIO_INTERFACE_NAME(fcntl)(int fd, int cmd, ...) { @@ -65,8 +95,17 @@ SYSIO_INTERFACE_NAME(fcntl)(int fd, int err = 0; fil = _sysio_fd_find(fd); if (!fil) { +#ifdef HAVE_LUSTRE_HACK + va_start(ap, cmd); + err = _sysio_fcntl(fd, cmd, ap); + va_end(ap); + if (err == -1) + err = -errno; + goto out; +#else err = -EBADF; goto out; +#endif } switch (cmd) { Index: module.mk =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/module.mk,v retrieving revision 1.3 retrieving revision 1.4 diff -u -w -b -B -p -r1.3 -r1.4 --- module.mk 24 Feb 2004 14:58:26 -0000 1.3 +++ module.mk 20 May 2004 19:31:48 -0000 1.4 @@ -4,7 +4,7 @@ # SRCDIR_SRCS = src/access.c src/chdir.c src/chmod.c \ src/chown.c src/dev.c src/dup.c src/fcntl.c \ - src/file.c src/fs.c src/fsync.c \ + src/fs.c src/fsync.c \ src/getdirentries.c src/init.c src/inode.c \ src/ioctl.c src/ioctx.c src/iowait.c \ src/link.c src/lseek.c src/mkdir.c \ @@ -13,4 +13,10 @@ SRCDIR_SRCS = src/access.c src/chdir.c s src/rmdir.c src/stat64.c src/stat.c \ src/symlink.c \ src/truncate.c src/unlink.c src/utime.c +if WITH_LUSTRE_HACK +SRCDIR_SRCS += src/file_hack.c src/stdlib.c +else +SRCDIR_SRCS += src/file.c +endif + SRCDIR_EXTRA = src/module.mk Index: open.c =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/open.c,v retrieving revision 1.19 retrieving revision 1.20 diff -u -w -b -B -p -r1.19 -r1.20 --- open.c 28 Apr 2004 11:21:46 -0000 1.19 +++ open.c 20 May 2004 19:31:48 -0000 1.20 @@ -267,7 +267,12 @@ SYSIO_INTERFACE_NAME(creat)(const char * #undef __creat sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), __creat) #undef creat64 +#ifndef HAVE_LUSTRE_HACK sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), SYSIO_INTERFACE_NAME(creat64)) +#else +/* XXX workaround SuSE SLES 8, glibc-2.2.5 */ +sysio_sym_strong_alias(SYSIO_INTERFACE_NAME(creat), SYSIO_INTERFACE_NAME(creat64)) +#endif #undef __creat64 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), __creat64) #endif |