2 * $Id: cnid_add.c,v 1.24 2002-01-18 04:51:27 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"
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) {
47 /* We create rc here because using errno is bad. Why? Well, if you
48 * use errno once, then call another function which resets it, you're
52 memset(&altkey, 0, sizeof(altkey));
53 memset(&altdata, 0, sizeof(altdata));
56 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
61 if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
62 if (rc == DB_LOCK_DEADLOCK) {
63 if ((ret = txn_abort(tid)) != 0) {
71 /* dev/ino database */
72 altkey.data = data->data;
73 altkey.size = CNID_DEVINO_LEN;
74 altdata.data = key->data;
75 altdata.size = key->size;
76 if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
77 if (rc == DB_LOCK_DEADLOCK) {
78 if ((ret = txn_abort(tid)) != 0) {
86 /* did/name database */
87 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
88 altkey.size = data->size - CNID_DEVINO_LEN;
89 if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
90 if (rc == DB_LOCK_DEADLOCK) {
91 if ((ret = txn_abort(tid)) != 0) {
99 if ((rc = txn_commit(tid, 0)) != 0) {
100 LOG(log_error, logtype_default, "add_cnid: Failed to commit transaction: %s", db_strerror(rc));
107 if ((ret = txn_abort(tid)) != 0) {
113 cnid_t cnid_add(void *CNID, const struct stat *st,
114 const cnid_t did, const char *name, const int len,
118 DBT key, data, rootinfo_key, rootinfo_data;
124 if (!(db = CNID) || !st || !name) {
125 return CNID_ERR_PARAM;
129 id = cnid_lookup(db, st, did, name, len);
130 /* ... Return id if it is valid, or if Rootinfo is read-only. */
131 if (id || (db->flags & CNIDFLAG_DB_RO)) {
133 LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u",
134 ntohl(did), name, ntohl(id));
139 /* Initialize our DBT data structures. */
140 memset(&key, 0, sizeof(key));
141 memset(&data, 0, sizeof(data));
143 /* Just tickle hint, and the key will change (gotta love pointers). */
145 key.size = sizeof(hint);
147 if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
148 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
149 return CNID_ERR_PATH;
152 data.size = CNID_HEADER_LEN + len + 1;
154 /* Start off with the hint. It should be in network byte order.
155 * We need to make sure that somebody doesn't add in restricted
156 * cnid's to the database. */
157 if (ntohl(hint) >= CNID_START) {
158 /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
159 rc = add_cnid(db, &key, &data);
161 case DB_KEYEXIST: /* Need to use RootInfo after all. */
164 LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s",
165 ntohl(hint), db_strerror(rc));
169 LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u",
170 ntohl(did), name, ntohl(hint));
176 /* We need to create a random sleep interval to prevent deadlocks. */
177 /*(void)srand(getpid() ^ time(NULL));
180 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
181 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
182 rootinfo_key.data = ROOTINFO_KEY;
183 rootinfo_key.size = ROOTINFO_KEYLEN;
186 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
187 LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
192 switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
193 &rootinfo_data, DB_RMW)) {
194 case DB_LOCK_DEADLOCK:
195 if ((rc = txn_abort(tid)) != 0) {
196 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
201 memcpy(&hint, rootinfo_data.data, sizeof(hint));
203 LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
207 hint = htonl(CNID_START);
209 LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s",
214 LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
219 if (id <= CNID_START) {
226 /* If we've hit the MAX CNID allowed, we return a fatal error. CNID needs
227 * to be recycled before proceding. */
228 if (id == CNID_MAX) {
230 LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s. CNID database has reached its limit.", name);
236 rootinfo_data.data = &hint;
237 rootinfo_data.size = sizeof(hint);
239 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
240 case DB_LOCK_DEADLOCK:
241 if ((rc = txn_abort(tid)) != 0) {
242 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
249 LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
253 /* The transaction finished, commit it. */
254 if ((rc = txn_commit(tid, 0)) != 0) {
255 LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
259 /* Now we need to add the CNID data to the databases. */
260 rc = add_cnid(db, &key, &data);
262 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));
267 LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));