]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
cnid_add: bugfix use a cursor for atomic operation.
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.34 2003-03-07 14:51:50 didg Exp $
3  *
4  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved. See COPYRIGHT.
6  *
7  * cnid_add (db, dev, ino, did, name, hint):
8  * add a name to the CNID database. we use both dev/ino and did/name
9  * to keep track of things.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 #ifdef CNID_DB
17 #include <stdio.h>
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <string.h>
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif /* HAVE_UNISTD_H */
24 #ifdef HAVE_FCNTL_H
25 #include <fcntl.h>
26 #endif /* HAVE_FCNTL_H */
27 #include <errno.h>
28 #include <atalk/logger.h>
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif /* HAVE_SYS_TIME_H */
32
33 #include <db.h>
34 #include <netatalk/endian.h>
35
36 #include <atalk/adouble.h>
37 #include <atalk/cnid.h>
38 #include <atalk/util.h>
39
40 #include "cnid_private.h"
41
42 #ifdef CNID_DB_CDB
43     #define tid    NULL
44 #endif /* CNID_DB_CDB */
45
46 /* add an entry to the CNID databases. we do this as a transaction
47  * to prevent messiness. */
48 static int add_cnid(CNID_private *db, DBT *key, DBT *data) {
49     DBT altkey, altdata;
50 #ifndef CNID_DB_CDB
51     DB_TXN *tid;
52 #endif /* CNID_DB_CDB */
53     int rc, ret;
54
55     memset(&altkey, 0, sizeof(altkey));
56     memset(&altdata, 0, sizeof(altdata));
57
58 #ifndef CNID_DB_CDB
59 retry:
60     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
61         return rc;
62     }
63 #endif /* CNID_DB_CDB */
64
65     /* main database */
66     if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
67 #ifndef CNID_DB_CDB
68         if (rc == DB_LOCK_DEADLOCK) {
69             if ((ret = txn_abort(tid)) != 0) {
70                 return ret;
71             }
72             goto retry;
73         }
74 #endif /* CNID_DB_CDB */
75         goto abort;
76     }
77
78     /* dev/ino database */
79     altkey.data = data->data;
80     altkey.size = CNID_DEVINO_LEN;
81     altdata.data = key->data;
82     altdata.size = key->size;
83     if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
84 #ifndef CNID_DB_CDB
85         if (rc == DB_LOCK_DEADLOCK) {
86             if ((ret = txn_abort(tid)) != 0) {
87                 return ret;
88             }
89             goto retry;
90         }
91 #endif /* CNID_DB_CDB */
92         goto abort;
93     }
94
95     /* did/name database */
96     altkey.data = (char *) data->data + CNID_DEVINO_LEN;
97     altkey.size = data->size - CNID_DEVINO_LEN;
98     if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
99 #ifndef CNID_DB_CDB
100         if (rc == DB_LOCK_DEADLOCK) {
101             if ((ret = txn_abort(tid)) != 0) {
102                 return ret;
103             }
104             goto retry;
105         }
106 #endif /* CNID_DB_CDB */
107         goto abort;
108     }
109
110 #ifndef CNID_DB_CDB
111     if ((rc = txn_commit(tid, 0)) != 0) {
112         LOG(log_error, logtype_default, "add_cnid: Failed to commit transaction: %s", db_strerror(rc));
113         return rc;
114     }
115 #endif /* CNID_DB_CDB */
116
117     return 0;
118
119 abort:
120 #ifndef CNID_DB_CDB
121     if ((ret = txn_abort(tid)) != 0) {
122         return ret;
123     }
124 #endif    
125     return rc;
126 }
127
128 /* ---------------------- */
129 #ifndef CNID_DB_CDB
130 static cnid_t get_cnid(CNID_private *db)
131 {
132     DBT rootinfo_key, rootinfo_data;
133     DB_TXN *tid;
134     int rc;
135     int flag;
136     cnid_t hint,id;
137
138     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
139     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
140     rootinfo_key.data = ROOTINFO_KEY;
141     rootinfo_key.size = ROOTINFO_KEYLEN;
142
143 retry:
144     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
145         LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
146         errno = CNID_ERR_DB;
147         return CNID_INVALID;
148     }
149     switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
150                                      &rootinfo_data, DB_RMW)) {
151     case DB_LOCK_DEADLOCK:
152         if ((rc = txn_abort(tid)) != 0) {
153             LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
154             errno = CNID_ERR_DB;
155             return CNID_INVALID;
156         }
157         goto retry;
158     case 0:
159         memcpy(&hint, rootinfo_data.data, sizeof(hint));
160         id = ntohl(hint);
161         /* If we've hit the max CNID allowed, we return a fatal error.  CNID
162          * needs to be recycled before proceding. */
163         if (++id == CNID_INVALID) {
164             txn_abort(tid);
165             LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s.  CNID database has reached its limit.", name);
166             errno = CNID_ERR_MAX;
167             return CNID_INVALID;
168         }
169         hint = htonl(id);
170 #ifdef DEBUG
171         LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
172 #endif
173         break;
174     case DB_NOTFOUND:
175         hint = htonl(CNID_START);
176 #ifdef DEBUG
177         LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
178 #endif
179         break;
180     default:
181         LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
182         goto cleanup_abort;
183     }
184
185     rootinfo_data.data = &hint;
186     rootinfo_data.size = sizeof(hint);
187
188     switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
189     case DB_LOCK_DEADLOCK:
190         if ((rc = txn_abort(tid)) != 0) {
191             LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
192             errno = CNID_ERR_DB;
193             return CNID_INVALID;
194         }
195         goto retry;
196     case 0:
197         /* The transaction finished, commit it. */
198         if ((rc = txn_commit(tid, 0)) != 0) {
199             LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
200             errno = CNID_ERR_DB;
201             return CNID_INVALID;
202         }
203         break;
204     default:
205         LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
206         goto cleanup_abort;
207     }
208     return hint;
209     
210 cleanup_abort:
211     txn_abort(tid);
212     errno = CNID_ERR_DB;
213     return CNID_INVALID;
214 }
215 #else
216 static cnid_t get_cnid(CNID_private *db)
217 {
218     DBT rootinfo_key, rootinfo_data;
219     DBC  *cursor;
220     int rc;
221     int flag;
222     cnid_t hint,id;
223     
224     if ((rc = db->db_didname->cursor(db->db_didname, NULL, &cursor, DB_WRITECURSOR) ) != 0) {
225         LOG(log_error, logtype_default, "get_cnid: Unable to get a cursor: %s", db_strerror(rc));
226         return CNID_INVALID;
227     }
228
229     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
230     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
231     rootinfo_key.data = ROOTINFO_KEY;
232     rootinfo_key.size = ROOTINFO_KEYLEN;
233
234     switch (rc = cursor->c_get(cursor, &rootinfo_key, &rootinfo_data, DB_SET)) {
235     case 0:
236         memcpy(&hint, rootinfo_data.data, sizeof(hint));
237         id = ntohl(hint);
238         /* If we've hit the max CNID allowed, we return a fatal error.  CNID
239          * needs to be recycled before proceding. */
240         if (++id == CNID_INVALID) {
241             LOG(log_error, logtype_default, "cnid_add: FATAL: CNID database has reached its limit.");
242             errno = CNID_ERR_MAX;
243             goto cleanup;
244         }
245         hint = htonl(id);
246         flag = DB_CURRENT;
247         break;
248     case DB_NOTFOUND:
249         hint = htonl(CNID_START);
250         flag = DB_KEYFIRST;
251         break;
252     default:
253         LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
254         errno = CNID_ERR_DB; 
255         goto cleanup;
256     }
257     
258     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
259     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
260     rootinfo_data.data = &hint;
261     rootinfo_data.size = sizeof(hint);
262     rootinfo_key.data = ROOTINFO_KEY;
263     rootinfo_key.size = ROOTINFO_KEYLEN;
264
265     switch (rc = cursor->c_put(cursor, &rootinfo_key, &rootinfo_data, flag)) {
266     case 0:
267         break;
268     default:
269         LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
270         errno = CNID_ERR_DB; 
271         goto cleanup;
272     }
273     if ((rc = cursor->c_close(cursor)) != 0) {
274         LOG(log_error, logtype_default, "get_cnid: Unable to close cursor: %s", db_strerror(rc));
275         errno = CNID_ERR_DB; 
276         return CNID_INVALID;
277     }
278     return hint;
279 cleanup:
280     if ((rc = cursor->c_close(cursor)) != 0) {
281         LOG(log_error, logtype_default, "get_cnid: Unable to close cursor: %s", db_strerror(rc));
282         return CNID_INVALID;
283     }
284     return CNID_INVALID;
285 }
286 #endif /* CNID_DB_CDB */
287
288 /* ------------------------ */
289 cnid_t cnid_add(void *CNID, const struct stat *st,
290                 const cnid_t did, const char *name, const int len,
291                 cnid_t hint)
292 {
293     CNID_private *db;
294     DBT key, data;
295     cnid_t id;
296     int rc;
297
298     if (!(db = CNID) || !st || !name) {
299         errno = CNID_ERR_PARAM;
300         return CNID_INVALID;
301     }
302
303     /* Do a lookup. */
304     id = cnid_lookup(db, st, did, name, len);
305     /* ... Return id if it is valid, or if Rootinfo is read-only. */
306     if (id || (db->flags & CNIDFLAG_DB_RO)) {
307 #ifdef DEBUG
308         LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
309 #endif
310         return id;
311     }
312
313     /* Initialize our DBT data structures. */
314     memset(&key, 0, sizeof(key));
315     memset(&data, 0, sizeof(data));
316
317     /* Just tickle hint, and the key will change (gotta love pointers). */
318     key.data = &hint;
319     key.size = sizeof(hint);
320
321     if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
322         LOG(log_error, logtype_default, "cnid_add: Path name is too long");
323         errno = CNID_ERR_PATH;
324         return CNID_INVALID;
325     }
326
327     data.size = CNID_HEADER_LEN + len + 1;
328
329     /* Start off with the hint.  It should be in network byte order.
330      * We need to make sure that somebody doesn't add in restricted
331      * cnid's to the database. */
332     if (ntohl(hint) >= CNID_START) {
333         /* If the key doesn't exist, add it in.  Don't fiddle with nextID. */
334         rc = add_cnid(db, &key, &data);
335         switch (rc) {
336         case DB_KEYEXIST: /* Need to use RootInfo after all. */
337             break;
338         default:
339             LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
340             errno = CNID_ERR_DB;
341             return CNID_INVALID;
342         case 0:
343 #ifdef DEBUG
344             LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
345 #endif
346             return hint;
347         }
348     }
349     hint = get_cnid(db);
350     if (hint == 0) {
351         errno = CNID_ERR_DB;
352         return CNID_INVALID;
353     }
354     
355     /* Now we need to add the CNID data to the databases. */
356     rc = add_cnid(db, &key, &data);
357     if (rc) {
358         LOG(log_error, logtype_default, "cnid_add: Failed to add CNID for %s to database using hint %u: %s", name, ntohl(hint), db_strerror(rc));
359         errno = CNID_ERR_DB;
360         return CNID_INVALID;
361     }
362
363 #ifdef DEBUG
364     LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
365 #endif
366
367     return hint;
368 }
369 #endif /* CNID_DB */
370