]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
da17aae10942adf649e0f473b23385db3ddb7991
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.c,v 1.22 2001-12-07 17:29:06 jmarcus Exp $
3  *
4  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved. See COPYRIGHT.
6  *
7  * CNID database support. 
8  *
9  * here's the deal:
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 --
14  *     start a database:
15  *     cnid = cnid_open(root_dir);
16  *
17  *     allocate a new id: 
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
21  *     requested.
22  *
23  *     given an id, get a did/name and dev/ino pair.
24  *     name = cnid_get(cnid, &id); given an id, return the corresponding
25  *     info.
26  *     return code = cnid_delete(cnid, id); delete an entry. 
27  *
28  * with AFP, CNIDs 0-2 have special meanings. here they are:
29  * 0 -- invalid cnid
30  * 1 -- parent of root directory (handled by afpd) 
31  * 2 -- root directory (handled by afpd)
32  *
33  * so, CNID_START begins at 3.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif /* HAVE_CONFIG_H */
39
40 #ifdef CNID_DB
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif /* HAVE_UNISTD_H */
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif /* HAVE_FCNTL_H */
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <syslog.h>
53
54 #include <db.h>
55
56 #include <atalk/adouble.h>
57 #include <atalk/cnid.h>
58 #include <atalk/util.h>
59
60 #include "cnid_private.h"
61
62 #ifndef MIN
63 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
64 #endif /* ! MIN */
65
66 #define DBHOME       ".AppleDB"
67 #define DBCNID       "cnid.db"
68 #define DBDEVINO     "devino.db"
69 #define DBDIDNAME    "didname.db"   /* did/full name mapping */
70 #define DBSHORTNAME  "shortname.db" /* did/8+3 mapping */
71 #define DBMACNAME    "macname.db"   /* did/31 mapping */
72 #define DBLONGNAME   "longname.db"  /* did/unicode mapping */
73 #define DBLOCKFILE   "cnid.lock"
74
75 #define DBHOMELEN    8
76 #define DBLEN        10
77
78 /* we version the did/name database so that we can change the format
79  * if necessary. the key is in the form of a did/name pair. in this case,
80  * we use 0/0. */
81 #define DBVERSION_KEY    "\0\0\0\0\0"
82 #define DBVERSION_KEYLEN 5
83 #define DBVERSION1       0x00000001U
84 #define DBVERSION        DBVERSION1
85
86 #if DB_VERSION_MINOR > 1
87 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
88 DB_INIT_LOG | DB_INIT_TXN)
89 #else /* DB_VERSION_MINOR < 1 */
90 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
91 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
92 /*#define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
93 DB_INIT_LOG | DB_INIT_TXN)*/
94 #endif /* DB_VERSION_MINOR */
95
96 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
97 * databases. */
98
99 /* the first compare that's always done. */
100 static __inline__ int compare_did(const DBT *a, const DBT *b)
101 {
102     u_int32_t dida, didb;
103
104     memcpy(&dida, a->data, sizeof(dida));
105     memcpy(&didb, b->data, sizeof(didb));
106     return dida - didb;
107 }
108
109 /* sort did's and then names. this is for unix paths.
110  * i.e., did/unixname lookups. */
111 #if DB_VERSION_MINOR > 1
112 static int compare_unix(DB *db, const DBT *a, const DBT *b)
113 #else /* DB_VERSION_MINOR < 1 */
114 static int compare_unix(const DBT *a, const DBT *b)
115 #endif /* DB_VERSION_MINOR */
116 {
117     u_int8_t *sa, *sb;
118     int len, ret;
119
120     /* sort by did */
121     if ((ret = compare_did(a, b)))
122         return ret;
123
124     sa = (u_int8_t *) a->data + 4; /* shift past did */
125     sb = (u_int8_t *) b->data + 4;
126     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
127         if ((ret = (*sa - *sb)))
128             return ret; /* sort by lexical ordering */
129
130     return a->size - b->size; /* sort by length */
131 }
132
133 /* sort did's and then names. this is for macified paths (i.e.,
134  * did/macname, and did/shortname. i think did/longname needs a
135  * unicode table to work. also, we can't use strdiacasecmp as that
136  * returns a match if a < b. */
137 #if DB_VERSION_MINOR > 1
138 static int compare_mac(DB *db, const DBT *a, const DBT *b)
139 #else /* DB_VERSION_MINOR < 1 */
140 static int compare_mac(const DBT *a, const DBT *b)
141 #endif /* DB_VERSION_MINOR */
142 {
143     u_int8_t *sa, *sb;
144     int len, ret;
145
146     /* sort by did */
147     if ((ret = compare_did(a, b)))
148         return ret;
149
150     sa = (u_int8_t *) a->data + 4;
151     sb = (u_int8_t *) b->data + 4;
152     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
153         if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
154             return ret; /* sort by lexical ordering */
155
156     return a->size - b->size; /* sort by length */
157 }
158
159
160 /* for unicode names -- right now it's the same as compare_mac. */
161 #if DB_VERSION_MINOR > 1
162 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
163 #else /* DB_VERSION_MINOR < 1 */
164 static int compare_unicode(const DBT *a, const DBT *b)
165 #endif /* DB_VERSION_MINOR */
166 {
167 #if DB_VERSION_MINOR > 1
168     return compare_mac(db,a,b);
169 #else /* DB_VERSION_MINOR < 1 */
170     return compare_mac(a,b);
171 #endif /* DB_VERSION_MINOR */
172 }
173
174 static int have_lock = 0;
175
176 void *cnid_open(const char *dir) {
177     struct stat st;
178     struct flock lock;
179     char path[MAXPATHLEN + 1];
180     CNID_private *db;
181     DBT key, data;
182     DB_TXN *tid;
183     u_int32_t DBEXTRAS = 0;
184     int open_flag, len;
185     int rc;
186
187     if (!dir) {
188         return NULL;
189     }
190
191     /* this checks .AppleDB */
192     if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
193         syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
194         return NULL;
195     }
196
197     if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
198         syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
199         return NULL;
200     }
201
202     db->magic = CNID_DB_MAGIC;
203
204     strcpy(path, dir);
205     if (path[len - 1] != '/') {
206         strcat(path, "/");
207         len++;
208     }
209
210     lock.l_type = F_WRLCK;
211     lock.l_whence = SEEK_SET;
212
213     strcpy(path + len, DBHOME);
214     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
215         syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed for %s", path);
216         goto fail_adouble;
217     }
218
219         /* Make sure cnid.lock goes in .AppleDB. */
220         strcat(path, "/");
221         len++;
222
223     /* Search for a byte lock.  This allows us to cleanup the log files
224      * at cnid_close() in a clean fashion.
225      *
226      * NOTE: This won't work if multiple volumes for the same user refer
227      * to the sahe directory. */
228     strcat(path, DBLOCKFILE);
229     if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
230         lock.l_start = 0;
231         lock.l_len = 1;
232         while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
233             if (++lock.l_start > MAXITER) {
234                 syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
235                 close(db->lockfd);
236                 db->lockfd = -1;
237                 break;
238             }
239         }
240     }
241     else {
242         syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
243     }
244
245     if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
246         /* We test to see if we have exclusive database access.  If we do, we
247          * will open the database with the DB_RECOVER flag.
248          */
249 #ifdef DEBUG
250         syslog(LOG_INFO, "cnid_open: Opening database environment %s with DB_RECOVER flag", path);
251 #endif
252         DBEXTRAS |= DB_RECOVER;
253         have_lock = 1;
254     }
255
256
257     path[len + DBHOMELEN] = '\0';
258     open_flag = DB_CREATE;
259
260     /* We need to be able to open the database environment with full
261      * transaction, logging, and locking support if we ever hope to 
262      * be a true multi-acess file server. */
263     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
264         syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
265         goto fail_lock;
266     }
267
268     /* Setup internal deadlock detection. */
269     if ((rc = db->dbenv->set_lk_detect(db->dbenv, DB_LOCK_DEFAULT)) != 0) {
270         syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
271         goto fail_lock;
272     }
273
274 #if DB_VERSION_MINOR > 1
275     /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
276     if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
277         syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
278         goto fail_lock;
279     }
280 #endif /* DB_VERSION_MINOR > 1 */
281
282     /* Open the database environment. */
283     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
284         if (rc == DB_RUNRECOVERY) {
285             /* This is the mother of all errors.  We _must_ fail here. */
286             syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
287             goto fail_lock;
288         }
289
290         /* We can't get a full transactional environment, so multi-access
291          * is out of the question.  Let's assume a read-only environment,
292          * and try to at least get a shared memory pool. */
293         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
294             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
295              * open the environment with no flags. */
296             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
297                 syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
298                        path, db_strerror(rc));
299                 goto fail_lock;
300             }
301         }
302         db->flags |= CNIDFLAG_DB_RO;
303         open_flag = DB_RDONLY;
304         syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
305     }
306
307     /* did/name reverse mapping.  We use a BTree for this one. */
308     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
309         syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
310                db_strerror(rc));
311         goto fail_appinit;
312     }
313
314     db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
315     if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
316                                    DB_BTREE, open_flag, 0666))) {
317         syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
318                db_strerror(rc));
319         goto fail_appinit;
320     }
321
322     /* Check for version.  This way we can update the database if we need
323      * to change the format in any way. */
324     memset(&key, 0, sizeof(key));
325     memset(&data, 0, sizeof(data));
326     key.data = DBVERSION_KEY;
327     key.size = DBVERSION_KEYLEN;
328
329 dbversion_retry:
330     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
331         syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
332                db_strerror(rc));
333         db->db_didname->close(db->db_didname, 0);
334         goto fail_appinit;
335     }
336
337     while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
338         int ret;
339         switch (rc) {
340         case DB_LOCK_DEADLOCK:
341             if ((ret = txn_abort(tid)) != 0) {
342                 syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
343                 db->db_didname->close(db->db_didname, 0);
344                 goto fail_appinit;
345             }
346             goto dbversion_retry;
347         case DB_NOTFOUND:
348             {
349                 u_int32_t version = htonl(DBVERSION);
350
351                 data.data = &version;
352                 data.size = sizeof(version);
353             }
354
355             if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
356                                            DB_NOOVERWRITE))) {
357                 if (ret == DB_LOCK_DEADLOCK) {
358                     if ((ret = txn_abort(tid)) != 0) {
359                         syslog(LOG_ERR, "cnid_open: txn_abort: %s",
360                                db_strerror(ret));
361                         db->db_didname->close(db->db_didname, 0);
362                         goto fail_appinit;
363                     }
364                     goto dbversion_retry;
365                 }
366                 else if (ret == DB_RUNRECOVERY) {
367                     /* At this point, we don't care if the transaction aborts
368                      * successfully or not. */
369                     txn_abort(tid);
370                     syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
371                            db_strerror(ret));
372                     db->db_didname->close(db->db_didname, 0);
373                     goto fail_appinit;
374                 }
375             }
376             break; /* while loop */
377         default:
378             txn_abort(tid);
379             syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
380                    db_strerror(rc));
381             db->db_didname->close(db->db_didname, 0);
382             goto fail_appinit;
383         }
384     }
385
386     if ((rc = txn_commit(tid, 0)) != 0) {
387         syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
388                db_strerror(rc));
389         db->db_didname->close(db->db_didname, 0);
390         goto fail_appinit;
391     }
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 #ifdef EXTENDED_DB
402     /* did/macname (31 character) mapping.  Use a BTree for this one. */
403     if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
404         syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
405                db_strerror(rc));
406         db->db_didname->close(db->db_didname, 0);
407         goto fail_appinit;
408     }
409
410     db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
411     if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
412         syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
413                db_strerror(rc));
414         db->db_didname->close(db->db_didname, 0);
415         goto fail_appinit;
416     }
417
418     /* did/shortname (DOS 8.3) mapping.  Use a BTree for this one. */
419     if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
420         syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
421                db_strerror(rc));
422         db->db_didname->close(db->db_didname, 0);
423         db->db_macname->close(db->db_macname, 0);
424         goto fail_appinit;
425     }
426
427     db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
428     if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
429         syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
430                db_strerror(rc));
431         db->db_didname->close(db->db_didname, 0);
432         db->db_macname->close(db->db_macname, 0);
433         goto fail_appinit;
434     }
435
436     /* did/longname (Unicode) mapping.  Use a BTree for this one. */
437     if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
438         syslog(LOG_ERR, "cnid_open: Failed to create did/longname database: %s",
439                db_strerror(rc));
440         db->db_didname->close(db->db_didname, 0);
441         db->db_macname->close(db->db_macname, 0);
442         db->db_shortname->close(db->db_shortname, 0);
443         goto fail_appinit;
444     }
445
446     db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
447     if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
448         syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
449                db_strerror(rc));
450         db->db_didname->close(db->db_didname, 0);
451         db->db_macname->close(db->db_macname, 0);
452         db->db_shortname->close(db->db_shortname, 0);
453         goto fail_appinit;
454     }
455 #endif /* EXTENDED_DB */
456
457     /* dev/ino reverse mapping.  Use a hash for this one. */
458     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
459         syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
460                db_strerror(rc));
461         db->db_didname->close(db->db_didname, 0);
462 #ifdef EXTENDED_DB
463         db->db_macname->close(db->db_macname, 0);
464         db->db_shortname->close(db->db_shortname, 0);
465         db->db_longname->close(db->db_longname, 0);
466 #endif /* EXTENDED_DB */
467         goto fail_appinit;
468     }
469
470     if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
471         syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
472                db_strerror(rc));
473         db->db_didname->close(db->db_didname, 0);
474 #ifdef EXTENDED_DB
475         db->db_macname->close(db->db_macname, 0);
476         db->db_shortname->close(db->db_shortname, 0);
477         db->db_longname->close(db->db_longname, 0);
478 #endif /* EXTENDED_DB */
479         goto fail_appinit;
480     }
481
482     /* Main CNID database.  Use a hash for this one. */
483     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
484         syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
485                db_strerror(rc));
486         db->db_didname->close(db->db_didname, 0);
487 #ifdef EXTENDED_DB
488         db->db_macname->close(db->db_macname, 0);
489         db->db_shortname->close(db->db_shortname, 0);
490         db->db_longname->close(db->db_longname, 0);
491 #endif /* EXTENDED_DB */
492         db->db_devino->close(db->db_devino, 0);
493         goto fail_appinit;
494     }
495
496
497     if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
498         syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
499                db_strerror(rc));
500         db->db_didname->close(db->db_didname, 0);
501 #ifdef EXTENDED_DB
502         db->db_macname->close(db->db_macname, 0);
503         db->db_shortname->close(db->db_shortname, 0);
504         db->db_longname->close(db->db_longname, 0);
505 #endif /* EXTENDED_DB */
506         goto fail_appinit;
507     }
508
509     return db;
510
511 fail_appinit:
512     syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
513     db->dbenv->close(db->dbenv, 0);
514
515 fail_lock:
516     if (db->lockfd > -1) {
517         close(db->lockfd);
518     }
519
520 fail_adouble:
521
522 fail_db:
523     free(db);
524     return NULL;
525 }
526 #endif /* CNID_DB */
527
528