04fd0b18fc23de29f12c052fd87b0708ceb465a0
[openafs.git] / src / ubik / phys.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <sys/types.h>
17 #ifdef AFS_NT40_ENV
18 #include <winsock2.h>
19 #include <io.h>
20 #include <fcntl.h>
21 #else
22 #include <sys/file.h>
23 #include <netinet/in.h>
24 #endif
25 #include <sys/stat.h>
26
27 /* #if defined (AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV) */
28 #if 0   /* temporary hack - klm */
29 /* nothing */
30 #else
31 #include <lwp.h>
32 #endif
33
34 #include <lock.h>
35 #include <errno.h>
36 #include <string.h>
37
38 #define UBIK_INTERNALS 1
39 #include "ubik.h"
40
41 /* these routines are called via the proc ptr in the ubik_dbase structure.  They provide access to
42  * the physical disk, by converting the file numbers being processed (>= 0 for user data space, < 0
43  * for ubik system files, such as the log) to actual pathnames to open, read, write, truncate, sync,
44  * etc.
45  */
46
47 #define MAXFDCACHE  4
48 static struct fdcache {
49     int fd;
50     int fileID;
51     int refCount;
52 } fdcache[MAXFDCACHE];
53
54 static char pbuffer[1024];
55
56 /* beware, when using this function, of the header in front of most files */
57 static int
58 uphys_open(register struct ubik_dbase *adbase, afs_int32 afid)
59 {
60     char temp[20];
61     register int fd;
62     static int initd;
63     register int i;
64     register struct fdcache *tfd;
65     struct fdcache *bestfd;
66
67     /* initialize package */
68     if (!initd) {
69         initd = 1;
70         tfd = fdcache;
71         for (i = 0; i < MAXFDCACHE; tfd++, i++) {
72             tfd->fd = -1;       /* invalid value */
73             tfd->fileID = -10000;       /* invalid value */
74             tfd->refCount = 0;
75         }
76     }
77
78     /* scan file descr cache */
79     for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
80         if (afid == tfd->fileID && tfd->refCount == 0) {        /* don't use open fd */
81             lseek(tfd->fd, 0, 0);       /* reset ptr just like open would have */
82             tfd->refCount++;
83             return tfd->fd;
84         }
85     }
86
87     /* not found, open it and try to enter in cache */
88     afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d", adbase->pathName, 
89                  (afid<0)?"SYS":"", (afid<0)?-afid:afid);
90     fd = open(pbuffer, O_CREAT | O_RDWR, 0600);
91     if (fd < 0) {
92         /* try opening read-only */
93         fd = open(pbuffer, O_RDONLY, 0);
94         if (fd < 0)
95             return fd;
96     }
97
98     /* enter it in the cache */
99     tfd = fdcache;
100     bestfd = NULL;
101     for (i = 0; i < MAXFDCACHE; i++, tfd++) {   /* look for empty slot */
102         if (tfd->fd == -1) {
103             bestfd = tfd;
104             break;
105         }
106     }
107     if (!bestfd) {              /* look for reclaimable slot */
108         tfd = fdcache;
109         for (i = 0; i < MAXFDCACHE; i++, tfd++) {
110             if (tfd->refCount == 0) {
111                 bestfd = tfd;
112                 break;
113             }
114         }
115     }
116     if (bestfd) {               /* found a usable slot */
117         tfd = bestfd;
118         if (tfd->fd >= 0)
119             close(tfd->fd);
120         tfd->fd = fd;
121         tfd->refCount = 1;      /* us */
122         tfd->fileID = afid;
123     }
124
125     /* finally, we're done */
126     return fd;
127 }
128
129 /* close the file, maintaining ref count in cache structure */
130 int
131 uphys_close(register int afd)
132 {
133     register int i;
134     register struct fdcache *tfd;
135
136     if (afd < 0)
137         return EBADF;
138     tfd = fdcache;
139     for (i = 0; i < MAXFDCACHE; i++, tfd++) {
140         if (tfd->fd == afd && tfd->fileID != -10000) {
141             tfd->refCount--;
142             return 0;
143         }
144     }
145     return close(afd);
146 }
147
148 int
149 uphys_stat(struct ubik_dbase *adbase, afs_int32 afid, struct ubik_stat *astat)
150 {
151     register int fd;
152     struct stat tstat;
153     register afs_int32 code;
154
155     fd = uphys_open(adbase, afid);
156     if (fd < 0)
157         return fd;
158     code = fstat(fd, &tstat);
159     uphys_close(fd);
160     if (code < 0) {
161         return code;
162     }
163     astat->mtime = tstat.st_mtime;
164     code = tstat.st_size - HDRSIZE;
165     if (code < 0)
166         astat->size = 0;
167     else
168         astat->size = code;
169     return 0;
170 }
171
172 int
173 uphys_read(register struct ubik_dbase *adbase, afs_int32 afile,
174            register char *abuffer, afs_int32 apos, afs_int32 alength)
175 {
176     register int fd;
177     register afs_int32 code;
178
179     fd = uphys_open(adbase, afile);
180     if (fd < 0)
181         return -1;
182     code = lseek(fd, apos + HDRSIZE, 0);
183     if (code < 0) {
184         uphys_close(fd);
185         return -1;
186     }
187     code = read(fd, abuffer, alength);
188     uphys_close(fd);
189     return code;
190 }
191
192 int
193 uphys_write(register struct ubik_dbase *adbase, afs_int32 afile,
194             register char *abuffer, afs_int32 apos, afs_int32 alength)
195 {
196     register int fd;
197     register afs_int32 code;
198     afs_int32 length;
199
200     fd = uphys_open(adbase, afile);
201     if (fd < 0)
202         return -1;
203     code = lseek(fd, apos + HDRSIZE, 0);
204     if (code < 0) {
205         uphys_close(fd);
206         return -1;
207     }
208     length = write(fd, abuffer, alength);
209     code = uphys_close(fd);
210     if (code)
211         return -1;
212     else
213         return length;
214 }
215
216 int
217 uphys_truncate(register struct ubik_dbase *adbase, afs_int32 afile,
218                afs_int32 asize)
219 {
220     register afs_int32 code, fd;
221     fd = uphys_open(adbase, afile);
222     if (fd < 0)
223         return UNOENT;
224     code = ftruncate(fd, asize + HDRSIZE);
225     uphys_close(fd);
226     return code;
227 }
228
229 /* get number of dbase files */
230 int
231 uphys_getnfiles(register struct ubik_dbase *adbase)
232 {
233     /* really should scan dir for data */
234     return 1;
235 }
236
237 /* get database label, with aversion in host order */
238 int
239 uphys_getlabel(register struct ubik_dbase *adbase, afs_int32 afile,
240                struct ubik_version *aversion)
241 {
242     struct ubik_hdr thdr;
243     register afs_int32 code, fd;
244
245     fd = uphys_open(adbase, afile);
246     if (fd < 0)
247         return UNOENT;
248     code = read(fd, &thdr, sizeof(thdr));
249     if (code != sizeof(thdr)) {
250         uphys_close(fd);
251         return EIO;
252     }
253     aversion->epoch = ntohl(thdr.version.epoch);
254     aversion->counter = ntohl(thdr.version.counter);
255     uphys_close(fd);
256     return 0;
257 }
258
259 /* label database, with aversion in host order */
260 int
261 uphys_setlabel(register struct ubik_dbase *adbase, afs_int32 afile,
262                struct ubik_version *aversion)
263 {
264     struct ubik_hdr thdr;
265     register afs_int32 code, fd;
266
267     fd = uphys_open(adbase, afile);
268     if (fd < 0)
269         return UNOENT;
270     thdr.version.epoch = htonl(aversion->epoch);
271     thdr.version.counter = htonl(aversion->counter);
272     thdr.magic = htonl(UBIK_MAGIC);
273     thdr.size = htons(HDRSIZE);
274     code = write(fd, &thdr, sizeof(thdr));
275     fsync(fd);                  /* preserve over crash */
276     uphys_close(fd);
277     if (code != sizeof(thdr)) {
278         return EIO;
279     }
280     return 0;
281 }
282
283 int
284 uphys_sync(register struct ubik_dbase *adbase, afs_int32 afile)
285 {
286     register afs_int32 code, fd;
287     fd = uphys_open(adbase, afile);
288     code = fsync(fd);
289     uphys_close(fd);
290     return code;
291 }
292
293 void
294 uphys_invalidate(register struct ubik_dbase *adbase, afs_int32 afid)
295 {
296     register int i;
297     register struct fdcache *tfd;
298
299     /* scan file descr cache */
300     for (tfd = fdcache, i = 0; i < MAXFDCACHE; i++, tfd++) {
301         if (afid == tfd->fileID) {
302             tfd->fileID = -10000;
303             if (tfd->fd >= 0 && tfd->refCount == 0)
304                 close(tfd->fd);
305             return;
306         }
307     }
308 }