]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
Re-added code to enable on-the-fly database recovery. This was originally
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.c,v 1.17 2001-11-04 17:34:15 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 void *cnid_open(const char *dir) {
175         struct stat st;
176         struct flock lock;
177         char path[MAXPATHLEN + 1];
178         CNID_private *db;
179         DBT key, data;
180         DB_TXN *tid;
181         u_int32_t DBEXTRAS = 0;
182         int open_flag, len;
183         int rc;
184
185         if (!dir) {
186                 return NULL;
187         }
188
189         /* this checks .AppleDB */
190         if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
191                 syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
192                 return NULL;
193         }
194
195         if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
196                 syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
197                 return NULL;
198         }
199
200         db->magic = CNID_DB_MAGIC;
201
202         strcpy(path, dir);
203         if (path[len - 1] != '/') {
204                 strcat(path, "/");
205                 len++;
206         }
207
208         lock.l_type = F_WRLCK;
209         lock.l_whence = SEEK_SET;
210
211         strcpy(path + len, DBHOME);
212         if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
213                 syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed");
214                 goto fail_adouble;
215         }
216
217         /* Search for a byte lock.  This allows us to cleanup the log files
218          * at cnid_close() in a clean fashion.
219          *
220          * NOTE: This won't work if multiple volumes for the same user refer
221          * to the sahe directory. */
222         strcat(path, DBLOCKFILE);
223         if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
224                 lock.l_start = 0;
225                 lock.l_len = 1;
226                 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
227                         if (++lock.l_start > MAXITER) {
228                                 syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock (lock failed)");
229                                 close(db->lockfd);
230                                 db->lockfd = -1;
231                                 break;
232                         }
233                 }
234         }
235         else {
236                 syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock (open() failed)");
237         }
238         
239         if (db->lockfd > -1 && lock.l_start == 0) {
240                 /* We test to see if we have exclusive database access.  If we do, we
241                  * will open the database with the DB_RECOVER flag.
242                  */
243 #ifdef DEBUG
244                 syslog(LOG_INFO, "cnid_open: Opening database with DB_RECOVER flag");
245 #endif
246                 DBEXTRAS |= DB_RECOVER;
247         }
248
249
250         path[len + DBHOMELEN] = '\0';
251         open_flag = DB_CREATE;
252
253         /* We need to be able to open the database environment with full 
254          * transaction, logging, and locking support if we ever hope to 
255          * be a true multi-acess file server. */
256         if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
257                 syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
258                 goto fail_lock;
259         }
260
261         /* Setup internal deadlock detection. */
262         if ((rc = db->dbenv->set_lk_detect(db->dbenv, DB_LOCK_DEFAULT)) != 0) {
263                 syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
264                 goto fail_lock;
265         }
266
267 #if DB_VERSION_MINOR > 1
268         /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
269 /*      if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
270                 syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
271                 goto fail_lock;
272         }*/
273 #endif /* DB_VERSION_MINOR > 1 */
274
275         /* Open the database environment. */
276         if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
277                 if (rc == DB_RUNRECOVERY) {
278                         /* This is the mother of all errors.  We _must_ fail here. */
279                         syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment.  Run db_recovery -c immediately");
280                         goto fail_lock;
281                 }
282
283                 /* We can't get a full transactional environment, so multi-access
284                  * is out of the question.  Let's assume a read-only environment,
285                  * and trry to at least get a shared memory pool. */
286                 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
287                         /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
288                          * open the environment with no flags. */
289                         if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
290                                 syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
291                                        path, db_strerror(rc));
292                                 goto fail_lock;
293                         }
294                 }
295                 db->flags |= CNIDFLAG_DB_RO;
296                 open_flag = DB_RDONLY;
297                 syslog(LOG_INFO, "cnid_open: Obtained read-only database environment");
298         }
299
300         /* did/name reverse mapping.  We use a BTree for this one. */
301         if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
302                 syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s", 
303                        db_strerror(rc));
304                 goto fail_appinit;
305         }
306
307         db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
308         if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
309                  DB_BTREE, open_flag, 0666))) {
310                 syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
311                        db_strerror(rc));
312                 goto fail_appinit;
313         }
314
315         /* Check for version.  This way we can update the database if we need
316          * to change the format in any way. */
317         memset(&key, 0, sizeof(key));
318         memset(&data, 0, sizeof(data));
319         key.data = DBVERSION_KEY;
320         key.size = DBVERSION_KEYLEN;
321
322 dbversion_retry:
323         if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
324                 syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
325                        db_strerror(rc));
326                 db->db_didname->close(db->db_didname, 0);
327                 goto fail_appinit;
328         }
329
330         while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
331                 int ret;
332                 switch (rc) {
333                         case DB_LOCK_DEADLOCK:
334                                 if ((ret = txn_abort(tid)) != 0) {
335                                         syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
336                                         db->db_didname->close(db->db_didname, 0);
337                                         goto fail_appinit;
338                                 }
339                                 goto dbversion_retry;
340                         case DB_NOTFOUND:
341                         {
342                                 u_int32_t version = htonl(DBVERSION);
343
344                                 data.data = &version;
345                                 data.size = sizeof(version);
346                         }
347
348                         if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
349                                       DB_NOOVERWRITE))) {
350                                 if (ret == DB_LOCK_DEADLOCK) {
351                                         if ((ret = txn_abort(tid)) != 0) {
352                                                 syslog(LOG_ERR, "cnid_open: txn_abort: %s",
353                                                        db_strerror(ret));
354                                                 db->db_didname->close(db->db_didname, 0);
355                                                 goto fail_appinit;
356                                         }
357                                         goto dbversion_retry;
358                                 }
359                                 else if (ret == DB_RUNRECOVERY) {
360                                         /* At this point, we don't care if the transaction aborts
361                                          * successfully or not. */
362                                         txn_abort(tid);
363                                         syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
364                                                db_strerror(ret));
365                                         db->db_didname->close(db->db_didname, 0);
366                                         goto fail_appinit;
367                                 }
368                         }
369                                 break; /* while loop */
370                         default:
371                                 txn_abort(tid);
372                                 syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
373                                        db_strerror(rc));
374                                 db->db_didname->close(db->db_didname, 0);
375                                 goto fail_appinit;
376                 }
377         }
378
379         if ((rc = txn_commit(tid, 0)) != 0) {
380                 syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
381                        db_strerror(rc));
382                 db->db_didname->close(db->db_didname, 0);
383                 goto fail_appinit;
384         }
385
386         /* TODO In the future we might check for version number here. */
387 #if 0
388         memcpy(&version, data.data, sizeof(version));
389         if (version != ntohl(DBVERSION)) {
390                 /* Do stuff here. */
391         }
392 #endif /* 0 */
393
394 #ifdef EXTENDED_DB
395         /* did/macname (31 character) mapping.  Use a BTree for this one. */
396         if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
397                 syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
398                        db_strerror(rc));
399                 db->db_didname->close(db->db_didname, 0);
400                 goto fail_appinit;
401         }
402
403         db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
404         if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
405                 syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
406                        db_strerror(rc));
407                 db->db_didname->close(db->db_didname, 0);
408                 goto fail_appinit;
409         }
410
411         /* did/shortname (DOS 8.3) mapping.  Use a BTree for this one. */
412         if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
413                 syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
414                        db_strerror(rc));
415                 db->db_didname->close(db->db_didname, 0);
416                 db->db_macname->close(db->db_macname, 0);
417                 goto fail_appinit;
418         }
419
420         db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
421         if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
422                 syslog(LOG_ERR, "cnid_open: Failed to open 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         /* did/longname (Unicode) mapping.  Use a BTree for this one. */
430         if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
431                 syslog(LOG_ERR, "cnid_open: Failed to create did/longname 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                 db->db_shortname->close(db->db_shortname, 0);
436                 goto fail_appinit;
437         }
438
439         db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
440         if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
441                 syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
442                        db_strerror(rc));
443                 db->db_didname->close(db->db_didname, 0);
444                 db->db_macname->close(db->db_macname, 0);
445                 db->db_shortname->close(db->db_shortname, 0);
446                 goto fail_appinit;
447         }
448 #endif /* EXTENDED_DB */
449
450         /* dev/ino reverse mapping.  Use a hash for this one. */
451         if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
452                 syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
453                        db_strerror(rc));
454                 db->db_didname->close(db->db_didname, 0);
455 #ifdef EXTENDED_DB
456                 db->db_macname->close(db->db_macname, 0);
457                 db->db_shortname->close(db->db_shortname, 0);
458                 db->db_longname->close(db->db_longname, 0);
459 #endif /* EXTENDED_DB */
460                 goto fail_appinit;
461         }
462
463         if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
464                 syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
465                        db_strerror(rc));
466                 db->db_didname->close(db->db_didname, 0);
467 #ifdef EXTENDED_DB
468                 db->db_macname->close(db->db_macname, 0);
469                 db->db_shortname->close(db->db_shortname, 0);
470                 db->db_longname->close(db->db_longname, 0);
471 #endif /* EXTENDED_DB */
472                 goto fail_appinit;
473         }
474
475         /* Main CNID database.  Use a hash for this one. */
476         if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
477                 syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
478                        db_strerror(rc));
479                 db->db_didname->close(db->db_didname, 0);
480 #ifdef EXTENDED_DB
481                 db->db_macname->close(db->db_macname, 0);
482                 db->db_shortname->close(db->db_shortname, 0);
483                 db->db_longname->close(db->db_longname, 0);
484 #endif /* EXTENDED_DB */
485                 db->db_devino->close(db->db_devino, 0);
486                 goto fail_appinit;
487         }
488
489
490         if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
491                 syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
492                        db_strerror(rc));
493                 db->db_didname->close(db->db_didname, 0);
494 #ifdef EXTENDED_DB
495                 db->db_macname->close(db->db_macname, 0);
496                 db->db_shortname->close(db->db_shortname, 0);
497                 db->db_longname->close(db->db_longname, 0);
498 #endif /* EXTENDED_DB */
499                 goto fail_appinit;
500         }
501
502         return db;
503
504 fail_appinit:
505         syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
506         db->dbenv->close(db->dbenv, 0);
507
508 fail_lock:
509         if (db->lockfd > -1) {
510                 close(db->lockfd);
511         }
512
513 fail_adouble:
514
515 fail_db:
516         free(db);
517         return NULL;
518 }
519 #endif /* CNID_DB */
520
521