2 * $Id: cnid_add.c,v 1.29 2002-02-02 19:11:37 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) {
49 memset(&altkey, 0, sizeof(altkey));
50 memset(&altdata, 0, sizeof(altdata));
53 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
58 if ((rc = db->db_cnid->put(db->db_cnid, tid, key, data, DB_NOOVERWRITE))) {
59 if (rc == DB_LOCK_DEADLOCK) {
60 if ((ret = txn_abort(tid)) != 0) {
68 /* dev/ino database */
69 altkey.data = data->data;
70 altkey.size = CNID_DEVINO_LEN;
71 altdata.data = key->data;
72 altdata.size = key->size;
73 if ((rc = db->db_devino->put(db->db_devino, tid, &altkey, &altdata, 0))) {
74 if (rc == DB_LOCK_DEADLOCK) {
75 if ((ret = txn_abort(tid)) != 0) {
83 /* did/name database */
84 altkey.data = (char *) data->data + CNID_DEVINO_LEN;
85 altkey.size = data->size - CNID_DEVINO_LEN;
86 if ((rc = db->db_didname->put(db->db_didname, tid, &altkey, &altdata, 0))) {
87 if (rc == DB_LOCK_DEADLOCK) {
88 if ((ret = txn_abort(tid)) != 0) {
96 if ((rc = txn_commit(tid, 0)) != 0) {
97 LOG(log_error, logtype_default, "add_cnid: Failed to commit transaction: %s", db_strerror(rc));
104 if ((ret = txn_abort(tid)) != 0) {
110 cnid_t cnid_add(void *CNID, const struct stat *st,
111 const cnid_t did, const char *name, const int len,
115 DBT key, data, rootinfo_key, rootinfo_data;
121 if (!(db = CNID) || !st || !name) {
122 errno = CNID_ERR_PARAM;
127 id = cnid_lookup(db, st, did, name, len);
128 /* ... Return id if it is valid, or if Rootinfo is read-only. */
129 if (id || (db->flags & CNIDFLAG_DB_RO)) {
131 LOG(log_info, logtype_default, "cnid_add: Looked up did %u, name %s as %u", ntohl(did), name, ntohl(id));
136 /* Initialize our DBT data structures. */
137 memset(&key, 0, sizeof(key));
138 memset(&data, 0, sizeof(data));
140 /* Just tickle hint, and the key will change (gotta love pointers). */
142 key.size = sizeof(hint);
144 if ((data.data = make_cnid_data(st, did, name, len)) == NULL) {
145 LOG(log_error, logtype_default, "cnid_add: Path name is too long");
146 errno = CNID_ERR_PATH;
150 data.size = CNID_HEADER_LEN + len + 1;
152 /* Start off with the hint. It should be in network byte order.
153 * We need to make sure that somebody doesn't add in restricted
154 * cnid's to the database. */
155 if (ntohl(hint) >= CNID_START) {
156 /* If the key doesn't exist, add it in. Don't fiddle with nextID. */
157 rc = add_cnid(db, &key, &data);
159 case DB_KEYEXIST: /* Need to use RootInfo after all. */
162 LOG(log_error, logtype_default, "cnid_add: Unable to add CNID %u: %s", ntohl(hint), db_strerror(rc));
167 LOG(log_info, logtype_default, "cnid_add: Used hint for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
173 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
174 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
175 rootinfo_key.data = ROOTINFO_KEY;
176 rootinfo_key.size = ROOTINFO_KEYLEN;
179 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
180 LOG(log_error, logtype_default, "cnid_add: Failed to begin transaction: %s", db_strerror(rc));
186 switch (rc = db->db_didname->get(db->db_didname, tid, &rootinfo_key,
187 &rootinfo_data, DB_RMW)) {
188 case DB_LOCK_DEADLOCK:
189 if ((rc = txn_abort(tid)) != 0) {
190 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
196 memcpy(&hint, rootinfo_data.data, sizeof(hint));
198 /* If we've hit the max CNID allowed, we return a fatal error. CNID
199 * needs to be recycled before proceding. */
200 if (++id == CNID_INVALID) {
202 LOG(log_error, logtype_default, "cnid_add: FATAL: Cannot add CNID for %s. CNID database has reached its limit.", name);
203 errno = CNID_ERR_MAX;
208 LOG(log_info, logtype_default, "cnid_add: Found rootinfo for did %u, name %s as %u", ntohl(did), name, ntohl(hint));
212 hint = htonl(CNID_START);
214 LOG(log_info, logtype_default, "cnid_add: Using CNID_START for did %u, name %s", ntohl(did), name);
218 LOG(log_error, logtype_default, "cnid_add: Unable to lookup rootinfo: %s", db_strerror(rc));
222 rootinfo_data.data = &hint;
223 rootinfo_data.size = sizeof(hint);
225 switch (rc = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
226 case DB_LOCK_DEADLOCK:
227 if ((rc = txn_abort(tid)) != 0) {
228 LOG(log_error, logtype_default, "cnid_add: txn_abort: %s", db_strerror(rc));
234 /* The transaction finished, commit it. */
235 if ((rc = txn_commit(tid, 0)) != 0) {
236 LOG(log_error, logtype_default, "cnid_add: Unable to commit transaction: %s", db_strerror(rc));
242 LOG(log_error, logtype_default, "cnid_add: Unable to update rootinfo: %s", db_strerror(rc));
246 /* Now we need to add the CNID data to the databases. */
247 rc = add_cnid(db, &key, &data);
249 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));
255 LOG(log_info, logtype_default, "cnid_add: Returned CNID for did %u, name %s as %u", ntohl(did), name, ntohl(hint));