]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2entry.c
Fix dbcache/entry lock deadlock. If dbcache lock is held, it's
[openldap] / servers / slapd / back-ldbm / id2entry.c
1 /* id2entry.c - routines to deal with the id2entry index */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8
9 #include "slap.h"
10 #include "back-ldbm.h"
11
12 int
13 id2entry_add( Backend *be, Entry *e )
14 {
15         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
16         struct dbcache  *db;
17         Datum           key, data;
18         int             len, rc, flags;
19
20         ldbm_datum_init( key );
21         ldbm_datum_init( data );
22
23         Debug( LDAP_DEBUG_TRACE, "=> id2entry_add( %lu, \"%s\" )\n", e->e_id,
24             e->e_dn, 0 );
25
26         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
27             == NULL ) {
28                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
29                     LDBM_SUFFIX, 0, 0 );
30                 return( -1 );
31         }
32
33         key.dptr = (char *) &e->e_id;
34         key.dsize = sizeof(ID);
35
36         ldap_pvt_thread_mutex_lock( &entry2str_mutex );
37         data.dptr = entry2str( e, &len, 1 );
38         data.dsize = len + 1;
39
40         /* store it */
41         flags = LDBM_REPLACE;
42         if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
43         rc = ldbm_cache_store( db, key, data, flags );
44
45         ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
46
47         ldbm_cache_close( be, db );
48         (void) cache_add_entry_lock( &li->li_cache, e, 0 );
49
50         Debug( LDAP_DEBUG_TRACE, "<= id2entry_add %d\n", rc, 0, 0 );
51
52         /* XXX should entries be born locked, i.e. apply writer lock here? */
53         return( rc );
54 }
55
56 int
57 id2entry_delete( Backend *be, Entry *e )
58 {
59         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
60         struct dbcache  *db;
61         Datum           key;
62         int             rc;
63
64         Debug(LDAP_DEBUG_TRACE, "=> id2entry_delete( %lu, \"%s\" )\n", e->e_id,
65             e->e_dn, 0 );
66
67 #ifdef LDAP_DEBUG
68         /* check for writer lock */
69         assert(ldap_pvt_thread_rdwr_writers(&e->e_rdwr) == 1);
70 #endif
71
72         ldbm_datum_init( key );
73
74         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
75                 == NULL ) {
76                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry%s\n",
77                     LDBM_SUFFIX, 0, 0 );
78                 return( -1 );
79         }
80
81         if ( cache_delete_entry( &li->li_cache, e ) != 0 ) {
82                 Debug(LDAP_DEBUG_ANY, "could not delete %lu (%s) from cache\n",
83                     e->e_id, e->e_dn, 0 );
84         }
85
86         key.dptr = (char *) &e->e_id;
87         key.dsize = sizeof(ID);
88
89         rc = ldbm_cache_delete( db, key );
90
91         ldbm_cache_close( be, db );
92
93         Debug( LDAP_DEBUG_TRACE, "<= id2entry_delete %d\n", rc, 0, 0 );
94         return( rc );
95 }
96
97 /* XXX returns entry with reader/writer lock */
98 Entry *
99 id2entry( Backend *be, ID id, int rw )
100 {
101         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
102         struct dbcache  *db;
103         Datum           key, data;
104         Entry           *e;
105
106         ldbm_datum_init( key );
107         ldbm_datum_init( data );
108
109         Debug( LDAP_DEBUG_TRACE, "=> id2entry_%s( %ld )\n",
110                 rw ? "w" : "r", id, 0 );
111
112         if ( (e = cache_find_entry_id( &li->li_cache, id, rw )) != NULL ) {
113                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s 0x%lx (cache)\n",
114                         rw ? "w" : "r", (unsigned long)e, 0 );
115                 return( e );
116         }
117
118         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_WRCREAT ))
119                 == NULL ) {
120                 Debug( LDAP_DEBUG_ANY, "Could not open id2entry%s\n",
121                     LDBM_SUFFIX, 0, 0 );
122                 return( NULL );
123         }
124
125         key.dptr = (char *) &id;
126         key.dsize = sizeof(ID);
127
128         data = ldbm_cache_fetch( db, key );
129
130         if ( data.dptr == NULL ) {
131                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) not found\n",
132                         rw ? "w" : "r", id, 0 );
133                 ldbm_cache_close( be, db );
134                 return( NULL );
135         }
136
137         e = str2entry( data.dptr );
138
139         ldbm_datum_free( db->dbc_db, data );
140         ldbm_cache_close( be, db );
141
142         if ( e == NULL ) {
143                 Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld )  (failed)\n",
144                         rw ? "w" : "r", id, 0 );
145                 return( NULL );
146         }
147
148         /* acquire required reader/writer lock */
149         if (entry_rdwr_lock(e, rw)) {
150                 /* XXX set DELETE flag?? */
151                 entry_free(e);
152                 return(NULL);
153         }
154
155         e->e_id = id;
156         (void) cache_add_entry_lock( &li->li_cache, e, 0 );
157
158         Debug( LDAP_DEBUG_TRACE, "<= id2entry_%s( %ld ) (disk)\n",
159                 rw ? "w" : "r", id, 0 );
160         return( e );
161 }
162
163 Entry *
164 id2entry_r( Backend *be, ID id )
165 {
166         return( id2entry( be, id, 0 ) );
167 }
168
169 Entry *
170 id2entry_w( Backend *be, ID id )
171 {
172         return( id2entry( be, id, 1 ) );
173 }
174