add670c154dee1b749c92c14bc258f96fd4cadba
[abartlet/samba.git/.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 "g_lock.h"
22
23 static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
24                                     struct server_id pid);
25
26 struct g_lock_ctx {
27         struct db_context *db;
28         struct messaging_context *msg;
29 };
30
31 /*
32  * The "g_lock.tdb" file contains records, indexed by the 0-terminated
33  * lockname. The record contains an array of "struct g_lock_rec"
34  * structures. Waiters have the lock_type with G_LOCK_PENDING or'ed.
35  */
36
37 struct g_lock_rec {
38         enum g_lock_type lock_type;
39         struct server_id pid;
40 };
41
42 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
43                                    struct messaging_context *msg)
44 {
45         struct g_lock_ctx *result;
46
47         result = talloc(mem_ctx, struct g_lock_ctx);
48         if (result == NULL) {
49                 return NULL;
50         }
51         result->msg = msg;
52
53         result->db = db_open(result, lock_path("g_lock.tdb"), 0,
54                              TDB_CLEAR_IF_FIRST, O_RDWR|O_CREAT, 0700);
55         if (result->db == NULL) {
56                 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb"));
57                 TALLOC_FREE(result);
58                 return NULL;
59         }
60         return result;
61 }
62
63 static bool g_lock_conflicts(enum g_lock_type lock_type,
64                              const struct g_lock_rec *rec)
65 {
66         enum g_lock_type rec_lock = rec->lock_type;
67
68         if ((rec_lock & G_LOCK_PENDING) != 0) {
69                 return false;
70         }
71
72         /*
73          * Only tested write locks so far. Very likely this routine
74          * needs to be fixed for read locks....
75          */
76         if ((lock_type == G_LOCK_READ) && (rec_lock == G_LOCK_READ)) {
77                 return false;
78         }
79         return true;
80 }
81
82 static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
83                          int *pnum_locks, struct g_lock_rec **plocks)
84 {
85         int i, num_locks;
86         struct g_lock_rec *locks;
87
88         if ((data.dsize % sizeof(struct g_lock_rec)) != 0) {
89                 DEBUG(1, ("invalid lock record length %d\n", (int)data.dsize));
90                 return false;
91         }
92
93         num_locks = data.dsize / sizeof(struct g_lock_rec);
94         locks = talloc_array(mem_ctx, struct g_lock_rec, num_locks);
95         if (locks == NULL) {
96                 DEBUG(1, ("talloc failed\n"));
97                 return false;
98         }
99
100         memcpy(locks, data.dptr, data.dsize);
101
102         DEBUG(10, ("locks:\n"));
103         for (i=0; i<num_locks; i++) {
104                 DEBUGADD(10, ("%s: %s %s\n",
105                               procid_str(talloc_tos(), &locks[i].pid),
106                               ((locks[i].lock_type & 1) == G_LOCK_READ) ?
107                               "read" : "write",
108                               (locks[i].lock_type & G_LOCK_PENDING) ?
109                               "(pending)" : "(owner)"));
110
111                 if (((locks[i].lock_type & G_LOCK_PENDING) == 0)
112                     && !process_exists(locks[i].pid)) {
113
114                         DEBUGADD(10, ("lock owner %s died -- discarding\n",
115                                       procid_str(talloc_tos(),
116                                                  &locks[i].pid)));
117
118                         if (i < (num_locks-1)) {
119                                 locks[i] = locks[num_locks-1];
120                         }
121                         num_locks -= 1;
122                 }
123         }
124
125         *plocks = locks;
126         *pnum_locks = num_locks;
127         return true;
128 }
129
130 static void g_lock_cleanup(int *pnum_locks, struct g_lock_rec *locks)
131 {
132         int i, num_locks;
133
134         num_locks = *pnum_locks;
135
136         DEBUG(10, ("g_lock_cleanup: %d locks\n", num_locks));
137
138         for (i=0; i<num_locks; i++) {
139                 if (process_exists(locks[i].pid)) {
140                         continue;
141                 }
142                 DEBUGADD(10, ("%s does not exist -- discarding\n",
143                               procid_str(talloc_tos(), &locks[i].pid)));
144
145                 if (i < (num_locks-1)) {
146                         locks[i] = locks[num_locks-1];
147                 }
148                 num_locks -= 1;
149         }
150         *pnum_locks = num_locks;
151         return;
152 }
153
154 static struct g_lock_rec *g_lock_addrec(TALLOC_CTX *mem_ctx,
155                                         struct g_lock_rec *locks,
156                                         int *pnum_locks,
157                                         const struct server_id pid,
158                                         enum g_lock_type lock_type)
159 {
160         struct g_lock_rec *result;
161         int num_locks = *pnum_locks;
162
163         result = talloc_realloc(mem_ctx, locks, struct g_lock_rec,
164                                 num_locks+1);
165         if (result == NULL) {
166                 return NULL;
167         }
168
169         result[num_locks].pid = pid;
170         result[num_locks].lock_type = lock_type;
171         *pnum_locks += 1;
172         return result;
173 }
174
175 static void g_lock_got_retry(struct messaging_context *msg,
176                              void *private_data,
177                              uint32_t msg_type,
178                              struct server_id server_id,
179                              DATA_BLOB *data);
180
181 static NTSTATUS g_lock_trylock(struct g_lock_ctx *ctx, const char *name,
182                                enum g_lock_type lock_type)
183 {
184         struct db_record *rec = NULL;
185         struct g_lock_rec *locks = NULL;
186         int i, num_locks;
187         struct server_id self;
188         int our_index;
189         TDB_DATA data;
190         NTSTATUS status = NT_STATUS_OK;
191         NTSTATUS store_status;
192
193 again:
194         rec = ctx->db->fetch_locked(ctx->db, talloc_tos(),
195                                     string_term_tdb_data(name));
196         if (rec == NULL) {
197                 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
198                 status = NT_STATUS_LOCK_NOT_GRANTED;
199                 goto done;
200         }
201
202         if (!g_lock_parse(talloc_tos(), rec->value, &num_locks, &locks)) {
203                 DEBUG(10, ("g_lock_parse for %s failed\n", name));
204                 status = NT_STATUS_INTERNAL_ERROR;
205                 goto done;
206         }
207
208         self = procid_self();
209         our_index = -1;
210
211         for (i=0; i<num_locks; i++) {
212                 if (procid_equal(&self, &locks[i].pid)) {
213                         if (our_index != -1) {
214                                 DEBUG(1, ("g_lock_trylock: Added ourself "
215                                           "twice!\n"));
216                                 status = NT_STATUS_INTERNAL_ERROR;
217                                 goto done;
218                         }
219                         if ((locks[i].lock_type & G_LOCK_PENDING) == 0) {
220                                 DEBUG(1, ("g_lock_trylock: Found ourself not "
221                                           "pending!\n"));
222                                 status = NT_STATUS_INTERNAL_ERROR;
223                                 goto done;
224                         }
225
226                         our_index = i;
227
228                         /* never conflict with ourself */
229                         continue;
230                 }
231                 if (g_lock_conflicts(lock_type, &locks[i])) {
232                         struct server_id pid = locks[i].pid;
233
234                         if (!process_exists(pid)) {
235                                 TALLOC_FREE(locks);
236                                 TALLOC_FREE(rec);
237                                 status = g_lock_force_unlock(ctx, name, pid);
238                                 if (!NT_STATUS_IS_OK(status)) {
239                                         DEBUG(1, ("Could not unlock dead lock "
240                                                   "holder!\n"));
241                                         goto done;
242                                 }
243                                 goto again;
244                         }
245                         lock_type |= G_LOCK_PENDING;
246                 }
247         }
248
249         if (our_index == -1) {
250                 /* First round, add ourself */
251
252                 locks = g_lock_addrec(talloc_tos(), locks, &num_locks,
253                                       self, lock_type);
254                 if (locks == NULL) {
255                         DEBUG(10, ("g_lock_addrec failed\n"));
256                         status = NT_STATUS_NO_MEMORY;
257                         goto done;
258                 }
259         } else {
260                 /*
261                  * Retry. We were pending last time. Overwrite the
262                  * stored lock_type with what we calculated, we might
263                  * have acquired the lock this time.
264                  */
265                 locks[our_index].lock_type = lock_type;
266         }
267
268         if (NT_STATUS_IS_OK(status) && ((lock_type & G_LOCK_PENDING) == 0)) {
269                 /*
270                  * Walk through the list of locks, search for dead entries
271                  */
272                 g_lock_cleanup(&num_locks, locks);
273         }
274
275         data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
276         store_status = rec->store(rec, data, 0);
277         if (!NT_STATUS_IS_OK(store_status)) {
278                 DEBUG(1, ("rec->store failed: %s\n",
279                           nt_errstr(store_status)));
280                 status = store_status;
281         }
282
283 done:
284         TALLOC_FREE(locks);
285         TALLOC_FREE(rec);
286
287         if (NT_STATUS_IS_OK(status) && (lock_type & G_LOCK_PENDING) != 0) {
288                 return STATUS_PENDING;
289         }
290
291         return NT_STATUS_OK;
292 }
293
294 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
295                      enum g_lock_type lock_type, struct timeval timeout)
296 {
297         struct tevent_timer *te = NULL;
298         NTSTATUS status;
299         bool retry = false;
300         struct timeval timeout_end;
301         struct timeval time_now;
302
303         DEBUG(10, ("Trying to acquire lock %d for %s\n", (int)lock_type,
304                    name));
305
306         if (lock_type & ~1) {
307                 DEBUG(1, ("Got invalid lock type %d for %s\n",
308                           (int)lock_type, name));
309                 return NT_STATUS_INVALID_PARAMETER;
310         }
311
312 #ifdef CLUSTER_SUPPORT
313         if (lp_clustering()) {
314                 status = ctdb_watch_us(messaging_ctdbd_connection());
315                 if (!NT_STATUS_IS_OK(status)) {
316                         DEBUG(10, ("could not register retry with ctdb: %s\n",
317                                    nt_errstr(status)));
318                         goto done;
319                 }
320         }
321 #endif
322
323         status = messaging_register(ctx->msg, &retry, MSG_DBWRAP_G_LOCK_RETRY,
324                                     g_lock_got_retry);
325         if (!NT_STATUS_IS_OK(status)) {
326                 DEBUG(10, ("messaging_register failed: %s\n",
327                            nt_errstr(status)));
328                 return status;
329         }
330
331         time_now = timeval_current();
332         timeout_end = timeval_sum(&time_now, &timeout);
333
334         while (true) {
335 #ifdef CLUSTER_SUPPORT
336                 fd_set _r_fds;
337 #endif
338                 fd_set *r_fds = NULL;
339                 int max_fd = 0;
340                 int ret;
341                 struct timeval select_timeout;
342
343                 status = g_lock_trylock(ctx, name, lock_type);
344                 if (NT_STATUS_IS_OK(status)) {
345                         DEBUG(10, ("Got lock %s\n", name));
346                         break;
347                 }
348                 if (!NT_STATUS_EQUAL(status, STATUS_PENDING)) {
349                         DEBUG(10, ("g_lock_trylock failed: %s\n",
350                                    nt_errstr(status)));
351                         break;
352                 }
353
354                 DEBUG(10, ("g_lock_trylock: Did not get lock, waiting...\n"));
355
356                 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
357                  *             !!! HACK ALERT --- FIX ME !!!
358                  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
359                  * What we really want to do here is to react to
360                  * MSG_DBWRAP_G_LOCK_RETRY messages that are either sent
361                  * by a client doing g_lock_unlock or by ourselves when
362                  * we receive a CTDB_SRVID_SAMBA_NOTIFY or
363                  * CTDB_SRVID_RECONFIGURE message from ctdbd, i.e. when
364                  * either a client holding a lock or a complete node
365                  * has died.
366                  *
367                  * Doing this properly involves calling tevent_loop_once(),
368                  * but doing this here with the main ctdbd messaging context
369                  * creates a nested event loop when g_lock_lock() is called
370                  * from the main event loop, e.g. in a tcon_and_X where the
371                  * share_info.tdb needs to be initialized and is locked by
372                  * another process, or when the remore registry is accessed
373                  * for writing and some other process already holds a lock
374                  * on the registry.tdb.
375                  *
376                  * So as a quick fix, we act a little coarsely here: we do
377                  * a select on the ctdb connection fd and when it is readable
378                  * or we get EINTR, then we retry without actually parsing
379                  * any ctdb packages or dispatching messages. This means that
380                  * we retry more often than intended by design, but this does
381                  * not harm and it is unobtrusive. When we have finished,
382                  * the main loop will pick up all the messages and ctdb
383                  * packets. The only extra twist is that we cannot use timed
384                  * events here but have to handcode a timeout.
385                  */
386
387 #ifdef CLUSTER_SUPPORT
388                 if (lp_clustering()) {
389                         struct ctdbd_connection *conn = messaging_ctdbd_connection();
390
391                         r_fds = &_r_fds;
392                         FD_ZERO(r_fds);
393                         max_fd = ctdbd_conn_get_fd(conn);
394                         FD_SET(max_fd, r_fds);
395                 }
396 #endif
397
398                 select_timeout = timeval_set(60, 0);
399
400                 ret = sys_select(max_fd + 1, r_fds, NULL, NULL,
401                                  &select_timeout);
402                 if (ret == -1) {
403                         if (errno != EINTR) {
404                                 DEBUG(1, ("error calling select: %s\n",
405                                           strerror(errno)));
406                                 status = NT_STATUS_INTERNAL_ERROR;
407                                 break;
408                         }
409                         /*
410                          * errno == EINTR:
411                          * This means a signal was received.
412                          * It might have been a MSG_DBWRAP_G_LOCK_RETRY message.
413                          * ==> retry
414                          */
415                 } else if (ret == 0) {
416                         if (timeval_expired(&timeout_end)) {
417                                 DEBUG(10, ("g_lock_lock timed out\n"));
418                                 status = NT_STATUS_LOCK_NOT_GRANTED;
419                                 break;
420                         } else {
421                                 DEBUG(10, ("select returned 0 but timeout not "
422                                            "not expired, retrying\n"));
423                         }
424                 } else if (ret != 1) {
425                         DEBUG(1, ("invalid return code of select: %d\n", ret));
426                         status = NT_STATUS_INTERNAL_ERROR;
427                         break;
428                 }
429                 /*
430                  * ret == 1:
431                  * This means ctdbd has sent us some data.
432                  * Might be a CTDB_SRVID_RECONFIGURE or a
433                  * CTDB_SRVID_SAMBA_NOTIFY message.
434                  * ==> retry
435                  */
436         }
437
438 #ifdef CLUSTER_SUPPORT
439 done:
440 #endif
441
442         if (!NT_STATUS_IS_OK(status)) {
443                 NTSTATUS unlock_status;
444
445                 unlock_status = g_lock_unlock(ctx, name);
446
447                 if (!NT_STATUS_IS_OK(unlock_status)) {
448                         DEBUG(1, ("Could not remove ourself from the locking "
449                                   "db: %s\n", nt_errstr(status)));
450                 }
451         }
452
453         messaging_deregister(ctx->msg, MSG_DBWRAP_G_LOCK_RETRY, &retry);
454         TALLOC_FREE(te);
455
456         return status;
457 }
458
459 static void g_lock_got_retry(struct messaging_context *msg,
460                              void *private_data,
461                              uint32_t msg_type,
462                              struct server_id server_id,
463                              DATA_BLOB *data)
464 {
465         bool *pretry = (bool *)private_data;
466
467         DEBUG(10, ("Got retry message from pid %s\n",
468                    procid_str(talloc_tos(), &server_id)));
469
470         *pretry = true;
471 }
472
473 static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
474                                     struct server_id pid)
475 {
476         struct db_record *rec = NULL;
477         struct g_lock_rec *locks = NULL;
478         int i, num_locks;
479         enum g_lock_type lock_type;
480         NTSTATUS status;
481
482         rec = ctx->db->fetch_locked(ctx->db, talloc_tos(),
483                                     string_term_tdb_data(name));
484         if (rec == NULL) {
485                 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
486                 status = NT_STATUS_INTERNAL_ERROR;
487                 goto done;
488         }
489
490         if (!g_lock_parse(talloc_tos(), rec->value, &num_locks, &locks)) {
491                 DEBUG(10, ("g_lock_parse for %s failed\n", name));
492                 status = NT_STATUS_INTERNAL_ERROR;
493                 goto done;
494         }
495
496         for (i=0; i<num_locks; i++) {
497                 if (procid_equal(&pid, &locks[i].pid)) {
498                         break;
499                 }
500         }
501
502         if (i == num_locks) {
503                 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
504                 status = NT_STATUS_INTERNAL_ERROR;
505                 goto done;
506         }
507
508         lock_type = locks[i].lock_type;
509
510         if (i < (num_locks-1)) {
511                 locks[i] = locks[num_locks-1];
512         }
513         num_locks -= 1;
514
515         if (num_locks == 0) {
516                 status = rec->delete_rec(rec);
517         } else {
518                 TDB_DATA data;
519                 data = make_tdb_data((uint8_t *)locks,
520                                      sizeof(struct g_lock_rec) * num_locks);
521                 status = rec->store(rec, data, 0);
522         }
523
524         if (!NT_STATUS_IS_OK(status)) {
525                 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
526                           nt_errstr(status)));
527                 goto done;
528         }
529
530         TALLOC_FREE(rec);
531
532         if ((lock_type & G_LOCK_PENDING) == 0) {
533                 int num_wakeups = 0;
534
535                 /*
536                  * We've been the lock holder. Others to retry. Don't
537                  * tell all others to avoid a thundering herd. In case
538                  * this leads to a complete stall because we miss some
539                  * processes, the loop in g_lock_lock tries at least
540                  * once a minute.
541                  */
542
543                 for (i=0; i<num_locks; i++) {
544                         if ((locks[i].lock_type & G_LOCK_PENDING) == 0) {
545                                 continue;
546                         }
547                         if (!process_exists(locks[i].pid)) {
548                                 continue;
549                         }
550
551                         /*
552                          * Ping all waiters to retry
553                          */
554                         status = messaging_send(ctx->msg, locks[i].pid,
555                                                 MSG_DBWRAP_G_LOCK_RETRY,
556                                                 &data_blob_null);
557                         if (!NT_STATUS_IS_OK(status)) {
558                                 DEBUG(1, ("sending retry to %s failed: %s\n",
559                                           procid_str(talloc_tos(),
560                                                      &locks[i].pid),
561                                           nt_errstr(status)));
562                         } else {
563                                 num_wakeups += 1;
564                         }
565                         if (num_wakeups > 5) {
566                                 break;
567                         }
568                 }
569         }
570 done:
571         /*
572          * For the error path, TALLOC_FREE(rec) as well. In the good
573          * path we have already freed it.
574          */
575         TALLOC_FREE(rec);
576
577         TALLOC_FREE(locks);
578         return status;
579 }
580
581 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
582 {
583         NTSTATUS status;
584
585         status = g_lock_force_unlock(ctx, name, procid_self());
586
587 #ifdef CLUSTER_SUPPORT
588         if (lp_clustering()) {
589                 ctdb_unwatch(messaging_ctdbd_connection());
590         }
591 #endif
592         return status;
593 }
594
595 struct g_lock_locks_state {
596         int (*fn)(const char *name, void *private_data);
597         void *private_data;
598 };
599
600 static int g_lock_locks_fn(struct db_record *rec, void *priv)
601 {
602         struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
603
604         if ((rec->key.dsize == 0) || (rec->key.dptr[rec->key.dsize-1] != 0)) {
605                 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
606                 return 0;
607         }
608         return state->fn((char *)rec->key.dptr, state->private_data);
609 }
610
611 int g_lock_locks(struct g_lock_ctx *ctx,
612                  int (*fn)(const char *name, void *private_data),
613                  void *private_data)
614 {
615         struct g_lock_locks_state state;
616
617         state.fn = fn;
618         state.private_data = private_data;
619
620         return ctx->db->traverse_read(ctx->db, g_lock_locks_fn, &state);
621 }
622
623 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
624                      int (*fn)(struct server_id pid,
625                                enum g_lock_type lock_type,
626                                void *private_data),
627                      void *private_data)
628 {
629         TDB_DATA data;
630         int i, num_locks;
631         struct g_lock_rec *locks = NULL;
632         bool ret;
633
634         if (ctx->db->fetch(ctx->db, talloc_tos(), string_term_tdb_data(name),
635                            &data) != 0) {
636                 return NT_STATUS_NOT_FOUND;
637         }
638
639         if ((data.dsize == 0) || (data.dptr == NULL)) {
640                 return NT_STATUS_OK;
641         }
642
643         ret = g_lock_parse(talloc_tos(), data, &num_locks, &locks);
644
645         TALLOC_FREE(data.dptr);
646
647         if (!ret) {
648                 DEBUG(10, ("g_lock_parse for %s failed\n", name));
649                 return NT_STATUS_INTERNAL_ERROR;
650         }
651
652         for (i=0; i<num_locks; i++) {
653                 if (fn(locks[i].pid, locks[i].lock_type, private_data) != 0) {
654                         break;
655                 }
656         }
657         TALLOC_FREE(locks);
658         return NT_STATUS_OK;
659 }
660
661 struct g_lock_get_state {
662         bool found;
663         struct server_id *pid;
664 };
665
666 static int g_lock_get_fn(struct server_id pid, enum g_lock_type lock_type,
667                          void *priv)
668 {
669         struct g_lock_get_state *state = (struct g_lock_get_state *)priv;
670
671         if ((lock_type & G_LOCK_PENDING) != 0) {
672                 return 0;
673         }
674
675         state->found = true;
676         *state->pid = pid;
677         return 1;
678 }
679
680 NTSTATUS g_lock_get(struct g_lock_ctx *ctx, const char *name,
681                     struct server_id *pid)
682 {
683         struct g_lock_get_state state;
684         NTSTATUS status;
685
686         state.found = false;
687         state.pid = pid;
688
689         status = g_lock_dump(ctx, name, g_lock_get_fn, &state);
690         if (!NT_STATUS_IS_OK(status)) {
691                 return status;
692         }
693         if (!state.found) {
694                 return NT_STATUS_NOT_FOUND;
695         }
696         return NT_STATUS_OK;
697 }