]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/cnid_add.c
in db result checking replaced EAGAIN by DB_LOCK_DEADLOCK; fixed a potential transact...
[netatalk.git] / libatalk / cnid / cnid_add.c
1 /*
2  * $Id: cnid_add.c,v 1.5 2001-08-16 14:30:29 uhees Exp $
3  *
4  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5  * All Rights Reserved. See COPYRIGHT.
6  *
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.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif /* HAVE_CONFIG_H */
15
16 #include <stdio.h>
17 #include <sys/param.h>
18 #include <sys/stat.h>
19 #include <string.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif /* HAVE_UNISTD_H */
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif /* HAVE_FCNTL_H */
26 #include <errno.h>
27 #include <syslog.h>
28
29 #include <db.h>
30 #include <netatalk/endian.h>
31
32 #include <atalk/adouble.h>
33 #include <atalk/cnid.h>
34 #include <atalk/util.h>
35
36 #include "cnid_private.h"
37
38 /* add an entry to the CNID databases. we do this as a transaction
39  * to prevent messiness. */
40 static int add_cnid(CNID_private *db, DB_TXN *ptid, DBT *key, DBT *data)
41 {
42   DBT altkey, altdata;
43   DB_TXN *tid;
44
45   memset(&altkey, 0, sizeof(altkey));
46   memset(&altdata, 0, sizeof(altdata));
47   
48 retry:
49   if ((errno = txn_begin(db->dbenv, ptid, &tid,0))) {
50     return errno;
51   }
52
53   /* main database */
54   if ((errno = db->db_cnid->put(db->db_cnid, tid,
55                                 key, data, DB_NOOVERWRITE))) {
56     txn_abort(tid);
57     if (errno == DB_LOCK_DEADLOCK)
58       goto retry;
59
60     return errno;
61   }
62
63   /* dev/ino database */
64   altkey.data = data->data;
65   altkey.size = CNID_DEVINO_LEN;
66   altdata.data = key->data;
67   altdata.size = key->size;
68   if ((errno = db->db_devino->put(db->db_devino, tid,
69                                   &altkey, &altdata, 0))) {
70     txn_abort(tid);
71     if (errno == DB_LOCK_DEADLOCK)
72       goto retry;
73
74     return errno;
75   }
76
77   /* did/name database */
78   altkey.data = (char *) data->data + CNID_DEVINO_LEN;
79   altkey.size = data->size - CNID_DEVINO_LEN;
80   if ((errno = db->db_didname->put(db->db_didname, tid,
81                                    &altkey, &altdata, 0))) {
82     txn_abort(tid);
83     if (errno == DB_LOCK_DEADLOCK)
84       goto retry;
85
86     return errno;
87   }
88
89   return txn_commit(tid, 0);
90 }
91
92 /* 0 is not a valid cnid. this will do a cnid_lookup beforehand and
93    return that cnid if it exists.  */
94 cnid_t cnid_add(void *CNID, const struct stat *st,
95                 const cnid_t did, const char *name, const int len,
96                 cnid_t hint)
97 {
98   CNID_private *db;
99   DBT key, data;
100   DBT rootinfo_key, rootinfo_data;
101   DB_TXN *tid;
102   cnid_t id, save;
103
104   int debug = 0;
105
106
107   if (!(db = CNID) || !st || !name) {
108     return 0;
109   }
110
111   /* do a lookup... */
112   id = cnid_lookup(db, st, did, name, len);
113   /* ...return id if it is valid or if RootInfo is read-only. */
114   if (id || (db->flags & CNIDFLAG_DB_RO)) {
115     if (debug)
116       syslog(LOG_ERR, "cnid_add: looked up did %d, name %s as %d", did, name, id);
117     return id;
118   }
119
120   /* initialize everything */
121   memset(&key, 0, sizeof(key));
122   memset(&data, 0, sizeof(data));
123
124   /* just set hint, and the key will change. */
125   key.data = &hint;
126   key.size = sizeof(hint);
127
128   if ((data.data =
129        make_cnid_data(st, did, name, len)) == NULL) {
130     syslog(LOG_ERR, "cnid_add: path name too long.");
131     goto cleanup_err;
132   }
133   data.size = CNID_HEADER_LEN + len + 1;
134
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, NULL, &key, &data);
141     switch (errno) {
142     case DB_KEYEXIST: /* need to use RootInfo after all. */
143       break;
144     default:
145       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
146       goto cleanup_err;
147     case 0:
148       if (debug)
149         syslog(LOG_ERR, "cnid_add: used hint for did %d, name %s as %d", did, name, hint);
150       return hint;
151     }
152   }
153
154   /* Abort and retry the modification. */
155   if (0) {
156 retry:    if ((errno = txn_abort(tid)) != 0)
157               syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
158           /* FALLTHROUGH */
159   }
160
161   /* Begin the transaction. */
162   if ((errno = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
163     syslog(LOG_ERR, "cnid_add: txn_begin failed (%d)", errno);
164     goto cleanup_err;
165   }
166
167   memset(&rootinfo_key, 0, sizeof(&rootinfo_key));
168   memset(&rootinfo_data, 0, sizeof(&rootinfo_data));
169
170   /* just set hint, and the key will change. */
171   rootinfo_key.data = ROOTINFO_KEY;
172   rootinfo_key.size = ROOTINFO_KEYLEN;
173
174   /* Get the key. */
175   switch (errno = db->db_didname->get(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
176   case DB_LOCK_DEADLOCK:
177           goto retry;
178   case 0:
179           memcpy (&hint, rootinfo_data.data, sizeof(hint));
180           if (debug)
181             syslog(LOG_ERR, "cnid_add: found rootinfo for did %d, name %s as %d", did, name, hint);
182           break;
183   case DB_NOTFOUND:
184           hint = htonl(CNID_START);
185           if (debug)
186             syslog(LOG_ERR, "cnid_add: using CNID_START for did %d, name %s as %d", did, name, hint);
187           break;
188   default:
189           syslog(LOG_ERR, "cnid_add: unable to lookup rootinfo (%d)", errno);
190                   goto cleanup_abort;
191  }
192
193   /* search for a new id. we keep the first id around to check for
194    * wrap-around. NOTE: i do it this way so that we can go back and
195    * fill in holes. */
196   save = id = ntohl(hint);
197   while ((errno = add_cnid(db, tid, &key, &data))) {
198     /* don't use any of the special CNIDs */
199     if (++id < CNID_START)
200       id = CNID_START;
201
202     if ((errno != DB_KEYEXIST) || (save == id)) {
203       syslog(LOG_ERR, "cnid_add: unable to add CNID(%x)", hint);
204       hint = 0;
205       goto cleanup_abort;
206     }
207     hint = htonl(id);
208   }
209
210   /* update RootInfo with the next id. */
211   rootinfo_data.data = &hint;
212   rootinfo_data.size = sizeof(hint);
213
214   switch (errno = db->db_didname->put(db->db_didname, tid, &rootinfo_key, &rootinfo_data, 0)) {
215   case DB_LOCK_DEADLOCK:
216           goto retry;
217   case 0:
218           break;
219   default:
220           syslog(LOG_ERR, "cnid_add: unable to update rootinfo (%d)", errno);
221           goto cleanup_abort;
222   }
223
224
225 cleanup_commit:
226   /* The transaction finished, commit it. */
227   if ((errno = txn_commit(tid, 0)) != 0) {
228     syslog(LOG_ERR, "cnid_add: txn_commit failed (%d)", errno);
229     goto cleanup_err;
230   }
231
232   if (debug)
233     syslog(LOG_ERR, "cnid_add: returned cnid for did %d, name %s as %d", did, name, hint);
234   return hint;
235
236 cleanup_abort:
237   txn_abort(tid);
238
239 cleanup_err:
240   return 0;
241 }