]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
816008ecfed8405720dc4978280c723a2bc4e51a
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.29 2002-02-02 19:11:37 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 /* 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, DBT *key, DBT *data) {
45     DBT altkey, altdata;
46     DB_TXN *tid;
47     int rc, ret;
48
49     memset(&altkey, 0, sizeof(altkey));
50     memset(&altdata, 0, sizeof(altdata));
51
52 retry:
53     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
54         return rc;
55     }
56
57     /* main database */
58     if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
59         if (rc == DB_LOCK_DEADLOCK) {
60             if ((ret = txn_abort(tid)) != 0) {
61                 return ret;
62             }
63             goto retry;
64         }
65         goto abort;
66     }
67
68     /* dev/ino database */
69     altkey.data = data->data;
70     altkey.size = CNID_DEVINO_LEN;
71     altdata.data = key->data;
72     altdata.size = key->size;
73     if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
74         if (rc == DB_LOCK_DEADLOCK) {
75             if ((ret = txn_abort(tid)) != 0) {
76                 return ret;
77             }
78             goto retry;
79         }
80         goto abort;
81     }
82
83     /* did/name database */
84     altkey.data = (char *) data->data + CNID_DEVINO_LEN;
85     altkey.size = data->size - CNID_DEVINO_LEN;
86     if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
87         if (rc == DB_LOCK_DEADLOCK) {
88             if ((ret = txn_abort(tid)) != 0) {
89                 return ret;
90             }
91             goto retry;
92         }
93         goto abort;
94     }
95
96     if ((rc = txn_commit(tid, 0)) != 0) {
97         LOG(log_error, logtype_default, "add_cnid: Failed to commit transaction: %s", db_strerror(rc));
98         return rc;
99     }
100
101     return 0;
102
103 abort:
104     if ((ret = txn_abort(tid)) != 0) {
105         return ret;
106     }
107     return rc;
108 }
109
110 cnid_t cnid_add(void *CNID, const struct stat *st,
111                 const cnid_t did, const char *name, const int len,
112                 cnid_t hint)
113 {
114     CNID_private *db;
115     DBT key, data, rootinfo_key, rootinfo_data;
116     DB_TXN *tid;
117     struct timeval t;
118     cnid_t id, save;
119     int rc;
120
121     if (!(db = CNID) || !st || !name) {
122         errno = CNID_ERR_PARAM;
123         return CNID_INVALID;
124     }
125
126     /* Do a lookup. */
127     id = cnid_lookup(db, st, did, name, len);
128     /* ... Return id if it is valid, or if Rootinfo is read-only. */
129     if (id || (db->flags & CNIDFLAG_DB_RO)) {
130 #ifdef DEBUG
131         LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
132 #endif
133         return id;
134     }
135
136     /* Initialize our DBT data structures. */
137     memset(&key, 0, sizeof(key));
138     memset(&data, 0, sizeof(data));
139
140     /* Just tickle hint, and the key will change (gotta love pointers). */
141     key.data = &hint;
142     key.size = sizeof(hint);
143
144     if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
145         LOG(log_error, logtype_default, "cnid_add: Path name is too long");
146         errno = CNID_ERR_PATH;
147         return CNID_INVALID;
148     }
149
150     data.size = CNID_HEADER_LEN + len + 1;
151
152     /* Start off with the hint.  It should be in network byte order.
153      * We need to make sure that somebody doesn't add in restricted
154      * cnid's to the database. */
155     if (ntohl(hint) >= CNID_START) {
156         /* If the key doesn't exist, add it in.  Don't fiddle with nextID. */
157         rc = add_cnid(db, &key, &data);
158         switch (rc) {
159         case DB_KEYEXIST: /* Need to use RootInfo after all. */
160             break;
161         default:
162             LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
163             errno = CNID_ERR_DB;
164             return CNID_INVALID;
165         case 0:
166 #ifdef DEBUG
167             LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
168 #endif
169             return hint;
170         }
171     }
172
173     memset(&rootinfo_key, 0, sizeof(rootinfo_key));
174     memset(&rootinfo_data, 0, sizeof(rootinfo_data));
175     rootinfo_key.data = ROOTINFO_KEY;
176     rootinfo_key.size = ROOTINFO_KEYLEN;
177
178 retry:
179     if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
180         LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
181         errno = CNID_ERR_DB;
182         return CNID_INVALID;
183     }
184
185     /* Get the key. */
186     switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
187                                      &rootinfo_data, DB_RMW)) {
188     case DB_LOCK_DEADLOCK:
189         if ((rc = txn_abort(tid)) != 0) {
190             LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
191             errno = CNID_ERR_DB;
192             return CNID_INVALID;
193         }
194         goto retry;
195     case 0:
196         memcpy(&hint, rootinfo_data.data, sizeof(hint));
197         id = ntohl(hint);
198         /* If we've hit the max CNID allowed, we return a fatal error.  CNID
199          * needs to be recycled before proceding. */
200         if (++id == CNID_INVALID) {
201             txn_abort(tid);
202             LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s.  CNID database has reached its limit.", name);
203             errno = CNID_ERR_MAX;
204             return CNID_INVALID;
205         }
206         hint = htonl(id);
207 #ifdef DEBUG
208         LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
209 #endif
210         break;
211     case DB_NOTFOUND:
212         hint = htonl(CNID_START);
213 #ifdef DEBUG
214         LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
215 #endif
216         break;
217     default:
218         LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
219         goto cleanup_abort;
220     }
221
222     rootinfo_data.data = &hint;
223     rootinfo_data.size = sizeof(hint);
224
225     switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
226     case DB_LOCK_DEADLOCK:
227         if ((rc = txn_abort(tid)) != 0) {
228             LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
229             errno = CNID_ERR_DB;
230             return CNID_INVALID; 
231         }
232         goto retry;
233     case 0:
234         /* The transaction finished, commit it. */
235         if ((rc = txn_commit(tid, 0)) != 0) {
236             LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
237             errno = CNID_ERR_DB;
238             return CNID_INVALID;
239         }
240         break;
241     default:
242         LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
243         goto cleanup_abort;
244     }
245
246     /* Now we need to add the CNID data to the databases. */
247     rc = add_cnid(db, &key, &data);
248     if (rc) {
249         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));
250         errno = CNID_ERR_DB;
251         return CNID_INVALID;
252     }
253
254 #ifdef DEBUG
255     LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
256 #endif
257
258     return hint;
259
260 cleanup_abort:
261     txn_abort(tid);
262
263     errno = CNID_ERR_DB;
264     return CNID_INVALID;
265 }
266 #endif /* CNID_DB */
267