2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2010
4 * All Rights Reserved. See COPYING.
9 #endif /* HAVE_CONFIG_H */
14 #endif /* HAVE_UNISTD_H */
17 #endif /* HAVE_FCNTL_H */
19 #ifdef HAVE_SYS_TIME_H
21 #endif /* HAVE_SYS_TIME_H */
23 #include <atalk/logger.h>
24 #include <atalk/cnid_dbd_private.h>
25 #include <atalk/cnid.h>
36 int add_cnid(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply)
41 memset(&key, 0, sizeof(key));
42 memset(&data, 0, sizeof(data));
44 key.data = &rply->cnid;
45 key.size = sizeof(rply->cnid);
47 data.data = pack_cnid_data(rqst);
48 data.size = CNID_HEADER_LEN + rqst->namelen + 1;
49 memcpy(data.data, &rply->cnid, sizeof(rply->cnid));
52 if ((rc = dbif_put(dbd, DBIF_CNID, &key, &data, DB_NOOVERWRITE))) {
53 /* This could indicate a database error or that the key already exists
54 (because of DB_NOOVERWRITE). In that case we still look at some sort of
55 database corruption since that is not supposed to happen. */
59 rply->result = CNID_DBD_RES_ERR_DUPLCNID;
62 /* FIXME: Should that not be logged for case 1:? */
63 LOG(log_error, logtype_cnid, "add_cnid: duplicate %x %s", rply->cnid
64 , (char *)data.data + CNID_NAME_OFS);
66 rqst->cnid = rply->cnid;
67 rc = dbd_update(dbd, rqst, rply);
69 rply->result = CNID_DBD_RES_ERR_DB;
82 /* ---------------------- */
83 int get_cnid(DBD *dbd, struct cnid_dbd_rply *rply)
86 static char buf[ROOTINFO_DATALEN];
87 DBT rootinfo_key, rootinfo_data;
91 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
92 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
93 rootinfo_key.data = ROOTINFO_KEY;
94 rootinfo_key.size = ROOTINFO_KEYLEN;
97 if ((rc = dbif_get(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0)) < 0) {
98 rply->result = CNID_DBD_RES_ERR_DB;
102 /* no rootinfo key yet */
103 memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
106 memcpy(buf, (char *)rootinfo_data.data, ROOTINFO_DATALEN);
107 memcpy(&hint, buf + CNID_TYPE_OFS, sizeof(hint));
109 if (id < CNID_START - 1)
114 /* If we've hit the max CNID allowed, we return an error. CNID
115 * needs to be recycled before proceding. */
116 if (++id == CNID_INVALID) {
117 rply->result = CNID_DBD_RES_ERR_MAX;
121 rootinfo_data.data = buf;
122 rootinfo_data.size = ROOTINFO_DATALEN;
124 memcpy(buf + CNID_TYPE_OFS, &hint, sizeof(hint));
126 if (dbif_put(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0) < 0) {
127 rply->result = CNID_DBD_RES_ERR_DB;
136 int dbd_stamp(DBD *dbd)
138 DBT rootinfo_key, rootinfo_data;
140 char buf[ROOTINFO_DATALEN];
141 char stamp[CNID_DEV_LEN];
143 memset(&rootinfo_key, 0, sizeof(rootinfo_key));
144 memset(&rootinfo_data, 0, sizeof(rootinfo_data));
145 rootinfo_key.data = ROOTINFO_KEY;
146 rootinfo_key.size = ROOTINFO_KEYLEN;
148 switch (dbif_get(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0)) {
150 hint = htonl(CNID_START);
151 memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
152 rootinfo_data.data = buf;
153 rootinfo_data.size = ROOTINFO_DATALEN;
154 if (dbif_stamp(dbd, stamp, CNID_DEV_LEN) < 0) {
157 memcpy((char *)rootinfo_data.data + CNID_TYPE_OFS, &hint, sizeof(hint));
158 memcpy((char *)rootinfo_data.data + CNID_DEV_OFS, stamp, sizeof(stamp));
159 if (dbif_put(dbd, DBIF_CNID, &rootinfo_key, &rootinfo_data, 0) < 0) {
164 case 1: /* we already have one */
172 /* ------------------------ */
173 /* We need a nolookup version for `dbd` */
174 int dbd_add(DBD *dbd, struct cnid_dbd_rqst *rqst, struct cnid_dbd_rply *rply, int nolookup)
178 LOG(log_debug, logtype_cnid, "dbd_add(did:%u, '%s', dev/ino:0x%llx/0x%llx) {start}",
179 ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
181 /* See if we have an entry already and return it if yes */
183 if (dbd_lookup(dbd, rqst, rply, 0) < 0) {
184 LOG(log_debug, logtype_cnid, "dbd_add(did:%u, '%s', dev/ino:0x%llx/0x%llx): error in dbd_lookup",
185 ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
189 if (rply->result == CNID_DBD_RES_OK) {
190 /* Found it. rply->cnid is the correct CNID now. */
191 LOG(log_debug, logtype_cnid, "dbd_add: dbd_lookup success --> CNID: %u", ntohl(rply->cnid));
196 LOG(log_debug, logtype_cnid, "dbd_add(did:%u, '%s', dev/ino:0x%llx/0x%llx): {adding to database ...}",
197 ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino);
200 if (get_cnid(dbd, rply) < 0) {
201 if (rply->result == CNID_DBD_RES_ERR_MAX) {
202 LOG(log_error, logtype_cnid, "dbd_add: FATAL: CNID database has reached its limit.");
203 /* This will cause an abort/rollback if transactions are used */
206 LOG(log_error, logtype_cnid, "dbd_add: Failed to compute CNID for %s, error reading/updating Rootkey", rqst->name);
211 if (add_cnid(dbd, rqst, rply) < 0) {
212 if (rply->result == CNID_DBD_RES_ERR_DUPLCNID) {
213 LOG(log_error, logtype_cnid, "dbd_add(DID: %u/\"%s\", dev/ino 0x%llx/0x%llx): Cannot add CNID: %u",
214 ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(rply->cnid));
215 /* abort/rollback, see above */
218 LOG(log_error, logtype_cnid, "dbd_add: Failed to add CNID for %s to database", rqst->name);
222 LOG(log_debug, logtype_cnid, "dbd_add(did:%u, '%s', dev/ino:0x%llx/0x%llx): Added with CNID: %u",
223 ntohl(rqst->did), rqst->name, (unsigned long long)rqst->dev, (unsigned long long)rqst->ino, ntohl(rply->cnid));
225 rply->result = CNID_DBD_RES_OK;