]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
bugfix: ifdef a transaction related call wen CDB is defined.
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.33 2003-02-07 15:00:55 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 cnid_t cnid_add(void *CNID, const struct stat *st,
129                 const cnid_t did, const char *name, const int len,
130                 cnid_t hint)
131 {
132     CNID_private *db;
133     DBT key, data, rootinfo_key, rootinfo_data;
134 #ifndef CNID_DB_CDB
135     DB_TXN *tid;
136 #endif /* CNID_DB_CDB */
137     cnid_t id;
138     int rc;
139
140     if (!(db = CNID) || !st || !name) {
141         errno = CNID_ERR_PARAM;
142         return CNID_INVALID;
143     }
144
145     /* Do a lookup. */
146     id = cnid_lookup(db, st, did, name, len);
147     /* ... Return id if it is valid, or if Rootinfo is read-only. */
148     if (id || (db->flags & CNIDFLAG_DB_RO)) {
149 #ifdef DEBUG
150         LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
151 #endif
152         return id;
153     }
154
155     /* Initialize our DBT data structures. */
156     memset(&key, 0, sizeof(key));
157     memset(&data, 0, sizeof(data));
158
159     /* Just tickle hint, and the key will change (gotta love pointers). */
160     key.data = &hint;
161     key.size = sizeof(hint);
162
163     if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
164         LOG(log_error, logtype_default, "cnid_add: Path name is too long");
165         errno = CNID_ERR_PATH;
166         return CNID_INVALID;
167     }
168
169     data.size = CNID_HEADER_LEN + len + 1;
170
171     /* Start off with the hint.  It should be in network byte order.
172      * We need to make sure that somebody doesn't add in restricted
173      * cnid's to the database. */
174     if (ntohl(hint) >= CNID_START) {
175         /* If the key doesn't exist, add it in.  Don't fiddle with nextID. */
176         rc = add_cnid(db, &key, &data);
177         switch (rc) {
178         case DB_KEYEXIST: /* Need to use RootInfo after all. */
179             break;
180         default:
181             LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
182             errno = CNID_ERR_DB;
183             return CNID_INVALID;
184         case 0:
185 #ifdef DEBUG
186             LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
187 #endif
188             return hint;
189         }
190     }
191
192     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
193     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
194     rootinfo_key.data = ROOTINFO_KEY;
195     rootinfo_key.size = ROOTINFO_KEYLEN;
196
197 #ifndef CNID_DB_CDB
198 retry:
199     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
200         LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
201         errno = CNID_ERR_DB;
202         return CNID_INVALID;
203     }
204 #endif /* CNID_DB_CDB */
205
206     /* Get the key. */
207 #ifdef CNID_DB_CDB
208     switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key,
209                                      &rootinfo_data, 0)) {
210 #else /* CNID_DB_CDB */
211     switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
212                                      &rootinfo_data, DB_RMW)) {
213     case DB_LOCK_DEADLOCK:
214         if ((rc = txn_abort(tid)) != 0) {
215             LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
216             errno = CNID_ERR_DB;
217             return CNID_INVALID;
218         }
219         goto retry;
220 #endif /* CNID_DB_CDB */
221     case 0:
222         memcpy(&hint, rootinfo_data.data, sizeof(hint));
223         id = ntohl(hint);
224         /* If we've hit the max CNID allowed, we return a fatal error.  CNID
225          * needs to be recycled before proceding. */
226         if (++id == CNID_INVALID) {
227 #ifndef CNID_DB_CDB
228             txn_abort(tid);
229 #endif /* CNID_DB_CDB */
230             LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s.  CNID database has reached its limit.", name);
231             errno = CNID_ERR_MAX;
232             return CNID_INVALID;
233         }
234         hint = htonl(id);
235 #ifdef DEBUG
236         LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
237 #endif
238         break;
239     case DB_NOTFOUND:
240         hint = htonl(CNID_START);
241 #ifdef DEBUG
242         LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
243 #endif
244         break;
245     default:
246         LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
247         goto cleanup_abort;
248     }
249
250     rootinfo_data.data = &hint;
251     rootinfo_data.size = sizeof(hint);
252
253     switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
254 #ifndef CNID_DB_CDB
255     case DB_LOCK_DEADLOCK:
256         if ((rc = txn_abort(tid)) != 0) {
257             LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
258             errno = CNID_ERR_DB;
259             return CNID_INVALID;
260         }
261         goto retry;
262 #endif /* CNID_DB_CDB */
263     case 0:
264 #ifndef CNID_DB_CDB
265         /* The transaction finished, commit it. */
266         if ((rc = txn_commit(tid, 0)) != 0) {
267             LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
268             errno = CNID_ERR_DB;
269             return CNID_INVALID;
270         }
271 #endif /* CNID_DB_CDB */
272         break;
273     default:
274         LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
275         goto cleanup_abort;
276     }
277
278     /* Now we need to add the CNID data to the databases. */
279     rc = add_cnid(db, &key, &data);
280     if (rc) {
281         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));
282         errno = CNID_ERR_DB;
283         return CNID_INVALID;
284     }
285
286 #ifdef DEBUG
287     LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
288 #endif
289
290     return hint;
291
292 cleanup_abort:
293 #ifndef CNID_DB_CDB
294     txn_abort(tid);
295 #endif /* CNID_DB_CDB */
296
297     errno = CNID_ERR_DB;
298     return CNID_INVALID;
299 }
300 #endif /* CNID_DB */
301