]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
Fix a bunch of problems with database contention and corruption. Note:
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /*
2  * $Id: cnid_open.c,v 1.10 2001-09-21 15:08:37 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
90 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
91 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
92 #endif
93
94 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID
95                             * databases. */
96
97 /* the first compare that's always done. */
98 static __inline__ int compare_did(const DBT *a, const DBT *b)
99 {
100   u_int32_t dida, didb;
101
102   memcpy(&dida, a->data, sizeof(dida));
103   memcpy(&didb, b->data, sizeof(didb));
104   return dida - didb;
105 }
106
107 /* sort did's and then names. this is for unix paths.
108  * i.e., did/unixname lookups. */
109 #if DB_VERSION_MINOR > 1
110 static int compare_unix(DB *db, const DBT *a, const DBT *b)
111 #else
112 static int compare_unix(const DBT *a, const DBT *b)
113 #endif
114 {
115   u_int8_t *sa, *sb;
116   int len, ret;
117
118   /* sort by did */
119   if ((ret = compare_did(a, b)))
120     return ret;
121
122   sa = (u_int8_t *) a->data + 4; /* shift past did */
123   sb = (u_int8_t *) b->data + 4;
124   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
125     if ((ret = (*sa - *sb)))
126       return ret; /* sort by lexical ordering */
127
128   return a->size - b->size; /* sort by length */
129 }
130
131 /* sort did's and then names. this is for macified paths (i.e.,
132  * did/macname, and did/shortname. i think did/longname needs a
133  * unicode table to work. also, we can't use strdiacasecmp as that
134  * returns a match if a < b. */
135 #if DB_VERSION_MINOR > 1
136 static int compare_mac(DB *db, const DBT *a, const DBT *b)
137 #else
138 static int compare_mac(const DBT *a, const DBT *b)
139 #endif
140 {
141   u_int8_t *sa, *sb;
142   int len, ret;
143
144   /* sort by did */
145   if ((ret = compare_did(a, b)))
146     return ret;
147
148   sa = (u_int8_t *) a->data + 4;
149   sb = (u_int8_t *) b->data + 4;
150   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
151     if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
152           return ret; /* sort by lexical ordering */
153
154   return a->size - b->size; /* sort by length */
155 }
156
157
158 /* for unicode names -- right now it's the same as compare_mac. */
159 #if DB_VERSION_MINOR > 1
160 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
161 #else
162 static int compare_unicode(const DBT *a, const DBT *b)
163 #endif
164 {
165 #if DB_VERSION_MINOR > 1
166         return compare_mac(db,a,b);
167 #else
168         return compare_mac(a,b);
169 #endif
170 }
171
172 void *cnid_open(const char *dir)
173 {
174   struct stat st;
175   struct flock lock;
176   char path[MAXPATHLEN + 1];
177   CNID_private *db;
178   DBT key, data;
179   int open_flag, len;
180
181   if (!dir)
182     return NULL;
183
184   /* this checks .AppleDB */
185   if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
186     syslog(LOG_ERR, "cnid_open: path too large");
187     return NULL;
188   }
189   
190   if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
191     syslog(LOG_ERR, "cnid_open: unable to allocate memory");
192     return NULL;
193   }
194   db->magic = CNID_DB_MAGIC;
195
196   strcpy(path, dir);
197   if (path[len - 1] != '/') {
198     strcat(path, "/");
199     len++;
200   }
201
202   lock.l_type = F_WRLCK;
203   lock.l_whence = SEEK_SET;
204
205 mkdir_appledb:
206   strcpy(path + len, DBHOME);
207   if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
208     syslog(LOG_ERR, "cnid_open: mkdir failed");
209     goto fail_adouble;
210   }
211
212   /* search for a byte lock. this allows us to cleanup the log files
213    * at cnid_close() in a clean fashion.
214    *
215    * NOTE: this won't work if multiple volumes for the same user refer
216    * to the same directory. */
217   strcat(path, DBLOCKFILE);
218   if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
219     lock.l_start = 0;
220     lock.l_len = 1;
221     while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
222       if (++lock.l_start > MAXITER) {
223         syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
224         close(db->lockfd);
225         db->lockfd = -1;
226         break;
227       }
228     }
229   }
230
231   path[len + DBHOMELEN] = '\0';
232   open_flag = DB_CREATE;
233   /* try a full-blown transactional environment */
234   if (db_env_create(&db->dbenv, 0)) {
235     syslog(LOG_ERR, "cnid_open: db_env_create failed");
236     goto fail_lock;
237   }
238
239 #if DB_VERSION_MINOR > 1
240   db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1);
241 #endif
242
243   /* Check to see if a DBENV already exists.  If it does, join it. */
244 /*  if (db->dbenv->open(db->dbenv, path, DB_JOINENV, 0666)) {*/
245   if (db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666)) {
246
247     /* try with a shared memory pool */
248         if (db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) {
249
250       /* try without any options. */
251       if (db->dbenv->open(db->dbenv, path, 0, 0666)) {
252                 syslog(LOG_ERR, "cnid_open: db_env_open failed");
253                 goto fail_lock;
254       }
255     }
256     db->flags |= CNIDFLAG_DB_RO;
257     open_flag = DB_RDONLY;
258     syslog(LOG_INFO, "cnid_open: read-only CNID database");
259   }
260   /*}*/
261
262
263   /* did/name reverse mapping. we use a btree for this one. */
264   if (db_create(&db->db_didname, db->dbenv, 0))
265         goto fail_appinit;
266
267   db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
268   if (db->db_didname->open(db->db_didname, DBDIDNAME, NULL, DB_BTREE, open_flag, 0666)) {
269     goto fail_appinit;
270   }
271
272   /* check for version. this way we can update the database if we need
273      to change the format in any way. */
274   memset(&key, 0, sizeof(key));
275   memset(&data, 0, sizeof(data));
276   key.data = DBVERSION_KEY;
277   key.size = DBVERSION_KEYLEN;
278   while ((errno = db->db_didname->get(db->db_didname, NULL, &key, &data, 0))) {
279     switch (errno) {
280         case DB_LOCK_DEADLOCK:
281       continue;
282
283     case DB_NOTFOUND:
284         {
285       u_int32_t version = htonl(DBVERSION);
286
287       data.data = &version;
288       data.size = sizeof(version);
289         }
290 dbversion_retry:
291       if (db->db_didname->put(db->db_didname, NULL, &key, &data,
292                               DB_NOOVERWRITE))
293         if (errno == DB_LOCK_DEADLOCK)
294           goto dbversion_retry;
295       break;
296     default:
297       /* uh oh. something bad happened. bail. */
298       db->db_didname->close(db->db_didname, 0);
299       goto fail_appinit;
300     }
301   }
302
303   /* XXX: in the future, we might check for version number here. */
304 #if 0
305   memcpy(&version, data.data, sizeof(version));
306   if (version != htonl(DBVERSION)) {
307     /* fix up stuff */
308   }
309 #endif /* 0 */
310
311 #ifdef EXTENDED_DB
312   /* did/macname mapping. btree this one. */
313   if (db_create(&db->db_macname, db->dbenv, 0))
314     goto fail_appinit;
315
316   db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
317   if (db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) {
318     db->db_didname->close(db->db_didname, 0);
319     goto fail_appinit;
320   }
321
322   /* did/shortname mapping */
323   if (db_create(&db->db_shortname, db->dbenv, 0))
324     goto fail_appinit;
325
326   db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
327   if (db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) {
328     db->db_didname->close(db->db_didname, 0);
329     db->db_macname->close(db->db_macname, 0);
330     goto fail_appinit;
331   }
332
333   /* did/longname mapping */
334   if (db_create(&db->db_longname, db->dbenv, 0))
335     goto fail_appinit;
336
337   db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
338   if (db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) {
339     db->db_didname->close(db->db_didname, 0);
340     db->db_macname->close(db->db_macname, 0);
341     db->db_shortname->close(db->db_shortname, 0);
342     goto fail_appinit;
343   }
344 #endif /* EXTENDED_DB */
345
346   /* dev/ino reverse mapping. we hash this one. */
347   if (db_create(&db->db_devino, db->dbenv, 0))
348     goto fail_appinit;
349
350   if (db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) {
351     db->db_didname->close(db->db_didname, 0);
352 #ifdef EXTENDED_DB
353     db->db_macname->close(db->db_macname, 0);
354     db->db_shortname->close(db->db_shortname, 0);
355     db->db_longname->close(db->db_longname, 0);
356 #endif /* EXTENDED_DB */
357     goto fail_appinit;
358   }
359
360   /* main cnid database. we hash this one as well. */
361   if (db_create(&db->db_cnid, db->dbenv, 0))
362     goto fail_appinit;
363
364   if (db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) {
365     db->db_didname->close(db->db_didname, 0);
366 #ifdef EXTENDED_DB
367     db->db_macname->close(db->db_macname, 0);
368     db->db_shortname->close(db->db_shortname, 0);
369     db->db_longname->close(db->db_longname, 0);
370 #endif /* EXTENDED_DB */
371     db->db_devino->close(db->db_devino, 0);
372     goto fail_appinit;
373   }
374
375   return db;
376
377 fail_appinit:
378   syslog(LOG_ERR, "cnid_open: db_open failed");
379   db->dbenv->close(db->dbenv, 0);
380
381 fail_lock:
382   if (db->lockfd > -1)
383     close(db->lockfd);
384
385 fail_adouble:
386
387 fail_db:
388   free(db);
389   return NULL;
390 }
391 #endif /* CNID_DB */