dm-bio-prison-v2.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2011-2017 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #ifndef DM_BIO_PRISON_V2_H
  8. #define DM_BIO_PRISON_V2_H
  9. #include "persistent-data/dm-block-manager.h" /* FIXME: for dm_block_t */
  10. #include "dm-thin-metadata.h" /* FIXME: for dm_thin_id */
  11. #include <linux/bio.h>
  12. #include <linux/rbtree.h>
  13. #include <linux/workqueue.h>
  14. /*----------------------------------------------------------------*/
  15. int dm_bio_prison_init_v2(void);
  16. void dm_bio_prison_exit_v2(void);
  17. /*
  18. * Sometimes we can't deal with a bio straight away. We put them in prison
  19. * where they can't cause any mischief. Bios are put in a cell identified
  20. * by a key, multiple bios can be in the same cell. When the cell is
  21. * subsequently unlocked the bios become available.
  22. */
  23. struct dm_bio_prison_v2;
  24. /*
  25. * Keys define a range of blocks within either a virtual or physical
  26. * device.
  27. */
  28. struct dm_cell_key_v2 {
  29. int virtual;
  30. dm_thin_id dev;
  31. dm_block_t block_begin, block_end;
  32. };
  33. /*
  34. * Treat this as opaque, only in header so callers can manage allocation
  35. * themselves.
  36. */
  37. struct dm_bio_prison_cell_v2 {
  38. // FIXME: pack these
  39. bool exclusive_lock;
  40. unsigned int exclusive_level;
  41. unsigned int shared_count;
  42. struct work_struct *quiesce_continuation;
  43. struct rb_node node;
  44. struct dm_cell_key_v2 key;
  45. struct bio_list bios;
  46. };
  47. struct dm_bio_prison_v2 *dm_bio_prison_create_v2(struct workqueue_struct *wq);
  48. void dm_bio_prison_destroy_v2(struct dm_bio_prison_v2 *prison);
  49. /*
  50. * These two functions just wrap a mempool. This is a transitory step:
  51. * Eventually all bio prison clients should manage their own cell memory.
  52. *
  53. * Like mempool_alloc(), dm_bio_prison_alloc_cell_v2() can only fail if called
  54. * in interrupt context or passed GFP_NOWAIT.
  55. */
  56. struct dm_bio_prison_cell_v2 *dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2 *prison,
  57. gfp_t gfp);
  58. void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2 *prison,
  59. struct dm_bio_prison_cell_v2 *cell);
  60. /*
  61. * Shared locks have a bio associated with them.
  62. *
  63. * If the lock is granted the caller can continue to use the bio, and must
  64. * call dm_cell_put_v2() to drop the reference count when finished using it.
  65. *
  66. * If the lock cannot be granted then the bio will be tracked within the
  67. * cell, and later given to the holder of the exclusive lock.
  68. *
  69. * See dm_cell_lock_v2() for discussion of the lock_level parameter.
  70. *
  71. * Compare *cell_result with cell_prealloc to see if the prealloc was used.
  72. * If cell_prealloc was used then inmate wasn't added to it.
  73. *
  74. * Returns true if the lock is granted.
  75. */
  76. bool dm_cell_get_v2(struct dm_bio_prison_v2 *prison,
  77. struct dm_cell_key_v2 *key,
  78. unsigned int lock_level,
  79. struct bio *inmate,
  80. struct dm_bio_prison_cell_v2 *cell_prealloc,
  81. struct dm_bio_prison_cell_v2 **cell_result);
  82. /*
  83. * Decrement the shared reference count for the lock. Returns true if
  84. * returning ownership of the cell (ie. you should free it).
  85. */
  86. bool dm_cell_put_v2(struct dm_bio_prison_v2 *prison,
  87. struct dm_bio_prison_cell_v2 *cell);
  88. /*
  89. * Locks a cell. No associated bio. Exclusive locks get priority. These
  90. * locks constrain whether the io locks are granted according to level.
  91. *
  92. * Shared locks will still be granted if the lock_level is > (not = to) the
  93. * exclusive lock level.
  94. *
  95. * If an _exclusive_ lock is already held then -EBUSY is returned.
  96. *
  97. * Return values:
  98. * < 0 - error
  99. * 0 - locked; no quiescing needed
  100. * 1 - locked; quiescing needed
  101. */
  102. int dm_cell_lock_v2(struct dm_bio_prison_v2 *prison,
  103. struct dm_cell_key_v2 *key,
  104. unsigned int lock_level,
  105. struct dm_bio_prison_cell_v2 *cell_prealloc,
  106. struct dm_bio_prison_cell_v2 **cell_result);
  107. void dm_cell_quiesce_v2(struct dm_bio_prison_v2 *prison,
  108. struct dm_bio_prison_cell_v2 *cell,
  109. struct work_struct *continuation);
  110. /*
  111. * Promotes an _exclusive_ lock to a higher lock level.
  112. *
  113. * Return values:
  114. * < 0 - error
  115. * 0 - promoted; no quiescing needed
  116. * 1 - promoted; quiescing needed
  117. */
  118. int dm_cell_lock_promote_v2(struct dm_bio_prison_v2 *prison,
  119. struct dm_bio_prison_cell_v2 *cell,
  120. unsigned int new_lock_level);
  121. /*
  122. * Adds any held bios to the bio list.
  123. *
  124. * There may be shared locks still held at this point even if you quiesced
  125. * (ie. different lock levels).
  126. *
  127. * Returns true if returning ownership of the cell (ie. you should free
  128. * it).
  129. */
  130. bool dm_cell_unlock_v2(struct dm_bio_prison_v2 *prison,
  131. struct dm_bio_prison_cell_v2 *cell,
  132. struct bio_list *bios);
  133. /*----------------------------------------------------------------*/
  134. #endif