2 * $Id: cnid_add.c,v 1.35 2003-06-06 21:22:43 srittau Exp $
4 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
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.
14 #endif /* HAVE_CONFIG_H */
18 #include <sys/param.h>
23 #endif /* HAVE_UNISTD_H */
26 #endif /* HAVE_FCNTL_H */
28 #include <atalk/logger.h>
29 #ifdef HAVE_SYS_TIME_H
31 #endif /* HAVE_SYS_TIME_H */
34 #include <netatalk/endian.h>
36 #include <atalk/adouble.h>
37 #include <atalk/cnid.h>
38 #include <atalk/util.h>
40 #define use_make_cnid_data
41 #include "cnid_private.h"
45 #endif /* CNID_DB_CDB */
47 /* add an entry to the CNID databases. we do this as a transaction
48 * to prevent messiness. */
49 static int add_cnid(CNID_private *db, DBT *key, DBT *data) {
53 #endif /* CNID_DB_CDB */
56 memset(&altkey, 0, sizeof(altkey));
57 memset(&altdata, 0, sizeof(altdata));
61 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
64 #endif /* CNID_DB_CDB */
67 if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
69 if (rc == DB_LOCK_DEADLOCK) {
70 if ((ret = txn_abort(tid)) != 0) {
75 #endif /* CNID_DB_CDB */
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))) {
86 if (rc == DB_LOCK_DEADLOCK) {
87 if ((ret = txn_abort(tid)) != 0) {
92 #endif /* CNID_DB_CDB */
96 /* did/name database */
97 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
98 altkey.size = data->size - CNID_DEVINO_LEN;
99 if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
101 if (rc == DB_LOCK_DEADLOCK) {
102 if ((ret = txn_abort(tid)) != 0) {
107 #endif /* CNID_DB_CDB */
112 if ((rc = txn_commit(tid, 0)) != 0) {
113 LOG(log_error, logtype_default, "add_cnid: Failed to commit transaction: %s", db_strerror(rc));
116 #endif /* CNID_DB_CDB */
122 if ((ret = txn_abort(tid)) != 0) {
129 /* ---------------------- */
131 static cnid_t get_cnid(CNID_private *db)
133 DBT rootinfo_key, rootinfo_data;
139 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
140 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
141 rootinfo_key.data = ROOTINFO_KEY;
142 rootinfo_key.size = ROOTINFO_KEYLEN;
145 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
146 LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
150 switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
151 &rootinfo_data, DB_RMW)) {
152 case DB_LOCK_DEADLOCK:
153 if ((rc = txn_abort(tid)) != 0) {
154 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
160 memcpy(&hint, rootinfo_data.data, sizeof(hint));
162 /* If we've hit the max CNID allowed, we return a fatal error. CNID
163 * needs to be recycled before proceding. */
164 if (++id == CNID_INVALID) {
166 LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s. CNID database has reached its limit.", name);
167 errno = CNID_ERR_MAX;
172 LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
176 hint = htonl(CNID_START);
178 LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
182 LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
186 rootinfo_data.data = &hint;
187 rootinfo_data.size = sizeof(hint);
189 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
190 case DB_LOCK_DEADLOCK:
191 if ((rc = txn_abort(tid)) != 0) {
192 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
198 /* The transaction finished, commit it. */
199 if ((rc = txn_commit(tid, 0)) != 0) {
200 LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
206 LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
217 static cnid_t get_cnid(CNID_private *db)
219 DBT rootinfo_key, rootinfo_data;
225 if ((rc = db->db_didname->cursor(db->db_didname, NULL, &cursor, DB_WRITECURSOR) ) != 0) {
226 LOG(log_error, logtype_default, "get_cnid: Unable to get a cursor: %s", db_strerror(rc));
230 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
231 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
232 rootinfo_key.data = ROOTINFO_KEY;
233 rootinfo_key.size = ROOTINFO_KEYLEN;
235 switch (rc = cursor->c_get(cursor, &rootinfo_key, &rootinfo_data, DB_SET)) {
237 memcpy(&hint, rootinfo_data.data, sizeof(hint));
239 /* If we've hit the max CNID allowed, we return a fatal error. CNID
240 * needs to be recycled before proceding. */
241 if (++id == CNID_INVALID) {
242 LOG(log_error, logtype_default, "cnid_add: FATAL: CNID database has reached its limit.");
243 errno = CNID_ERR_MAX;
250 hint = htonl(CNID_START);
254 LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
259 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
260 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
261 rootinfo_data.data = &hint;
262 rootinfo_data.size = sizeof(hint);
263 rootinfo_key.data = ROOTINFO_KEY;
264 rootinfo_key.size = ROOTINFO_KEYLEN;
266 switch (rc = cursor->c_put(cursor, &rootinfo_key, &rootinfo_data, flag)) {
270 LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
274 if ((rc = cursor->c_close(cursor)) != 0) {
275 LOG(log_error, logtype_default, "get_cnid: Unable to close cursor: %s", db_strerror(rc));
281 if ((rc = cursor->c_close(cursor)) != 0) {
282 LOG(log_error, logtype_default, "get_cnid: Unable to close cursor: %s", db_strerror(rc));
287 #endif /* CNID_DB_CDB */
289 /* ------------------------ */
290 cnid_t cnid_add(void *CNID, const struct stat *st,
291 const cnid_t did, const char *name, const int len,
299 if (!(db = CNID) || !st || !name) {
300 errno = CNID_ERR_PARAM;
305 id = cnid_lookup(db, st, did, name, len);
306 /* ... Return id if it is valid, or if Rootinfo is read-only. */
307 if (id || (db->flags & CNIDFLAG_DB_RO)) {
309 LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
314 /* Initialize our DBT data structures. */
315 memset(&key, 0, sizeof(key));
316 memset(&data, 0, sizeof(data));
318 /* Just tickle hint, and the key will change (gotta love pointers). */
320 key.size = sizeof(hint);
322 if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
323 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
324 errno = CNID_ERR_PATH;
328 data.size = CNID_HEADER_LEN + len + 1;
330 /* Start off with the hint. It should be in network byte order.
331 * We need to make sure that somebody doesn't add in restricted
332 * cnid's to the database. */
333 if (ntohl(hint) >= CNID_START) {
334 /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
335 rc = add_cnid(db, &key, &data);
337 case DB_KEYEXIST: /* Need to use RootInfo after all. */
340 LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
345 LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
356 /* Now we need to add the CNID data to the databases. */
357 rc = add_cnid(db, &key, &data);
359 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));
365 LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));