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