lock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. Unix SMB/CIFS implementation.
  3. trivial database library
  4. Copyright (C) Andrew Tridgell 1999-2005
  5. Copyright (C) Paul `Rusty' Russell 2000
  6. Copyright (C) Jeremy Allison 2000-2003
  7. ** NOTE! The following LGPL license applies to the tdb
  8. ** library. This does NOT imply that all of Samba is released
  9. ** under the LGPL
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 3 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "tdb_private.h"
  22. void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *ptr)
  23. {
  24. tdb->interrupt_sig_ptr = ptr;
  25. }
  26. static int fcntl_lock(struct tdb_context *tdb,
  27. int rw, off_t off, off_t len, bool waitflag)
  28. {
  29. struct flock fl;
  30. fl.l_type = rw;
  31. fl.l_whence = SEEK_SET;
  32. fl.l_start = off;
  33. fl.l_len = len;
  34. fl.l_pid = 0;
  35. if (waitflag)
  36. return fcntl(tdb->fd, F_SETLKW, &fl);
  37. else
  38. return fcntl(tdb->fd, F_SETLK, &fl);
  39. }
  40. static int fcntl_unlock(struct tdb_context *tdb, int rw, off_t off, off_t len)
  41. {
  42. struct flock fl;
  43. #if 0 /* Check they matched up locks and unlocks correctly. */
  44. char line[80];
  45. FILE *locks;
  46. bool found = false;
  47. locks = fopen("/proc/locks", "r");
  48. while (fgets(line, 80, locks)) {
  49. char *p;
  50. int type, start, l;
  51. /* eg. 1: FLOCK ADVISORY WRITE 2440 08:01:2180826 0 EOF */
  52. p = strchr(line, ':') + 1;
  53. if (strncmp(p, " POSIX ADVISORY ", strlen(" POSIX ADVISORY ")))
  54. continue;
  55. p += strlen(" FLOCK ADVISORY ");
  56. if (strncmp(p, "READ ", strlen("READ ")) == 0)
  57. type = F_RDLCK;
  58. else if (strncmp(p, "WRITE ", strlen("WRITE ")) == 0)
  59. type = F_WRLCK;
  60. else
  61. abort();
  62. p += 6;
  63. if (atoi(p) != getpid())
  64. continue;
  65. p = strchr(strchr(p, ' ') + 1, ' ') + 1;
  66. start = atoi(p);
  67. p = strchr(p, ' ') + 1;
  68. if (strncmp(p, "EOF", 3) == 0)
  69. l = 0;
  70. else
  71. l = atoi(p) - start + 1;
  72. if (off == start) {
  73. if (len != l) {
  74. fprintf(stderr, "Len %u should be %u: %s",
  75. (int)len, l, line);
  76. abort();
  77. }
  78. if (type != rw) {
  79. fprintf(stderr, "Type %s wrong: %s",
  80. rw == F_RDLCK ? "READ" : "WRITE", line);
  81. abort();
  82. }
  83. found = true;
  84. break;
  85. }
  86. }
  87. if (!found) {
  88. fprintf(stderr, "Unlock on %u@%u not found!\n",
  89. (int)off, (int)len);
  90. abort();
  91. }
  92. fclose(locks);
  93. #endif
  94. fl.l_type = F_UNLCK;
  95. fl.l_whence = SEEK_SET;
  96. fl.l_start = off;
  97. fl.l_len = len;
  98. fl.l_pid = 0;
  99. return fcntl(tdb->fd, F_SETLKW, &fl);
  100. }
  101. /* a byte range locking function - return 0 on success
  102. this functions locks/unlocks 1 byte at the specified offset.
  103. On error, errno is also set so that errors are passed back properly
  104. through tdb_open().
  105. note that a len of zero means lock to end of file
  106. */
  107. int tdb_brlock(struct tdb_context *tdb,
  108. int rw_type, tdb_off_t offset, size_t len,
  109. enum tdb_lock_flags flags)
  110. {
  111. int ret;
  112. if (tdb->flags & TDB_NOLOCK) {
  113. return 0;
  114. }
  115. if (flags & TDB_LOCK_MARK_ONLY) {
  116. return 0;
  117. }
  118. if ((rw_type == F_WRLCK) && (tdb->read_only || tdb->traverse_read)) {
  119. tdb->ecode = TDB_ERR_RDONLY;
  120. return -1;
  121. }
  122. do {
  123. ret = fcntl_lock(tdb, rw_type, offset, len,
  124. flags & TDB_LOCK_WAIT);
  125. /* Check for a sigalarm break. */
  126. if (ret == -1 && errno == EINTR &&
  127. tdb->interrupt_sig_ptr &&
  128. *tdb->interrupt_sig_ptr) {
  129. break;
  130. }
  131. } while (ret == -1 && errno == EINTR);
  132. if (ret == -1) {
  133. tdb->ecode = TDB_ERR_LOCK;
  134. /* Generic lock error. errno set by fcntl.
  135. * EAGAIN is an expected return from non-blocking
  136. * locks. */
  137. if (!(flags & TDB_LOCK_PROBE) && errno != EAGAIN) {
  138. TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d flags=%d len=%d\n",
  139. tdb->fd, offset, rw_type, flags, (int)len));
  140. }
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. int tdb_brunlock(struct tdb_context *tdb,
  146. int rw_type, tdb_off_t offset, size_t len)
  147. {
  148. int ret;
  149. if (tdb->flags & TDB_NOLOCK) {
  150. return 0;
  151. }
  152. do {
  153. ret = fcntl_unlock(tdb, rw_type, offset, len);
  154. } while (ret == -1 && errno == EINTR);
  155. if (ret == -1) {
  156. TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brunlock failed (fd=%d) at offset %d rw_type=%d len=%d\n",
  157. tdb->fd, offset, rw_type, (int)len));
  158. }
  159. return ret;
  160. }
  161. /*
  162. upgrade a read lock to a write lock. This needs to be handled in a
  163. special way as some OSes (such as solaris) have too conservative
  164. deadlock detection and claim a deadlock when progress can be
  165. made. For those OSes we may loop for a while.
  166. */
  167. int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len)
  168. {
  169. int count = 1000;
  170. while (count--) {
  171. struct timeval tv;
  172. if (tdb_brlock(tdb, F_WRLCK, offset, len,
  173. TDB_LOCK_WAIT|TDB_LOCK_PROBE) == 0) {
  174. return 0;
  175. }
  176. if (errno != EDEADLK) {
  177. break;
  178. }
  179. /* sleep for as short a time as we can - more portable than usleep() */
  180. tv.tv_sec = 0;
  181. tv.tv_usec = 1;
  182. select(0, NULL, NULL, NULL, &tv);
  183. }
  184. TDB_LOG((tdb, TDB_DEBUG_TRACE,"tdb_brlock_upgrade failed at offset %d\n", offset));
  185. return -1;
  186. }
  187. /* lock a list in the database. list -1 is the alloc list */
  188. static int _tdb_lock(struct tdb_context *tdb, int list, int ltype,
  189. enum tdb_lock_flags flags)
  190. {
  191. struct tdb_lock_type *new_lck;
  192. int i;
  193. /* a global lock allows us to avoid per chain locks */
  194. if (tdb->global_lock.count &&
  195. (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
  196. return 0;
  197. }
  198. if (tdb->global_lock.count) {
  199. tdb->ecode = TDB_ERR_LOCK;
  200. return -1;
  201. }
  202. if (list < -1 || list >= (int)tdb->header.hash_size) {
  203. tdb->ecode = TDB_ERR_LOCK;
  204. TDB_LOG((tdb, TDB_DEBUG_ERROR,"tdb_lock: invalid list %d for ltype=%d\n",
  205. list, ltype));
  206. return -1;
  207. }
  208. if (tdb->flags & TDB_NOLOCK)
  209. return 0;
  210. for (i=0; i<tdb->num_lockrecs; i++) {
  211. if (tdb->lockrecs[i].list == list) {
  212. if (tdb->lockrecs[i].count == 0) {
  213. /*
  214. * Can't happen, see tdb_unlock(). It should
  215. * be an assert.
  216. */
  217. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock: "
  218. "lck->count == 0 for list %d", list));
  219. }
  220. /*
  221. * Just increment the in-memory struct, posix locks
  222. * don't stack.
  223. */
  224. tdb->lockrecs[i].count++;
  225. return 0;
  226. }
  227. }
  228. new_lck = (struct tdb_lock_type *)realloc(
  229. tdb->lockrecs,
  230. sizeof(*tdb->lockrecs) * (tdb->num_lockrecs+1));
  231. if (new_lck == NULL) {
  232. errno = ENOMEM;
  233. return -1;
  234. }
  235. tdb->lockrecs = new_lck;
  236. /* Since fcntl locks don't nest, we do a lock for the first one,
  237. and simply bump the count for future ones */
  238. if (tdb->methods->brlock(tdb, ltype, FREELIST_TOP+4*list, 1, flags)) {
  239. return -1;
  240. }
  241. tdb->num_locks++;
  242. tdb->lockrecs[tdb->num_lockrecs].list = list;
  243. tdb->lockrecs[tdb->num_lockrecs].count = 1;
  244. tdb->lockrecs[tdb->num_lockrecs].ltype = ltype;
  245. tdb->num_lockrecs += 1;
  246. return 0;
  247. }
  248. /* lock a list in the database. list -1 is the alloc list */
  249. int tdb_lock(struct tdb_context *tdb, int list, int ltype)
  250. {
  251. int ret;
  252. ret = _tdb_lock(tdb, list, ltype, TDB_LOCK_WAIT);
  253. if (ret) {
  254. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lock failed on list %d "
  255. "ltype=%d (%s)\n", list, ltype, strerror(errno)));
  256. }
  257. return ret;
  258. }
  259. /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
  260. int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype)
  261. {
  262. return _tdb_lock(tdb, list, ltype, TDB_LOCK_NOWAIT);
  263. }
  264. static int _tdb_unlock(struct tdb_context *tdb, int list, int ltype,
  265. bool mark_lock)
  266. {
  267. int ret = -1;
  268. int i;
  269. struct tdb_lock_type *lck = NULL;
  270. /* a global lock allows us to avoid per chain locks */
  271. if (tdb->global_lock.count &&
  272. (ltype == tdb->global_lock.ltype || ltype == F_RDLCK)) {
  273. return 0;
  274. }
  275. if (tdb->global_lock.count) {
  276. tdb->ecode = TDB_ERR_LOCK;
  277. return -1;
  278. }
  279. if (tdb->flags & TDB_NOLOCK)
  280. return 0;
  281. /* Sanity checks */
  282. if (list < -1 || list >= (int)tdb->header.hash_size) {
  283. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: list %d invalid (%d)\n", list, tdb->header.hash_size));
  284. return ret;
  285. }
  286. for (i=0; i<tdb->num_lockrecs; i++) {
  287. if (tdb->lockrecs[i].list == list) {
  288. lck = &tdb->lockrecs[i];
  289. break;
  290. }
  291. }
  292. if ((lck == NULL) || (lck->count == 0)) {
  293. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: count is 0\n"));
  294. return -1;
  295. }
  296. if (lck->count > 1) {
  297. lck->count--;
  298. return 0;
  299. }
  300. /*
  301. * This lock has count==1 left, so we need to unlock it in the
  302. * kernel. We don't bother with decrementing the in-memory array
  303. * element, we're about to overwrite it with the last array element
  304. * anyway.
  305. */
  306. if (mark_lock) {
  307. ret = 0;
  308. } else {
  309. ret = tdb->methods->brunlock(tdb, ltype,
  310. FREELIST_TOP+4*list, 1);
  311. }
  312. tdb->num_locks--;
  313. /*
  314. * Shrink the array by overwriting the element just unlocked with the
  315. * last array element.
  316. */
  317. if (tdb->num_lockrecs > 1) {
  318. *lck = tdb->lockrecs[tdb->num_lockrecs-1];
  319. }
  320. tdb->num_lockrecs -= 1;
  321. /*
  322. * We don't bother with realloc when the array shrinks, but if we have
  323. * a completely idle tdb we should get rid of the locked array.
  324. */
  325. if (tdb->num_lockrecs == 0) {
  326. SAFE_FREE(tdb->lockrecs);
  327. }
  328. if (ret)
  329. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlock: An error occurred unlocking!\n"));
  330. return ret;
  331. }
  332. int tdb_unlock(struct tdb_context *tdb, int list, int ltype)
  333. {
  334. return _tdb_unlock(tdb, list, ltype, false);
  335. }
  336. /*
  337. get the transaction lock
  338. */
  339. int tdb_transaction_lock(struct tdb_context *tdb, int ltype)
  340. {
  341. if (tdb->global_lock.count) {
  342. return 0;
  343. }
  344. if (tdb->transaction_lock_count > 0) {
  345. tdb->transaction_lock_count++;
  346. return 0;
  347. }
  348. if (tdb->methods->brlock(tdb, ltype, TRANSACTION_LOCK, 1, TDB_LOCK_WAIT) == -1) {
  349. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_lock: failed to get transaction lock\n"));
  350. tdb->ecode = TDB_ERR_LOCK;
  351. return -1;
  352. }
  353. tdb->transaction_lock_count++;
  354. return 0;
  355. }
  356. /*
  357. release the transaction lock
  358. */
  359. int tdb_transaction_unlock(struct tdb_context *tdb, int ltype)
  360. {
  361. int ret;
  362. if (tdb->global_lock.count) {
  363. return 0;
  364. }
  365. if (tdb->transaction_lock_count > 1) {
  366. tdb->transaction_lock_count--;
  367. return 0;
  368. }
  369. ret = tdb->methods->brunlock(tdb, ltype, TRANSACTION_LOCK, 1);
  370. if (ret == 0) {
  371. tdb->transaction_lock_count = 0;
  372. }
  373. return ret;
  374. }
  375. /* lock/unlock entire database */
  376. static int _tdb_lockall(struct tdb_context *tdb, int ltype,
  377. enum tdb_lock_flags flags)
  378. {
  379. /* There are no locks on read-only dbs */
  380. if (tdb->read_only || tdb->traverse_read) {
  381. tdb->ecode = TDB_ERR_LOCK;
  382. return -1;
  383. }
  384. if (tdb->global_lock.count && tdb->global_lock.ltype == ltype) {
  385. tdb->global_lock.count++;
  386. return 0;
  387. }
  388. if (tdb->global_lock.count) {
  389. /* a global lock of a different type exists */
  390. tdb->ecode = TDB_ERR_LOCK;
  391. return -1;
  392. }
  393. if (tdb->num_locks != 0) {
  394. /* can't combine global and chain locks */
  395. tdb->ecode = TDB_ERR_LOCK;
  396. return -1;
  397. }
  398. if (tdb->methods->brlock(tdb, ltype,
  399. FREELIST_TOP, 4*tdb->header.hash_size,
  400. flags)) {
  401. if (flags & TDB_LOCK_WAIT) {
  402. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_lockall failed (%s)\n", strerror(errno)));
  403. }
  404. return -1;
  405. }
  406. tdb->global_lock.count = 1;
  407. tdb->global_lock.ltype = ltype;
  408. return 0;
  409. }
  410. /* unlock entire db */
  411. static int _tdb_unlockall(struct tdb_context *tdb, int ltype, bool mark_lock)
  412. {
  413. /* There are no locks on read-only dbs */
  414. if (tdb->read_only || tdb->traverse_read) {
  415. tdb->ecode = TDB_ERR_LOCK;
  416. return -1;
  417. }
  418. if (tdb->global_lock.ltype != ltype || tdb->global_lock.count == 0) {
  419. tdb->ecode = TDB_ERR_LOCK;
  420. return -1;
  421. }
  422. if (tdb->global_lock.count > 1) {
  423. tdb->global_lock.count--;
  424. return 0;
  425. }
  426. if (!mark_lock &&
  427. tdb->methods->brunlock(tdb, ltype,
  428. FREELIST_TOP, 4*tdb->header.hash_size)) {
  429. TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_unlockall failed (%s)\n", strerror(errno)));
  430. return -1;
  431. }
  432. tdb->global_lock.count = 0;
  433. tdb->global_lock.ltype = 0;
  434. return 0;
  435. }
  436. /* lock entire database with write lock */
  437. int tdb_lockall(struct tdb_context *tdb)
  438. {
  439. tdb_trace(tdb, "tdb_lockall");
  440. return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_WAIT);
  441. }
  442. /* lock entire database with write lock - mark only */
  443. int tdb_lockall_mark(struct tdb_context *tdb)
  444. {
  445. tdb_trace(tdb, "tdb_lockall_mark");
  446. return _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_MARK_ONLY);
  447. }
  448. /* unlock entire database with write lock - unmark only */
  449. int tdb_lockall_unmark(struct tdb_context *tdb)
  450. {
  451. tdb_trace(tdb, "tdb_lockall_unmark");
  452. return _tdb_unlockall(tdb, F_WRLCK, true);
  453. }
  454. /* lock entire database with write lock - nonblocking varient */
  455. int tdb_lockall_nonblock(struct tdb_context *tdb)
  456. {
  457. int ret = _tdb_lockall(tdb, F_WRLCK, TDB_LOCK_NOWAIT);
  458. tdb_trace_ret(tdb, "tdb_lockall_nonblock", ret);
  459. return ret;
  460. }
  461. /* unlock entire database with write lock */
  462. int tdb_unlockall(struct tdb_context *tdb)
  463. {
  464. tdb_trace(tdb, "tdb_unlockall");
  465. return _tdb_unlockall(tdb, F_WRLCK, false);
  466. }
  467. /* lock entire database with read lock */
  468. int tdb_lockall_read(struct tdb_context *tdb)
  469. {
  470. tdb_trace(tdb, "tdb_lockall_read");
  471. return _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_WAIT);
  472. }
  473. /* lock entire database with read lock - nonblock varient */
  474. int tdb_lockall_read_nonblock(struct tdb_context *tdb)
  475. {
  476. int ret = _tdb_lockall(tdb, F_RDLCK, TDB_LOCK_NOWAIT);
  477. tdb_trace_ret(tdb, "tdb_lockall_read_nonblock", ret);
  478. return ret;
  479. }
  480. /* unlock entire database with read lock */
  481. int tdb_unlockall_read(struct tdb_context *tdb)
  482. {
  483. tdb_trace(tdb, "tdb_unlockall_read");
  484. return _tdb_unlockall(tdb, F_RDLCK, false);
  485. }
  486. /* lock/unlock one hash chain. This is meant to be used to reduce
  487. contention - it cannot guarantee how many records will be locked */
  488. int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key)
  489. {
  490. int ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
  491. tdb_trace_1rec(tdb, "tdb_chainlock", key);
  492. return ret;
  493. }
  494. /* lock/unlock one hash chain, non-blocking. This is meant to be used
  495. to reduce contention - it cannot guarantee how many records will be
  496. locked */
  497. int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key)
  498. {
  499. int ret = tdb_lock_nonblock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
  500. tdb_trace_1rec_ret(tdb, "tdb_chainlock_nonblock", key, ret);
  501. return ret;
  502. }
  503. /* mark a chain as locked without actually locking it. Warning! use with great caution! */
  504. int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key)
  505. {
  506. int ret = _tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK,
  507. TDB_LOCK_MARK_ONLY);
  508. tdb_trace_1rec(tdb, "tdb_chainlock_mark", key);
  509. return ret;
  510. }
  511. /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
  512. int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key)
  513. {
  514. tdb_trace_1rec(tdb, "tdb_chainlock_unmark", key);
  515. return _tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK, true);
  516. }
  517. int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key)
  518. {
  519. tdb_trace_1rec(tdb, "tdb_chainunlock", key);
  520. return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);
  521. }
  522. int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key)
  523. {
  524. int ret;
  525. ret = tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
  526. tdb_trace_1rec(tdb, "tdb_chainlock_read", key);
  527. return ret;
  528. }
  529. int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key)
  530. {
  531. tdb_trace_1rec(tdb, "tdb_chainunlock_read", key);
  532. return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);
  533. }
  534. /* record lock stops delete underneath */
  535. int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off)
  536. {
  537. if (tdb->global_lock.count) {
  538. return 0;
  539. }
  540. return off ? tdb->methods->brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
  541. }
  542. /*
  543. Write locks override our own fcntl readlocks, so check it here.
  544. Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
  545. an error to fail to get the lock here.
  546. */
  547. int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off)
  548. {
  549. struct tdb_traverse_lock *i;
  550. for (i = &tdb->travlocks; i; i = i->next)
  551. if (i->off == off)
  552. return -1;
  553. return tdb->methods->brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
  554. }
  555. int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off)
  556. {
  557. return tdb->methods->brunlock(tdb, F_WRLCK, off, 1);
  558. }
  559. /* fcntl locks don't stack: avoid unlocking someone else's */
  560. int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off)
  561. {
  562. struct tdb_traverse_lock *i;
  563. uint32_t count = 0;
  564. if (tdb->global_lock.count) {
  565. return 0;
  566. }
  567. if (off == 0)
  568. return 0;
  569. for (i = &tdb->travlocks; i; i = i->next)
  570. if (i->off == off)
  571. count++;
  572. return (count == 1 ? tdb->methods->brunlock(tdb, F_RDLCK, off, 1) : 0);
  573. }