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