dm-bio-prison-v1.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2011-2017 Red Hat, Inc.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #ifndef DM_BIO_PRISON_H
  7. #define DM_BIO_PRISON_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. /*----------------------------------------------------------------*/
  13. /*
  14. * Sometimes we can't deal with a bio straight away. We put them in prison
  15. * where they can't cause any mischief. Bios are put in a cell identified
  16. * by a key, multiple bios can be in the same cell. When the cell is
  17. * subsequently unlocked the bios become available.
  18. */
  19. struct dm_bio_prison;
  20. /*
  21. * Keys define a range of blocks within either a virtual or physical
  22. * device.
  23. */
  24. struct dm_cell_key {
  25. int virtual;
  26. dm_thin_id dev;
  27. dm_block_t block_begin, block_end;
  28. };
  29. /*
  30. * Treat this as opaque, only in header so callers can manage allocation
  31. * themselves.
  32. */
  33. struct dm_bio_prison_cell {
  34. struct list_head user_list; /* for client use */
  35. struct rb_node node;
  36. struct dm_cell_key key;
  37. struct bio *holder;
  38. struct bio_list bios;
  39. };
  40. struct dm_bio_prison *dm_bio_prison_create(void);
  41. void dm_bio_prison_destroy(struct dm_bio_prison *prison);
  42. /*
  43. * These two functions just wrap a mempool. This is a transitory step:
  44. * Eventually all bio prison clients should manage their own cell memory.
  45. *
  46. * Like mempool_alloc(), dm_bio_prison_alloc_cell() can only fail if called
  47. * in interrupt context or passed GFP_NOWAIT.
  48. */
  49. struct dm_bio_prison_cell *dm_bio_prison_alloc_cell(struct dm_bio_prison *prison,
  50. gfp_t gfp);
  51. void dm_bio_prison_free_cell(struct dm_bio_prison *prison,
  52. struct dm_bio_prison_cell *cell);
  53. /*
  54. * Creates, or retrieves a cell that overlaps the given key.
  55. *
  56. * Returns 1 if pre-existing cell returned, zero if new cell created using
  57. * @cell_prealloc.
  58. */
  59. int dm_get_cell(struct dm_bio_prison *prison,
  60. struct dm_cell_key *key,
  61. struct dm_bio_prison_cell *cell_prealloc,
  62. struct dm_bio_prison_cell **cell_result);
  63. /*
  64. * An atomic op that combines retrieving or creating a cell, and adding a
  65. * bio to it.
  66. *
  67. * Returns 1 if the cell was already held, 0 if @inmate is the new holder.
  68. */
  69. int dm_bio_detain(struct dm_bio_prison *prison,
  70. struct dm_cell_key *key,
  71. struct bio *inmate,
  72. struct dm_bio_prison_cell *cell_prealloc,
  73. struct dm_bio_prison_cell **cell_result);
  74. void dm_cell_release(struct dm_bio_prison *prison,
  75. struct dm_bio_prison_cell *cell,
  76. struct bio_list *bios);
  77. void dm_cell_release_no_holder(struct dm_bio_prison *prison,
  78. struct dm_bio_prison_cell *cell,
  79. struct bio_list *inmates);
  80. void dm_cell_error(struct dm_bio_prison *prison,
  81. struct dm_bio_prison_cell *cell, blk_status_t error);
  82. /*
  83. * Visits the cell and then releases. Guarantees no new inmates are
  84. * inserted between the visit and release.
  85. */
  86. void dm_cell_visit_release(struct dm_bio_prison *prison,
  87. void (*visit_fn)(void *, struct dm_bio_prison_cell *),
  88. void *context, struct dm_bio_prison_cell *cell);
  89. /*
  90. * Rather than always releasing the prisoners in a cell, the client may
  91. * want to promote one of them to be the new holder. There is a race here
  92. * though between releasing an empty cell, and other threads adding new
  93. * inmates. So this function makes the decision with its lock held.
  94. *
  95. * This function can have two outcomes:
  96. * i) An inmate is promoted to be the holder of the cell (return value of 0).
  97. * ii) The cell has no inmate for promotion and is released (return value of 1).
  98. */
  99. int dm_cell_promote_or_release(struct dm_bio_prison *prison,
  100. struct dm_bio_prison_cell *cell);
  101. /*----------------------------------------------------------------*/
  102. /*
  103. * We use the deferred set to keep track of pending reads to shared blocks.
  104. * We do this to ensure the new mapping caused by a write isn't performed
  105. * until these prior reads have completed. Otherwise the insertion of the
  106. * new mapping could free the old block that the read bios are mapped to.
  107. */
  108. struct dm_deferred_set;
  109. struct dm_deferred_entry;
  110. struct dm_deferred_set *dm_deferred_set_create(void);
  111. void dm_deferred_set_destroy(struct dm_deferred_set *ds);
  112. struct dm_deferred_entry *dm_deferred_entry_inc(struct dm_deferred_set *ds);
  113. void dm_deferred_entry_dec(struct dm_deferred_entry *entry, struct list_head *head);
  114. int dm_deferred_set_add_work(struct dm_deferred_set *ds, struct list_head *work);
  115. /*----------------------------------------------------------------*/
  116. #endif