tdb1_lock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 "tdb1_private.h"
  22. /* list -1 is the alloc list, otherwise a hash chain. */
  23. static tdb1_off_t lock_offset(int list)
  24. {
  25. return TDB1_FREELIST_TOP + 4*list;
  26. }
  27. /* a byte range locking function - return 0 on success
  28. this functions locks/unlocks 1 byte at the specified offset.
  29. On error, errno is also set so that errors are passed back properly
  30. through tdb1_open().
  31. note that a len of zero means lock to end of file
  32. */
  33. int tdb1_brlock(struct tdb1_context *tdb,
  34. int rw_type, tdb1_off_t offset, size_t len,
  35. enum tdb_lock_flags flags)
  36. {
  37. enum TDB_ERROR ecode = tdb_brlock(tdb, rw_type, offset, len, flags
  38. | TDB_LOCK_NOCHECK);
  39. if (ecode == TDB_SUCCESS)
  40. return 0;
  41. tdb->last_error = ecode;
  42. return -1;
  43. }
  44. int tdb1_brunlock(struct tdb1_context *tdb,
  45. int rw_type, tdb1_off_t offset, size_t len)
  46. {
  47. enum TDB_ERROR ecode = tdb_brunlock(tdb, rw_type, offset, len);
  48. if (ecode == TDB_SUCCESS)
  49. return 0;
  50. tdb->last_error = ecode;
  51. return -1;
  52. }
  53. int tdb1_allrecord_upgrade(struct tdb1_context *tdb)
  54. {
  55. enum TDB_ERROR ecode = tdb_allrecord_upgrade(tdb, TDB1_FREELIST_TOP);
  56. if (ecode == TDB_SUCCESS)
  57. return 0;
  58. tdb->last_error = ecode;
  59. return -1;
  60. }
  61. static struct tdb_lock *tdb1_find_nestlock(struct tdb1_context *tdb,
  62. tdb1_off_t offset)
  63. {
  64. unsigned int i;
  65. for (i=0; i<tdb->file->num_lockrecs; i++) {
  66. if (tdb->file->lockrecs[i].off == offset) {
  67. return &tdb->file->lockrecs[i];
  68. }
  69. }
  70. return NULL;
  71. }
  72. /* lock an offset in the database. */
  73. int tdb1_nest_lock(struct tdb1_context *tdb, uint32_t offset, int ltype,
  74. enum tdb_lock_flags flags)
  75. {
  76. enum TDB_ERROR ecode;
  77. if (offset >= lock_offset(tdb->header.hash_size)) {
  78. tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
  79. "tdb1_lock: invalid offset %u for"
  80. " ltype=%d",
  81. offset, ltype);
  82. return -1;
  83. }
  84. ecode = tdb_nest_lock(tdb, offset, ltype, flags | TDB_LOCK_NOCHECK);
  85. if (unlikely(ecode != TDB_SUCCESS)) {
  86. tdb->last_error = ecode;
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. static int tdb1_lock_and_recover(struct tdb1_context *tdb)
  92. {
  93. int ret;
  94. /* We need to match locking order in transaction commit. */
  95. if (tdb1_brlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0,
  96. TDB_LOCK_WAIT|TDB_LOCK_NOCHECK)) {
  97. return -1;
  98. }
  99. if (tdb1_brlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1,
  100. TDB_LOCK_WAIT|TDB_LOCK_NOCHECK)) {
  101. tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
  102. return -1;
  103. }
  104. ret = tdb1_transaction_recover(tdb);
  105. tdb1_brunlock(tdb, F_WRLCK, TDB1_OPEN_LOCK, 1);
  106. tdb1_brunlock(tdb, F_WRLCK, TDB1_FREELIST_TOP, 0);
  107. return ret;
  108. }
  109. static bool have_data_locks(const struct tdb1_context *tdb)
  110. {
  111. unsigned int i;
  112. for (i = 0; i < tdb->file->num_lockrecs; i++) {
  113. if (tdb->file->lockrecs[i].off >= lock_offset(-1))
  114. return true;
  115. }
  116. return false;
  117. }
  118. static int tdb1_lock_list(struct tdb1_context *tdb, int list, int ltype,
  119. enum tdb_lock_flags waitflag)
  120. {
  121. int ret;
  122. bool check = false;
  123. /* a allrecord lock allows us to avoid per chain locks */
  124. if (tdb->file->allrecord_lock.count &&
  125. (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
  126. return 0;
  127. }
  128. if (tdb->file->allrecord_lock.count) {
  129. tdb->last_error = TDB_ERR_LOCK;
  130. ret = -1;
  131. } else {
  132. /* Only check when we grab first data lock. */
  133. check = !have_data_locks(tdb);
  134. ret = tdb1_nest_lock(tdb, lock_offset(list), ltype, waitflag);
  135. if (ret == 0 && check && tdb1_needs_recovery(tdb)) {
  136. tdb1_nest_unlock(tdb, lock_offset(list), ltype);
  137. if (tdb1_lock_and_recover(tdb) == -1) {
  138. return -1;
  139. }
  140. return tdb1_lock_list(tdb, list, ltype, waitflag);
  141. }
  142. }
  143. return ret;
  144. }
  145. /* lock a list in the database. list -1 is the alloc list */
  146. int tdb1_lock(struct tdb1_context *tdb, int list, int ltype)
  147. {
  148. int ret;
  149. ret = tdb1_lock_list(tdb, list, ltype, TDB_LOCK_WAIT);
  150. /* Don't log for EAGAIN and EINTR: they could have overridden lock fns */
  151. if (ret && errno != EAGAIN && errno != EINTR) {
  152. tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
  153. "tdb1_lock failed on list %d "
  154. "ltype=%d (%s)", list, ltype, strerror(errno));
  155. }
  156. return ret;
  157. }
  158. int tdb1_nest_unlock(struct tdb1_context *tdb, uint32_t offset, int ltype)
  159. {
  160. enum TDB_ERROR ecode;
  161. /* Sanity checks */
  162. if (offset >= lock_offset(tdb->header.hash_size)) {
  163. tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
  164. "tdb1_unlock: offset %u invalid (%d)",
  165. offset, tdb->header.hash_size);
  166. return -1;
  167. }
  168. ecode = tdb_nest_unlock(tdb, offset, ltype);
  169. if (unlikely(ecode != TDB_SUCCESS)) {
  170. tdb->last_error = ecode;
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. int tdb1_unlock(struct tdb1_context *tdb, int list, int ltype)
  176. {
  177. /* a global lock allows us to avoid per chain locks */
  178. if (tdb->file->allrecord_lock.count &&
  179. (ltype == tdb->file->allrecord_lock.ltype || ltype == F_RDLCK)) {
  180. return 0;
  181. }
  182. if (tdb->file->allrecord_lock.count) {
  183. tdb->last_error = TDB_ERR_LOCK;
  184. return -1;
  185. }
  186. return tdb1_nest_unlock(tdb, lock_offset(list), ltype);
  187. }
  188. /*
  189. get the transaction lock
  190. */
  191. int tdb1_transaction_lock(struct tdb1_context *tdb, int ltype,
  192. enum tdb_lock_flags lockflags)
  193. {
  194. return tdb1_nest_lock(tdb, TDB1_TRANSACTION_LOCK, ltype, lockflags);
  195. }
  196. /*
  197. release the transaction lock
  198. */
  199. int tdb1_transaction_unlock(struct tdb1_context *tdb, int ltype)
  200. {
  201. return tdb1_nest_unlock(tdb, TDB1_TRANSACTION_LOCK, ltype);
  202. }
  203. /* lock/unlock entire database. It can only be upgradable if you have some
  204. * other way of guaranteeing exclusivity (ie. transaction write lock).
  205. * We do the locking gradually to avoid being starved by smaller locks. */
  206. int tdb1_allrecord_lock(struct tdb1_context *tdb, int ltype,
  207. enum tdb_lock_flags flags, bool upgradable)
  208. {
  209. enum TDB_ERROR ecode;
  210. /* tdb_lock_gradual() doesn't know about tdb->traverse_read. */
  211. if (tdb->traverse_read && !(tdb->flags & TDB_NOLOCK)) {
  212. tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
  213. TDB_LOG_USE_ERROR,
  214. "tdb1_allrecord_lock during"
  215. " tdb1_read_traverse");
  216. return -1;
  217. }
  218. if (tdb->file->allrecord_lock.count
  219. && tdb->file->allrecord_lock.ltype == ltype) {
  220. tdb->file->allrecord_lock.count++;
  221. return 0;
  222. }
  223. if (tdb1_have_extra_locks(tdb)) {
  224. /* can't combine global and chain locks */
  225. tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
  226. TDB_LOG_USE_ERROR,
  227. "tdb1_allrecord_lock holding"
  228. " other locks");
  229. return -1;
  230. }
  231. if (upgradable && ltype != F_RDLCK) {
  232. /* tdb error: you can't upgrade a write lock! */
  233. tdb->last_error = tdb_logerr(tdb, TDB_ERR_LOCK,
  234. TDB_LOG_ERROR,
  235. "tdb1_allrecord_lock cannot"
  236. " have upgradable write lock");
  237. return -1;
  238. }
  239. /* We cover two kinds of locks:
  240. * 1) Normal chain locks. Taken for almost all operations.
  241. * 3) Individual records locks. Taken after normal or free
  242. * chain locks.
  243. *
  244. * It is (1) which cause the starvation problem, so we're only
  245. * gradual for that. */
  246. ecode = tdb_lock_gradual(tdb, ltype, flags | TDB_LOCK_NOCHECK,
  247. TDB1_FREELIST_TOP, tdb->header.hash_size * 4);
  248. if (ecode != TDB_SUCCESS) {
  249. tdb->last_error = ecode;
  250. return -1;
  251. }
  252. /* Grab individual record locks. */
  253. if (tdb1_brlock(tdb, ltype, lock_offset(tdb->header.hash_size), 0,
  254. flags) == -1) {
  255. tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP,
  256. tdb->header.hash_size * 4);
  257. return -1;
  258. }
  259. /* FIXME: Temporary cast. */
  260. tdb->file->allrecord_lock.owner = (void *)(struct tdb1_context *)tdb;
  261. tdb->file->allrecord_lock.count = 1;
  262. /* If it's upgradable, it's actually exclusive so we can treat
  263. * it as a write lock. */
  264. tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
  265. tdb->file->allrecord_lock.off = upgradable;
  266. if (tdb1_needs_recovery(tdb)) {
  267. tdb1_allrecord_unlock(tdb, ltype);
  268. if (tdb1_lock_and_recover(tdb) == -1) {
  269. return -1;
  270. }
  271. return tdb1_allrecord_lock(tdb, ltype, flags, upgradable);
  272. }
  273. return 0;
  274. }
  275. /* unlock entire db */
  276. int tdb1_allrecord_unlock(struct tdb1_context *tdb, int ltype)
  277. {
  278. /* Don't try this during r/o traversal! */
  279. if (tdb->traverse_read) {
  280. tdb->last_error = TDB_ERR_LOCK;
  281. return -1;
  282. }
  283. if (tdb->file->allrecord_lock.count == 0) {
  284. tdb->last_error = TDB_ERR_LOCK;
  285. return -1;
  286. }
  287. /* Upgradable locks are marked as write locks. */
  288. if (tdb->file->allrecord_lock.ltype != ltype
  289. && (!tdb->file->allrecord_lock.off || ltype != F_RDLCK)) {
  290. tdb->last_error = TDB_ERR_LOCK;
  291. return -1;
  292. }
  293. if (tdb->file->allrecord_lock.count > 1) {
  294. tdb->file->allrecord_lock.count--;
  295. return 0;
  296. }
  297. if (tdb1_brunlock(tdb, ltype, TDB1_FREELIST_TOP, 0)) {
  298. tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
  299. "tdb1_unlockall failed (%s)", strerror(errno));
  300. return -1;
  301. }
  302. tdb->file->allrecord_lock.count = 0;
  303. tdb->file->allrecord_lock.ltype = 0;
  304. return 0;
  305. }
  306. /* lock entire database with write lock */
  307. int tdb1_lockall(struct tdb1_context *tdb)
  308. {
  309. return tdb1_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
  310. }
  311. /* unlock entire database with write lock */
  312. int tdb1_unlockall(struct tdb1_context *tdb)
  313. {
  314. return tdb1_allrecord_unlock(tdb, F_WRLCK);
  315. }
  316. /* lock entire database with read lock */
  317. int tdb1_lockall_read(struct tdb1_context *tdb)
  318. {
  319. return tdb1_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
  320. }
  321. /* unlock entire database with read lock */
  322. int tdb1_unlockall_read(struct tdb1_context *tdb)
  323. {
  324. return tdb1_allrecord_unlock(tdb, F_RDLCK);
  325. }
  326. /* lock/unlock one hash chain. This is meant to be used to reduce
  327. contention - it cannot guarantee how many records will be locked */
  328. int tdb1_chainlock(struct tdb1_context *tdb, TDB_DATA key)
  329. {
  330. int ret = tdb1_lock(tdb,
  331. TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
  332. F_WRLCK);
  333. return ret;
  334. }
  335. int tdb1_chainunlock(struct tdb1_context *tdb, TDB_DATA key)
  336. {
  337. return tdb1_unlock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
  338. F_WRLCK);
  339. }
  340. int tdb1_chainlock_read(struct tdb1_context *tdb, TDB_DATA key)
  341. {
  342. int ret;
  343. ret = tdb1_lock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
  344. F_RDLCK);
  345. return ret;
  346. }
  347. int tdb1_chainunlock_read(struct tdb1_context *tdb, TDB_DATA key)
  348. {
  349. return tdb1_unlock(tdb, TDB1_BUCKET(tdb_hash(tdb, key.dptr, key.dsize)),
  350. F_RDLCK);
  351. }
  352. /* record lock stops delete underneath */
  353. int tdb1_lock_record(struct tdb1_context *tdb, tdb1_off_t off)
  354. {
  355. if (tdb->file->allrecord_lock.count) {
  356. return 0;
  357. }
  358. return off ? tdb1_brlock(tdb, F_RDLCK, off, 1, TDB_LOCK_WAIT) : 0;
  359. }
  360. /*
  361. Write locks override our own fcntl readlocks, so check it here.
  362. Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
  363. an error to fail to get the lock here.
  364. */
  365. int tdb1_write_lock_record(struct tdb1_context *tdb, tdb1_off_t off)
  366. {
  367. struct tdb1_traverse_lock *i;
  368. for (i = &tdb->travlocks; i; i = i->next)
  369. if (i->off == off)
  370. return -1;
  371. if (tdb->file->allrecord_lock.count) {
  372. if (tdb->file->allrecord_lock.ltype == F_WRLCK) {
  373. return 0;
  374. }
  375. return -1;
  376. }
  377. return tdb1_brlock(tdb, F_WRLCK, off, 1, TDB_LOCK_NOWAIT|TDB_LOCK_PROBE);
  378. }
  379. int tdb1_write_unlock_record(struct tdb1_context *tdb, tdb1_off_t off)
  380. {
  381. if (tdb->file->allrecord_lock.count) {
  382. return 0;
  383. }
  384. return tdb1_brunlock(tdb, F_WRLCK, off, 1);
  385. }
  386. /* fcntl locks don't stack: avoid unlocking someone else's */
  387. int tdb1_unlock_record(struct tdb1_context *tdb, tdb1_off_t off)
  388. {
  389. struct tdb1_traverse_lock *i;
  390. uint32_t count = 0;
  391. if (tdb->file->allrecord_lock.count) {
  392. return 0;
  393. }
  394. if (off == 0)
  395. return 0;
  396. for (i = &tdb->travlocks; i; i = i->next)
  397. if (i->off == off)
  398. count++;
  399. return (count == 1 ? tdb1_brunlock(tdb, F_RDLCK, off, 1) : 0);
  400. }
  401. bool tdb1_have_extra_locks(struct tdb1_context *tdb)
  402. {
  403. unsigned int extra = tdb->file->num_lockrecs;
  404. /* A transaction holds the lock for all records. */
  405. if (!tdb->transaction && tdb->file->allrecord_lock.count) {
  406. return true;
  407. }
  408. /* We always hold the active lock if CLEAR_IF_FIRST. */
  409. if (tdb1_find_nestlock(tdb, TDB1_ACTIVE_LOCK)) {
  410. extra--;
  411. }
  412. /* In a transaction, we expect to hold the transaction lock */
  413. if (tdb->transaction
  414. && tdb1_find_nestlock(tdb, TDB1_TRANSACTION_LOCK)) {
  415. extra--;
  416. }
  417. return extra;
  418. }
  419. /* The transaction code uses this to remove all locks. */
  420. void tdb1_release_transaction_locks(struct tdb1_context *tdb)
  421. {
  422. unsigned int i, active = 0;
  423. if (tdb->file->allrecord_lock.count != 0) {
  424. tdb1_brunlock(tdb, tdb->file->allrecord_lock.ltype, TDB1_FREELIST_TOP, 0);
  425. tdb->file->allrecord_lock.count = 0;
  426. }
  427. for (i=0;i<tdb->file->num_lockrecs;i++) {
  428. struct tdb_lock *lck = &tdb->file->lockrecs[i];
  429. /* Don't release the active lock! Copy it to first entry. */
  430. if (lck->off == TDB1_ACTIVE_LOCK) {
  431. tdb->file->lockrecs[active++] = *lck;
  432. } else {
  433. tdb1_brunlock(tdb, lck->ltype, lck->off, 1);
  434. }
  435. }
  436. tdb->file->num_lockrecs = active;
  437. if (tdb->file->num_lockrecs == 0) {
  438. SAFE_FREE(tdb->file->lockrecs);
  439. }
  440. }