dm-block-manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright (C) 2011 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm-block-manager.h"
  7. #include "dm-persistent-data-internal.h"
  8. #include "../dm-bufio.h"
  9. #include <linux/crc32c.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/rwsem.h>
  13. #include <linux/device-mapper.h>
  14. #include <linux/stacktrace.h>
  15. #define DM_MSG_PREFIX "block manager"
  16. /*----------------------------------------------------------------*/
  17. /*
  18. * This is a read/write semaphore with a couple of differences.
  19. *
  20. * i) There is a restriction on the number of concurrent read locks that
  21. * may be held at once. This is just an implementation detail.
  22. *
  23. * ii) Recursive locking attempts are detected and return EINVAL. A stack
  24. * trace is also emitted for the previous lock acquisition.
  25. *
  26. * iii) Priority is given to write locks.
  27. */
  28. #define MAX_HOLDERS 4
  29. #define MAX_STACK 10
  30. typedef unsigned long stack_entries[MAX_STACK];
  31. struct block_lock {
  32. spinlock_t lock;
  33. __s32 count;
  34. struct list_head waiters;
  35. struct task_struct *holders[MAX_HOLDERS];
  36. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  37. struct stack_trace traces[MAX_HOLDERS];
  38. stack_entries entries[MAX_HOLDERS];
  39. #endif
  40. };
  41. struct waiter {
  42. struct list_head list;
  43. struct task_struct *task;
  44. int wants_write;
  45. };
  46. static unsigned __find_holder(struct block_lock *lock,
  47. struct task_struct *task)
  48. {
  49. unsigned i;
  50. for (i = 0; i < MAX_HOLDERS; i++)
  51. if (lock->holders[i] == task)
  52. break;
  53. BUG_ON(i == MAX_HOLDERS);
  54. return i;
  55. }
  56. /* call this *after* you increment lock->count */
  57. static void __add_holder(struct block_lock *lock, struct task_struct *task)
  58. {
  59. unsigned h = __find_holder(lock, NULL);
  60. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  61. struct stack_trace *t;
  62. #endif
  63. get_task_struct(task);
  64. lock->holders[h] = task;
  65. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  66. t = lock->traces + h;
  67. t->nr_entries = 0;
  68. t->max_entries = MAX_STACK;
  69. t->entries = lock->entries[h];
  70. t->skip = 2;
  71. save_stack_trace(t);
  72. #endif
  73. }
  74. /* call this *before* you decrement lock->count */
  75. static void __del_holder(struct block_lock *lock, struct task_struct *task)
  76. {
  77. unsigned h = __find_holder(lock, task);
  78. lock->holders[h] = NULL;
  79. put_task_struct(task);
  80. }
  81. static int __check_holder(struct block_lock *lock)
  82. {
  83. unsigned i;
  84. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  85. static struct stack_trace t;
  86. static stack_entries entries;
  87. #endif
  88. for (i = 0; i < MAX_HOLDERS; i++) {
  89. if (lock->holders[i] == current) {
  90. DMERR("recursive lock detected in metadata");
  91. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  92. DMERR("previously held here:");
  93. print_stack_trace(lock->traces + i, 4);
  94. DMERR("subsequent acquisition attempted here:");
  95. t.nr_entries = 0;
  96. t.max_entries = MAX_STACK;
  97. t.entries = entries;
  98. t.skip = 3;
  99. save_stack_trace(&t);
  100. print_stack_trace(&t, 4);
  101. #endif
  102. return -EINVAL;
  103. }
  104. }
  105. return 0;
  106. }
  107. static void __wait(struct waiter *w)
  108. {
  109. for (;;) {
  110. set_task_state(current, TASK_UNINTERRUPTIBLE);
  111. if (!w->task)
  112. break;
  113. schedule();
  114. }
  115. set_task_state(current, TASK_RUNNING);
  116. }
  117. static void __wake_waiter(struct waiter *w)
  118. {
  119. struct task_struct *task;
  120. list_del(&w->list);
  121. task = w->task;
  122. smp_mb();
  123. w->task = NULL;
  124. wake_up_process(task);
  125. }
  126. /*
  127. * We either wake a few readers or a single writer.
  128. */
  129. static void __wake_many(struct block_lock *lock)
  130. {
  131. struct waiter *w, *tmp;
  132. BUG_ON(lock->count < 0);
  133. list_for_each_entry_safe(w, tmp, &lock->waiters, list) {
  134. if (lock->count >= MAX_HOLDERS)
  135. return;
  136. if (w->wants_write) {
  137. if (lock->count > 0)
  138. return; /* still read locked */
  139. lock->count = -1;
  140. __add_holder(lock, w->task);
  141. __wake_waiter(w);
  142. return;
  143. }
  144. lock->count++;
  145. __add_holder(lock, w->task);
  146. __wake_waiter(w);
  147. }
  148. }
  149. static void bl_init(struct block_lock *lock)
  150. {
  151. int i;
  152. spin_lock_init(&lock->lock);
  153. lock->count = 0;
  154. INIT_LIST_HEAD(&lock->waiters);
  155. for (i = 0; i < MAX_HOLDERS; i++)
  156. lock->holders[i] = NULL;
  157. }
  158. static int __available_for_read(struct block_lock *lock)
  159. {
  160. return lock->count >= 0 &&
  161. lock->count < MAX_HOLDERS &&
  162. list_empty(&lock->waiters);
  163. }
  164. static int bl_down_read(struct block_lock *lock)
  165. {
  166. int r;
  167. struct waiter w;
  168. spin_lock(&lock->lock);
  169. r = __check_holder(lock);
  170. if (r) {
  171. spin_unlock(&lock->lock);
  172. return r;
  173. }
  174. if (__available_for_read(lock)) {
  175. lock->count++;
  176. __add_holder(lock, current);
  177. spin_unlock(&lock->lock);
  178. return 0;
  179. }
  180. get_task_struct(current);
  181. w.task = current;
  182. w.wants_write = 0;
  183. list_add_tail(&w.list, &lock->waiters);
  184. spin_unlock(&lock->lock);
  185. __wait(&w);
  186. put_task_struct(current);
  187. return 0;
  188. }
  189. static int bl_down_read_nonblock(struct block_lock *lock)
  190. {
  191. int r;
  192. spin_lock(&lock->lock);
  193. r = __check_holder(lock);
  194. if (r)
  195. goto out;
  196. if (__available_for_read(lock)) {
  197. lock->count++;
  198. __add_holder(lock, current);
  199. r = 0;
  200. } else
  201. r = -EWOULDBLOCK;
  202. out:
  203. spin_unlock(&lock->lock);
  204. return r;
  205. }
  206. static void bl_up_read(struct block_lock *lock)
  207. {
  208. spin_lock(&lock->lock);
  209. BUG_ON(lock->count <= 0);
  210. __del_holder(lock, current);
  211. --lock->count;
  212. if (!list_empty(&lock->waiters))
  213. __wake_many(lock);
  214. spin_unlock(&lock->lock);
  215. }
  216. static int bl_down_write(struct block_lock *lock)
  217. {
  218. int r;
  219. struct waiter w;
  220. spin_lock(&lock->lock);
  221. r = __check_holder(lock);
  222. if (r) {
  223. spin_unlock(&lock->lock);
  224. return r;
  225. }
  226. if (lock->count == 0 && list_empty(&lock->waiters)) {
  227. lock->count = -1;
  228. __add_holder(lock, current);
  229. spin_unlock(&lock->lock);
  230. return 0;
  231. }
  232. get_task_struct(current);
  233. w.task = current;
  234. w.wants_write = 1;
  235. /*
  236. * Writers given priority. We know there's only one mutator in the
  237. * system, so ignoring the ordering reversal.
  238. */
  239. list_add(&w.list, &lock->waiters);
  240. spin_unlock(&lock->lock);
  241. __wait(&w);
  242. put_task_struct(current);
  243. return 0;
  244. }
  245. static void bl_up_write(struct block_lock *lock)
  246. {
  247. spin_lock(&lock->lock);
  248. __del_holder(lock, current);
  249. lock->count = 0;
  250. if (!list_empty(&lock->waiters))
  251. __wake_many(lock);
  252. spin_unlock(&lock->lock);
  253. }
  254. static void report_recursive_bug(dm_block_t b, int r)
  255. {
  256. if (r == -EINVAL)
  257. DMERR("recursive acquisition of block %llu requested.",
  258. (unsigned long long) b);
  259. }
  260. /*----------------------------------------------------------------*/
  261. /*
  262. * Block manager is currently implemented using dm-bufio. struct
  263. * dm_block_manager and struct dm_block map directly onto a couple of
  264. * structs in the bufio interface. I want to retain the freedom to move
  265. * away from bufio in the future. So these structs are just cast within
  266. * this .c file, rather than making it through to the public interface.
  267. */
  268. static struct dm_buffer *to_buffer(struct dm_block *b)
  269. {
  270. return (struct dm_buffer *) b;
  271. }
  272. dm_block_t dm_block_location(struct dm_block *b)
  273. {
  274. return dm_bufio_get_block_number(to_buffer(b));
  275. }
  276. EXPORT_SYMBOL_GPL(dm_block_location);
  277. void *dm_block_data(struct dm_block *b)
  278. {
  279. return dm_bufio_get_block_data(to_buffer(b));
  280. }
  281. EXPORT_SYMBOL_GPL(dm_block_data);
  282. struct buffer_aux {
  283. struct dm_block_validator *validator;
  284. struct block_lock lock;
  285. int write_locked;
  286. };
  287. static void dm_block_manager_alloc_callback(struct dm_buffer *buf)
  288. {
  289. struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
  290. aux->validator = NULL;
  291. bl_init(&aux->lock);
  292. }
  293. static void dm_block_manager_write_callback(struct dm_buffer *buf)
  294. {
  295. struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
  296. if (aux->validator) {
  297. aux->validator->prepare_for_write(aux->validator, (struct dm_block *) buf,
  298. dm_bufio_get_block_size(dm_bufio_get_client(buf)));
  299. }
  300. }
  301. /*----------------------------------------------------------------
  302. * Public interface
  303. *--------------------------------------------------------------*/
  304. struct dm_block_manager {
  305. struct dm_bufio_client *bufio;
  306. bool read_only:1;
  307. };
  308. struct dm_block_manager *dm_block_manager_create(struct block_device *bdev,
  309. unsigned block_size,
  310. unsigned cache_size,
  311. unsigned max_held_per_thread)
  312. {
  313. int r;
  314. struct dm_block_manager *bm;
  315. bm = kmalloc(sizeof(*bm), GFP_KERNEL);
  316. if (!bm) {
  317. r = -ENOMEM;
  318. goto bad;
  319. }
  320. bm->bufio = dm_bufio_client_create(bdev, block_size, max_held_per_thread,
  321. sizeof(struct buffer_aux),
  322. dm_block_manager_alloc_callback,
  323. dm_block_manager_write_callback);
  324. if (IS_ERR(bm->bufio)) {
  325. r = PTR_ERR(bm->bufio);
  326. kfree(bm);
  327. goto bad;
  328. }
  329. bm->read_only = false;
  330. return bm;
  331. bad:
  332. return ERR_PTR(r);
  333. }
  334. EXPORT_SYMBOL_GPL(dm_block_manager_create);
  335. void dm_block_manager_destroy(struct dm_block_manager *bm)
  336. {
  337. dm_bufio_client_destroy(bm->bufio);
  338. kfree(bm);
  339. }
  340. EXPORT_SYMBOL_GPL(dm_block_manager_destroy);
  341. unsigned dm_bm_block_size(struct dm_block_manager *bm)
  342. {
  343. return dm_bufio_get_block_size(bm->bufio);
  344. }
  345. EXPORT_SYMBOL_GPL(dm_bm_block_size);
  346. dm_block_t dm_bm_nr_blocks(struct dm_block_manager *bm)
  347. {
  348. return dm_bufio_get_device_size(bm->bufio);
  349. }
  350. static int dm_bm_validate_buffer(struct dm_block_manager *bm,
  351. struct dm_buffer *buf,
  352. struct buffer_aux *aux,
  353. struct dm_block_validator *v)
  354. {
  355. if (unlikely(!aux->validator)) {
  356. int r;
  357. if (!v)
  358. return 0;
  359. r = v->check(v, (struct dm_block *) buf, dm_bufio_get_block_size(bm->bufio));
  360. if (unlikely(r)) {
  361. DMERR_LIMIT("%s validator check failed for block %llu", v->name,
  362. (unsigned long long) dm_bufio_get_block_number(buf));
  363. return r;
  364. }
  365. aux->validator = v;
  366. } else {
  367. if (unlikely(aux->validator != v)) {
  368. DMERR_LIMIT("validator mismatch (old=%s vs new=%s) for block %llu",
  369. aux->validator->name, v ? v->name : "NULL",
  370. (unsigned long long) dm_bufio_get_block_number(buf));
  371. return -EINVAL;
  372. }
  373. }
  374. return 0;
  375. }
  376. int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
  377. struct dm_block_validator *v,
  378. struct dm_block **result)
  379. {
  380. struct buffer_aux *aux;
  381. void *p;
  382. int r;
  383. p = dm_bufio_read(bm->bufio, b, (struct dm_buffer **) result);
  384. if (IS_ERR(p))
  385. return PTR_ERR(p);
  386. aux = dm_bufio_get_aux_data(to_buffer(*result));
  387. r = bl_down_read(&aux->lock);
  388. if (unlikely(r)) {
  389. dm_bufio_release(to_buffer(*result));
  390. report_recursive_bug(b, r);
  391. return r;
  392. }
  393. aux->write_locked = 0;
  394. r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v);
  395. if (unlikely(r)) {
  396. bl_up_read(&aux->lock);
  397. dm_bufio_release(to_buffer(*result));
  398. return r;
  399. }
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(dm_bm_read_lock);
  403. int dm_bm_write_lock(struct dm_block_manager *bm,
  404. dm_block_t b, struct dm_block_validator *v,
  405. struct dm_block **result)
  406. {
  407. struct buffer_aux *aux;
  408. void *p;
  409. int r;
  410. if (bm->read_only)
  411. return -EPERM;
  412. p = dm_bufio_read(bm->bufio, b, (struct dm_buffer **) result);
  413. if (IS_ERR(p))
  414. return PTR_ERR(p);
  415. aux = dm_bufio_get_aux_data(to_buffer(*result));
  416. r = bl_down_write(&aux->lock);
  417. if (r) {
  418. dm_bufio_release(to_buffer(*result));
  419. report_recursive_bug(b, r);
  420. return r;
  421. }
  422. aux->write_locked = 1;
  423. r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v);
  424. if (unlikely(r)) {
  425. bl_up_write(&aux->lock);
  426. dm_bufio_release(to_buffer(*result));
  427. return r;
  428. }
  429. return 0;
  430. }
  431. EXPORT_SYMBOL_GPL(dm_bm_write_lock);
  432. int dm_bm_read_try_lock(struct dm_block_manager *bm,
  433. dm_block_t b, struct dm_block_validator *v,
  434. struct dm_block **result)
  435. {
  436. struct buffer_aux *aux;
  437. void *p;
  438. int r;
  439. p = dm_bufio_get(bm->bufio, b, (struct dm_buffer **) result);
  440. if (IS_ERR(p))
  441. return PTR_ERR(p);
  442. if (unlikely(!p))
  443. return -EWOULDBLOCK;
  444. aux = dm_bufio_get_aux_data(to_buffer(*result));
  445. r = bl_down_read_nonblock(&aux->lock);
  446. if (r < 0) {
  447. dm_bufio_release(to_buffer(*result));
  448. report_recursive_bug(b, r);
  449. return r;
  450. }
  451. aux->write_locked = 0;
  452. r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v);
  453. if (unlikely(r)) {
  454. bl_up_read(&aux->lock);
  455. dm_bufio_release(to_buffer(*result));
  456. return r;
  457. }
  458. return 0;
  459. }
  460. int dm_bm_write_lock_zero(struct dm_block_manager *bm,
  461. dm_block_t b, struct dm_block_validator *v,
  462. struct dm_block **result)
  463. {
  464. int r;
  465. struct buffer_aux *aux;
  466. void *p;
  467. if (bm->read_only)
  468. return -EPERM;
  469. p = dm_bufio_new(bm->bufio, b, (struct dm_buffer **) result);
  470. if (IS_ERR(p))
  471. return PTR_ERR(p);
  472. memset(p, 0, dm_bm_block_size(bm));
  473. aux = dm_bufio_get_aux_data(to_buffer(*result));
  474. r = bl_down_write(&aux->lock);
  475. if (r) {
  476. dm_bufio_release(to_buffer(*result));
  477. return r;
  478. }
  479. aux->write_locked = 1;
  480. aux->validator = v;
  481. return 0;
  482. }
  483. EXPORT_SYMBOL_GPL(dm_bm_write_lock_zero);
  484. void dm_bm_unlock(struct dm_block *b)
  485. {
  486. struct buffer_aux *aux;
  487. aux = dm_bufio_get_aux_data(to_buffer(b));
  488. if (aux->write_locked) {
  489. dm_bufio_mark_buffer_dirty(to_buffer(b));
  490. bl_up_write(&aux->lock);
  491. } else
  492. bl_up_read(&aux->lock);
  493. dm_bufio_release(to_buffer(b));
  494. }
  495. EXPORT_SYMBOL_GPL(dm_bm_unlock);
  496. int dm_bm_flush(struct dm_block_manager *bm)
  497. {
  498. if (bm->read_only)
  499. return -EPERM;
  500. return dm_bufio_write_dirty_buffers(bm->bufio);
  501. }
  502. EXPORT_SYMBOL_GPL(dm_bm_flush);
  503. void dm_bm_prefetch(struct dm_block_manager *bm, dm_block_t b)
  504. {
  505. dm_bufio_prefetch(bm->bufio, b, 1);
  506. }
  507. bool dm_bm_is_read_only(struct dm_block_manager *bm)
  508. {
  509. return bm->read_only;
  510. }
  511. EXPORT_SYMBOL_GPL(dm_bm_is_read_only);
  512. void dm_bm_set_read_only(struct dm_block_manager *bm)
  513. {
  514. bm->read_only = true;
  515. }
  516. EXPORT_SYMBOL_GPL(dm_bm_set_read_only);
  517. void dm_bm_set_read_write(struct dm_block_manager *bm)
  518. {
  519. bm->read_only = false;
  520. }
  521. EXPORT_SYMBOL_GPL(dm_bm_set_read_write);
  522. u32 dm_bm_checksum(const void *data, size_t len, u32 init_xor)
  523. {
  524. return crc32c(~(u32) 0, data, len) ^ init_xor;
  525. }
  526. EXPORT_SYMBOL_GPL(dm_bm_checksum);
  527. /*----------------------------------------------------------------*/
  528. MODULE_LICENSE("GPL");
  529. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  530. MODULE_DESCRIPTION("Immutable metadata library for dm");
  531. /*----------------------------------------------------------------*/