2 * $Id: cnid_add.c,v 1.30.2.1 2003-02-08 03:07:24 jmarcus 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 #include "cnid_private.h"
44 #endif /* CNID_DB_CDB */
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) {
52 #endif /* CNID_DB_CDB */
55 memset(&altkey, 0, sizeof(altkey));
56 memset(&altdata, 0, sizeof(altdata));
60 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
63 #endif /* CNID_DB_CDB */
66 if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
68 if (rc == DB_LOCK_DEADLOCK) {
69 if ((ret = txn_abort(tid)) != 0) {
74 #endif /* CNID_DB_CDB */
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))) {
85 if (rc == DB_LOCK_DEADLOCK) {
86 if ((ret = txn_abort(tid)) != 0) {
91 #endif /* CNID_DB_CDB */
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))) {
100 if (rc == DB_LOCK_DEADLOCK) {
101 if ((ret = txn_abort(tid)) != 0) {
106 #endif /* 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));
115 #endif /* CNID_DB_CDB */
121 if ((ret = txn_abort(tid)) != 0) {
124 #endif /* CNID_DB_CDB */
128 cnid_t cnid_add(void *CNID, const struct stat *st,
129 const cnid_t did, const char *name, const int len,
133 DBT key, data, rootinfo_key, rootinfo_data;
136 #endif /* CNID_DB_CDB */
141 if (!(db = CNID) || !st || !name) {
142 errno = CNID_ERR_PARAM;
147 id = cnid_lookup(db, st, did, name, len);
148 /* ... Return id if it is valid, or if Rootinfo is read-only. */
149 if (id || (db->flags & CNIDFLAG_DB_RO)) {
151 LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
156 /* Initialize our DBT data structures. */
157 memset(&key, 0, sizeof(key));
158 memset(&data, 0, sizeof(data));
160 /* Just tickle hint, and the key will change (gotta love pointers). */
162 key.size = sizeof(hint);
164 if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
165 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
166 errno = CNID_ERR_PATH;
170 data.size = CNID_HEADER_LEN + len + 1;
172 /* Start off with the hint. It should be in network byte order.
173 * We need to make sure that somebody doesn't add in restricted
174 * cnid's to the database. */
175 if (ntohl(hint) >= CNID_START) {
176 /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
177 rc = add_cnid(db, &key, &data);
179 case DB_KEYEXIST: /* Need to use RootInfo after all. */
182 LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
187 LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
193 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
194 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
195 rootinfo_key.data = ROOTINFO_KEY;
196 rootinfo_key.size = ROOTINFO_KEYLEN;
200 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
201 LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
205 #endif /* CNID_DB_CDB */
209 switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key,
210 &rootinfo_data, 0)) {
211 #else /* CNID_DB_CDB */
212 switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
213 &rootinfo_data, DB_RMW)) {
214 case DB_LOCK_DEADLOCK:
215 if ((rc = txn_abort(tid)) != 0) {
216 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
221 #endif /* CNID_DB_CDB */
223 memcpy(&hint, rootinfo_data.data, sizeof(hint));
225 /* If we've hit the max CNID allowed, we return a fatal error. CNID
226 * needs to be recycled before proceding. */
227 if (++id == CNID_INVALID) {
230 #endif /* CNID_DB_CDB */
231 LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s. CNID database has reached its limit.", name);
232 errno = CNID_ERR_MAX;
237 LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
241 hint = htonl(CNID_START);
243 LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
247 LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
251 rootinfo_data.data = &hint;
252 rootinfo_data.size = sizeof(hint);
254 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
256 case DB_LOCK_DEADLOCK:
257 if ((rc = txn_abort(tid)) != 0) {
258 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
263 #endif /* CNID_DB_CDB */
266 /* The transaction finished, commit it. */
267 if ((rc = txn_commit(tid, 0)) != 0) {
268 LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
272 #endif /* CNID_DB_CDB */
275 LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
279 /* Now we need to add the CNID data to the databases. */
280 rc = add_cnid(db, &key, &data);
282 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));
288 LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
296 #endif /* CNID_DB_CDB */