2 * $Id: cnid_add.c,v 1.14.2.3 2001-12-15 06:35:28 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 */
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 #define MAX_ABORTS 255
44 /* add an entry to the CNID databases. we do this as a transaction
45 * to prevent messiness. */
46 static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data) {
49 /* We create rc here because using errno is bad. Why? Well, if you
50 * use errno once, then call another function which resets it, you're
52 int rc, ret, aborts = 0;
54 memset(&altkey, 0, sizeof(altkey));
55 memset(&altdata, 0, sizeof(altdata));
59 if ((rc = txn_abort(tid)) != 0) {
62 if (++aborts > MAX_ABORTS) {
63 return DB_LOCK_DEADLOCK;
67 if ((rc = txn_begin(db->dbenv, ptid, &tid, 0)) != 0) {
72 if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
73 if (rc == DB_LOCK_DEADLOCK) {
81 /* dev/ino database */
82 altkey.data = data->data;
83 altkey.size = CNID_DEVINO_LEN;
84 altdata.data = key->data;
85 altdata.size = key->size;
86 if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
87 if (rc == DB_LOCK_DEADLOCK) {
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))) {
99 if (rc == DB_LOCK_DEADLOCK) {
106 if ((rc = txn_commit(tid, 0)) != 0) {
107 syslog(LOG_ERR, "add_cnid: Failed to commit transaction: %s",
114 if ((ret = txn_abort(tid)) != 0) {
121 cnid_t cnid_add(void *CNID, const struct stat *st,
122 const cnid_t did, const char *name, const int len,
126 DBT key, data, rootinfo_key, rootinfo_data;
132 if (!(db = CNID) || !st || !name) {
137 id = cnid_lookup(db, st, did, name, len);
138 /* ... Return id if it is valid, or if Rootinfo is read-only. */
139 if (id || (db->flags & CNIDFLAG_DB_RO)) {
141 syslog(LOG_INFO, "cnid_add: Looked up did %u, name %s as %u",
142 ntohl(did), name, ntohl(id));
147 /* Initialize our DBT data structures. */
148 memset(&key, 0, sizeof(key));
149 memset(&data, 0, sizeof(data));
151 /* Just tickle hint, and the key will change (gotta love pointers). */
153 key.size = sizeof(hint);
155 if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
156 syslog(LOG_ERR, "cnid_add: Path name is too long");
160 data.size = CNID_HEADER_LEN + len + 1;
162 /* Start off with the hint. It should be in network byte order.
163 * We need to make sure that somebody doesn't add in restricted
164 * cnid's to the database. */
165 if (ntohl(hint) >= CNID_START) {
166 /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
167 rc = add_cnid(db, NULL, &key, &data);
169 case DB_KEYEXIST: /* Need to use RootInfo after all. */
172 syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
173 ntohl(hint), db_strerror(rc));
177 syslog(LOG_INFO, "cnid_add: Used hint for did %u, name %s as %u",
178 ntohl(did), name, ntohl(hint));
184 /* We need to create a random sleep interval to prevent deadlocks. */
185 (void)srand(getpid() ^ time(NULL));
188 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
189 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
190 rootinfo_key.data = ROOTINFO_KEY;
191 rootinfo_key.size = ROOTINFO_KEYLEN;
195 switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key,
196 &rootinfo_data, 0)) {
197 case DB_LOCK_DEADLOCK:
198 if ((rc = txn_abort(tid)) != 0) {
199 syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
204 memcpy(&hint, rootinfo_data.data, sizeof(hint));
206 syslog(LOG_INFO, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
210 hint = htonl(CNID_START);
212 syslog(LOG_INFO, "cnid_add: Using CNID_START for did %u, name %s",
217 syslog(LOG_ERR, "cnid_add: Unable to lookup rootinfo: %s",
225 t.tv_usec = rand() % 1000000;
227 syslog(LOG_INFO, "cnid_add: Hitting MAX_ABORTS, sleeping");
229 (void)select(0, NULL, NULL, NULL, &t);
231 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
232 syslog(LOG_ERR, "cnid_add: Failed to begin transaction: %s",
237 /* Search for a new id. We keep the first id around to check for
238 * wrap-around. NOTE: I do it this way so that we can go back and
240 save = id = ntohl(hint);
241 while ((rc = add_cnid(db, tid, &key, &data)) != 0) {
242 /* Don't use any special CNIDs. */
243 if (++id < CNID_START) {
246 if (rc == DB_LOCK_DEADLOCK) {
247 if ((rc = txn_abort(tid)) != 0) {
248 syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
254 if ((rc != DB_KEYEXIST) || (save == id)) {
255 syslog(LOG_ERR, "cnid_add: Unable to add CNID %u: %s",
256 ntohl(hint), db_strerror(rc));
262 rootinfo_data.data = &hint;
263 rootinfo_data.size = sizeof(hint);
265 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
266 case DB_LOCK_DEADLOCK:
267 if ((rc = txn_abort(tid)) != 0) {
268 syslog(LOG_ERR, "cnid_add: txn_abort: %s", db_strerror(rc));
275 syslog(LOG_ERR, "cnid_add: Unable to update rootinfo: %s",
281 /* The transaction finished, commit it. */
282 if ((rc = txn_commit(tid, 0)) != 0) {
283 syslog(LOG_ERR, "cnid_add: Unable to commit transaction: %s",
289 syslog(LOG_INFO, "cnid_add: Returned CNID for did %u, name %s as %u",
290 ntohl(did), name, ntohl(hint));