s3:lib: make use of samba_tevent_context_init()
[metze/samba/wip.git] / source3 / lib / g_lock.c
1 /*
2    Unix SMB/CIFS implementation.
3    global locks based on dbwrap and messaging
4    Copyright (C) 2009 by Volker Lendecke
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 "system/filesys.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_open.h"
24 #include "dbwrap/dbwrap_watch.h"
25 #include "g_lock.h"
26 #include "util_tdb.h"
27 #include "ctdbd_conn.h"
28 #include "../lib/util/select.h"
29 #include "../lib/util/tevent_ntstatus.h"
30 #include "system/select.h"
31 #include "messages.h"
32 #include "serverid.h"
33
34 struct g_lock_ctx {
35         struct db_context *db;
36         struct messaging_context *msg;
37 };
38
39 /*
40  * The "g_lock.tdb" file contains records, indexed by the 0-terminated
41  * lockname. The record contains an array of "struct g_lock_rec"
42  * structures.
43  */
44
45 struct g_lock_rec {
46         enum g_lock_type lock_type;
47         struct server_id pid;
48 };
49
50 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
51                                    struct messaging_context *msg)
52 {
53         struct g_lock_ctx *result;
54
55         result = talloc(mem_ctx, struct g_lock_ctx);
56         if (result == NULL) {
57                 return NULL;
58         }
59         result->msg = msg;
60
61         result->db = db_open(result, lock_path("g_lock.tdb"), 0,
62                              TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
63                              O_RDWR|O_CREAT, 0600,
64                              DBWRAP_LOCK_ORDER_2);
65         if (result->db == NULL) {
66                 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
67                 TALLOC_FREE(result);
68                 return NULL;
69         }
70         dbwrap_watch_db(result->db, msg);
71         return result;
72 }
73
74 static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
75 {
76         /*
77          * Only tested write locks so far. Very likely this routine
78          * needs to be fixed for read locks....
79          */
80         if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
81                 return false;
82         }
83         return true;
84 }
85
86 static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
87                          unsigned *pnum_locks, struct g_lock_rec **plocks)
88 {
89         unsigned num_locks;
90         struct g_lock_rec *locks;
91
92         if ((data.dsize % sizeof(struct g_lock_rec)) != 0) {
93                 DEBUG(1, ("invalid lock record length %d\n", (int)data.dsize));
94                 return false;
95         }
96         num_locks = data.dsize / sizeof(struct g_lock_rec);
97         locks = talloc_memdup(mem_ctx, data.dptr, data.dsize);
98         if (locks == NULL) {
99                 DEBUG(1, ("talloc_memdup failed\n"));
100                 return false;
101         }
102         *plocks = locks;
103         *pnum_locks = num_locks;
104         return true;
105 }
106
107 static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
108                                enum g_lock_type type)
109 {
110         TDB_DATA data;
111         unsigned i, num_locks;
112         struct g_lock_rec *locks, *tmp;
113         NTSTATUS status;
114         bool modified = false;
115
116         data = dbwrap_record_get_value(rec);
117
118         if (!g_lock_parse(talloc_tos(), data, &num_locks, &locks)) {
119                 return NT_STATUS_INTERNAL_ERROR;
120         }
121
122         for (i=0; i<num_locks; i++) {
123                 if (serverid_equal(&self, &locks[i].pid)) {
124                         status = NT_STATUS_INTERNAL_ERROR;
125                         goto done;
126                 }
127                 if (g_lock_conflicts(type, locks[i].lock_type)) {
128                         struct server_id pid = locks[i].pid;
129
130                         /*
131                          * As the serverid_exists might recurse into
132                          * the g_lock code, we use
133                          * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
134                          */
135                         pid.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
136
137                         if (serverid_exists(&pid)) {
138                                 status = NT_STATUS_LOCK_NOT_GRANTED;
139                                 goto done;
140                         }
141
142                         /*
143                          * Delete stale conflicting entry
144                          */
145                         locks[i] = locks[num_locks-1];
146                         num_locks -= 1;
147                         modified = true;
148                 }
149         }
150
151         tmp = talloc_realloc(talloc_tos(), locks, struct g_lock_rec,
152                              num_locks+1);
153         if (tmp == NULL) {
154                 status = NT_STATUS_NO_MEMORY;
155                 goto done;
156         }
157         locks = tmp;
158
159         ZERO_STRUCT(locks[num_locks]);
160         locks[num_locks].pid = self;
161         locks[num_locks].lock_type = type;
162         num_locks += 1;
163         modified = true;
164
165         status = NT_STATUS_OK;
166 done:
167         if (modified) {
168                 NTSTATUS store_status;
169
170                 data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
171                 store_status = dbwrap_record_store(rec, data, 0);
172                 if (!NT_STATUS_IS_OK(store_status)) {
173                         DEBUG(1, ("rec->store failed: %s\n",
174                                   nt_errstr(store_status)));
175                         status = store_status;
176                 }
177         }
178         TALLOC_FREE(locks);
179         return status;
180 }
181
182 struct g_lock_lock_state {
183         struct tevent_context *ev;
184         struct g_lock_ctx *ctx;
185         const char *name;
186         enum g_lock_type type;
187 };
188
189 static void g_lock_lock_retry(struct tevent_req *subreq);
190
191 struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
192                                     struct tevent_context *ev,
193                                     struct g_lock_ctx *ctx,
194                                     const char *name,
195                                     enum g_lock_type type)
196 {
197         struct tevent_req *req, *subreq;
198         struct g_lock_lock_state *state;
199         struct db_record *rec;
200         struct server_id self;
201         NTSTATUS status;
202
203         req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
204         if (req == NULL) {
205                 return NULL;
206         }
207         state->ev = ev;
208         state->ctx = ctx;
209         state->name = name;
210         state->type = type;
211
212         rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
213                                   string_term_tdb_data(state->name));
214         if (rec == NULL) {
215                 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
216                 tevent_req_nterror(req, NT_STATUS_LOCK_NOT_GRANTED);
217                 return tevent_req_post(req, ev);
218         }
219
220         self = messaging_server_id(state->ctx->msg);
221
222         status = g_lock_trylock(rec, self, state->type);
223         if (NT_STATUS_IS_OK(status)) {
224                 TALLOC_FREE(rec);
225                 tevent_req_done(req);
226                 return tevent_req_post(req, ev);
227         }
228         if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
229                 TALLOC_FREE(rec);
230                 tevent_req_nterror(req, status);
231                 return tevent_req_post(req, ev);
232         }
233         subreq = dbwrap_record_watch_send(state, state->ev, rec,
234                                           state->ctx->msg);
235         TALLOC_FREE(rec);
236         if (tevent_req_nomem(subreq, req)) {
237                 return tevent_req_post(req, ev);
238         }
239         if (!tevent_req_set_endtime(
240                     subreq, state->ev,
241                     timeval_current_ofs(5 + sys_random() % 5, 0))) {
242                 tevent_req_oom(req);
243                 return tevent_req_post(req, ev);
244         }
245         tevent_req_set_callback(subreq, g_lock_lock_retry, req);
246         return req;
247 }
248
249 static void g_lock_lock_retry(struct tevent_req *subreq)
250 {
251         struct tevent_req *req = tevent_req_callback_data(
252                 subreq, struct tevent_req);
253         struct g_lock_lock_state *state = tevent_req_data(
254                 req, struct g_lock_lock_state);
255         struct server_id self = messaging_server_id(state->ctx->msg);
256         struct db_record *rec;
257         NTSTATUS status;
258
259         status = dbwrap_record_watch_recv(subreq, talloc_tos(), &rec);
260         TALLOC_FREE(subreq);
261
262         if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
263                 rec = dbwrap_fetch_locked(
264                         state->ctx->db, talloc_tos(),
265                         string_term_tdb_data(state->name));
266                 if (rec == NULL) {
267                         status = map_nt_error_from_unix(errno);
268                 } else {
269                         status = NT_STATUS_OK;
270                 }
271         }
272
273         if (tevent_req_nterror(req, status)) {
274                 return;
275         }
276         status = g_lock_trylock(rec, self, state->type);
277         if (NT_STATUS_IS_OK(status)) {
278                 TALLOC_FREE(rec);
279                 tevent_req_done(req);
280                 return;
281         }
282         if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
283                 TALLOC_FREE(rec);
284                 tevent_req_nterror(req, status);
285                 return;
286         }
287         subreq = dbwrap_record_watch_send(state, state->ev, rec,
288                                           state->ctx->msg);
289         TALLOC_FREE(rec);
290         if (tevent_req_nomem(subreq, req)) {
291                 return;
292         }
293         if (!tevent_req_set_endtime(
294                     subreq, state->ev,
295                     timeval_current_ofs(5 + sys_random() % 5, 0))) {
296                 tevent_req_oom(req);
297                 return;
298         }
299         tevent_req_set_callback(subreq, g_lock_lock_retry, req);
300         return;
301
302 }
303
304 NTSTATUS g_lock_lock_recv(struct tevent_req *req)
305 {
306         return tevent_req_simple_recv_ntstatus(req);
307 }
308
309 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
310                      enum g_lock_type type, struct timeval timeout)
311 {
312         TALLOC_CTX *frame = talloc_stackframe();
313         struct tevent_context *ev;
314         struct tevent_req *req;
315         struct timeval end;
316         NTSTATUS status = NT_STATUS_NO_MEMORY;
317
318         ev = samba_tevent_context_init(frame);
319         if (ev == NULL) {
320                 goto fail;
321         }
322         req = g_lock_lock_send(frame, ev, ctx, name, type);
323         if (req == NULL) {
324                 goto fail;
325         }
326         end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
327         if (!tevent_req_set_endtime(req, ev, end)) {
328                 goto fail;
329         }
330         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
331                 goto fail;
332         }
333         status = g_lock_lock_recv(req);
334  fail:
335         TALLOC_FREE(frame);
336         return status;
337 }
338
339 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
340 {
341         struct server_id self = messaging_server_id(ctx->msg);
342         struct db_record *rec = NULL;
343         struct g_lock_rec *locks = NULL;
344         unsigned i, num_locks;
345         NTSTATUS status;
346         TDB_DATA value;
347
348         rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
349                                   string_term_tdb_data(name));
350         if (rec == NULL) {
351                 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
352                 status = NT_STATUS_INTERNAL_ERROR;
353                 goto done;
354         }
355
356         value = dbwrap_record_get_value(rec);
357
358         if (!g_lock_parse(talloc_tos(), value, &num_locks, &locks)) {
359                 DEBUG(10, ("g_lock_parse for %s failed\n", name));
360                 status = NT_STATUS_FILE_INVALID;
361                 goto done;
362         }
363         for (i=0; i<num_locks; i++) {
364                 if (serverid_equal(&self, &locks[i].pid)) {
365                         break;
366                 }
367         }
368         if (i == num_locks) {
369                 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
370                 status = NT_STATUS_NOT_FOUND;
371                 goto done;
372         }
373
374         locks[i] = locks[num_locks-1];
375         num_locks -= 1;
376
377         if (num_locks == 0) {
378                 status = dbwrap_record_delete(rec);
379         } else {
380                 TDB_DATA data;
381                 data = make_tdb_data((uint8_t *)locks,
382                                      sizeof(struct g_lock_rec) * num_locks);
383                 status = dbwrap_record_store(rec, data, 0);
384         }
385         if (!NT_STATUS_IS_OK(status)) {
386                 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
387                           nt_errstr(status)));
388                 goto done;
389         }
390
391         status = NT_STATUS_OK;
392 done:
393         TALLOC_FREE(rec);
394         TALLOC_FREE(locks);
395         return status;
396 }
397
398 struct g_lock_locks_state {
399         int (*fn)(const char *name, void *private_data);
400         void *private_data;
401 };
402
403 static int g_lock_locks_fn(struct db_record *rec, void *priv)
404 {
405         TDB_DATA key;
406         struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
407
408         key = dbwrap_record_get_key(rec);
409         if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
410                 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
411                 return 0;
412         }
413         return state->fn((char *)key.dptr, state->private_data);
414 }
415
416 int g_lock_locks(struct g_lock_ctx *ctx,
417                  int (*fn)(const char *name, void *private_data),
418                  void *private_data)
419 {
420         struct g_lock_locks_state state;
421         NTSTATUS status;
422         int count;
423
424         state.fn = fn;
425         state.private_data = private_data;
426
427         status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
428         if (!NT_STATUS_IS_OK(status)) {
429                 return -1;
430         } else {
431                 return count;
432         }
433 }
434
435 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
436                      int (*fn)(struct server_id pid,
437                                enum g_lock_type lock_type,
438                                void *private_data),
439                      void *private_data)
440 {
441         TDB_DATA data;
442         unsigned i, num_locks;
443         struct g_lock_rec *locks = NULL;
444         bool ret;
445         NTSTATUS status;
446
447         status = dbwrap_fetch_bystring(ctx->db, talloc_tos(), name, &data);
448         if (!NT_STATUS_IS_OK(status)) {
449                 return status;
450         }
451
452         if ((data.dsize == 0) || (data.dptr == NULL)) {
453                 return NT_STATUS_OK;
454         }
455
456         ret = g_lock_parse(talloc_tos(), data, &num_locks, &locks);
457
458         TALLOC_FREE(data.dptr);
459
460         if (!ret) {
461                 DEBUG(10, ("g_lock_parse for %s failed\n", name));
462                 return NT_STATUS_INTERNAL_ERROR;
463         }
464
465         for (i=0; i<num_locks; i++) {
466                 if (fn(locks[i].pid, locks[i].lock_type, private_data) != 0) {
467                         break;
468                 }
469         }
470         TALLOC_FREE(locks);
471         return NT_STATUS_OK;
472 }
473
474 struct g_lock_get_state {
475         bool found;
476         struct server_id *pid;
477 };
478
479 static int g_lock_get_fn(struct server_id pid, enum g_lock_type lock_type,
480                          void *priv)
481 {
482         struct g_lock_get_state *state = (struct g_lock_get_state *)priv;
483         state->found = true;
484         *state->pid = pid;
485         return 1;
486 }
487
488 NTSTATUS g_lock_get(struct g_lock_ctx *ctx, const char *name,
489                     struct server_id *pid)
490 {
491         struct g_lock_get_state state;
492         NTSTATUS status;
493
494         state.found = false;
495         state.pid = pid;
496
497         status = g_lock_dump(ctx, name, g_lock_get_fn, &state);
498         if (!NT_STATUS_IS_OK(status)) {
499                 return status;
500         }
501         if (!state.found) {
502                 return NT_STATUS_NOT_FOUND;
503         }
504         return NT_STATUS_OK;
505 }
506
507 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
508                             struct tevent_context **pev,
509                             struct messaging_context **pmsg,
510                             struct g_lock_ctx **pg_ctx)
511 {
512         struct tevent_context *ev = NULL;
513         struct messaging_context *msg = NULL;
514         struct g_lock_ctx *g_ctx = NULL;
515
516         ev = samba_tevent_context_init(mem_ctx);
517         if (ev == NULL) {
518                 d_fprintf(stderr, "ERROR: could not init event context\n");
519                 goto fail;
520         }
521         msg = messaging_init(mem_ctx, ev);
522         if (msg == NULL) {
523                 d_fprintf(stderr, "ERROR: could not init messaging context\n");
524                 goto fail;
525         }
526         g_ctx = g_lock_ctx_init(mem_ctx, msg);
527         if (g_ctx == NULL) {
528                 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
529                 goto fail;
530         }
531
532         *pev = ev;
533         *pmsg = msg;
534         *pg_ctx = g_ctx;
535         return true;
536 fail:
537         TALLOC_FREE(g_ctx);
538         TALLOC_FREE(msg);
539         TALLOC_FREE(ev);
540         return false;
541 }
542
543 NTSTATUS g_lock_do(const char *name, enum g_lock_type lock_type,
544                    struct timeval timeout,
545                    void (*fn)(void *private_data), void *private_data)
546 {
547         struct tevent_context *ev = NULL;
548         struct messaging_context *msg = NULL;
549         struct g_lock_ctx *g_ctx = NULL;
550         NTSTATUS status;
551
552         if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
553                 status = NT_STATUS_ACCESS_DENIED;
554                 goto done;
555         }
556
557         status = g_lock_lock(g_ctx, name, lock_type, timeout);
558         if (!NT_STATUS_IS_OK(status)) {
559                 goto done;
560         }
561         fn(private_data);
562         g_lock_unlock(g_ctx, name);
563
564 done:
565         TALLOC_FREE(g_ctx);
566         TALLOC_FREE(msg);
567         TALLOC_FREE(ev);
568         return status;
569 }