]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
28f50dac1a19ff6f7195dc6fba3d47e7c6f7aef7
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.15 2001-11-27 23:38:17 jmarcus 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 <syslog.h>
29 #include <time.h>
30
31 #include <db.h>
32 #include <netatalk/endian.h>
33
34 #include <atalk/adouble.h>
35 #include <atalk/cnid.h>
36 #include <atalk/util.h>
37
38 #include "cnid_private.h"
39
40 #define MAX_ABORTS 255
41
42 /* add an entry to the CNID databases. we do this as a transaction
43  * to prevent messiness. */
44 static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data) {
45     DBT altkey, altdata;
46     DB_TXN *tid;
47     /* We create rc here because using errno is bad.  Why?  Well, if you
48      * use errno once, then call another function which resets it, you're
49      * screwed. */
50     int rc, ret, aborts = 0;
51
52     memset(&altkey, 0, sizeof(altkey));
53     memset(&altdata, 0, sizeof(altdata));
54
55     if (0) {
56 retry:
57         if ((rc = txn_abort(tid)) != 0) {
58             return rc;
59         }
60         if (++aborts > MAX_ABORTS) {
61             return DB_LOCK_DEADLOCK;
62         }
63     }
64
65     if ((rc = txn_begin(db->dbenv, ptid, &tid, 0)) != 0) {
66         return rc;
67     }
68
69     /* main database */
70     if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
71         if (rc == DB_LOCK_DEADLOCK) {
72             goto retry;
73         }
74
75         goto abort;
76     }
77
78
79     /* dev/ino database */
80     altkey.data = data->data;
81     altkey.size = CNID_DEVINO_LEN;
82     altdata.data = key->data;
83     altdata.size = key->size;
84     if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
85         if (rc == DB_LOCK_DEADLOCK) {
86             goto retry;
87         }
88
89         goto abort;
90     }
91
92
93     /* did/name database */
94     altkey.data = (char *) data->data + CNID_DEVINO_LEN;
95     altkey.size = data->size - CNID_DEVINO_LEN;
96     if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
97         if (rc == DB_LOCK_DEADLOCK) {
98             goto retry;
99         }
100
101         goto abort;
102     }
103
104     if ((rc = txn_commit(tid, 0)) != 0) {
105         syslog(LOG_ERR, "add_cnid: Failed to commit transaction: %s",
106                db_strerror(rc));
107         return rc;
108     }
109     return 0;
110
111 abort:
112     if ((ret = txn_abort(tid)) != 0) {
113         return ret;
114     }
115     return rc;
116
117 }
118
119 cnid_t cnid_add(void *CNID, const struct stat *st,
120                 const cnid_t did, const char *name, const int len,
121                 cnid_t hint)
122 {
123     CNID_private *db;
124     DBT key, data, rootinfo_key, rootinfo_data;
125     DB_TXN *tid;
126     struct timeval t;
127     cnid_t id, save;
128     int rc;
129
130     if (!(db = CNID) || !st || !name) {
131         return 0;
132     }
133
134     /* Do a lookup. */
135     id = cnid_lookup(db, st, did, name, len);
136     /* ... Return id if it is valid, or if Rootinfo is read-only. */
137     if (id || (db->flags & CNIDFLAG_DB_RO)) {
138 #ifdef DEBUG
139         syslog(LOG_INFO, "cnid_add: Looked up did %u, name %s as %u",
140                ntohl(did), name, ntohl(id));
141 #endif
142         return id;
143     }
144
145     /* Initialize our DBT data structures. */
146     memset(&key, 0, sizeof(key));
147     memset(&data, 0, sizeof(data));
148
149     /* Just tickle hint, and the key will change (gotta love pointers). */
150     key.data = &hint;
151     key.size = sizeof(hint);
152
153     if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
154         syslog(LOG_ERR, "cnid_add: Path name is too long");
155         goto cleanup_err;
156     }
157
158     data.size = CNID_HEADER_LEN + len + 1;
159
160     /* Start off with the hint.  It should be in network byte order.
161      * We need to make sure that somebody doesn't add in restricted
162      * cnid's to the database. */
163     if (ntohl(hint) >= CNID_START) {
164         /* If the key doesn't exist, add it in.  Don't fiddle with nextID. */
165         rc = add_cnid(db, NULL, &key, &data);
166         switch (rc) {
167         case DB_KEYEXIST: /* Need to use RootInfo after all. */
168             break;
169         default:
170             syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
171                    ntohl(hint), db_strerror(rc));
172             goto cleanup_err;
173         case 0:
174 #ifdef DEBUG
175             syslog(LOG_INFO, "cnid_add: Used hint for did %u, name %s as %u",
176                    ntohl(did), name, ntohl(hint));
177 #endif
178             return hint;
179         }
180     }
181
182     /* We need to create a random sleep interval to prevent deadlocks. */
183     (void)srand(getpid() ^ time(NULL));
184     t.tv_sec = 0;
185
186     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
187     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
188     rootinfo_key.data = ROOTINFO_KEY;
189     rootinfo_key.size = ROOTINFO_KEYLEN;
190
191     /* Get the key. */
192 retry_get:
193     switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key,
194                                      &rootinfo_data, 0)) {
195     case DB_LOCK_DEADLOCK:
196         if ((rc = txn_abort(tid)) != 0) {
197             syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
198             goto cleanup_err;
199         }
200         goto retry_get;
201     case 0:
202         memcpy(&hint, rootinfo_data.data, sizeof(hint));
203 #ifdef DEBUG
204         syslog(LOG_INFO, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
205 #endif
206         break;
207     case DB_NOTFOUND:
208         hint = htonl(CNID_START);
209 #ifdef DEBUG
210         syslog(LOG_INFO, "cnid_add: Using CNID_START for did %u, name %s",
211                ntohl(did), name);
212 #endif
213         break;
214     default:
215         syslog(LOG_ERR, "cnid_add: Unable to lookup rootinfo: %s",
216                db_strerror(rc));
217         goto cleanup_err;
218     }
219
220
221 retry:
222     t.tv_usec = rand() % 1000000;
223 #ifdef DEBUG
224     syslog(LOG_INFO, "cnid_add: Hitting MAX_ABORTS, sleeping");
225 #endif
226     (void)select(0, NULL, NULL, NULL, &t);
227     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
228         syslog(LOG_ERR, "cnid_add: Failed to begin transaction: %s",
229                db_strerror(rc));
230         goto cleanup_err;
231     }
232
233     /* Search for a new id.  We keep the first id around to check for
234      * wrap-around.  NOTE: I do it this way so that we can go back and
235      * fill in holes. */
236     save = id = ntohl(hint);
237     while ((rc = add_cnid(db, tid, &key, &data)) != 0) {
238         /* Don't use any special CNIDs. */
239         if (++id < CNID_START) {
240             id = CNID_START;
241         }
242         if (rc == DB_LOCK_DEADLOCK) {
243             if ((rc = txn_abort(tid)) != 0) {
244                 syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
245                 goto cleanup_err;
246             }
247             goto retry;
248         }
249
250         if ((rc != DB_KEYEXIST) || (save == id)) {
251             syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
252                    ntohl(hint), db_strerror(rc));
253             goto cleanup_abort;
254         }
255         hint = htonl(id);
256     }
257
258     rootinfo_data.data = &hint;
259     rootinfo_data.size = sizeof(hint);
260
261     switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
262     case DB_LOCK_DEADLOCK:
263         if ((rc = txn_abort(tid)) != 0) {
264             syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
265             goto cleanup_err;
266         }
267         goto retry;
268     case 0:
269         break;
270     default:
271         syslog(LOG_ERR, "cnid_add: Unable to update rootinfo: %s",
272                db_strerror(rc));
273         goto cleanup_abort;
274     }
275
276 cleanup_commit:
277     /* The transaction finished, commit it. */
278     if ((rc = txn_commit(tid, 0)) != 0) {
279         syslog(LOG_ERR, "cnid_add: Unable to commit transaction: %s",
280                db_strerror(rc));
281         goto cleanup_err;
282     }
283
284 #ifdef DEBUG
285     syslog(LOG_INFO, "cnid_add: Returned CNID for did %u, name %s as %u",
286            ntohl(did), name, ntohl(hint));
287 #endif
288     return hint;
289
290 cleanup_abort:
291     txn_abort(tid);
292
293 cleanup_err:
294     return 0;
295 }
296 #endif /* CNID_DB */
297