1*7c478bd9Sstevel@tonic-gate /*- 2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information. 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1997, 1998 5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved. 6*7c478bd9Sstevel@tonic-gate */ 7*7c478bd9Sstevel@tonic-gate 8*7c478bd9Sstevel@tonic-gate #include "config.h" 9*7c478bd9Sstevel@tonic-gate 10*7c478bd9Sstevel@tonic-gate #ifndef lint 11*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)os_rw.c 10.11 (Sleepycat) 10/12/98"; 12*7c478bd9Sstevel@tonic-gate #endif /* not lint */ 13*7c478bd9Sstevel@tonic-gate 14*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES 15*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate #include <errno.h> 18*7c478bd9Sstevel@tonic-gate #include <unistd.h> 19*7c478bd9Sstevel@tonic-gate #endif 20*7c478bd9Sstevel@tonic-gate 21*7c478bd9Sstevel@tonic-gate #include "db_int.h" 22*7c478bd9Sstevel@tonic-gate #include "os_jump.h" 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate /* 25*7c478bd9Sstevel@tonic-gate * __os_io -- 26*7c478bd9Sstevel@tonic-gate * Do an I/O. 27*7c478bd9Sstevel@tonic-gate * 28*7c478bd9Sstevel@tonic-gate * PUBLIC: int __os_io __P((DB_IO *, int, ssize_t *)); 29*7c478bd9Sstevel@tonic-gate */ 30*7c478bd9Sstevel@tonic-gate int 31*7c478bd9Sstevel@tonic-gate __os_io(db_iop, op, niop) 32*7c478bd9Sstevel@tonic-gate DB_IO *db_iop; 33*7c478bd9Sstevel@tonic-gate int op; 34*7c478bd9Sstevel@tonic-gate ssize_t *niop; 35*7c478bd9Sstevel@tonic-gate { 36*7c478bd9Sstevel@tonic-gate int ret; 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate #ifdef HAVE_PREAD 39*7c478bd9Sstevel@tonic-gate switch (op) { 40*7c478bd9Sstevel@tonic-gate case DB_IO_READ: 41*7c478bd9Sstevel@tonic-gate if (__db_jump.j_read != NULL) 42*7c478bd9Sstevel@tonic-gate goto slow; 43*7c478bd9Sstevel@tonic-gate *niop = pread(db_iop->fd_io, db_iop->buf, 44*7c478bd9Sstevel@tonic-gate db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize); 45*7c478bd9Sstevel@tonic-gate break; 46*7c478bd9Sstevel@tonic-gate case DB_IO_WRITE: 47*7c478bd9Sstevel@tonic-gate if (__db_jump.j_write != NULL) 48*7c478bd9Sstevel@tonic-gate goto slow; 49*7c478bd9Sstevel@tonic-gate *niop = pwrite(db_iop->fd_io, db_iop->buf, 50*7c478bd9Sstevel@tonic-gate db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize); 51*7c478bd9Sstevel@tonic-gate break; 52*7c478bd9Sstevel@tonic-gate } 53*7c478bd9Sstevel@tonic-gate if (*niop == db_iop->bytes) 54*7c478bd9Sstevel@tonic-gate return (0); 55*7c478bd9Sstevel@tonic-gate slow: 56*7c478bd9Sstevel@tonic-gate #endif 57*7c478bd9Sstevel@tonic-gate if (db_iop->mutexp != NULL) 58*7c478bd9Sstevel@tonic-gate (void)__db_mutex_lock(db_iop->mutexp, db_iop->fd_lock); 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate if ((ret = __os_seek(db_iop->fd_io, 61*7c478bd9Sstevel@tonic-gate db_iop->pagesize, db_iop->pgno, 0, 0, SEEK_SET)) != 0) 62*7c478bd9Sstevel@tonic-gate goto err; 63*7c478bd9Sstevel@tonic-gate switch (op) { 64*7c478bd9Sstevel@tonic-gate case DB_IO_READ: 65*7c478bd9Sstevel@tonic-gate ret = 66*7c478bd9Sstevel@tonic-gate __os_read(db_iop->fd_io, db_iop->buf, db_iop->bytes, niop); 67*7c478bd9Sstevel@tonic-gate break; 68*7c478bd9Sstevel@tonic-gate case DB_IO_WRITE: 69*7c478bd9Sstevel@tonic-gate ret = 70*7c478bd9Sstevel@tonic-gate __os_write(db_iop->fd_io, db_iop->buf, db_iop->bytes, niop); 71*7c478bd9Sstevel@tonic-gate break; 72*7c478bd9Sstevel@tonic-gate } 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate err: if (db_iop->mutexp != NULL) 75*7c478bd9Sstevel@tonic-gate (void)__db_mutex_unlock(db_iop->mutexp, db_iop->fd_lock); 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate return (ret); 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate /* 82*7c478bd9Sstevel@tonic-gate * __os_read -- 83*7c478bd9Sstevel@tonic-gate * Read from a file handle. 84*7c478bd9Sstevel@tonic-gate * 85*7c478bd9Sstevel@tonic-gate * PUBLIC: int __os_read __P((int, void *, size_t, ssize_t *)); 86*7c478bd9Sstevel@tonic-gate */ 87*7c478bd9Sstevel@tonic-gate int 88*7c478bd9Sstevel@tonic-gate __os_read(fd, addr, len, nrp) 89*7c478bd9Sstevel@tonic-gate int fd; 90*7c478bd9Sstevel@tonic-gate void *addr; 91*7c478bd9Sstevel@tonic-gate size_t len; 92*7c478bd9Sstevel@tonic-gate ssize_t *nrp; 93*7c478bd9Sstevel@tonic-gate { 94*7c478bd9Sstevel@tonic-gate size_t offset; 95*7c478bd9Sstevel@tonic-gate ssize_t nr; 96*7c478bd9Sstevel@tonic-gate u_int8_t *taddr; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate for (taddr = addr, 99*7c478bd9Sstevel@tonic-gate offset = 0; offset < len; taddr += nr, offset += nr) { 100*7c478bd9Sstevel@tonic-gate if ((nr = __db_jump.j_read != NULL ? 101*7c478bd9Sstevel@tonic-gate __db_jump.j_read(fd, taddr, len - offset) : 102*7c478bd9Sstevel@tonic-gate read(fd, taddr, len - offset)) < 0) 103*7c478bd9Sstevel@tonic-gate return (errno); 104*7c478bd9Sstevel@tonic-gate if (nr == 0) 105*7c478bd9Sstevel@tonic-gate break; 106*7c478bd9Sstevel@tonic-gate } 107*7c478bd9Sstevel@tonic-gate *nrp = taddr - (u_int8_t *)addr; 108*7c478bd9Sstevel@tonic-gate return (0); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate /* 112*7c478bd9Sstevel@tonic-gate * __os_write -- 113*7c478bd9Sstevel@tonic-gate * Write to a file handle. 114*7c478bd9Sstevel@tonic-gate * 115*7c478bd9Sstevel@tonic-gate * PUBLIC: int __os_write __P((int, void *, size_t, ssize_t *)); 116*7c478bd9Sstevel@tonic-gate */ 117*7c478bd9Sstevel@tonic-gate int 118*7c478bd9Sstevel@tonic-gate __os_write(fd, addr, len, nwp) 119*7c478bd9Sstevel@tonic-gate int fd; 120*7c478bd9Sstevel@tonic-gate void *addr; 121*7c478bd9Sstevel@tonic-gate size_t len; 122*7c478bd9Sstevel@tonic-gate ssize_t *nwp; 123*7c478bd9Sstevel@tonic-gate { 124*7c478bd9Sstevel@tonic-gate size_t offset; 125*7c478bd9Sstevel@tonic-gate ssize_t nw; 126*7c478bd9Sstevel@tonic-gate u_int8_t *taddr; 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate for (taddr = addr, 129*7c478bd9Sstevel@tonic-gate offset = 0; offset < len; taddr += nw, offset += nw) 130*7c478bd9Sstevel@tonic-gate if ((nw = __db_jump.j_write != NULL ? 131*7c478bd9Sstevel@tonic-gate __db_jump.j_write(fd, taddr, len - offset) : 132*7c478bd9Sstevel@tonic-gate write(fd, taddr, len - offset)) < 0) 133*7c478bd9Sstevel@tonic-gate return (errno); 134*7c478bd9Sstevel@tonic-gate *nwp = len; 135*7c478bd9Sstevel@tonic-gate return (0); 136*7c478bd9Sstevel@tonic-gate } 137