]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cdb/cnid_cdb_open.c
Spotlight: use async Tracker SPARQL API
[netatalk.git] / libatalk / cnid / cdb / cnid_cdb_open.c
1 /*
2  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3  * All Rights Reserved. See COPYRIGHT.
4  *
5  * CNID database support. 
6  *
7  * here's the deal:
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 --
12  *     start a database:
13  *     cnid = cnid_open(root_dir);
14  *
15  *     allocate a new id: 
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
19  *     requested.
20  *
21  *     given an id, get a did/name and dev/ino pair.
22  *     name = cnid_get(cnid, &id); given an id, return the corresponding
23  *     info.
24  *     return code = cnid_delete(cnid, id); delete an entry. 
25  *
26  * with AFP, CNIDs 0-2 have special meanings. here they are:
27  * 0 -- invalid cnid
28  * 1 -- parent of root directory (handled by afpd) 
29  * 2 -- root directory (handled by afpd)
30  *
31  * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so, 
32  * CNID_START begins at 17.
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif /* HAVE_CONFIG_H */
38
39 #ifdef CNID_BACKEND_CDB
40
41 #include <atalk/volume.h>
42 #include <atalk/cnid_private.h>
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 #define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
59
60 #define MAXITER     0xFFFF      /* maximum number of simultaneously open CNID
61                                  * databases. */
62
63 static char *old_dbfiles[] = {"cnid.db", NULL};
64
65 /* --------------- */
66 static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
67 {
68 int len;
69  
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;
74     return (0);
75 }
76  
77 /* --------------- */
78 static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
79 {
80     memset(skey, 0, sizeof(DBT));
81     skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
82     skey->size = CNID_DEVINO_LEN;
83     return (0);
84 }
85  
86 /* --------------- */
87 static int  my_associate (DB *p, DB *s,
88                    int (*callback)(DB *, const DBT *,const DBT *, DBT *),
89                    u_int32_t flags)
90 {
91 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
92     return p->associate(p, NULL, s, callback, flags);
93 #else
94 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
95     return p->associate(p,       s, callback, flags);
96 #else
97     return 0; /* FIXME */
98 #endif
99 #endif
100 }
101
102 /* --------------- */
103 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
104 {
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);
107 #else
108     return p->open(p, f, d, t, flags, mode);
109 #endif
110 }
111
112 /* --------------- */
113 static struct _cnid_db *cnid_cdb_new(struct vol *vol)
114 {
115     struct _cnid_db *cdb;
116     int major, minor, patch;
117     char *version_str;
118
119     version_str = db_version(&major, &minor, &patch);
120
121     /* check library match, ignore if only patch level changed */
122     if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
123     {
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); 
125         return NULL;
126     }
127
128     if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
129         return NULL;
130
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;
144     return cdb;
145 }
146
147 /* --------------- */
148 static int upgrade_required(char *dbdir)
149 {
150     char path[MAXPATHLEN + 1];
151     int len, i;
152     int found = 0;
153     struct stat st;
154     
155     strcpy(path, dbdir);
156
157     len = strlen(path);
158     if (path[len - 1] != '/') {
159         strcat(path, "/");
160         len++;
161     }
162     
163     for (i = 0; old_dbfiles[i] != NULL; i++) {
164         strcpy(path + len, old_dbfiles[i]);
165         if ( !(stat(path, &st) < 0) ) {
166             found++;
167             continue;
168         }
169         if (errno != ENOENT) {
170             LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
171             found++;
172         }
173     }
174     return found;
175 }
176
177 /* --------------- */
178 struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
179 {
180     struct stat st;
181     char path[MAXPATHLEN + 1];
182     CNID_private *db;
183     struct _cnid_db *cdb;
184     int open_flag, len;
185     static int first = 0;
186     int rc;
187     struct vol *vol = args->cnid_args_vol;
188
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);
193         return NULL;
194     }
195
196     if ((cdb = cnid_cdb_new(vol)) == NULL) {
197         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
198         return NULL;
199     }
200
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");
203         goto fail_cdb;
204     }
205
206     cdb->cnid_db_private = (void *) db;
207
208     strcpy(path, vol->v_path);
209     if (path[len - 1] != '/') {
210         strcat(path, "/");
211         len++;
212     }
213
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);
217         goto fail_adouble;
218     }
219
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");
222         goto fail_adouble;
223     }
224
225     /* Print out the version of BDB we're linked against. */
226     if (!first) {
227         first = 1;
228         LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
229     }
230
231     open_flag = DB_CREATE;
232
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));
238         goto fail_lock;
239     }
240
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);
249             goto fail_lock;
250         }
251
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));
260                 goto fail_lock;
261             }
262         }
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);
266     }
267
268     /* ---------------------- */
269     /* Main CNID database.  Use a hash for this one. */
270
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",
273             db_strerror(rc));
274         goto fail_appinit;
275     }
276
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",
279             db_strerror(rc));
280         goto fail_appinit;
281     }
282
283     /* ---------------------- */
284     /* did/name reverse mapping.  We use a BTree for this one. */
285
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",
288             db_strerror(rc));
289         goto fail_appinit;
290     }
291
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",
294             db_strerror(rc));
295         goto fail_appinit;
296     }
297
298     /* ---------------------- */
299     /* dev/ino reverse mapping.  Use a hash for this one. */
300
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",
303             db_strerror(rc));
304         goto fail_appinit;
305     }
306
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",
309             db_strerror(rc));
310         goto fail_appinit;
311     }
312
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",
317             db_strerror(rc));
318         goto fail_appinit;
319     }
320  
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",
323             db_strerror(rc));
324         goto fail_appinit;
325     }
326  
327     /* ---------------------- */
328     /* Check for version. "cdb" only supports CNID_VERSION_0, cf cnid_private.h */
329
330     DBT key, data;
331     uint32_t version;
332
333     memset(&key, 0, sizeof(key));
334     memset(&data, 0, sizeof(data));
335     key.data = ROOTINFO_KEY;
336     key.size = ROOTINFO_KEYLEN;
337
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);
345             goto fail_appinit;
346         }
347     }
348
349     return cdb;
350
351   fail_appinit:
352     if (db->db_didname)
353         db->db_didname->close(db->db_didname, 0);
354     if (db->db_devino)
355         db->db_devino->close(db->db_devino, 0);
356     if (db->db_cnid)
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);
360
361   fail_lock:
362
363   fail_adouble:
364
365     free(db);
366
367   fail_cdb:
368     free(cdb);
369
370     return NULL;
371 }
372
373 struct _cnid_module cnid_cdb_module = {
374     "cdb",
375     {NULL, NULL},
376     cnid_cdb_open,
377     CNID_FLAG_SETUID | CNID_FLAG_BLOCK
378 };
379
380
381 #endif /* CNID_BACKEND_CDB */