2 * $Id: cnid_db3_add.c,v 1.4 2009-10-29 13:17:29 didg 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 */
16 #ifdef CNID_BACKEND_DB3
19 #include <sys/param.h>
24 #endif /* HAVE_UNISTD_H */
27 #endif /* HAVE_FCNTL_H */
29 #include <atalk/logger.h>
30 #ifdef HAVE_SYS_TIME_H
32 #endif /* HAVE_SYS_TIME_H */
39 #include <netatalk/endian.h>
41 #include <atalk/adouble.h>
43 #include <atalk/util.h>
45 #include "cnid_db3_private.h"
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) {
54 memset(&altkey, 0, sizeof(altkey));
55 memset(&altdata, 0, sizeof(altdata));
58 if ((rc = db3_txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
63 if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
64 if (rc == DB_LOCK_DEADLOCK) {
65 if ((ret = db3_txn_abort(tid)) != 0) {
73 /* dev/ino database */
74 altkey.data = data->data;
75 altkey.size = CNID_DEVINO_LEN;
76 altdata.data = key->data;
77 altdata.size = key->size;
78 if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
79 if (rc == DB_LOCK_DEADLOCK) {
80 if ((ret = db3_txn_abort(tid)) != 0) {
88 /* did/name database */
89 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
90 altkey.size = data->size - CNID_DEVINO_LEN;
91 if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
92 if (rc == DB_LOCK_DEADLOCK) {
93 if ((ret = db3_txn_abort(tid)) != 0) {
101 if ((rc = db3_txn_commit(tid, 0)) != 0) {
102 LOG(log_error, logtype_default, "add_cnid: Failed to commit transaction: %s", db_strerror(rc));
109 if ((ret = db3_txn_abort(tid)) != 0) {
115 /* ---------------------- */
116 static cnid_t get_cnid(CNID_private *db)
118 DBT rootinfo_key, rootinfo_data;
123 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
124 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
125 rootinfo_key.data = ROOTINFO_KEY;
126 rootinfo_key.size = ROOTINFO_KEYLEN;
129 if ((rc = db3_txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
130 LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
134 switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
135 &rootinfo_data, DB_RMW)) {
136 case DB_LOCK_DEADLOCK:
137 if ((rc = db3_txn_abort(tid)) != 0) {
138 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
144 memcpy(&hint, rootinfo_data.data, sizeof(hint));
146 /* If we've hit the max CNID allowed, we return a fatal error. CNID
147 * needs to be recycled before proceding. */
148 if (++id == CNID_INVALID) {
150 LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID. CNID database has reached its limit.");
151 errno = CNID_ERR_MAX;
157 LOG(log_debug, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
161 hint = htonl(CNID_START);
164 LOG(log_debug, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
168 LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
172 rootinfo_data.data = &hint;
173 rootinfo_data.size = sizeof(hint);
175 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
176 case DB_LOCK_DEADLOCK:
177 if ((rc = db3_txn_abort(tid)) != 0) {
178 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
184 /* The transaction finished, commit it. */
185 if ((rc = db3_txn_commit(tid, 0)) != 0) {
186 LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
192 LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
203 /* ------------------------ */
204 cnid_t cnid_db3_add(struct _cnid_db *cdb, const struct stat *st,
205 const cnid_t did, char *name, const size_t len,
213 if (!cdb || !(db = cdb->_private) || !st || !name) {
214 errno = CNID_ERR_PARAM;
219 id = cnid_db3_lookup(cdb, st, did, name, len);
220 /* ... Return id if it is valid, or if Rootinfo is read-only. */
221 if (id || (db->flags & CNIDFLAG_DB_RO)) {
223 LOG(log_debug, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
228 /* Initialize our DBT data structures. */
229 memset(&key, 0, sizeof(key));
230 memset(&data, 0, sizeof(data));
232 /* Just tickle hint, and the key will change (gotta love pointers). */
234 key.size = sizeof(hint);
236 if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
237 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
238 errno = CNID_ERR_PATH;
242 data.size = CNID_HEADER_LEN + len + 1;
244 /* Start off with the hint. It should be in network byte order.
245 * We need to make sure that somebody doesn't add in restricted
246 * cnid's to the database. */
247 if (ntohl(hint) >= CNID_START) {
248 /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
249 rc = add_cnid(db, &key, &data);
251 case DB_KEYEXIST: /* Need to use RootInfo after all. */
254 LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
259 LOG(log_debug, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
270 /* Now we need to add the CNID data to the databases. */
271 rc = add_cnid(db, &key, &data);
273 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));
279 LOG(log_debug, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
285 #endif /* CNID_BACKEND_DB3 */