[Libsysio-commit] b_lustre: libsysio/src bypass.c file.c read.c write.c
Brought to you by:
lward
|
From: Mei <me...@us...> - 2003-09-08 03:58:23
|
Update of /cvsroot/libsysio/libsysio/src
In directory sc8-pr-cvs1:/tmp/cvs-serv21121/src
Modified Files:
Tag: b_lustre
bypass.c file.c read.c write.c
Log Message:
- make possible return real fd number when allocation file structure
- make socket driver use above
- temporarily disable entry/leave checkpoint because of re-entranc
problem, which introduced by socket driver. later reorganize it.
- disable bypass read/write, but keep bypass_fcntl
- related fixes in read/write
Index: bypass.c
===================================================================
RCS file: /cvsroot/libsysio/libsysio/src/Attic/bypass.c,v
retrieving revision 1.1.2.2
retrieving revision 1.1.2.3
diff -u -w -b -B -p -r1.1.2.2 -r1.1.2.3
--- bypass.c 19 Jun 2003 12:24:30 -0000 1.1.2.2
+++ bypass.c 8 Sep 2003 03:58:17 -0000 1.1.2.3
@@ -18,6 +18,7 @@
#include "bypass.h"
+#if 0
ioid_t __bypass_ireadv(int fd, const struct iovec *iov, int count)
{
struct ioctx *ioctx;
@@ -91,6 +92,7 @@ ioid_t __bypass_pwritev(int fd, const st
return __bypass_iwritev(fd, iov, count);
}
+#endif
int __bypass_fcntl(int fd, int cmd, va_list ap)
{
Index: file.c
===================================================================
RCS file: /cvsroot/libsysio/libsysio/src/file.c,v
retrieving revision 1.4.8.1
retrieving revision 1.4.8.2
diff -u -w -b -B -p -r1.4.8.1 -r1.4.8.2
--- file.c 15 Aug 2003 07:43:15 -0000 1.4.8.1
+++ file.c 8 Sep 2003 03:58:17 -0000 1.4.8.2
@@ -41,6 +41,7 @@
* le...@sa...
*/
+#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
@@ -58,8 +59,38 @@
/*
* The open files table and it's size.
*/
-static struct file **_sysio_oftab = NULL;
-static size_t _sysio_oftab_size = 0;
+typedef struct oftab {
+ struct file **table;
+ size_t size;
+ int offset;
+ int max;
+} oftab_t;
+
+#define OFTAB_LOW (0)
+#define OFTAB_HIGH (1)
+
+#define OFTAB_OFLIMIT (0x7FFFFFFF)
+static oftab_t _sysio_oftab[2] = { {NULL, 0, 0, 0}, {NULL, 0, 0, OFTAB_OFLIMIT} };
+
+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) {
+ printf("Fail to get _SC_OPEN_MAX\n");
+ exit(-1);
+ }
+ _sysio_oftab[OFTAB_LOW].max = native_max_fds - 1;
+ _sysio_oftab[OFTAB_HIGH].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.
@@ -113,12 +144,14 @@ _sysio_fcompletio(struct ioctx *ioctx)
* Grow (or truncate) the file descriptor table.
*/
static int
-fd_grow(size_t n)
+fd_grow(oftab_t *oftab, size_t n)
{
int fd;
size_t count;
struct file **noftab, **filp;
+ n++; /* index -> size */
+
/*
* Sanity check the new size.
*/
@@ -126,19 +159,24 @@ fd_grow(size_t n)
if ((size_t )fd != n)
return -EMFILE;
+ if (n > oftab->max)
+ return -ERANGE;
+
if (n < 8)
n = 8;
- if (n >= _sysio_oftab_size && n - _sysio_oftab_size < _sysio_oftab_size)
+ if (oftab->size == 0)
+ n += 8;
+ else if (n >= oftab->size && n - oftab->size < oftab->size)
n = (n + 1) * 2;
- noftab = realloc(_sysio_oftab, n * sizeof(struct file *));
+ noftab = realloc(oftab->table, n * sizeof(struct file *));
if (!noftab)
return -ENOMEM;
- _sysio_oftab = noftab;
- count = _sysio_oftab_size;
- _sysio_oftab_size = n;
+ oftab->table = noftab;
+ count = oftab->size;
+ oftab->size = n;
if (n < count)
return 0;
- filp = _sysio_oftab + count;
+ filp = oftab->table + count;
n -= count;
while (n--)
*filp++ = NULL;
@@ -146,28 +184,58 @@ fd_grow(size_t n)
}
/*
- * Find a free slot in the open files table.
+ * target < 0: Find a free slot in the open files table.
+ * target >= 0: get slot [target]
*/
static int
-find_free_fildes()
+find_free_fildes(oftab_t *oftab, int target)
{
- size_t n;
+ int n;
int err;
struct file **filp;
- for (n = 0, filp = _sysio_oftab;
- n < _sysio_oftab_size && *filp;
+ if (target < 0) {
+ for (n = 0, filp = oftab->table;
+ n < oftab->size && *filp;
n++, filp++)
;
- if (n >= _sysio_oftab_size) {
- err = fd_grow(n);
+ } else
+ n = target - oftab->offset;
+
+ if (n >= oftab->size) {
+ err = fd_grow(oftab, n);
if (err)
return err;
- filp = &_sysio_oftab[n];
+ filp = &oftab->table[n];
assert(!*filp);
}
- return n;
+ return oftab->offset + n;
+}
+
+/*
+ * Find open file record from file descriptor, clear this entry
+ * if clear is non-0.
+ */
+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;
}
/*
@@ -176,10 +244,7 @@ find_free_fildes()
struct file *
_sysio_fd_find(int fd)
{
- if (fd < 0 || (unsigned )fd >= _sysio_oftab_size)
- return NULL;
-
- return _sysio_oftab[fd];
+ return __sysio_fd_get(fd, 0);
}
/*
@@ -190,12 +255,10 @@ _sysio_fd_close(int fd)
{
struct file *fil;
- fil = _sysio_fd_find(fd);
+ fil = __sysio_fd_get(fd, 1);
if (!fil)
return -EBADF;
- _sysio_oftab[fd] = NULL;
-
F_RELE(fil);
return 0;
@@ -210,33 +273,30 @@ _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.
*/
- if (fd < 0) {
- fd = find_free_fildes();
+ fd = find_free_fildes(oftab, fd);
if (fd < 0)
return fd;
- }
- if ((unsigned )fd >= _sysio_oftab_size) {
- err = fd_grow(fd);
- if (err)
- return err;
- }
+ assert(fd < oftab->offset + oftab->size);
/*
* Remember old.
*/
- ofil = _sysio_fd_find(fd);
- /*
- * Take the entry.
- */
- _sysio_oftab[fd] = fil;
+ ofil = __sysio_fd_get(fd, 1);
if (ofil)
F_RELE(ofil);
+ oftab->table[fd - oftab->offset] = fil;
+
return fd;
}
@@ -252,6 +312,8 @@ _sysio_fd_dup2(int oldfd, int newfd)
struct file *fil;
int err;
+ init_oftab();
+
if (oldfd == newfd)
return 0;
@@ -259,6 +321,10 @@ _sysio_fd_dup2(int oldfd, int newfd)
if (!fil)
return -EBADF;
+ /* old & new must belong to the same oftab */
+ if (select_oftab(oldfd) != select_oftab(newfd))
+ return -EINVAL;
+
err = _sysio_fd_set(fil, newfd);
if (!err)
F_REF(fil);
@@ -270,17 +336,24 @@ _sysio_fd_close_all()
{
int fd;
struct file **filp;
+ oftab_t *oftab;
+ int i;
+
/*
* Close all open descriptors.
+ * XXX only close the upper part of open files.
*/
- for (fd = 0, filp = _sysio_oftab;
- (size_t )fd < _sysio_oftab_size;
+ for (i = 1; i >= 1; i--) {
+ oftab = &_sysio_oftab[i];
+ for (fd = 0, filp = oftab->table;
+ (size_t )fd < oftab->size;
fd++, filp++) {
if (!*filp)
continue;
F_RELE(*filp);
*filp = NULL;
+ }
}
/*
Index: read.c
===================================================================
RCS file: /cvsroot/libsysio/libsysio/src/read.c,v
retrieving revision 1.2.8.6
retrieving revision 1.2.8.7
diff -u -w -b -B -p -r1.2.8.6 -r1.2.8.7
--- read.c 18 Aug 2003 13:56:04 -0000 1.2.8.6
+++ read.c 8 Sep 2003 03:58:17 -0000 1.2.8.7
@@ -56,7 +56,6 @@
#include "sysio.h"
#include "file.h"
#include "inode.h"
-#include "bypass.h"
/*
* Schedule asynchronous read of iovec at some file extent.
@@ -109,9 +108,9 @@ ipreadv(int fd, const struct iovec *iov,
fil = _sysio_fd_find(fd);
if (!fil) {
- rc = __bypass_preadv(fd, iov, count, offset);
+ errno = -EBADF;
SYSIO_LEAVE;
- return rc;
+ return IOID_FAIL;
}
ioctxp = do_ipreadv(fil, iov, count, offset);
@@ -176,9 +175,9 @@ ireadv(int fd, const struct iovec *iov,
fil = _sysio_fd_find(fd);
if (!fil) {
- rc = __bypass_ireadv(fd, iov, count);
+ errno = -EBADF;
SYSIO_LEAVE;
- return rc;
+ return IOID_FAIL;
}
ioctxp = do_ipreadv(fil, iov, count, fil->f_pos);
Index: write.c
===================================================================
RCS file: /cvsroot/libsysio/libsysio/src/write.c,v
retrieving revision 1.2.10.6
retrieving revision 1.2.10.7
diff -u -w -b -B -p -r1.2.10.6 -r1.2.10.7
--- write.c 18 Aug 2003 13:56:04 -0000 1.2.10.6
+++ write.c 8 Sep 2003 03:58:17 -0000 1.2.10.7
@@ -57,7 +57,6 @@
#include "inode.h"
#include "fs.h"
#include "mount.h"
-#include "bypass.h"
/*
* Schedule asynchronous write of iovec at some file extent.
@@ -114,9 +113,9 @@ ipwritev(int fd, const struct iovec *iov
fil = _sysio_fd_find(fd);
if (!fil) {
- rc = __bypass_pwritev(fd, iov, count, offset);
+ errno = -EBADF;
SYSIO_LEAVE;
- return rc;
+ return IOID_FAIL;
}
ioctxp = do_ipwritev(fil, iov, count, offset);
@@ -180,9 +179,9 @@ iwritev(int fd, const struct iovec *iov,
fil = _sysio_fd_find(fd);
if (!fil) {
- rc = __bypass_iwritev(fd, iov, count);
+ errno = -EBADF;
SYSIO_LEAVE;
- return rc;
+ return IOID_FAIL;
}
ioctxp = do_ipwritev(fil, iov, count, fil->f_pos);
|