]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
910e6aa19af54c91a2601cb474eabbdb3e1dc96c
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.c,v 1.23 2001-12-10 03:51:56 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 #define DBRECOVERFILE "cnid.dbrecover"
75
76 #define DBHOMELEN    8
77 #define DBLEN        10
78
79 /* we version the did/name database so that we can change the format
80  * if necessary. the key is in the form of a did/name pair. in this case,
81  * we use 0/0. */
82 #define DBVERSION_KEY    "\0\0\0\0\0"
83 #define DBVERSION_KEYLEN 5
84 #define DBVERSION1       0x00000001U
85 #define DBVERSION        DBVERSION1
86
87 #if DB_VERSION_MINOR > 1
88 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
89 DB_INIT_LOG | DB_INIT_TXN)
90 #else /* DB_VERSION_MINOR < 1 */
91 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
92 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
93 /*#define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
94 DB_INIT_LOG | DB_INIT_TXN)*/
95 #endif /* DB_VERSION_MINOR */
96
97 /* Let's try and use the random deadlock decider if available.  This adds
98  * a bit of entropy to the mix that might be beneficial.  If random isn't
99  * available, we'll decide deadlocks by kicking off the youngest process.
100  * If we can't do that, then let DB3 use its default deadlock detector. */
101 #ifdef DB_LOCK_RANDOM
102 #define DEAD_LOCK_DETECT DB_LOCK_RANDOM
103 #elif defined DB_LOCK_YOUNGEST
104 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
105 #else /* DB_LOCK_RANDOM */
106 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
107 #endif /* DB_LOCK_RANDOM */
108
109 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
110 * databases. */
111
112 /* the first compare that's always done. */
113 static __inline__ int compare_did(const DBT *a, const DBT *b)
114 {
115     u_int32_t dida, didb;
116
117     memcpy(&dida, a->data, sizeof(dida));
118     memcpy(&didb, b->data, sizeof(didb));
119     return dida - didb;
120 }
121
122 /* sort did's and then names. this is for unix paths.
123  * i.e., did/unixname lookups. */
124 #if DB_VERSION_MINOR > 1
125 static int compare_unix(DB *db, const DBT *a, const DBT *b)
126 #else /* DB_VERSION_MINOR < 1 */
127 static int compare_unix(const DBT *a, const DBT *b)
128 #endif /* DB_VERSION_MINOR */
129 {
130     u_int8_t *sa, *sb;
131     int len, ret;
132
133     /* sort by did */
134     if ((ret = compare_did(a, b)))
135         return ret;
136
137     sa = (u_int8_t *) a->data + 4; /* shift past did */
138     sb = (u_int8_t *) b->data + 4;
139     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
140         if ((ret = (*sa - *sb)))
141             return ret; /* sort by lexical ordering */
142
143     return a->size - b->size; /* sort by length */
144 }
145
146 /* sort did's and then names. this is for macified paths (i.e.,
147  * did/macname, and did/shortname. i think did/longname needs a
148  * unicode table to work. also, we can't use strdiacasecmp as that
149  * returns a match if a < b. */
150 #if DB_VERSION_MINOR > 1
151 static int compare_mac(DB *db, const DBT *a, const DBT *b)
152 #else /* DB_VERSION_MINOR < 1 */
153 static int compare_mac(const DBT *a, const DBT *b)
154 #endif /* DB_VERSION_MINOR */
155 {
156     u_int8_t *sa, *sb;
157     int len, ret;
158
159     /* sort by did */
160     if ((ret = compare_did(a, b)))
161         return ret;
162
163     sa = (u_int8_t *) a->data + 4;
164     sb = (u_int8_t *) b->data + 4;
165     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
166         if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
167             return ret; /* sort by lexical ordering */
168
169     return a->size - b->size; /* sort by length */
170 }
171
172
173 /* for unicode names -- right now it's the same as compare_mac. */
174 #if DB_VERSION_MINOR > 1
175 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
176 #else /* DB_VERSION_MINOR < 1 */
177 static int compare_unicode(const DBT *a, const DBT *b)
178 #endif /* DB_VERSION_MINOR */
179 {
180 #if DB_VERSION_MINOR > 1
181     return compare_mac(db,a,b);
182 #else /* DB_VERSION_MINOR < 1 */
183     return compare_mac(a,b);
184 #endif /* DB_VERSION_MINOR */
185 }
186
187 static int have_lock = 0;
188
189 void *cnid_open(const char *dir) {
190     struct stat st, rsb;
191     struct flock lock;
192     char path[MAXPATHLEN + 1];
193         char recover_file[MAXPATHLEN + 1];
194     CNID_private *db;
195     DBT key, data;
196     DB_TXN *tid;
197     u_int32_t DBEXTRAS = 0;
198     int open_flag, len;
199     int rc, rfd = -1;
200
201     if (!dir) {
202         return NULL;
203     }
204
205     /* this checks .AppleDB */
206     if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
207         syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
208         return NULL;
209     }
210
211     if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
212         syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
213         return NULL;
214     }
215
216     db->magic = CNID_DB_MAGIC;
217
218     strcpy(path, dir);
219     if (path[len - 1] != '/') {
220         strcat(path, "/");
221         len++;
222     }
223
224     lock.l_type = F_WRLCK;
225     lock.l_whence = SEEK_SET;
226
227     strcpy(path + len, DBHOME);
228     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
229         syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed for %s", path);
230         goto fail_adouble;
231     }
232
233         /* Make sure cnid.lock goes in .AppleDB. */
234         strcat(path, "/");
235         len++;
236
237         /* Create a file to represent database recovery.  While this file
238          * exists, the database is being recovered, and all other clients will
239          * sleep until recovery is complete, and this file goes away. */
240         strcpy(recover_file, path);
241         strcat(recover_file, DBRECOVERFILE);
242         if (!have_lock) {
243                 if (stat(recover_file, &rsb) < 0) {
244                         if ((rfd = open(recover_file, O_RDWR | O_CREAT, 0666)) > -1) {
245                         DBEXTRAS |= DB_RECOVER;
246                                 have_lock = 1;
247                         }
248                 }
249                 else {
250                         while(stat(recover_file, &rsb) == 0) {
251                                 sleep(1);
252                         }
253                 }
254         }
255
256     /* Search for a byte lock.  This allows us to cleanup the log files
257      * at cnid_close() in a clean fashion.
258      *
259      * NOTE: This won't work if multiple volumes for the same user refer
260      * to the sahe directory. */
261     strcat(path, DBLOCKFILE);
262     if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
263         lock.l_start = 0;
264         lock.l_len = 1;
265         while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
266                 if (++lock.l_start > MAXITER) {
267                 syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
268                 close(db->lockfd);
269                 db->lockfd = -1;
270                 break;
271                 }
272         }
273         }
274     else {
275         syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
276     }
277
278     path[len + DBHOMELEN] = '\0';
279     open_flag = DB_CREATE;
280
281     /* We need to be able to open the database environment with full
282      * transaction, logging, and locking support if we ever hope to 
283      * be a true multi-acess file server. */
284     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
285         syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
286         goto fail_lock;
287     }
288
289     /* Setup internal deadlock detection. */
290     if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
291         syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
292         goto fail_lock;
293     }
294
295 #if DB_VERSION_MINOR > 1
296     /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
297     if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
298         syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
299         goto fail_lock;
300     }
301 #endif /* DB_VERSION_MINOR > 1 */
302
303     /* Open the database environment. */
304     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
305         if (rc == DB_RUNRECOVERY) {
306             /* This is the mother of all errors.  We _must_ fail here. */
307             syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
308             goto fail_lock;
309         }
310
311         /* We can't get a full transactional environment, so multi-access
312          * is out of the question.  Let's assume a read-only environment,
313          * and try to at least get a shared memory pool. */
314         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
315             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
316              * open the environment with no flags. */
317             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
318                 syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
319                        path, db_strerror(rc));
320                 goto fail_lock;
321             }
322         }
323         db->flags |= CNIDFLAG_DB_RO;
324         open_flag = DB_RDONLY;
325         syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
326     }
327
328         /* If we have the recovery lock, close the file, remove it, so other
329          * clients can proceed opening the DB environment. */
330         if (rfd > -1) {
331                 close(rfd);
332                 (void)remove(recover_file);
333                 rfd = -1;
334         }
335
336     /* did/name reverse mapping.  We use a BTree for this one. */
337     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
338         syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
339                db_strerror(rc));
340         goto fail_appinit;
341     }
342
343     db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
344     if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
345                                    DB_BTREE, open_flag, 0666))) {
346         syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
347                db_strerror(rc));
348         goto fail_appinit;
349     }
350
351     /* Check for version.  This way we can update the database if we need
352      * to change the format in any way. */
353     memset(&key, 0, sizeof(key));
354     memset(&data, 0, sizeof(data));
355     key.data = DBVERSION_KEY;
356     key.size = DBVERSION_KEYLEN;
357
358 dbversion_retry:
359     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
360         syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
361                db_strerror(rc));
362         db->db_didname->close(db->db_didname, 0);
363         goto fail_appinit;
364     }
365
366     while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
367         int ret;
368         switch (rc) {
369         case DB_LOCK_DEADLOCK:
370             if ((ret = txn_abort(tid)) != 0) {
371                 syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
372                 db->db_didname->close(db->db_didname, 0);
373                 goto fail_appinit;
374             }
375             goto dbversion_retry;
376         case DB_NOTFOUND:
377             {
378                 u_int32_t version = htonl(DBVERSION);
379
380                 data.data = &version;
381                 data.size = sizeof(version);
382             }
383
384             if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
385                                            DB_NOOVERWRITE))) {
386                 if (ret == DB_LOCK_DEADLOCK) {
387                     if ((ret = txn_abort(tid)) != 0) {
388                         syslog(LOG_ERR, "cnid_open: txn_abort: %s",
389                                db_strerror(ret));
390                         db->db_didname->close(db->db_didname, 0);
391                         goto fail_appinit;
392                     }
393                     goto dbversion_retry;
394                 }
395                 else if (ret == DB_RUNRECOVERY) {
396                     /* At this point, we don't care if the transaction aborts
397                      * successfully or not. */
398                     txn_abort(tid);
399                     syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
400                            db_strerror(ret));
401                     db->db_didname->close(db->db_didname, 0);
402                     goto fail_appinit;
403                 }
404             }
405             break; /* while loop */
406         default:
407             txn_abort(tid);
408             syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
409                    db_strerror(rc));
410             db->db_didname->close(db->db_didname, 0);
411             goto fail_appinit;
412         }
413     }
414
415     if ((rc = txn_commit(tid, 0)) != 0) {
416         syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
417                db_strerror(rc));
418         db->db_didname->close(db->db_didname, 0);
419         goto fail_appinit;
420     }
421
422     /* TODO In the future we might check for version number here. */
423 #if 0
424     memcpy(&version, data.data, sizeof(version));
425     if (version != ntohl(DBVERSION)) {
426         /* Do stuff here. */
427     }
428 #endif /* 0 */
429
430 #ifdef EXTENDED_DB
431     /* did/macname (31 character) mapping.  Use a BTree for this one. */
432     if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
433         syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
434                db_strerror(rc));
435         db->db_didname->close(db->db_didname, 0);
436         goto fail_appinit;
437     }
438
439     db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
440     if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
441         syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
442                db_strerror(rc));
443         db->db_didname->close(db->db_didname, 0);
444         goto fail_appinit;
445     }
446
447     /* did/shortname (DOS 8.3) mapping.  Use a BTree for this one. */
448     if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
449         syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
450                db_strerror(rc));
451         db->db_didname->close(db->db_didname, 0);
452         db->db_macname->close(db->db_macname, 0);
453         goto fail_appinit;
454     }
455
456     db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
457     if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
458         syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
459                db_strerror(rc));
460         db->db_didname->close(db->db_didname, 0);
461         db->db_macname->close(db->db_macname, 0);
462         goto fail_appinit;
463     }
464
465     /* did/longname (Unicode) mapping.  Use a BTree for this one. */
466     if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
467         syslog(LOG_ERR, "cnid_open: Failed to create did/longname database: %s",
468                db_strerror(rc));
469         db->db_didname->close(db->db_didname, 0);
470         db->db_macname->close(db->db_macname, 0);
471         db->db_shortname->close(db->db_shortname, 0);
472         goto fail_appinit;
473     }
474
475     db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
476     if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
477         syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
478                db_strerror(rc));
479         db->db_didname->close(db->db_didname, 0);
480         db->db_macname->close(db->db_macname, 0);
481         db->db_shortname->close(db->db_shortname, 0);
482         goto fail_appinit;
483     }
484 #endif /* EXTENDED_DB */
485
486     /* dev/ino reverse mapping.  Use a hash for this one. */
487     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
488         syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
489                db_strerror(rc));
490         db->db_didname->close(db->db_didname, 0);
491 #ifdef EXTENDED_DB
492         db->db_macname->close(db->db_macname, 0);
493         db->db_shortname->close(db->db_shortname, 0);
494         db->db_longname->close(db->db_longname, 0);
495 #endif /* EXTENDED_DB */
496         goto fail_appinit;
497     }
498
499     if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
500         syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
501                db_strerror(rc));
502         db->db_didname->close(db->db_didname, 0);
503 #ifdef EXTENDED_DB
504         db->db_macname->close(db->db_macname, 0);
505         db->db_shortname->close(db->db_shortname, 0);
506         db->db_longname->close(db->db_longname, 0);
507 #endif /* EXTENDED_DB */
508         goto fail_appinit;
509     }
510
511     /* Main CNID database.  Use a hash for this one. */
512     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
513         syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
514                db_strerror(rc));
515         db->db_didname->close(db->db_didname, 0);
516 #ifdef EXTENDED_DB
517         db->db_macname->close(db->db_macname, 0);
518         db->db_shortname->close(db->db_shortname, 0);
519         db->db_longname->close(db->db_longname, 0);
520 #endif /* EXTENDED_DB */
521         db->db_devino->close(db->db_devino, 0);
522         goto fail_appinit;
523     }
524
525
526     if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
527         syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
528                db_strerror(rc));
529         db->db_didname->close(db->db_didname, 0);
530 #ifdef EXTENDED_DB
531         db->db_macname->close(db->db_macname, 0);
532         db->db_shortname->close(db->db_shortname, 0);
533         db->db_longname->close(db->db_longname, 0);
534 #endif /* EXTENDED_DB */
535         goto fail_appinit;
536     }
537
538     return db;
539
540 fail_appinit:
541     syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
542     db->dbenv->close(db->dbenv, 0);
543
544 fail_lock:
545     if (db->lockfd > -1) {
546         close(db->lockfd);
547     }
548
549 fail_adouble:
550
551 fail_db:
552     free(db);
553     return NULL;
554 }
555 #endif /* CNID_DB */
556
557