3 * $Id: cnid_cdb_open.c,v 1.1.4.8 2004-02-07 19:46:09 didg Exp $
5 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
8 * CNID database support.
11 * 1) afpd already caches did's.
12 * 2) the database stores cnid's as both did/name and dev/ino pairs.
13 * 3) RootInfo holds the value of the NextID.
14 * 4) the cnid database gets called in the following manner --
16 * cnid = cnid_open(root_dir);
19 * newid = cnid_add(cnid, dev, ino, parent did,
20 * name, id); id is a hint for a specific id. pass 0 if you don't
21 * care. if the id is already assigned, you won't get what you
24 * given an id, get a did/name and dev/ino pair.
25 * name = cnid_get(cnid, &id); given an id, return the corresponding
27 * return code = cnid_delete(cnid, id); delete an entry.
29 * with AFP, CNIDs 0-2 have special meanings. here they are:
31 * 1 -- parent of root directory (handled by afpd)
32 * 2 -- root directory (handled by afpd)
34 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
35 * CNID_START begins at 17.
40 #endif /* HAVE_CONFIG_H */
42 #ifdef CNID_BACKEND_CDB
43 #include "cnid_cdb_private.h"
46 #define MIN(a, b) ((a) < (b) ? (a) : (b))
49 #define DBHOME ".AppleDB"
50 #define DBCNID "cnid2.db"
51 #define DBDEVINO "devino.db"
52 #define DBDIDNAME "didname.db" /* did/full name mapping */
53 #define DBLOCKFILE "cnid.lock"
58 /* we version the did/name database so that we can change the format
59 * if necessary. the key is in the form of a did/name pair. in this case,
61 #define DBVERSION_KEY "\0\0\0\0\0"
62 #define DBVERSION_KEYLEN 5
63 #define DBVERSION1 0x00000001U
64 #define DBVERSION DBVERSION1
66 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
68 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
71 static char *old_dbfiles[] = {"cnid.db", NULL};
73 /* -----------------------
74 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
76 static int my_yield(void)
83 ret = select(0, NULL, NULL, NULL, &t);
88 static int didname(dbp, pkey, pdata, skey)
90 const DBT *pkey, *pdata;
95 memset(skey, 0, sizeof(DBT));
96 skey->data = (char *)pdata->data + CNID_DID_OFS;
97 len = strlen((char *)skey->data + CNID_DID_LEN);
98 skey->size = CNID_DID_LEN + len + 1;
102 /* --------------- */
103 static int devino(dbp, pkey, pdata, skey)
105 const DBT *pkey, *pdata;
108 memset(skey, 0, sizeof(DBT));
109 skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
110 skey->size = CNID_DEVINO_LEN;
114 /* --------------- */
115 static int my_associate (DB *p, DB *s,
116 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
119 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
120 return p->associate(p, NULL, s, callback, flags);
122 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
123 return p->associate(p, s, callback, flags);
125 return 0; /* FIXME */
130 /* --------------- */
131 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
133 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
134 return p->open(p, NULL, f, d, t, flags, mode);
136 return p->open(p, f, d, t, flags, mode);
140 /* --------------- */
141 static struct _cnid_db *cnid_cdb_new(const char *volpath)
143 struct _cnid_db *cdb;
144 int major, minor, patch;
147 version_str = db_version(&major, &minor, &patch);
149 /* check library match, ignore if only patch level changed */
150 if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
152 LOG(log_error, logtype_cnid, "cnid_cdb_new: the Berkeley DB library version used does not match the version compiled with: (%u.%u)/(%u.%u)", DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
156 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
159 if ((cdb->volpath = strdup(volpath)) == NULL) {
164 cdb->flags = CNID_FLAG_PERSISTENT;
166 cdb->cnid_add = cnid_cdb_add;
167 cdb->cnid_delete = cnid_cdb_delete;
168 cdb->cnid_get = cnid_cdb_get;
169 cdb->cnid_lookup = cnid_cdb_lookup;
170 cdb->cnid_nextid = NULL; /*cnid_cdb_nextid;*/
171 cdb->cnid_resolve = cnid_cdb_resolve;
172 cdb->cnid_update = cnid_cdb_update;
173 cdb->cnid_close = cnid_cdb_close;
178 /* --------------- */
179 static int upgrade_required(char *dbdir)
181 char path[MAXPATHLEN + 1];
189 if (path[len - 1] != '/') {
194 for (i = 0; old_dbfiles[i] != NULL; i++) {
195 strcpy(path + len, old_dbfiles[i]);
196 if ( !(stat(path, &st) < 0) ) {
200 if (errno != ENOENT) {
201 LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
208 /* --------------- */
209 struct _cnid_db *cnid_cdb_open(const char *dir, mode_t mask)
212 char path[MAXPATHLEN + 1];
214 struct _cnid_db *cdb;
216 static int first = 0;
219 if (!dir || *dir == 0) {
223 /* this checks .AppleDB.
224 We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
225 if ((len = strlen(dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
226 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
230 if ((cdb = cnid_cdb_new(dir)) == NULL) {
231 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
235 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
236 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
240 cdb->_private = (void *) db;
241 db->magic = CNID_DB_MAGIC;
244 if (path[len - 1] != '/') {
249 strcpy(path + len, DBHOME);
250 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
251 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
255 if (upgrade_required(path)) {
256 LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
260 open_flag = DB_CREATE;
262 /* We need to be able to open the database environment with full
263 * transaction, logging, and locking support if we ever hope to
264 * be a true multi-acess file server. */
265 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
266 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
270 /* Open the database environment. */
271 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
272 LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
273 /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
274 if (rc == DB_RUNRECOVERY) {
275 /* This is the mother of all errors. We _must_ fail here. */
276 LOG(log_error, logtype_default,
277 "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
281 /* We can't get a full transactional environment, so multi-access
282 * is out of the question. Let's assume a read-only environment,
283 * and try to at least get a shared memory pool. */
284 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
285 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
286 * open the environment with no flags. */
287 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
288 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
292 db->flags |= CNIDFLAG_DB_RO;
293 open_flag = DB_RDONLY;
294 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
297 /* ---------------------- */
298 /* Main CNID database. Use a hash for this one. */
300 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
301 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
306 if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
307 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
312 /* ---------------------- */
313 /* did/name reverse mapping. We use a BTree for this one. */
315 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
316 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
321 if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~mask))) {
322 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
327 /* ---------------------- */
328 /* dev/ino reverse mapping. Use a hash for this one. */
330 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
331 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
336 if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
337 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
342 /* ---------------------- */
343 /* Associate the secondary with the primary. */
344 if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
345 LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
350 if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
351 LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
358 /* ---------------------- */
359 /* Check for version. This way we can update the database if we need
360 * to change the format in any way. */
361 memset(&key, 0, sizeof(key));
362 memset(&pkey, 0, sizeof(DBT));
363 memset(&data, 0, sizeof(data));
364 key.data = DBVERSION_KEY;
365 key.size = DBVERSION_KEYLEN;
367 if ((rc = db->db_didname->pget(db->db_didname, NULL, &key, &pkey, &data, 0)) != 0) {
370 u_int32_t version = htonl(DBVERSION);
372 data.data = &version;
373 data.size = sizeof(version);
375 if ((ret = db->db_didname->put(db->db_cnid, NULL, &key, &data,
377 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
379 db->db_didname->close(db->db_didname, 0);
385 /* TODO In the future we might check for version number here. */
387 memcpy(&version, data.data, sizeof(version));
388 if (version != ntohl(DBVERSION)) {
393 /* Print out the version of BDB we're linked against. */
396 LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
399 db_env_set_func_yield(my_yield);
404 db->db_didname->close(db->db_didname, 0);
406 db->db_devino->close(db->db_devino, 0);
408 db->db_cnid->close(db->db_cnid, 0);
409 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
410 db->dbenv->close(db->dbenv, 0);
419 if (cdb->volpath != NULL)
426 struct _cnid_module cnid_cdb_module = {
430 CNID_FLAG_SETUID | CNID_FLAG_BLOCK
434 #endif /* CNID_BACKEND_CDB */