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