2 * $Id: cnid_cdb_open.c,v 1.4 2009-11-24 12:18:19 didg Exp $
4 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
7 * CNID database support.
10 * 1) afpd already caches did's.
11 * 2) the database stores cnid's as both did/name and dev/ino pairs.
12 * 3) RootInfo holds the value of the NextID.
13 * 4) the cnid database gets called in the following manner --
15 * cnid = cnid_open(root_dir);
18 * newid = cnid_add(cnid, dev, ino, parent did,
19 * name, id); id is a hint for a specific id. pass 0 if you don't
20 * care. if the id is already assigned, you won't get what you
23 * given an id, get a did/name and dev/ino pair.
24 * name = cnid_get(cnid, &id); given an id, return the corresponding
26 * return code = cnid_delete(cnid, id); delete an entry.
28 * with AFP, CNIDs 0-2 have special meanings. here they are:
30 * 1 -- parent of root directory (handled by afpd)
31 * 2 -- root directory (handled by afpd)
33 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
34 * CNID_START begins at 17.
39 #endif /* HAVE_CONFIG_H */
41 #ifdef CNID_BACKEND_CDB
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 /* we version the did/name database so that we can change the format
58 * if necessary. the key is in the form of a did/name pair. in this case,
60 #define DBVERSION_KEY "\0\0\0\0\0"
61 #define DBVERSION_KEYLEN 5
62 #define DBVERSION1 0x00000001U
63 #define DBVERSION DBVERSION1
65 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
67 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
70 static char *old_dbfiles[] = {"cnid.db", NULL};
72 /* -----------------------
73 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
75 static int my_yield(void)
82 ret = select(0, NULL, NULL, NULL, &t);
87 static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
91 memset(skey, 0, sizeof(DBT));
92 skey->data = (char *)pdata->data + CNID_DID_OFS;
93 len = strlen((char *)skey->data + CNID_DID_LEN);
94 skey->size = CNID_DID_LEN + len + 1;
99 static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
101 memset(skey, 0, sizeof(DBT));
102 skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
103 skey->size = CNID_DEVINO_LEN;
107 /* --------------- */
108 static int my_associate (DB *p, DB *s,
109 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
112 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
113 return p->associate(p, NULL, s, callback, flags);
115 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
116 return p->associate(p, s, callback, flags);
118 return 0; /* FIXME */
123 /* --------------- */
124 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
126 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
127 return p->open(p, NULL, f, d, t, flags, mode);
129 return p->open(p, f, d, t, flags, mode);
133 /* --------------- */
134 static struct _cnid_db *cnid_cdb_new(const char *volpath)
136 struct _cnid_db *cdb;
137 int major, minor, patch;
140 version_str = db_version(&major, &minor, &patch);
142 /* check library match, ignore if only patch level changed */
143 if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
145 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);
149 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
152 if ((cdb->volpath = strdup(volpath)) == NULL) {
157 cdb->flags = CNID_FLAG_PERSISTENT;
159 cdb->cnid_add = cnid_cdb_add;
160 cdb->cnid_delete = cnid_cdb_delete;
161 cdb->cnid_get = cnid_cdb_get;
162 cdb->cnid_lookup = cnid_cdb_lookup;
163 cdb->cnid_nextid = NULL; /*cnid_cdb_nextid;*/
164 cdb->cnid_resolve = cnid_cdb_resolve;
165 cdb->cnid_update = cnid_cdb_update;
166 cdb->cnid_close = cnid_cdb_close;
167 cdb->cnid_getstamp = cnid_cdb_getstamp;
168 cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
173 /* --------------- */
174 static int upgrade_required(char *dbdir)
176 char path[MAXPATHLEN + 1];
184 if (path[len - 1] != '/') {
189 for (i = 0; old_dbfiles[i] != NULL; i++) {
190 strcpy(path + len, old_dbfiles[i]);
191 if ( !(stat(path, &st) < 0) ) {
195 if (errno != ENOENT) {
196 LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
203 /* --------------- */
204 struct _cnid_db *cnid_cdb_open(const char *dir, mode_t mask, u_int32_t flags _U_)
207 char path[MAXPATHLEN + 1];
209 struct _cnid_db *cdb;
211 static int first = 0;
214 if (!dir || *dir == 0) {
218 /* this checks .AppleDB.
219 We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
220 if ((len = strlen(dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
221 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
225 if ((cdb = cnid_cdb_new(dir)) == NULL) {
226 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
230 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
231 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
235 cdb->_private = (void *) db;
236 db->magic = CNID_DB_MAGIC;
239 if (path[len - 1] != '/') {
244 strcpy(path + len, DBHOME);
245 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
246 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
250 if (upgrade_required(path)) {
251 LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
255 /* Print out the version of BDB we're linked against. */
258 LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
261 open_flag = DB_CREATE;
263 /* We need to be able to open the database environment with full
264 * transaction, logging, and locking support if we ever hope to
265 * be a true multi-acess file server. */
266 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
267 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
271 /* Open the database environment. */
272 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
273 LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
274 /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
275 if (rc == DB_RUNRECOVERY) {
276 /* This is the mother of all errors. We _must_ fail here. */
277 LOG(log_error, logtype_default,
278 "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
282 /* We can't get a full transactional environment, so multi-access
283 * is out of the question. Let's assume a read-only environment,
284 * and try to at least get a shared memory pool. */
285 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
286 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
287 * open the environment with no flags. */
288 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
289 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
293 db->flags |= CNIDFLAG_DB_RO;
294 open_flag = DB_RDONLY;
295 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
298 /* ---------------------- */
299 /* Main CNID database. Use a hash for this one. */
301 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
302 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
307 if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
308 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
313 /* ---------------------- */
314 /* did/name reverse mapping. We use a BTree for this one. */
316 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
317 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
322 if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~mask))) {
323 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
328 /* ---------------------- */
329 /* dev/ino reverse mapping. Use a hash for this one. */
331 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
332 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
337 if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
338 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
343 /* ---------------------- */
344 /* Associate the secondary with the primary. */
345 if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
346 LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
351 if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
352 LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
359 /* ---------------------- */
360 /* Check for version. This way we can update the database if we need
361 * to change the format in any way. */
362 memset(&key, 0, sizeof(key));
363 memset(&pkey, 0, sizeof(DBT));
364 memset(&data, 0, sizeof(data));
365 key.data = DBVERSION_KEY;
366 key.size = DBVERSION_KEYLEN;
368 if ((rc = db->db_didname->pget(db->db_didname, NULL, &key, &pkey, &data, 0)) != 0) {
371 u_int32_t version = htonl(DBVERSION);
373 data.data = &version;
374 data.size = sizeof(version);
376 if ((ret = db->db_didname->put(db->db_cnid, NULL, &key, &data,
378 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
380 db->db_didname->close(db->db_didname, 0);
386 /* TODO In the future we might check for version number here. */
388 memcpy(&version, data.data, sizeof(version));
389 if (version != ntohl(DBVERSION)) {
394 db_env_set_func_yield(my_yield);
399 db->db_didname->close(db->db_didname, 0);
401 db->db_devino->close(db->db_devino, 0);
403 db->db_cnid->close(db->db_cnid, 0);
404 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
405 db->dbenv->close(db->dbenv, 0);
414 if (cdb->volpath != NULL)
421 struct _cnid_module cnid_cdb_module = {
425 CNID_FLAG_SETUID | CNID_FLAG_BLOCK
429 #endif /* CNID_BACKEND_CDB */