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/volume.h>
42 #include <atalk/cnid_private.h>
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 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
60 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
63 static char *old_dbfiles[] = {"cnid.db", NULL};
66 static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
70 memset(skey, 0, sizeof(DBT));
71 skey->data = (char *)pdata->data + CNID_DID_OFS;
72 len = strlen((char *)skey->data + CNID_DID_LEN);
73 skey->size = CNID_DID_LEN + len + 1;
78 static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
80 memset(skey, 0, sizeof(DBT));
81 skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
82 skey->size = CNID_DEVINO_LEN;
87 static int my_associate (DB *p, DB *s,
88 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
91 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
92 return p->associate(p, NULL, s, callback, flags);
94 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
95 return p->associate(p, s, callback, flags);
102 /* --------------- */
103 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
105 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
106 return p->open(p, NULL, f, d, t, flags, mode);
108 return p->open(p, f, d, t, flags, mode);
112 /* --------------- */
113 static struct _cnid_db *cnid_cdb_new(struct vol *vol)
115 struct _cnid_db *cdb;
116 int major, minor, patch;
119 version_str = db_version(&major, &minor, &patch);
121 /* check library match, ignore if only patch level changed */
122 if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
124 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);
128 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
131 cdb->cnid_db_vol = vol;
132 cdb->cnid_db_flags = CNID_FLAG_PERSISTENT;
133 cdb->cnid_add = cnid_cdb_add;
134 cdb->cnid_delete = cnid_cdb_delete;
135 cdb->cnid_get = cnid_cdb_get;
136 cdb->cnid_lookup = cnid_cdb_lookup;
137 cdb->cnid_nextid = NULL; /*cnid_cdb_nextid;*/
138 cdb->cnid_resolve = cnid_cdb_resolve;
139 cdb->cnid_update = cnid_cdb_update;
140 cdb->cnid_close = cnid_cdb_close;
141 cdb->cnid_getstamp = cnid_cdb_getstamp;
142 cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
143 cdb->cnid_wipe = NULL;
147 /* --------------- */
148 static int upgrade_required(char *dbdir)
150 char path[MAXPATHLEN + 1];
158 if (path[len - 1] != '/') {
163 for (i = 0; old_dbfiles[i] != NULL; i++) {
164 strcpy(path + len, old_dbfiles[i]);
165 if ( !(stat(path, &st) < 0) ) {
169 if (errno != ENOENT) {
170 LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
177 /* --------------- */
178 struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
181 char path[MAXPATHLEN + 1];
183 struct _cnid_db *cdb;
185 static int first = 0;
187 struct vol *vol = args->cnid_args_vol;
189 /* this checks .AppleDB.
190 We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
191 if ((len = strlen(vol->v_path)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
192 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", vol->v_path);
196 if ((cdb = cnid_cdb_new(vol)) == NULL) {
197 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
201 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
202 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
206 cdb->cnid_db_private = (void *) db;
208 strcpy(path, vol->v_path);
209 if (path[len - 1] != '/') {
214 strcpy(path + len, DBHOME);
215 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~vol->v_umask) < 0)) {
216 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
220 if (upgrade_required(path)) {
221 LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
225 /* Print out the version of BDB we're linked against. */
228 LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
231 open_flag = DB_CREATE;
233 /* We need to be able to open the database environment with full
234 * transaction, logging, and locking support if we ever hope to
235 * be a true multi-acess file server. */
236 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
237 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
241 /* Open the database environment. */
242 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~vol->v_umask)) != 0) {
243 LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
244 /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
245 if (rc == DB_RUNRECOVERY) {
246 /* This is the mother of all errors. We _must_ fail here. */
247 LOG(log_error, logtype_default,
248 "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
252 /* We can't get a full transactional environment, so multi-access
253 * is out of the question. Let's assume a read-only environment,
254 * and try to at least get a shared memory pool. */
255 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~vol->v_umask)) != 0) {
256 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
257 * open the environment with no flags. */
258 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~vol->v_umask)) != 0) {
259 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
263 db->flags |= CNIDFLAG_DB_RO;
264 open_flag = DB_RDONLY;
265 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
268 /* ---------------------- */
269 /* Main CNID database. Use a hash for this one. */
271 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
272 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
277 if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~vol->v_umask)) != 0) {
278 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
283 /* ---------------------- */
284 /* did/name reverse mapping. We use a BTree for this one. */
286 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
287 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
292 if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~vol->v_umask))) {
293 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
298 /* ---------------------- */
299 /* dev/ino reverse mapping. Use a hash for this one. */
301 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
302 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
307 if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~vol->v_umask)) != 0) {
308 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
313 /* ---------------------- */
314 /* Associate the secondary with the primary. */
315 if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
316 LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
321 if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
322 LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
327 /* ---------------------- */
328 /* Check for version. "cdb" only supports CNID_VERSION_0, cf cnid_private.h */
333 memset(&key, 0, sizeof(key));
334 memset(&data, 0, sizeof(data));
335 key.data = ROOTINFO_KEY;
336 key.size = ROOTINFO_KEYLEN;
338 if ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0)) == 0) {
339 /* If not found, ignore it */
340 memcpy(&version, data.data + CNID_DID_OFS, sizeof(version));
341 version = ntohl(version);
342 LOG(log_debug, logtype_default, "CNID db version %u", version);
343 if (version != CNID_VERSION_0) {
344 LOG(log_error, logtype_default, "Unsupported CNID db version %u, use CNID backend \"dbd\"", version);
353 db->db_didname->close(db->db_didname, 0);
355 db->db_devino->close(db->db_devino, 0);
357 db->db_cnid->close(db->db_cnid, 0);
358 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
359 db->dbenv->close(db->dbenv, 0);
373 struct _cnid_module cnid_cdb_module = {
377 CNID_FLAG_SETUID | CNID_FLAG_BLOCK
381 #endif /* CNID_BACKEND_CDB */