Fix syntax error when supergroups are not enabled
[openafs.git] / src / budb / database.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
14 #ifdef AFS_NT40_ENV
15 #include <winsock2.h>
16 #else
17 #include <netinet/in.h>
18 #endif
19 #include <sys/types.h>
20 #include <afs/stds.h>
21 #include <ubik.h>
22 #include <afs/bubasics.h>
23 #include "budb_errs.h"
24 #include "database.h"
25 #include "error_macros.h"
26 #include "budb_internal.h"
27 #include "afs/audit.h"
28 #include <string.h>
29
30 int pollCount;
31 struct memoryDB db;             /* really allocate it here */
32
33 void
34 db_panic(char *reason)
35 {
36     LogError(0, "db_panic: %s\n", reason);
37     BUDB_EXIT(-1);
38 }
39
40 afs_int32
41 InitDB(void)
42 {
43     afs_int32 code;
44
45     pollCount = 0;
46
47     memset(&db, 0, sizeof(db));
48     Lock_Init(&db.lock);
49     if ((code = InitDBalloc()) || (code = InitDBhash()))
50         return code;
51     return 0;
52 }
53
54 /* package up seek and write into one procedure for ease of use */
55
56 /* dbwrite 
57  *      write a portion of the database
58  * entry:
59  *      pos - offset into the database (disk address). If this is in the
60  *              database header, then buff must be a ptr to a portion of
61  *              the in-core header
62  *      buff - the information to write
63  *      len - size of the write
64  */
65
66 afs_int32
67 dbwrite(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
68 {
69     afs_int32 code = 0;
70
71     if (((pos < sizeof(db.h)) && (buff != (char *)&db.h + pos))
72         || (pos >= ntohl(db.h.eofPtr))) {
73         Log("dbwrite: Illegal attempt to write at location 0 or past EOF\n");
74         ERROR(BUDB_IO);
75     }
76
77     code = ubik_Seek(ut, 0, pos);
78     if (code) {
79         LogError(code, "dbwrite: ubik_Seek to %d failed\n", pos);
80         ERROR(code);
81     }
82     code = ubik_Write(ut, buff, len);
83     if (code) {
84         LogError(code, "dbwrite: ubik_Write failed\n");
85         ERROR(code);
86     }
87
88   error_exit:
89     if (((++pollCount) % 4) == 0) {     /* Poll every 4 reads/writes */
90 #ifndef AFS_PTHREAD_ENV
91         IOMGR_Poll();
92 #endif
93         pollCount = 0;
94     }
95     return code;
96 }
97
98 /* same thing for read */
99
100 afs_int32
101 dbread(struct ubik_trans *ut, afs_int32 pos, void *buff, afs_int32 len)
102 {
103     afs_int32 code = 0;
104
105     if (pos >= ntohl(db.h.eofPtr)) {
106         LogError(0, "dbread: Attempt to read @%d (past EOF)\n", pos);
107         ERROR(BUDB_IO);
108     }
109
110     code = ubik_Seek(ut, 0, pos);
111     if (code) {
112         LogError(code, "dbread: ubik_Seek to %d failed\n", pos);
113         ERROR(code);
114     }
115     code = ubik_Read(ut, buff, len);
116     if (code) {
117         LogError(code, "dbread: ubik_Read pos %d, buff %d, len %d\n", pos,
118                  buff, len);
119         ERROR(code);
120     }
121
122   error_exit:
123     if (((++pollCount) % 4) == 0) {     /* Poll every 4 reads/writes */
124 #ifndef AFS_PTHREAD_ENV
125         IOMGR_Poll();
126 #endif
127         pollCount = 0;
128     }
129     return code;
130 }
131
132 /* Same as dbread excepts it does checking */
133 afs_int32
134 cdbread(struct ubik_trans *ut, int type, afs_int32 pos, void *buff, afs_int32 len)
135 {
136     afs_int32 code = 0;
137
138     code = checkDiskAddress(pos, type, 0, 0);
139     if (code) {
140         LogError(code, "cdbread: Bad Address for block %d (addr 0x%x)\n",
141                  type, pos);
142         ERROR(code);
143     }
144
145     code = ubik_Seek(ut, 0, pos);
146     if (code) {
147         LogError(code, "cdbread: ubik_Seek to 0x%x failed\n", pos);
148         ERROR(code);
149     }
150     code = ubik_Read(ut, buff, len);
151     if (code) {
152         LogError(code, "cdbread: ubik_Read pos 0x%x, buff %d, len %d\n", pos,
153                  buff, len);
154         ERROR(code);
155     }
156
157   error_exit:
158     if (((++pollCount) % 4) == 0) {     /* Poll every 4 reads/writes */
159 #ifndef AFS_PTHREAD_ENV
160         IOMGR_Poll();
161 #endif
162         pollCount = 0;
163     }
164     return code;
165 }
166
167 /* check that the database has been initialized.  Be careful to fail in a safe
168    manner, to avoid bogusly reinitializing the db.  */
169
170 afs_int32
171 CheckInit(struct ubik_trans *ut, 
172           int (*db_init) (struct ubik_trans *ut)) /* call if rebuilding DB */
173 {
174     register afs_int32 code;
175
176     /* Don't read header if not necessary */
177     if (!ubik_CacheUpdate(ut))
178         return 0;
179
180     ObtainWriteLock(&db.lock);
181
182     db.h.eofPtr = htonl(sizeof(db.h));  /* for sanity check in dbread */
183     code = dbread(ut, 0, (char *)&db.h, sizeof(db.h));
184     if (code)
185         ERROR(code);
186
187     if ((ntohl(db.h.version) != BUDB_VERSION)
188         || (ntohl(db.h.checkVersion) != BUDB_VERSION)) {
189
190         if ((ntohl(db.h.version) == 0) || (ntohl(db.h.checkVersion) == 0))
191             ERROR(BUDB_EMPTY);
192
193         LogError(0, "DB version should be %d; Initial = %d; Terminal = %d\n",
194                  BUDB_VERSION, ntohl(db.h.version), ntohl(db.h.checkVersion));
195         ERROR(BUDB_IO);
196     }
197
198     db.readTime = time(0);
199     ht_Reset(&db.volName);
200     ht_Reset(&db.tapeName);
201     ht_Reset(&db.dumpName);
202     ht_Reset(&db.dumpIden);
203
204   error_exit:
205     ReleaseWriteLock(&db.lock);
206     if (code) {
207         if ((code == UEOF) || (code == BUDB_EMPTY)) {
208             if (db_init) {
209                 LogDebug(0, "No data base - Building new one\n");
210
211                 /* try to write a good header */
212                 memset(&db.h, 0, sizeof(db.h));
213                 db.h.version = htonl(BUDB_VERSION);
214                 db.h.checkVersion = htonl(BUDB_VERSION);
215                 db.h.lastUpdate = db.h.lastDumpId = htonl(time(0));
216                 db.h.eofPtr = htonl(sizeof(db.h));
217
218                 /* text ptrs cleared by bzero */
219                 ht_DBInit();
220
221                 code = dbwrite(ut, 0, (char *)&db.h, sizeof(db.h));
222                 if (code)
223                     code = BUDB_IO;     /* return the error code */
224                 else
225                     code = db_init(ut); /* initialize the db */
226             } else {
227                 LogDebug(0, "No data base\n");
228                 code = BUDB_EMPTY;
229             }
230         } else {
231             LogDebug(0, "I/O Error\n");
232             code = BUDB_IO;
233         }
234     }
235     return code;
236 }