]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cdb/cnid_cdb_open.c
Enhanced machine type
[netatalk.git] / libatalk / cnid / cdb / cnid_cdb_open.c
1 /*
2
3  * $Id: cnid_cdb_open.c,v 1.5 2010-03-31 09:47:32 franklahm Exp $
4  *
5  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
6  * All Rights Reserved. See COPYRIGHT.
7  *
8  * CNID database support. 
9  *
10  * here's the deal:
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 --
15  *     start a database:
16  *     cnid = cnid_open(root_dir);
17  *
18  *     allocate a new id: 
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
22  *     requested.
23  *
24  *     given an id, get a did/name and dev/ino pair.
25  *     name = cnid_get(cnid, &id); given an id, return the corresponding
26  *     info.
27  *     return code = cnid_delete(cnid, id); delete an entry. 
28  *
29  * with AFP, CNIDs 0-2 have special meanings. here they are:
30  * 0 -- invalid cnid
31  * 1 -- parent of root directory (handled by afpd) 
32  * 2 -- root directory (handled by afpd)
33  *
34  * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so, 
35  * CNID_START begins at 17.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif /* HAVE_CONFIG_H */
41
42 #ifdef CNID_BACKEND_CDB
43 #include "cnid_cdb_private.h"
44
45 #ifndef MIN
46 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
47 #endif /* ! MIN */
48
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"
54
55 #define DBHOMELEN    8
56 #define DBLEN        10
57
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,
60  * we use 0/0. */
61 #define DBVERSION_KEY    "\0\0\0\0\0"
62 #define DBVERSION_KEYLEN 5
63 #define DBVERSION1       0x00000001U
64 #define DBVERSION        DBVERSION1
65
66 #define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
67
68 #define MAXITER     0xFFFF      /* maximum number of simultaneously open CNID
69                                  * databases. */
70
71 static char *old_dbfiles[] = {"cnid.db", NULL};
72
73 /* -----------------------
74  * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
75 */
76 static int my_yield(void)
77 {
78     struct timeval t;
79     int ret;
80
81     t.tv_sec = 0;
82     t.tv_usec = 1000;
83     ret = select(0, NULL, NULL, NULL, &t);
84     return 0;
85 }
86
87 /* --------------- */
88 static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
89 {
90 int len;
91  
92     memset(skey, 0, sizeof(DBT));
93     skey->data = (char *)pdata->data + CNID_DID_OFS;
94     len = strlen((char *)skey->data + CNID_DID_LEN);
95     skey->size = CNID_DID_LEN + len + 1;
96     return (0);
97 }
98  
99 /* --------------- */
100 static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
101 {
102     memset(skey, 0, sizeof(DBT));
103     skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
104     skey->size = CNID_DEVINO_LEN;
105     return (0);
106 }
107  
108 /* --------------- */
109 static int  my_associate (DB *p, DB *s,
110                    int (*callback)(DB *, const DBT *,const DBT *, DBT *),
111                    u_int32_t flags)
112 {
113 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
114     return p->associate(p, NULL, s, callback, flags);
115 #else
116 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
117     return p->associate(p,       s, callback, flags);
118 #else
119     return 0; /* FIXME */
120 #endif
121 #endif
122 }
123
124 /* --------------- */
125 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
126 {
127 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
128     return p->open(p, NULL, f, d, t, flags, mode);
129 #else
130     return p->open(p, f, d, t, flags, mode);
131 #endif
132 }
133
134 /* --------------- */
135 static struct _cnid_db *cnid_cdb_new(const char *volpath)
136 {
137     struct _cnid_db *cdb;
138     int major, minor, patch;
139     char *version_str;
140
141     version_str = db_version(&major, &minor, &patch);
142
143     /* check library match, ignore if only patch level changed */
144     if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
145     {
146         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); 
147         return NULL;
148     }
149
150     if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
151         return NULL;
152
153     if ((cdb->volpath = strdup(volpath)) == NULL) {
154         free(cdb);
155         return NULL;
156     }
157
158     cdb->flags = CNID_FLAG_PERSISTENT;
159
160     cdb->cnid_add = cnid_cdb_add;
161     cdb->cnid_delete = cnid_cdb_delete;
162     cdb->cnid_get = cnid_cdb_get;
163     cdb->cnid_lookup = cnid_cdb_lookup;
164     cdb->cnid_nextid = NULL;    /*cnid_cdb_nextid;*/
165     cdb->cnid_resolve = cnid_cdb_resolve;
166     cdb->cnid_update = cnid_cdb_update;
167     cdb->cnid_close = cnid_cdb_close;
168     cdb->cnid_getstamp = cnid_cdb_getstamp;
169     cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
170
171     return cdb;
172 }
173
174 /* --------------- */
175 static int upgrade_required(char *dbdir)
176 {
177     char path[MAXPATHLEN + 1];
178     int len, i;
179     int found = 0;
180     struct stat st;
181     
182     strcpy(path, dbdir);
183
184     len = strlen(path);
185     if (path[len - 1] != '/') {
186         strcat(path, "/");
187         len++;
188     }
189     
190     for (i = 0; old_dbfiles[i] != NULL; i++) {
191         strcpy(path + len, old_dbfiles[i]);
192         if ( !(stat(path, &st) < 0) ) {
193             found++;
194             continue;
195         }
196         if (errno != ENOENT) {
197             LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
198             found++;
199         }
200     }
201     return found;
202 }
203
204 /* --------------- */
205 struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
206 {
207     struct stat st;
208     char path[MAXPATHLEN + 1];
209     CNID_private *db;
210     struct _cnid_db *cdb;
211     int open_flag, len;
212     static int first = 0;
213     int rc;
214
215     if (!args->dir || *args->dir == 0) {
216         return NULL;
217     }
218
219     /* this checks .AppleDB.
220        We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
221     if ((len = strlen(args->dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
222         LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", args->dir);
223         return NULL;
224     }
225
226     if ((cdb = cnid_cdb_new(args->dir)) == NULL) {
227         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
228         return NULL;
229     }
230
231     if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
232         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
233         goto fail_cdb;
234     }
235
236     cdb->_private = (void *) db;
237     db->magic = CNID_DB_MAGIC;
238
239     strcpy(path, args->dir);
240     if (path[len - 1] != '/') {
241         strcat(path, "/");
242         len++;
243     }
244
245     strcpy(path + len, DBHOME);
246     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~args->mask) < 0)) {
247         LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
248         goto fail_adouble;
249     }
250
251     if (upgrade_required(path)) {
252         LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
253         goto fail_adouble;
254     }
255
256     /* Print out the version of BDB we're linked against. */
257     if (!first) {
258         first = 1;
259         LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
260     }
261
262     open_flag = DB_CREATE;
263
264     /* We need to be able to open the database environment with full
265      * transaction, logging, and locking support if we ever hope to 
266      * be a true multi-acess file server. */
267     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
268         LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
269         goto fail_lock;
270     }
271
272     /* Open the database environment. */
273     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~args->mask)) != 0) {
274         LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
275         /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
276         if (rc == DB_RUNRECOVERY) {
277             /* This is the mother of all errors.  We _must_ fail here. */
278             LOG(log_error, logtype_default,
279                 "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
280             goto fail_lock;
281         }
282
283         /* We can't get a full transactional environment, so multi-access
284          * is out of the question.  Let's assume a read-only environment,
285          * and try to at least get a shared memory pool. */
286         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~args->mask)) != 0) {
287             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
288              * open the environment with no flags. */
289             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~args->mask)) != 0) {
290                 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
291                 goto fail_lock;
292             }
293         }
294         db->flags |= CNIDFLAG_DB_RO;
295         open_flag = DB_RDONLY;
296         LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
297     }
298
299     /* ---------------------- */
300     /* Main CNID database.  Use a hash for this one. */
301
302     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
303         LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
304             db_strerror(rc));
305         goto fail_appinit;
306     }
307
308     if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
309         LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
310             db_strerror(rc));
311         goto fail_appinit;
312     }
313
314     /* ---------------------- */
315     /* did/name reverse mapping.  We use a BTree for this one. */
316
317     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
318         LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
319             db_strerror(rc));
320         goto fail_appinit;
321     }
322
323     if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~args->mask))) {
324         LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
325             db_strerror(rc));
326         goto fail_appinit;
327     }
328
329     /* ---------------------- */
330     /* dev/ino reverse mapping.  Use a hash for this one. */
331
332     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
333         LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
334             db_strerror(rc));
335         goto fail_appinit;
336     }
337
338     if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
339         LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
340             db_strerror(rc));
341         goto fail_appinit;
342     }
343
344     /* ---------------------- */
345     /* Associate the secondary with the primary. */ 
346     if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
347         LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
348             db_strerror(rc));
349         goto fail_appinit;
350     }
351  
352     if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
353         LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
354             db_strerror(rc));
355         goto fail_appinit;
356     }
357  
358 #if 0
359     DBT key, pkey, data;
360     /* ---------------------- */
361     /* Check for version.  This way we can update the database if we need
362      * to change the format in any way. */
363     memset(&key, 0, sizeof(key));
364     memset(&pkey, 0, sizeof(DBT));
365     memset(&data, 0, sizeof(data));
366     key.data = DBVERSION_KEY;
367     key.size = DBVERSION_KEYLEN;
368
369     if ((rc = db->db_didname->pget(db->db_didname, NULL, &key, &pkey, &data, 0)) != 0) {
370         int ret;
371         {
372             u_int32_t version = htonl(DBVERSION);
373
374             data.data = &version;
375             data.size = sizeof(version);
376         }
377         if ((ret = db->db_didname->put(db->db_cnid, NULL, &key, &data,
378                                        DB_NOOVERWRITE))) {
379             LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
380                 db_strerror(ret));
381             db->db_didname->close(db->db_didname, 0);
382             goto fail_appinit;
383         }
384     }
385 #endif
386
387     /* TODO In the future we might check for version number here. */
388 #if 0
389     memcpy(&version, data.data, sizeof(version));
390     if (version != ntohl(DBVERSION)) {
391         /* Do stuff here. */
392     }
393 #endif /* 0 */
394
395     db_env_set_func_yield(my_yield);
396     return cdb;
397
398   fail_appinit:
399     if (db->db_didname)
400         db->db_didname->close(db->db_didname, 0);
401     if (db->db_devino)
402         db->db_devino->close(db->db_devino, 0);
403     if (db->db_cnid)
404         db->db_cnid->close(db->db_cnid, 0);
405     LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
406     db->dbenv->close(db->dbenv, 0);
407
408   fail_lock:
409
410   fail_adouble:
411
412     free(db);
413
414   fail_cdb:
415     if (cdb->volpath != NULL)
416         free(cdb->volpath);
417     free(cdb);
418
419     return NULL;
420 }
421
422 struct _cnid_module cnid_cdb_module = {
423     "cdb",
424     {NULL, NULL},
425     cnid_cdb_open,
426     CNID_FLAG_SETUID | CNID_FLAG_BLOCK
427 };
428
429
430 #endif /* CNID_BACKEND_CDB */