]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb1_lock.c
2252e4791b91c9d8d2afb392b9fd3c87f86d9056
[ccan] / ccan / tdb2 / tdb1_lock.c
1  /*
2    Unix SMB/CIFS implementation.
3
4    trivial database library
5
6    Copyright (C) Andrew Tridgell              1999-2005
7    Copyright (C) Paul `Rusty' Russell              2000
8    Copyright (C) Jeremy Allison                    2000-2003
9
10      ** NOTE! The following LGPL license applies to the tdb
11      ** library. This does NOT imply that all of Samba is released
12      ** under the LGPL
13
14    This library is free software; you can redistribute it and/or
15    modify it under the terms of the GNU Lesser General Public
16    License as published by the Free Software Foundation; either
17    version 3 of the License, or (at your option) any later version.
18
19    This library is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    Lesser General Public License for more details.
23
24    You should have received a copy of the GNU Lesser General Public
25    License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include "tdb1_private.h"
29
30 /* list -1 is the alloc list, otherwise a hash chain. */
31 static tdb1_off_t lock_offset(int list)
32 {
33         return TDB1_FREELIST_TOP + 4*list;
34 }
35
36 /* a byte range locking function - return 0 on success
37    this functions locks/unlocks 1 byte at the specified offset.
38
39    On error, errno is also set so that errors are passed back properly
40    through tdb1_open().
41
42    note that a len of zero means lock to end of file
43 */
44 int tdb1_brlock(struct tdb1_context *tdb,
45                int rw_type, tdb1_off_t offset, size_t len,
46                enum tdb_lock_flags flags)
47 {
48         enum TDB_ERROR ecode = tdb_brlock(tdb, rw_type, offset, len, flags
49                                           | TDB_LOCK_NOCHECK);
50         if (ecode == TDB_SUCCESS)
51                 return 0;
52         tdb->last_error = ecode;
53         return -1;
54 }
55
56 int tdb1_brunlock(struct tdb1_context *tdb,
57                  int rw_type, tdb1_off_t offset, size_t len)
58 {
59         enum TDB_ERROR ecode = tdb_brunlock(tdb, rw_type, offset, len);
60         if (ecode == TDB_SUCCESS)
61                 return 0;
62         tdb->last_error = ecode;
63         return -1;
64 }
65
66 int tdb1_allrecord_upgrade(struct tdb1_context *tdb)
67 {
68         enum TDB_ERROR ecode = tdb_allrecord_upgrade(tdb, TDB1_FREELIST_TOP);
69         if (ecode == TDB_SUCCESS)
70                 return 0;
71         tdb->last_error = ecode;
72         return -1;
73 }
74
75 static struct tdb_lock *tdb1_find_nestlock(struct tdb1_context *tdb,
76                                            tdb1_off_t offset)
77 {
78         unsigned int i;
79
80         for (i=0; i<tdb->file->num_lockrecs; i++) {
81                 if (tdb->file->lockrecs[i].off == offset) {
82                         return &tdb->file->lockrecs[i];
83                 }
84         }
85         return NULL;
86 }
87
88 /* lock an offset in the database. */
89 int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
90                   enum tdb_lock_flags flags)
91 {
92         enum TDB_ERROR ecode;
93
94         if (offset >= lock_offset(tdb->header.hash_size)) {
95                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
96                                         "tdb1_lock: invalid offset %u for"
97                                         " ltype=%d",
98                                         offset, ltype);
99                 return -1;
100         }
101
102         ecode = tdb_nest_lock(tdb, offset, ltype, flags | TDB_LOCK_NOCHECK);
103         if (unlikely(ecode != TDB_SUCCESS)) {
104                 tdb->last_error = ecode;
105                 return -1;
106         }
107         return 0;
108 }
109
110 static int tdb1_lock_and_recover(struct tdb1_context *tdb)
111 {
112         int ret;
113
114         /* We need to match locking order in transaction commit. */
115         if (tdb1_brlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0,
116                         TDB_LOCK_WAIT|TDB_LOCK_NOCHECK)) {
117                 return -1;
118         }
119
120         if (tdb1_brlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1,
121                         TDB_LOCK_WAIT|TDB_LOCK_NOCHECK)) {
122                 tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
123                 return -1;
124         }
125
126         ret = tdb1_transaction_recover(tdb);
127
128         tdb1_brunlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1);
129         tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
130
131         return ret;
132 }
133
134 static bool have_data_locks(const struct tdb1_context *tdb)
135 {
136         unsigned int i;
137
138         for (i = 0; i < tdb->file->num_lockrecs; i++) {
139                 if (tdb->file->lockrecs[i].off >= lock_offset(-1))
140                         return true;
141         }
142         return false;
143 }
144
145 static int tdb1_lock_list(struct tdb1_context *tdb, int list, int ltype,
146                          enum tdb_lock_flags waitflag)
147 {
148         int ret;
149         bool check = false;
150
151         /* a allrecord lock allows us to avoid per chain locks */
152         if (tdb->file->allrecord_lock.count &&
153             (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
154                 return 0;
155         }
156
157         if (tdb->file->allrecord_lock.count) {
158                 tdb->last_error = TDB_ERR_LOCK;
159                 ret = -1;
160         } else {
161                 /* Only check when we grab first data lock. */
162                 check = !have_data_locks(tdb);
163                 ret = tdb1_nest_lock(tdb, lock_offset(list), ltype, waitflag);
164
165                 if (ret == 0 && check && tdb1_needs_recovery(tdb)) {
166                         tdb1_nest_unlock(tdb, lock_offset(list), ltype);
167
168                         if (tdb1_lock_and_recover(tdb) == -1) {
169                                 return -1;
170                         }
171                         return tdb1_lock_list(tdb, list, ltype, waitflag);
172                 }
173         }
174         return ret;
175 }
176
177 /* lock a list in the database. list -1 is the alloc list */
178 int tdb1_lock(struct tdb1_context *tdb, int list, int ltype)
179 {
180         int ret;
181
182         ret = tdb1_lock_list(tdb, list, ltype, TDB_LOCK_WAIT);
183         if (ret) {
184                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
185                            "tdb1_lock failed on list %d "
186                            "ltype=%d (%s)",  list, ltype, strerror(errno));
187         }
188         return ret;
189 }
190
191 int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype)
192 {
193         enum TDB_ERROR ecode;
194
195         /* Sanity checks */
196         if (offset >= lock_offset(tdb->header.hash_size)) {
197                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
198                                         "tdb1_unlock: offset %u invalid (%d)",
199                                         offset, tdb->header.hash_size);
200                 return -1;
201         }
202
203         ecode = tdb_nest_unlock(tdb, offset, ltype);
204         if (unlikely(ecode != TDB_SUCCESS)) {
205                 tdb->last_error = ecode;
206                 return -1;
207         }
208         return 0;
209 }
210
211 int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype)
212 {
213         /* a global lock allows us to avoid per chain locks */
214         if (tdb->file->allrecord_lock.count &&
215             (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
216                 return 0;
217         }
218
219         if (tdb->file->allrecord_lock.count) {
220                 tdb->last_error = TDB_ERR_LOCK;
221                 return -1;
222         }
223
224         return tdb1_nest_unlock(tdb, lock_offset(list), ltype);
225 }
226
227 /*
228   get the transaction lock
229  */
230 int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
231                          enum tdb_lock_flags lockflags)
232 {
233         return tdb1_nest_lock(tdb, TDB1_TRANSACTION_LOCK, ltype, lockflags);
234 }
235
236 /*
237   release the transaction lock
238  */
239 int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype)
240 {
241         return tdb1_nest_unlock(tdb, TDB1_TRANSACTION_LOCK, ltype);
242 }
243
244 /* lock/unlock entire database.  It can only be upgradable if you have some
245  * other way of guaranteeing exclusivity (ie. transaction write lock).
246  * We do the locking gradually to avoid being starved by smaller locks. */
247 int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
248                        enum tdb_lock_flags flags, bool upgradable)
249 {
250         enum TDB_ERROR ecode;
251
252         /* tdb_lock_gradual() doesn't know about tdb->traverse_read. */
253         if (tdb->traverse_read && !(tdb->flags & TDB_NOLOCK)) {
254                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
255                                              TDB_LOG_USE_ERROR,
256                                              "tdb1_allrecord_lock during"
257                                              " tdb1_read_traverse");
258                 return -1;
259         }
260
261         if (tdb->file->allrecord_lock.count
262             && tdb->file->allrecord_lock.ltype == ltype) {
263                 tdb->file->allrecord_lock.count++;
264                 return 0;
265         }
266
267         if (tdb1_have_extra_locks(tdb)) {
268                 /* can't combine global and chain locks */
269                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
270                                              TDB_LOG_USE_ERROR,
271                                              "tdb1_allrecord_lock holding"
272                                              " other locks");
273                 return -1;
274         }
275
276         if (upgradable && ltype != F_RDLCK) {
277                 /* tdb error: you can't upgrade a write lock! */
278                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
279                                              TDB_LOG_ERROR,
280                                              "tdb1_allrecord_lock cannot"
281                                              " have upgradable write lock");
282                 return -1;
283         }
284
285         /* We cover two kinds of locks:
286          * 1) Normal chain locks.  Taken for almost all operations.
287          * 3) Individual records locks.  Taken after normal or free
288          *    chain locks.
289          *
290          * It is (1) which cause the starvation problem, so we're only
291          * gradual for that. */
292         ecode = tdb_lock_gradual(tdb, ltype, flags | TDB_LOCK_NOCHECK,
293                                  TDB1_FREELIST_TOP, tdb->header.hash_size * 4);
294         if (ecode != TDB_SUCCESS) {
295                 tdb->last_error = ecode;
296                 return -1;
297         }
298
299         /* Grab individual record locks. */
300         if (tdb1_brlock(tdb, ltype, lock_offset(tdb->header.hash_size), 0,
301                        flags) == -1) {
302                 tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP,
303                              tdb->header.hash_size * 4);
304                 return -1;
305         }
306
307         /* FIXME: Temporary cast. */
308         tdb->file->allrecord_lock.owner = (void *)(struct tdb1_context *)tdb;
309         tdb->file->allrecord_lock.count = 1;
310         /* If it's upgradable, it's actually exclusive so we can treat
311          * it as a write lock. */
312         tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
313         tdb->file->allrecord_lock.off = upgradable;
314
315         if (tdb1_needs_recovery(tdb)) {
316                 tdb1_allrecord_unlock(tdb, ltype);
317                 if (tdb1_lock_and_recover(tdb) == -1) {
318                         return -1;
319                 }
320                 return tdb1_allrecord_lock(tdb, ltype, flags, upgradable);
321         }
322
323         return 0;
324 }
325
326
327
328 /* unlock entire db */
329 int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype)
330 {
331         /* There are no locks on read-only dbs */
332         if (tdb->read_only || tdb->traverse_read) {
333                 tdb->last_error = TDB_ERR_LOCK;
334                 return -1;
335         }
336
337         if (tdb->file->allrecord_lock.count == 0) {
338                 tdb->last_error = TDB_ERR_LOCK;
339                 return -1;
340         }
341
342         /* Upgradable locks are marked as write locks. */
343         if (tdb->file->allrecord_lock.ltype != ltype
344             && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
345                 tdb->last_error = TDB_ERR_LOCK;
346                 return -1;
347         }
348
349         if (tdb->file->allrecord_lock.count > 1) {
350                 tdb->file->allrecord_lock.count--;
351                 return 0;
352         }
353
354         if (tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP, 0)) {
355                 tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
356                            "tdb1_unlockall failed (%s)", strerror(errno));
357                 return -1;
358         }
359
360         tdb->file->allrecord_lock.count = 0;
361         tdb->file->allrecord_lock.ltype = 0;
362
363         return 0;
364 }
365
366 /* lock entire database with write lock */
367 int tdb1_lockall(struct tdb1_context *tdb)
368 {
369         return tdb1_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
370 }
371
372 /* unlock entire database with write lock */
373 int tdb1_unlockall(struct tdb1_context *tdb)
374 {
375         return tdb1_allrecord_unlock(tdb, F_WRLCK);
376 }
377
378 /* lock entire database with read lock */
379 int tdb1_lockall_read(struct tdb1_context *tdb)
380 {
381         return tdb1_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
382 }
383
384 /* unlock entire database with read lock */
385 int tdb1_unlockall_read(struct tdb1_context *tdb)
386 {
387         return tdb1_allrecord_unlock(tdb, F_RDLCK);
388 }
389
390 /* lock/unlock one hash chain. This is meant to be used to reduce
391    contention - it cannot guarantee how many records will be locked */
392 int tdb1_chainlock(struct tdb1_context *tdb, TDB_DATA key)
393 {
394         int ret = tdb1_lock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_WRLCK);
395         return ret;
396 }
397
398 int tdb1_chainunlock(struct tdb1_context *tdb, TDB_DATA key)
399 {
400         return tdb1_unlock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_WRLCK);
401 }
402
403 int tdb1_chainlock_read(struct tdb1_context *tdb, TDB_DATA key)
404 {
405         int ret;
406         ret = tdb1_lock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_RDLCK);
407         return ret;
408 }
409
410 int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB_DATA key)
411 {
412         return tdb1_unlock(tdb, TDB1_BUCKET(tdb->hash_fn(&key)), F_RDLCK);
413 }
414
415 /* record lock stops delete underneath */
416 int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off)
417 {
418         if (tdb->file->allrecord_lock.count) {
419                 return 0;
420         }
421         return off ? tdb1_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
422 }
423
424 /*
425   Write locks override our own fcntl readlocks, so check it here.
426   Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
427   an error to fail to get the lock here.
428 */
429 int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off)
430 {
431         struct tdb1_traverse_lock *i;
432         for (i = &tdb->travlocks; i; i = i->next)
433                 if (i->off == off)
434                         return -1;
435         if (tdb->file->allrecord_lock.count) {
436                 if (tdb->file->allrecord_lock.ltype == F_WRLCK) {
437                         return 0;
438                 }
439                 return -1;
440         }
441         return tdb1_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
442 }
443
444 int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off)
445 {
446         if (tdb->file->allrecord_lock.count) {
447                 return 0;
448         }
449         return tdb1_brunlock(tdb, F_WRLCK, off, 1);
450 }
451
452 /* fcntl locks don't stack: avoid unlocking someone else's */
453 int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off)
454 {
455         struct tdb1_traverse_lock *i;
456         uint32_t count = 0;
457
458         if (tdb->file->allrecord_lock.count) {
459                 return 0;
460         }
461
462         if (off == 0)
463                 return 0;
464         for (i = &tdb->travlocks; i; i = i->next)
465                 if (i->off == off)
466                         count++;
467         return (count == 1 ? tdb1_brunlock(tdb, F_RDLCK, off, 1) : 0);
468 }
469
470 bool tdb1_have_extra_locks(struct tdb1_context *tdb)
471 {
472         unsigned int extra = tdb->file->num_lockrecs;
473
474         /* A transaction holds the lock for all records. */
475         if (!tdb->transaction && tdb->file->allrecord_lock.count) {
476                 return true;
477         }
478
479         /* We always hold the active lock if CLEAR_IF_FIRST. */
480         if (tdb1_find_nestlock(tdb, TDB1_ACTIVE_LOCK)) {
481                 extra--;
482         }
483
484         /* In a transaction, we expect to hold the transaction lock */
485         if (tdb->transaction
486             && tdb1_find_nestlock(tdb, TDB1_TRANSACTION_LOCK)) {
487                 extra--;
488         }
489
490         return extra;
491 }
492
493 /* The transaction code uses this to remove all locks. */
494 void tdb1_release_transaction_locks(struct tdb1_context *tdb)
495 {
496         unsigned int i, active = 0;
497
498         if (tdb->file->allrecord_lock.count != 0) {
499                 tdb1_brunlock(tdb, tdb->file->allrecord_lock.ltype, TDB1_FREELIST_TOP, 0);
500                 tdb->file->allrecord_lock.count = 0;
501         }
502
503         for (i=0;i<tdb->file->num_lockrecs;i++) {
504                 struct tdb_lock *lck = &tdb->file->lockrecs[i];
505
506                 /* Don't release the active lock!  Copy it to first entry. */
507                 if (lck->off == TDB1_ACTIVE_LOCK) {
508                         tdb->file->lockrecs[active++] = *lck;
509                 } else {
510                         tdb1_brunlock(tdb, lck->ltype, lck->off, 1);
511                 }
512         }
513         tdb->file->num_lockrecs = active;
514         if (tdb->file->num_lockrecs == 0) {
515                 SAFE_FREE(tdb->file->lockrecs);
516         }
517 }