48fa7961986f2c57d4664d02f2e4fd67a164d4dc
[sahlberg/ctdb.git] / server / ctdb_lockwait.c
1 /* 
2    wait for a tdb chain lock
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/tevent/tevent.h"
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "db_wrap.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/util/dlinklist.h"
27 #include "../include/ctdb_private.h"
28
29
30 struct lockwait_handle {
31         struct lockwait_handle *next, *prev;
32         struct ctdb_context *ctdb;
33         struct ctdb_db_context *ctdb_db;
34         struct fd_event *fde;
35         int fd[2];
36         pid_t child;
37         void *private_data;
38         void (*callback)(void *);
39         TDB_DATA key;
40         struct timeval start_time;
41 };
42
43 /* If we managed to obtain a lock, find any overflow records which wanted the
44  * same one and do all the callbacks at once. */
45 static void do_overflow(struct ctdb_db_context *ctdb_db,
46                         TDB_DATA key)
47 {
48         struct lockwait_handle *i, *next;
49         TALLOC_CTX *tmp_ctx = talloc_new(ctdb_db);
50
51         for (i = ctdb_db->lockwait_overflow; i; i = next) {
52                 /* Careful: destructor removes it from list! */
53                 next = i->next;
54                 if (key.dsize == i->key.dsize
55                     && memcmp(key.dptr, i->key.dptr, key.dsize) == 0) {
56                         /* Callback might free them, so reparent. */
57                         talloc_steal(tmp_ctx, i);
58                         i->callback(i->private_data);
59                 }
60         }
61
62         /* This will free them if callback didn't. */
63         talloc_free(tmp_ctx);
64
65         /* Remove one from the overflow queue if there is one. */
66         if (ctdb_db->lockwait_overflow) {
67                 i = ctdb->lockwait_overflow;
68                 ctdb_lockwait(ctdb_db, i->key, i->callback, i->private_data);
69                 talloc_free(i);
70         }
71 }
72
73 static void lockwait_handler(struct event_context *ev, struct fd_event *fde, 
74                              uint16_t flags, void *private_data)
75 {
76         struct lockwait_handle *h = talloc_get_type(private_data, 
77                                                      struct lockwait_handle);
78         void (*callback)(void *) = h->callback;
79         void *p = h->private_data;
80         pid_t child = h->child;
81         TDB_DATA key = h->key;
82         struct tdb_context *tdb = h->ctdb_db->ltdb->tdb;
83         TALLOC_CTX *tmp_ctx = talloc_new(ev);
84
85         key.dptr = talloc_memdup(tmp_ctx, key.dptr, key.dsize);
86         h->ctdb_db->pending_requests--;
87
88         talloc_set_destructor(h, NULL);
89         CTDB_UPDATE_LATENCY(h->ctdb, h->ctdb_db, "lockwait", lockwait_latency, h->start_time);
90         CTDB_DECREMENT_STAT(h->ctdb, pending_lockwait_calls);
91
92         /* the handle needs to go away when the context is gone - when
93            the handle goes away this implicitly closes the pipe, which
94            kills the child holding the lock */
95         talloc_steal(tmp_ctx, h);
96
97         if (h->ctdb->flags & CTDB_FLAG_TORTURE) {
98                 if (tdb_chainlock_nonblock(tdb, key) == 0) {
99                         ctdb_fatal(h->ctdb, "got chain lock while lockwait child active");
100                 }
101         }
102
103         tdb_chainlock_mark(tdb, key);
104         callback(p);
105         if (h->ctdb_db->lockwait_overflow) {
106                 do_overflow(h->ctdb_db, key);
107         }
108         tdb_chainlock_unmark(tdb, key);
109
110         kill(child, SIGKILL);
111         talloc_free(tmp_ctx);
112 }
113
114 static int lockwait_destructor(struct lockwait_handle *h)
115 {
116         CTDB_DECREMENT_STAT(h->ctdb, pending_lockwait_calls);
117         kill(h->child, SIGKILL);
118         h->ctdb_db->pending_requests--;
119         return 0;
120 }
121
122 static int overflow_lockwait_destructor(struct lockwait_handle *h)
123 {
124         CTDB_DECREMENT_STAT(h->ctdb, pending_lockwait_calls);
125         DLIST_REMOVE(h->ctdb_db->lockwait_overflow, h);
126         return 0;
127 }
128
129 /*
130   setup a non-blocking chainlock on a tdb record. If this function
131   returns NULL then it could not get the chainlock. Otherwise it
132   returns a opaque handle, and will call callback() once it has
133   managed to get the chainlock. You can cancel it by using talloc_free
134   on the returned handle.
135
136   It is the callers responsibility to unlock the chainlock once
137   acquired
138  */
139 struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
140                                       TDB_DATA key,
141                                       void (*callback)(void *private_data),
142                                       void *private_data)
143 {
144         struct lockwait_handle *result;
145         int ret;
146         pid_t parent = getpid();
147
148         CTDB_INCREMENT_STAT(ctdb_db->ctdb, lockwait_calls);
149         CTDB_INCREMENT_STAT(ctdb_db->ctdb, pending_lockwait_calls);
150
151         if (!(result = talloc_zero(private_data, struct lockwait_handle))) {
152                 CTDB_DECREMENT_STAT(ctdb_db->ctdb, pending_lockwait_calls);
153                 return NULL;
154         }
155
156         result->callback = callback;
157         result->private_data = private_data;
158         result->ctdb = ctdb_db->ctdb;
159         result->ctdb_db = ctdb_db;
160         result->key = key;
161
162         /* Don't fire off too many children at once! */
163         if (ctdb_db->pending_requests > 200) {
164                 DLIST_ADD_END(ctdb_db->lockwait_overflow, result, NULL);
165                 talloc_set_destructor(result, overflow_lockwait_destructor);
166                 DEBUG(DEBUG_DEBUG, (__location__ " Created overflow for %s\n",
167                                     ctdb_db->db_name));
168                 return result;
169         }
170
171         ret = pipe(result->fd);
172
173         if (ret != 0) {
174                 talloc_free(result);
175                 CTDB_DECREMENT_STAT(ctdb_db->ctdb, pending_lockwait_calls);
176                 return NULL;
177         }
178
179         result->child = ctdb_fork(ctdb_db->ctdb);
180
181         if (result->child == (pid_t)-1) {
182                 close(result->fd[0]);
183                 close(result->fd[1]);
184                 talloc_free(result);
185                 CTDB_DECREMENT_STAT(ctdb_db->ctdb, pending_lockwait_calls);
186                 return NULL;
187         }
188
189         if (result->child == 0) {
190                 char c = 0;
191                 close(result->fd[0]);
192                 debug_extra = talloc_asprintf(NULL, "chainlock-%s:", ctdb_db->db_name);
193                 tdb_chainlock(ctdb_db->ltdb->tdb, key);
194                 write(result->fd[1], &c, 1);
195                 /* make sure we die when our parent dies */
196                 while (kill(parent, 0) == 0 || errno != ESRCH) {
197                         sleep(5);
198                 }
199                 _exit(0);
200         }
201
202         close(result->fd[1]);
203         set_close_on_exec(result->fd[0]);
204
205         DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child lockwait process\n", result->fd[0]));
206
207         ctdb_db->pending_requests++;
208         talloc_set_destructor(result, lockwait_destructor);
209
210         result->fde = event_add_fd(ctdb_db->ctdb->ev, result, result->fd[0],
211                                    EVENT_FD_READ, lockwait_handler,
212                                    (void *)result);
213         if (result->fde == NULL) {
214                 talloc_free(result);
215                 CTDB_DECREMENT_STAT(ctdb_db->ctdb, pending_lockwait_calls);
216                 return NULL;
217         }
218         tevent_fd_set_auto_close(result->fde);
219
220         result->start_time = timeval_current();
221         return result;
222 }