2 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
5 * cnid_add (db, dev, ino, did, name, hint):
6 * add a name to the CNID database. we use both dev/ino and did/name
7 * to keep track of things. */
18 #include <netatalk/endian.h>
20 #include <atalk/adouble.h>
21 #include <atalk/cnid.h>
22 #include <atalk/util.h>
24 #include "cnid_private.h"
26 /* add an entry to the CNID databases. we do this as a transaction
27 * to prevent messiness. */
28 static int add_cnid(CNID_private *db, DBT *key, DBT *data)
34 txnp = db->dbenv.tx_info;
35 memset(&altkey, 0, sizeof(altkey));
36 memset(&altdata, 0, sizeof(altdata));
39 if (errno = txn_begin(txnp, NULL, &tid)) {
44 if (errno = db->db_cnid->put(db->db_cnid, tid,
45 key, data, DB_NOOVERWRITE)) {
53 /* dev/ino database */
54 altkey.data = data->data;
55 altkey.size = CNID_DEVINO_LEN;
56 altdata.data = key->data;
57 altdata.size = key->size;
58 if ((errno = db->db_devino->put(db->db_devino, tid,
59 &altkey, &altdata, 0))) {
67 /* did/name database */
68 altkey.data = data->data + CNID_DEVINO_LEN;
69 altkey.size = data->size - CNID_DEVINO_LEN;
70 if (errno = db->db_didname->put(db->db_didname, tid,
71 &altkey, &altdata, 0)) {
79 return txn_commit(tid);
82 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
83 return that cnid if it exists. */
84 cnid_t cnid_add(void *CNID, const struct stat *st,
85 const cnid_t did, const char *name, const int len,
94 if (!(db = CNID) || !st || !name)
97 /* just do a lookup if RootInfo is read-only. */
98 if (db->flags & (CNIDFLAG_ROOTINFO_RO | CNIDFLAG_DB_RO))
99 return cnid_lookup(db, st, did, name, len);
101 /* initialize everything */
102 memset(&key, 0, sizeof(key));
103 memset(&data, 0, sizeof(data));
105 /* acquire a lock on RootInfo. as the cnid database is the only user
106 * of RootInfo, we just use our own locks.
108 * NOTE: we lock it here to serialize access to the database. */
109 lock.l_type = F_WRLCK;
110 lock.l_whence = SEEK_SET;
111 lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
112 lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
113 if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW, &lock) < 0) {
114 syslog(LOG_ERR, "cnid_add: can't establish lock: %m");
118 /* if it's already stored, just return it */
119 if ((id = cnid_lookup(db, st, did, name, len))) {
124 /* just set hint, and the key will change. */
126 key.size = sizeof(hint);
129 make_cnid_data(st, did, name, len)) == NULL) {
130 syslog(LOG_ERR, "cnid_add: path name too long.");
133 data.size = CNID_HEADER_LEN + len + 1;
135 /* start off with the hint. it should be in network byte order.
136 * we need to make sure that somebody doesn't add in restricted
137 * cnid's to the database. */
138 if (ntohl(hint) >= CNID_START) {
139 /* if the key doesn't exist, add it in. don't fiddle with nextID. */
140 errno = add_cnid(db, &key, &data);
142 case DB_KEYEXIST: /* need to use RootInfo after all. */
145 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
153 /* no need to refresh the header file */
154 memcpy(&hint, ad_entry(&db->rootinfo, ADEID_DID), sizeof(hint));
156 /* search for a new id. we keep the first id around to check for
157 * wrap-around. NOTE: i do it this way so that we can go back and
159 save = id = ntohl(hint);
160 while (errno = add_cnid(db, &key, &data)) {
161 /* don't use any of the special CNIDs */
162 if (++id < CNID_START)
165 if ((errno != DB_KEYEXIST) || (save == id)) {
166 syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
173 /* update RootInfo with the next id. */
175 memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
176 ad_flush(&db->rootinfo, ADFLAGS_HF);
179 lock.l_type = F_UNLCK;
180 fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);