]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_open.c
Initial revision
[netatalk.git] / libatalk / cnid / cnid_open.c
1 /* 
2  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3  * All Rights Reserved. See COPYRIGHT.
4  *
5  * CNID database support. 
6  *
7  * here's the deal:
8  *  1) afpd already caches did's. 
9  *  2) the database stores cnid's as both did/name and dev/ino pairs. 
10  *  3) RootInfo holds the value of the NextID.
11  *  4) the cnid database gets called in the following manner --
12  *     start a database:
13  *     cnid = cnid_open(root_dir);
14  *
15  *     allocate a new id: 
16  *     newid = cnid_add(cnid, dev, ino, parent did,
17  *     name, id); id is a hint for a specific id. pass 0 if you don't
18  *     care. if the id is already assigned, you won't get what you
19  *     requested.
20  *
21  *     given an id, get a did/name and dev/ino pair.
22  *     name = cnid_get(cnid, &id); given an id, return the corresponding
23  *     info.
24  *     return code = cnid_delete(cnid, id); delete an entry. 
25  *
26  * with AFP, CNIDs 0-2 have special meanings. here they are:
27  * 0 -- invalid cnid
28  * 1 -- parent of root directory (handled by afpd) 
29  * 2 -- root directory (handled by afpd)
30  *
31  * so, CNID_START begins at 3.
32  */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <syslog.h>
41
42 #include <db.h>
43
44 #include <atalk/adouble.h>
45 #include <atalk/cnid.h>
46 #include <atalk/util.h>
47
48 #include "cnid_private.h"
49
50 #ifndef MIN
51 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
52 #endif
53
54 #define ROOTINFO     "RootInfo"
55 #define ROOTINFO_LEN 8
56
57 #define DBHOME       ".AppleDB"
58 #define DBCNID       "cnid.db"
59 #define DBDEVINO     "devino.db"
60 #define DBDIDNAME    "didname.db"   /* did/full name mapping */
61 #define DBSHORTNAME  "shortname.db" /* did/8+3 mapping */
62 #define DBMACNAME    "macname.db"   /* did/31 mapping */
63 #define DBLONGNAME   "longname.db"  /* did/unicode mapping */
64 #define DBLOCKFILE   "/cnid.lock"
65
66 #define DBHOMELEN    8
67 #define DBLEN        10
68
69 /* we version the did/name database so that we can change the format
70  * if necessary. the key is in the form of a did/name pair. in this case,
71  * we use 0/0. */
72 #define DBVERSION_KEY    "\0\0\0\0\0"
73 #define DBVERSION_KEYLEN 5
74 #define DBVERSION1       0x00000001U
75 #define DBVERSION        DBVERSION1
76
77 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
78 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC | DB_RECOVER)
79
80 #define MAXITER     0xFFFF /* maximum number of simultaneously open CNID 
81                             * databases. */
82
83 /* the first compare that's always done. */
84 static __inline__ int compare_did(const DBT *a, const DBT *b)
85 {
86   u_int32_t dida, didb;
87
88   memcpy(&dida, a->data, sizeof(dida));
89   memcpy(&didb, b->data, sizeof(didb));
90   return dida - didb;
91 }
92
93 /* sort did's and then names. this is for unix paths.
94  * i.e., did/unixname lookups. */
95 static int compare_unix(const DBT *a, const DBT *b)
96 {
97   u_int8_t *sa, *sb;
98   int len, ret;
99
100   /* sort by did */
101   if (ret = compare_did(a, b))
102     return ret;
103
104   sa = a->data + 4; /* shift past did */
105   sb = b->data + 4;
106   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
107     if (ret = (*sa - *sb))
108       return ret; /* sort by lexical ordering */
109   return a->size - b->size; /* sort by length */
110 }
111
112 /* sort did's and then names. this is for macified paths (i.e.,
113  * did/macname, and did/shortname. i think did/longname needs a
114  * unicode table to work. also, we can't use strdiacasecmp as that
115  * returns a match if a < b. */
116 static int compare_mac(const DBT *a, const DBT *b) 
117 {
118   u_int8_t *sa, *sb;
119   int len, ret;
120   
121   /* sort by did */
122   if (ret = compare_did(a, b))
123     return ret;
124
125   sa = a->data + 4;
126   sb = b->data + 4;
127   for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
128     if (ret = (_diacasemap[*sa] - _diacasemap[*sb]))
129       return ret; /* sort by lexical ordering */
130   return a->size - b->size; /* sort by length */
131 }
132
133
134 /* for unicode names -- right now it's the same as compare_mac. */
135 #define compare_unicode(a, b) compare_mac((a), (b))
136
137 void *cnid_open(const char *dir)
138 {
139   struct stat st;
140   struct flock lock;
141   char path[MAXPATHLEN + 1];
142   CNID_private *db;
143   DB_INFO dbi;
144   DBT key, data;
145   int open_flag, len;
146
147   if (!dir)
148     return NULL;
149
150   /* this checks both RootInfo and .AppleDB */
151   if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
152     syslog(LOG_ERR, "cnid_open: path too large");
153     return NULL;
154   }
155   
156   if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
157     syslog(LOG_ERR, "cnid_open: unable to allocate memory");
158     return NULL;
159   }
160   db->magic = CNID_DB_MAGIC;
161     
162   strcpy(path, dir);
163   if (path[len - 1] != '/') {
164     strcat(path, "/");
165     len++;
166   }
167
168   lock.l_type = F_WRLCK;
169   lock.l_whence = SEEK_SET;
170
171   /* we create and initialize RootInfo if it doesn't exist. */
172   strcat(path, ROOTINFO);
173   if (ad_open(path, ADFLAGS_HF, O_RDWR, 0666, &db->rootinfo) < 0) {
174     cnid_t id;
175
176     /* see if we can open it read-only. if it's read-only, we can't
177      * add CNIDs. */
178     memset(&db->rootinfo, 0, sizeof(db->rootinfo));
179     if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0666, &db->rootinfo) == 0) {
180       db->flags = CNIDFLAG_ROOTINFO_RO;
181       syslog(LOG_INFO, "cnid_open: read-only RootInfo");
182       goto mkdir_appledb;
183     }
184
185     /* create the file */
186     memset(&db->rootinfo, 0, sizeof(db->rootinfo));
187     if (ad_open(path, ADFLAGS_HF, O_CREAT | O_RDWR, 0666, 
188                 &db->rootinfo) < 0) {
189       syslog(LOG_ERR, "cnid_open: ad_open(RootInfo)");
190       goto fail_db;
191     }
192
193     /* lock the RootInfo file. this and cnid_add are the only places
194      * that should fiddle with RootInfo. */
195     lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
196     lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
197     if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW,  &lock) < 0) {
198       syslog(LOG_ERR, "cnid_open: can't establish lock: %m");
199       goto fail_adouble;
200     }
201     
202     /* store the beginning CNID */
203     id = htonl(CNID_START);
204     memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
205     ad_flush(&db->rootinfo, ADFLAGS_HF);
206
207     /* unlock it */
208     lock.l_type = F_UNLCK;
209     fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
210     lock.l_type = F_WRLCK;
211   }
212
213 mkdir_appledb:
214   strcpy(path + len, DBHOME);
215   if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
216     syslog(LOG_ERR, "cnid_open: mkdir failed");
217     goto fail_adouble;
218   }
219
220   /* search for a byte lock. this allows us to cleanup the log files
221    * at cnid_close() in a clean fashion.
222    *
223    * NOTE: this won't work if multiple volumes for the same user refer
224    * to the same directory. */
225   strcat(path, DBLOCKFILE);
226   if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
227     lock.l_start = 0;
228     lock.l_len = 1;
229     while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
230       if (++lock.l_start > MAXITER) {
231         syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
232         close(db->lockfd);
233         db->lockfd = -1;
234         break;
235       }      
236     }
237   } 
238
239   path[len + DBHOMELEN] = '\0';
240   open_flag = DB_CREATE;
241   /* try a full-blown transactional environment */
242   if (db_appinit(path, NULL, &db->dbenv, DBOPTIONS)) {
243
244     /* try with a shared memory pool */
245     memset(&db->dbenv, 0, sizeof(db->dbenv));
246     if (db_appinit(path, NULL, &db->dbenv, DB_INIT_MPOOL)) {
247
248       /* try without any options. */
249       memset(&db->dbenv, 0, sizeof(db->dbenv));
250       if (db_appinit(path, NULL, &db->dbenv, 0)) {
251         syslog(LOG_ERR, "cnid_open: db_appinit failed");
252         goto fail_lock;
253       }
254     }
255     db->flags |= CNIDFLAG_DB_RO;
256     open_flag = DB_RDONLY;
257     syslog(LOG_INFO, "cnid_open: read-only CNID database");
258   }
259
260   memset(&dbi, 0, sizeof(dbi));
261
262   /* did/name reverse mapping. we use a btree for this one. */
263   dbi.bt_compare = compare_unix;
264   if (db_open(DBDIDNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
265               &db->db_didname)) {
266     goto fail_appinit;
267   }
268
269   /* check for version. this way we can update the database if we need
270      to change the format in any way. */
271   memset(&key, 0, sizeof(key));
272   memset(&data, 0, sizeof(data));
273   key.data = DBVERSION_KEY;
274   key.len = DBVERSION_KEY_LEN;
275   while (errno = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) {
276     switch (errno) {
277     case EAGAIN:
278       continue;
279     
280     case DB_NOTFOUND:
281       u_int32_t version = htonl(DBVERSION);
282
283       data.data = &version;
284       data.len = sizeof(version);
285 dbversion_retry:
286       if (db->db_didname->put(db->db_didname, NULL, &key, &data,
287                               DB_NOOVERWRITE))
288         if (errno == EAGAIN)
289           goto dbversion_retry;
290       break;
291     default:
292       /* uh oh. something bad happened. bail. */
293       db->db_didname->close(db->db_didname, 0);
294       goto fail_appinit;
295     }
296   }
297   
298   /* XXX: in the future, we might check for version number here. */
299 #if 0
300   memcpy(&version, data.data, sizeof(version));
301   if (version != htonl(DBVERSION)) {
302     /* fix up stuff */
303   }
304 #endif
305
306   /* did/macname mapping. btree this one. */
307   dbi.bt_compare = compare_mac;
308   if (db_open(DBMACNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
309               &db->db_macname)) {
310     db->db_didname->close(db->db_didname, 0);
311     goto fail_appinit;
312   }
313
314   /* did/shortname mapping */
315   if (db_open(DBSHORTNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
316               &db->db_shortname)) {
317     db->db_didname->close(db->db_didname, 0);
318     db->db_macname->close(db->db_macname, 0);
319     goto fail_appinit;
320   }
321
322   /* did/longname mapping */
323   dbi.bt_compare = compare_unicode;
324   if (db_open(DBLONGNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
325               &db->db_longname)) {
326     db->db_didname->close(db->db_didname, 0);
327     db->db_macname->close(db->db_macname, 0);
328     db->db_shortname->close(db->db_shortname, 0);
329     goto fail_appinit;
330   }
331
332   /* dev/ino reverse mapping. we hash this one. */
333   dbi.bt_compare = NULL;
334   if (db_open(DBDEVINO, DB_HASH, open_flag, 0666, &db->dbenv, &dbi,
335               &db->db_devino)) {
336     db->db_didname->close(db->db_didname, 0);
337     db->db_macname->close(db->db_macname, 0);
338     db->db_shortname->close(db->db_shortname, 0);
339     db->db_longname->close(db->db_longname, 0);
340     goto fail_appinit;
341   }
342
343   /* main cnid database. we hash this one as well. */
344   if (db_open(DBCNID, DB_HASH, open_flag, 0666, &db->dbenv, &dbi,
345               &db->db_cnid)) {
346     db->db_didname->close(db->db_didname, 0);
347     db->db_macname->close(db->db_macname, 0);
348     db->db_shortname->close(db->db_shortname, 0);
349     db->db_longname->close(db->db_longname, 0);
350     db->db_devino->close(db->db_devino, 0);
351     goto fail_appinit;
352   }
353   return db;
354   
355 fail_appinit:
356   syslog(LOG_ERR, "cnid_open: db_open failed");
357   db_appexit(&db->dbenv);
358
359 fail_lock:
360   if (db->lockfd > -1)
361     close(db->lockfd);
362
363 fail_adouble:
364   ad_close(&db->rootinfo, ADFLAGS_HF);
365
366 fail_db:
367   free(db);
368   return NULL;
369 }