2 * $Id: cnid_add.c,v 1.12 2001-10-19 02:35:47 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 */
31 #include <netatalk/endian.h>
33 #include <atalk/adouble.h>
34 #include <atalk/cnid.h>
35 #include <atalk/util.h>
37 #include "cnid_private.h"
39 /* add an entry to the CNID databases. we do this as a transaction
40 * to prevent messiness. */
41 static int add_cnid(CNID_private *db, DBT *key, DBT *data)
45 /* We create rc here because using errno is bad. Why? Well, if you
46 * use errno once, then call another function which resets it, you're
50 memset(&altkey, 0, sizeof(altkey));
51 memset(&altdata, 0, sizeof(altdata));
54 if ((rc = txn_begin(db->dbenv, NULL, &tid,0))) {
60 if ((rc = db->db_cnid->put(db->db_cnid, tid,
61 key, data, DB_NOOVERWRITE))) {
63 if (rc == DB_LOCK_DEADLOCK) {
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,
77 &altkey, &altdata, 0))) {
79 if (rc == DB_LOCK_DEADLOCK) {
87 /* did/name database */
88 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
89 altkey.size = data->size - CNID_DEVINO_LEN;
90 if ((rc = db->db_didname->put(db->db_didname, tid,
91 &altkey, &altdata, 0))) {
93 if (rc == DB_LOCK_DEADLOCK) {
100 return txn_commit(tid, 0);
103 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
104 return that cnid if it exists. */
105 cnid_t cnid_add(void *CNID, const struct stat *st,
106 const cnid_t did, const char *name, const int len,
111 DBT rootinfo_key, rootinfo_data;
119 if (!(db = CNID) || !st || !name) {
124 id = cnid_lookup(db, st, did, name, len);
125 /* ...return id if it is valid or if RootInfo is read-only. */
126 if (id || (db->flags & CNIDFLAG_DB_RO)) {
128 syslog(LOG_ERR, "cnid_add: looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
132 /* initialize everything */
133 memset(&key, 0, sizeof(key));
134 memset(&data, 0, sizeof(data));
136 /* just set hint, and the key will change. */
138 key.size = sizeof(hint);
141 make_cnid_data(st, did, name, len)) == NULL) {
142 syslog(LOG_ERR, "cnid_add: path name too long.");
145 data.size = CNID_HEADER_LEN + len + 1;
147 /* start off with the hint. it should be in network byte order.
148 * we need to make sure that somebody doesn't add in restricted
149 * cnid's to the database. */
150 if (ntohl(hint) >= CNID_START) {
151 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
152 rc = add_cnid(db, &key, &data);
154 case DB_KEYEXIST: /* need to use RootInfo after all. */
157 syslog(LOG_ERR, "cnid_add: unable to add CNID(%u)", ntohl(hint));
161 syslog(LOG_ERR, "cnid_add: used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
167 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
168 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
170 /* just set hint, and the key will change. */
171 rootinfo_key.data = ROOTINFO_KEY;
172 rootinfo_key.size = ROOTINFO_KEYLEN;
176 switch (rc = db->db_didname->get(db->db_didname, NULL, &rootinfo_key, &rootinfo_data, 0)) {
177 case DB_LOCK_DEADLOCK:
180 memcpy (&hint, rootinfo_data.data, sizeof(hint));
182 syslog(LOG_ERR, "cnid_add: found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
185 hint = htonl(CNID_START);
187 syslog(LOG_ERR, "cnid_add: using CNID_START for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
190 syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", rc);
194 /* search for a new id. we keep the first id around to check for
195 * wrap-around. NOTE: i do it this way so that we can go back and
197 save = id = ntohl(hint);
198 while ((rc = add_cnid(db, &key, &data))) {
199 /* don't use any of the special CNIDs */
200 if (++id < CNID_START)
203 if ((rc != DB_KEYEXIST) || (save == id)) {
204 syslog(LOG_ERR, "cnid_add: unable to add CNID(%u)", ntohl(hint));
211 /* update RootInfo with the next id. */
212 rootinfo_data.data = &hint;
213 rootinfo_data.size = sizeof(hint);
215 /* Abort and retry the modification. */
217 retry: if ((rc = txn_abort(tid)) != 0)
218 syslog(LOG_ERR, "cnid_add: txn_abort failed (%d)", rc);
222 /* Begin the transaction. */
223 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
224 syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", rc);
228 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
229 case DB_LOCK_DEADLOCK:
234 syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", rc);
240 /* The transaction finished, commit it. */
241 if ((rc = txn_commit(tid, 0)) != 0) {
242 syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", rc);
247 syslog(LOG_ERR, "cnid_add: returned cnid for did %u, name %s as %u", ntohl(did), name, ntohl(hint));