2 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
5 * CNID database support.
8 * 1) afpd already caches did's.
9 * 2) the database stores cnid's as both did/name and dev/ino pairs.
10 * 3) RootInfo holds the value of the NextID.
11 * 4) the cnid database gets called in the following manner --
13 * cnid = cnid_open(root_dir);
16 * newid = cnid_add(cnid, dev, ino, parent did,
17 * name, id); id is a hint for a specific id. pass 0 if you don't
18 * care. if the id is already assigned, you won't get what you
21 * given an id, get a did/name and dev/ino pair.
22 * name = cnid_get(cnid, &id); given an id, return the corresponding
24 * return code = cnid_delete(cnid, id); delete an entry.
26 * with AFP, CNIDs 0-2 have special meanings. here they are:
28 * 1 -- parent of root directory (handled by afpd)
29 * 2 -- root directory (handled by afpd)
31 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
32 * CNID_START begins at 17.
37 #endif /* HAVE_CONFIG_H */
39 #ifdef CNID_BACKEND_CDB
41 #include <atalk/cnid_private.h>
42 #include "cnid_cdb_private.h"
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
48 #define DBHOME ".AppleDB"
49 #define DBCNID "cnid2.db"
50 #define DBDEVINO "devino.db"
51 #define DBDIDNAME "didname.db" /* did/full name mapping */
52 #define DBLOCKFILE "cnid.lock"
57 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
59 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
62 static char *old_dbfiles[] = {"cnid.db", NULL};
65 static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
69 memset(skey, 0, sizeof(DBT));
70 skey->data = (char *)pdata->data + CNID_DID_OFS;
71 len = strlen((char *)skey->data + CNID_DID_LEN);
72 skey->size = CNID_DID_LEN + len + 1;
77 static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
79 memset(skey, 0, sizeof(DBT));
80 skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
81 skey->size = CNID_DEVINO_LEN;
86 static int my_associate (DB *p, DB *s,
87 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
90 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
91 return p->associate(p, NULL, s, callback, flags);
93 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
94 return p->associate(p, s, callback, flags);
101 /* --------------- */
102 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
104 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
105 return p->open(p, NULL, f, d, t, flags, mode);
107 return p->open(p, f, d, t, flags, mode);
111 /* --------------- */
112 static struct _cnid_db *cnid_cdb_new(const char *volpath)
114 struct _cnid_db *cdb;
115 int major, minor, patch;
118 version_str = db_version(&major, &minor, &patch);
120 /* check library match, ignore if only patch level changed */
121 if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
123 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);
127 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
130 if ((cdb->volpath = strdup(volpath)) == NULL) {
135 cdb->flags = CNID_FLAG_PERSISTENT;
137 cdb->cnid_add = cnid_cdb_add;
138 cdb->cnid_delete = cnid_cdb_delete;
139 cdb->cnid_get = cnid_cdb_get;
140 cdb->cnid_lookup = cnid_cdb_lookup;
141 cdb->cnid_nextid = NULL; /*cnid_cdb_nextid;*/
142 cdb->cnid_resolve = cnid_cdb_resolve;
143 cdb->cnid_update = cnid_cdb_update;
144 cdb->cnid_close = cnid_cdb_close;
145 cdb->cnid_getstamp = cnid_cdb_getstamp;
146 cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
151 /* --------------- */
152 static int upgrade_required(char *dbdir)
154 char path[MAXPATHLEN + 1];
162 if (path[len - 1] != '/') {
167 for (i = 0; old_dbfiles[i] != NULL; i++) {
168 strcpy(path + len, old_dbfiles[i]);
169 if ( !(stat(path, &st) < 0) ) {
173 if (errno != ENOENT) {
174 LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
181 /* --------------- */
182 struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
185 char path[MAXPATHLEN + 1];
187 struct _cnid_db *cdb;
189 static int first = 0;
192 if (!args->dir || *args->dir == 0) {
196 /* this checks .AppleDB.
197 We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
198 if ((len = strlen(args->dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
199 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", args->dir);
203 if ((cdb = cnid_cdb_new(args->dir)) == NULL) {
204 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
208 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
209 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
213 cdb->_private = (void *) db;
214 db->magic = CNID_DB_MAGIC;
216 strcpy(path, args->dir);
217 if (path[len - 1] != '/') {
222 strcpy(path + len, DBHOME);
223 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~args->mask) < 0)) {
224 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
228 if (upgrade_required(path)) {
229 LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
233 /* Print out the version of BDB we're linked against. */
236 LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
239 open_flag = DB_CREATE;
241 /* We need to be able to open the database environment with full
242 * transaction, logging, and locking support if we ever hope to
243 * be a true multi-acess file server. */
244 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
245 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
249 /* Open the database environment. */
250 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~args->mask)) != 0) {
251 LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
252 /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
253 if (rc == DB_RUNRECOVERY) {
254 /* This is the mother of all errors. We _must_ fail here. */
255 LOG(log_error, logtype_default,
256 "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
260 /* We can't get a full transactional environment, so multi-access
261 * is out of the question. Let's assume a read-only environment,
262 * and try to at least get a shared memory pool. */
263 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~args->mask)) != 0) {
264 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
265 * open the environment with no flags. */
266 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~args->mask)) != 0) {
267 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
271 db->flags |= CNIDFLAG_DB_RO;
272 open_flag = DB_RDONLY;
273 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
276 /* ---------------------- */
277 /* Main CNID database. Use a hash for this one. */
279 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
280 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
285 if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
286 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
291 /* ---------------------- */
292 /* did/name reverse mapping. We use a BTree for this one. */
294 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
295 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
300 if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~args->mask))) {
301 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
306 /* ---------------------- */
307 /* dev/ino reverse mapping. Use a hash for this one. */
309 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
310 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
315 if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
316 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
321 /* ---------------------- */
322 /* Associate the secondary with the primary. */
323 if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
324 LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
329 if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
330 LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
335 /* ---------------------- */
336 /* Check for version. "cdb" only supports CNID_VERSION_0, cf cnid_private.h */
341 memset(&key, 0, sizeof(key));
342 memset(&data, 0, sizeof(data));
343 key.data = ROOTINFO_KEY;
344 key.size = ROOTINFO_KEYLEN;
346 if ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0)) == 0) {
347 /* If not found, ignore it */
348 memcpy(&version, data.data + CNID_DID_OFS, sizeof(version));
349 version = ntohl(version);
350 LOG(log_debug, logtype_default, "CNID db version %u", version);
351 if (version != CNID_VERSION_0) {
352 LOG(log_error, logtype_default, "Unsupported CNID db version %u, use CNID backend \"dbd\"", version);
361 db->db_didname->close(db->db_didname, 0);
363 db->db_devino->close(db->db_devino, 0);
365 db->db_cnid->close(db->db_cnid, 0);
366 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
367 db->dbenv->close(db->dbenv, 0);
376 if (cdb->volpath != NULL)
383 struct _cnid_module cnid_cdb_module = {
387 CNID_FLAG_SETUID | CNID_FLAG_BLOCK
391 #endif /* CNID_BACKEND_CDB */