dm-bio-prison-v2.h 4.5 KB

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