]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cdb/cnid_cdb_open.c
- merge branch-netatalk-afp-3x-dev, HEAD was tagged before
[netatalk.git] / libatalk / cnid / cdb / cnid_cdb_open.c
1
2 /*
3  * $Id: cnid_cdb_open.c,v 1.2 2005-04-28 20:49:59 bfernhomberg 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(dbp, pkey, pdata, skey)
89 DB *dbp _U_;
90 const DBT *pkey _U_, *pdata;
91 DBT *skey;
92 {
93 int len;
94  
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;
99     return (0);
100 }
101  
102 /* --------------- */
103 static int devino(dbp, pkey, pdata, skey)
104 DB *dbp _U_;
105 const DBT *pkey _U_, *pdata;
106 DBT *skey;
107 {
108     memset(skey, 0, sizeof(DBT));
109     skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
110     skey->size = CNID_DEVINO_LEN;
111     return (0);
112 }
113  
114 /* --------------- */
115 static int  my_associate (DB *p, DB *s,
116                    int (*callback)(DB *, const DBT *,const DBT *, DBT *),
117                    u_int32_t flags)
118 {
119 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
120     return p->associate(p, NULL, s, callback, flags);
121 #else
122 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
123     return p->associate(p,       s, callback, flags);
124 #else
125     return 0; /* FIXME */
126 #endif
127 #endif
128 }
129
130 /* --------------- */
131 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
132 {
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);
135 #else
136     return p->open(p, f, d, t, flags, mode);
137 #endif
138 }
139
140 /* --------------- */
141 static struct _cnid_db *cnid_cdb_new(const char *volpath)
142 {
143     struct _cnid_db *cdb;
144     int major, minor, patch;
145     char *version_str;
146
147     version_str = db_version(&major, &minor, &patch);
148
149     /* check library match, ignore if only patch level changed */
150     if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
151     {
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); 
153         return NULL;
154     }
155
156     if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
157         return NULL;
158
159     if ((cdb->volpath = strdup(volpath)) == NULL) {
160         free(cdb);
161         return NULL;
162     }
163
164     cdb->flags = CNID_FLAG_PERSISTENT;
165
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;
174     cdb->cnid_getstamp = cnid_cdb_getstamp;
175     cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
176
177     return cdb;
178 }
179
180 /* --------------- */
181 static int upgrade_required(char *dbdir)
182 {
183     char path[MAXPATHLEN + 1];
184     int len, i;
185     int found = 0;
186     struct stat st;
187     
188     strcpy(path, dbdir);
189
190     len = strlen(path);
191     if (path[len - 1] != '/') {
192         strcat(path, "/");
193         len++;
194     }
195     
196     for (i = 0; old_dbfiles[i] != NULL; i++) {
197         strcpy(path + len, old_dbfiles[i]);
198         if ( !(stat(path, &st) < 0) ) {
199             found++;
200             continue;
201         }
202         if (errno != ENOENT) {
203             LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
204             found++;
205         }
206     }
207     return found;
208 }
209
210 /* --------------- */
211 struct _cnid_db *cnid_cdb_open(const char *dir, mode_t mask)
212 {
213     struct stat st;
214     char path[MAXPATHLEN + 1];
215     CNID_private *db;
216     struct _cnid_db *cdb;
217     int open_flag, len;
218     static int first = 0;
219     int rc;
220
221     if (!dir || *dir == 0) {
222         return NULL;
223     }
224
225     /* this checks .AppleDB.
226        We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
227     if ((len = strlen(dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
228         LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
229         return NULL;
230     }
231
232     if ((cdb = cnid_cdb_new(dir)) == NULL) {
233         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
234         return NULL;
235     }
236
237     if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
238         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
239         goto fail_cdb;
240     }
241
242     cdb->_private = (void *) db;
243     db->magic = CNID_DB_MAGIC;
244
245     strcpy(path, dir);
246     if (path[len - 1] != '/') {
247         strcat(path, "/");
248         len++;
249     }
250
251     strcpy(path + len, DBHOME);
252     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
253         LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
254         goto fail_adouble;
255     }
256
257     if (upgrade_required(path)) {
258         LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
259         goto fail_adouble;
260     }
261
262     /* Print out the version of BDB we're linked against. */
263     if (!first) {
264         first = 1;
265         LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
266     }
267
268     open_flag = DB_CREATE;
269
270     /* We need to be able to open the database environment with full
271      * transaction, logging, and locking support if we ever hope to 
272      * be a true multi-acess file server. */
273     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
274         LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
275         goto fail_lock;
276     }
277
278     /* Open the database environment. */
279     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
280         LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
281         /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
282         if (rc == DB_RUNRECOVERY) {
283             /* This is the mother of all errors.  We _must_ fail here. */
284             LOG(log_error, logtype_default,
285                 "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
286             goto fail_lock;
287         }
288
289         /* We can't get a full transactional environment, so multi-access
290          * is out of the question.  Let's assume a read-only environment,
291          * and try to at least get a shared memory pool. */
292         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
293             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
294              * open the environment with no flags. */
295             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
296                 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
297                 goto fail_lock;
298             }
299         }
300         db->flags |= CNIDFLAG_DB_RO;
301         open_flag = DB_RDONLY;
302         LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
303     }
304
305     /* ---------------------- */
306     /* Main CNID database.  Use a hash for this one. */
307
308     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
309         LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
310             db_strerror(rc));
311         goto fail_appinit;
312     }
313
314     if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
315         LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
316             db_strerror(rc));
317         goto fail_appinit;
318     }
319
320     /* ---------------------- */
321     /* did/name reverse mapping.  We use a BTree for this one. */
322
323     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
324         LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
325             db_strerror(rc));
326         goto fail_appinit;
327     }
328
329     if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~mask))) {
330         LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
331             db_strerror(rc));
332         goto fail_appinit;
333     }
334
335     /* ---------------------- */
336     /* dev/ino reverse mapping.  Use a hash for this one. */
337
338     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
339         LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
340             db_strerror(rc));
341         goto fail_appinit;
342     }
343
344     if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~mask)) != 0) {
345         LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
346             db_strerror(rc));
347         goto fail_appinit;
348     }
349
350     /* ---------------------- */
351     /* Associate the secondary with the primary. */ 
352     if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
353         LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
354             db_strerror(rc));
355         goto fail_appinit;
356     }
357  
358     if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
359         LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
360             db_strerror(rc));
361         goto fail_appinit;
362     }
363  
364 #if 0
365     DBT key, pkey, data;
366     /* ---------------------- */
367     /* Check for version.  This way we can update the database if we need
368      * to change the format in any way. */
369     memset(&key, 0, sizeof(key));
370     memset(&pkey, 0, sizeof(DBT));
371     memset(&data, 0, sizeof(data));
372     key.data = DBVERSION_KEY;
373     key.size = DBVERSION_KEYLEN;
374
375     if ((rc = db->db_didname->pget(db->db_didname, NULL, &key, &pkey, &data, 0)) != 0) {
376         int ret;
377         {
378             u_int32_t version = htonl(DBVERSION);
379
380             data.data = &version;
381             data.size = sizeof(version);
382         }
383         if ((ret = db->db_didname->put(db->db_cnid, NULL, &key, &data,
384                                        DB_NOOVERWRITE))) {
385             LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
386                 db_strerror(ret));
387             db->db_didname->close(db->db_didname, 0);
388             goto fail_appinit;
389         }
390     }
391 #endif
392
393     /* TODO In the future we might check for version number here. */
394 #if 0
395     memcpy(&version, data.data, sizeof(version));
396     if (version != ntohl(DBVERSION)) {
397         /* Do stuff here. */
398     }
399 #endif /* 0 */
400
401     db_env_set_func_yield(my_yield);
402     return cdb;
403
404   fail_appinit:
405     if (db->db_didname)
406         db->db_didname->close(db->db_didname, 0);
407     if (db->db_devino)
408         db->db_devino->close(db->db_devino, 0);
409     if (db->db_cnid)
410         db->db_cnid->close(db->db_cnid, 0);
411     LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
412     db->dbenv->close(db->dbenv, 0);
413
414   fail_lock:
415
416   fail_adouble:
417
418     free(db);
419
420   fail_cdb:
421     if (cdb->volpath != NULL)
422         free(cdb->volpath);
423     free(cdb);
424
425     return NULL;
426 }
427
428 struct _cnid_module cnid_cdb_module = {
429     "cdb",
430     {NULL, NULL},
431     cnid_cdb_open,
432     CNID_FLAG_SETUID | CNID_FLAG_BLOCK
433 };
434
435
436 #endif /* CNID_BACKEND_CDB */