vdo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #ifndef VDO_H
  6. #define VDO_H
  7. #include <linux/atomic.h>
  8. #include <linux/blk_types.h>
  9. #include <linux/completion.h>
  10. #include <linux/dm-kcopyd.h>
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. #include "admin-state.h"
  14. #include "encodings.h"
  15. #include "funnel-workqueue.h"
  16. #include "packer.h"
  17. #include "physical-zone.h"
  18. #include "statistics.h"
  19. #include "thread-registry.h"
  20. #include "types.h"
  21. enum notifier_state {
  22. /* Notifications are allowed but not in progress */
  23. MAY_NOTIFY,
  24. /* A notification is in progress */
  25. NOTIFYING,
  26. /* Notifications are not allowed */
  27. MAY_NOT_NOTIFY,
  28. /* A notification has completed */
  29. NOTIFIED,
  30. };
  31. /**
  32. * typedef vdo_read_only_notification_fn - A function to notify a listener that the VDO has gone
  33. * read-only.
  34. * @listener: The object to notify.
  35. * @parent: The completion to notify in order to acknowledge the notification.
  36. */
  37. typedef void (*vdo_read_only_notification_fn)(void *listener, struct vdo_completion *parent);
  38. /*
  39. * An object to be notified when the VDO enters read-only mode
  40. */
  41. struct read_only_listener {
  42. /* The listener */
  43. void *listener;
  44. /* The method to call to notify the listener */
  45. vdo_read_only_notification_fn notify;
  46. /* A pointer to the next listener */
  47. struct read_only_listener *next;
  48. };
  49. struct vdo_thread {
  50. struct vdo *vdo;
  51. thread_id_t thread_id;
  52. struct vdo_work_queue *queue;
  53. /*
  54. * Each thread maintains its own notion of whether the VDO is read-only so that the
  55. * read-only state can be checked from any base thread without worrying about
  56. * synchronization or thread safety. This does mean that knowledge of the VDO going
  57. * read-only does not occur simultaneously across the VDO's threads, but that does not seem
  58. * to cause any problems.
  59. */
  60. bool is_read_only;
  61. /*
  62. * A list of objects waiting to be notified on this thread that the VDO has entered
  63. * read-only mode.
  64. */
  65. struct read_only_listener *listeners;
  66. struct registered_thread allocating_thread;
  67. };
  68. /* Keep struct bio statistics atomically */
  69. struct atomic_bio_stats {
  70. atomic64_t read; /* Number of not REQ_WRITE bios */
  71. atomic64_t write; /* Number of REQ_WRITE bios */
  72. atomic64_t discard; /* Number of REQ_DISCARD bios */
  73. atomic64_t flush; /* Number of REQ_FLUSH bios */
  74. atomic64_t empty_flush; /* Number of REQ_PREFLUSH bios without data */
  75. atomic64_t fua; /* Number of REQ_FUA bios */
  76. };
  77. /* Counters are atomic since updates can arrive concurrently from arbitrary threads. */
  78. struct atomic_statistics {
  79. atomic64_t bios_submitted;
  80. atomic64_t bios_completed;
  81. atomic64_t flush_out;
  82. atomic64_t invalid_advice_pbn_count;
  83. atomic64_t no_space_error_count;
  84. atomic64_t read_only_error_count;
  85. struct atomic_bio_stats bios_in;
  86. struct atomic_bio_stats bios_in_partial;
  87. struct atomic_bio_stats bios_out;
  88. struct atomic_bio_stats bios_out_completed;
  89. struct atomic_bio_stats bios_acknowledged;
  90. struct atomic_bio_stats bios_acknowledged_partial;
  91. struct atomic_bio_stats bios_meta;
  92. struct atomic_bio_stats bios_meta_completed;
  93. struct atomic_bio_stats bios_journal;
  94. struct atomic_bio_stats bios_journal_completed;
  95. struct atomic_bio_stats bios_page_cache;
  96. struct atomic_bio_stats bios_page_cache_completed;
  97. };
  98. struct read_only_notifier {
  99. /* The completion for entering read-only mode */
  100. struct vdo_completion completion;
  101. /* A completion waiting for notifications to be drained or enabled */
  102. struct vdo_completion *waiter;
  103. /* Lock to protect the next two fields */
  104. spinlock_t lock;
  105. /* The code of the error which put the VDO into read-only mode */
  106. int read_only_error;
  107. /* The current state of the notifier (values described above) */
  108. enum notifier_state state;
  109. };
  110. /*
  111. * The thread ID returned when the current thread is not a vdo thread, or can not be determined
  112. * (usually due to being at interrupt context).
  113. */
  114. #define VDO_INVALID_THREAD_ID ((thread_id_t) -1)
  115. struct thread_config {
  116. zone_count_t logical_zone_count;
  117. zone_count_t physical_zone_count;
  118. zone_count_t hash_zone_count;
  119. thread_count_t bio_thread_count;
  120. thread_count_t thread_count;
  121. thread_id_t admin_thread;
  122. thread_id_t journal_thread;
  123. thread_id_t packer_thread;
  124. thread_id_t dedupe_thread;
  125. thread_id_t bio_ack_thread;
  126. thread_id_t cpu_thread;
  127. thread_id_t *logical_threads;
  128. thread_id_t *physical_threads;
  129. thread_id_t *hash_zone_threads;
  130. thread_id_t *bio_threads;
  131. };
  132. struct thread_count_config;
  133. struct vdo_super_block {
  134. /* The vio for reading and writing the super block to disk */
  135. struct vio vio;
  136. /* A buffer to hold the super block */
  137. u8 *buffer;
  138. /* Whether this super block may not be written */
  139. bool unwritable;
  140. };
  141. struct data_vio_pool;
  142. struct vdo_administrator {
  143. struct vdo_completion completion;
  144. struct admin_state state;
  145. atomic_t busy;
  146. u32 phase;
  147. struct completion callback_sync;
  148. };
  149. struct vdo {
  150. char thread_name_prefix[MAX_VDO_WORK_QUEUE_NAME_LEN];
  151. struct vdo_thread *threads;
  152. vdo_action_fn action;
  153. struct vdo_completion *completion;
  154. struct vio_tracer *vio_tracer;
  155. /* The atomic version of the state of this vdo */
  156. atomic_t state;
  157. /* The full state of all components */
  158. struct vdo_component_states states;
  159. /*
  160. * A counter value to attach to thread names and log messages to identify the individual
  161. * device.
  162. */
  163. unsigned int instance;
  164. /* The read-only notifier */
  165. struct read_only_notifier read_only_notifier;
  166. /* The load-time configuration of this vdo */
  167. struct device_config *device_config;
  168. /* The thread mapping */
  169. struct thread_config thread_config;
  170. /* The super block */
  171. struct vdo_super_block super_block;
  172. /* The partitioning of the underlying storage */
  173. struct layout layout;
  174. struct layout next_layout;
  175. struct dm_kcopyd_client *partition_copier;
  176. /* The block map */
  177. struct block_map *block_map;
  178. /* The journal for block map recovery */
  179. struct recovery_journal *recovery_journal;
  180. /* The slab depot */
  181. struct slab_depot *depot;
  182. /* The compressed-block packer */
  183. struct packer *packer;
  184. /* Whether incoming data should be compressed */
  185. bool compressing;
  186. /* The handler for flush requests */
  187. struct flusher *flusher;
  188. /* The state the vdo was in when loaded (primarily for unit tests) */
  189. enum vdo_state load_state;
  190. /* The logical zones of this vdo */
  191. struct logical_zones *logical_zones;
  192. /* The physical zones of this vdo */
  193. struct physical_zones *physical_zones;
  194. /* The hash lock zones of this vdo */
  195. struct hash_zones *hash_zones;
  196. /* Bio submission manager used for sending bios to the storage device. */
  197. struct io_submitter *io_submitter;
  198. /* The pool of data_vios for servicing incoming bios */
  199. struct data_vio_pool *data_vio_pool;
  200. /* The manager for administrative operations */
  201. struct vdo_administrator admin;
  202. /* Flags controlling administrative operations */
  203. const struct admin_state_code *suspend_type;
  204. bool allocations_allowed;
  205. bool dump_on_shutdown;
  206. atomic_t processing_message;
  207. /*
  208. * Statistics
  209. * Atomic stats counters
  210. */
  211. struct atomic_statistics stats;
  212. /* Used to gather statistics without allocating memory */
  213. struct vdo_statistics stats_buffer;
  214. /* Protects the stats_buffer */
  215. struct mutex stats_mutex;
  216. /* A list of all device_configs referencing this vdo */
  217. struct list_head device_config_list;
  218. /* This VDO's list entry for the device registry */
  219. struct list_head registration;
  220. /* Underlying block device info. */
  221. u64 starting_sector_offset;
  222. struct volume_geometry geometry;
  223. /* N blobs of context data for LZ4 code, one per CPU thread. */
  224. char **compression_context;
  225. };
  226. /**
  227. * vdo_uses_bio_ack_queue() - Indicate whether the vdo is configured to use a separate work queue
  228. * for acknowledging received and processed bios.
  229. * @vdo: The vdo.
  230. *
  231. * Note that this directly controls the handling of write operations, but the compile-time flag
  232. * VDO_USE_BIO_ACK_QUEUE_FOR_READ is also checked for read operations.
  233. *
  234. * Return: Whether a bio-acknowledgement work queue is in use.
  235. */
  236. static inline bool vdo_uses_bio_ack_queue(struct vdo *vdo)
  237. {
  238. return vdo->device_config->thread_counts.bio_ack_threads > 0;
  239. }
  240. /**
  241. * typedef vdo_filter_fn - Method type for vdo matching methods.
  242. * @vdo: The vdo to match.
  243. * @context: A parameter for the filter to use.
  244. *
  245. * Return: True if the vdo matches the filter criteria, false if it doesn't.
  246. */
  247. typedef bool (*vdo_filter_fn)(struct vdo *vdo, const void *context);
  248. void vdo_initialize_device_registry_once(void);
  249. struct vdo * __must_check vdo_find_matching(vdo_filter_fn filter, const void *context);
  250. int __must_check vdo_make_thread(struct vdo *vdo, thread_id_t thread_id,
  251. const struct vdo_work_queue_type *type,
  252. unsigned int queue_count, void *contexts[]);
  253. static inline int __must_check vdo_make_default_thread(struct vdo *vdo,
  254. thread_id_t thread_id)
  255. {
  256. return vdo_make_thread(vdo, thread_id, NULL, 1, NULL);
  257. }
  258. int __must_check vdo_make(unsigned int instance, struct device_config *config,
  259. char **reason, struct vdo **vdo_ptr);
  260. void vdo_destroy(struct vdo *vdo);
  261. void vdo_load_super_block(struct vdo *vdo, struct vdo_completion *parent);
  262. struct block_device * __must_check vdo_get_backing_device(const struct vdo *vdo);
  263. const char * __must_check vdo_get_device_name(const struct dm_target *target);
  264. int __must_check vdo_synchronous_flush(struct vdo *vdo);
  265. const struct admin_state_code * __must_check vdo_get_admin_state(const struct vdo *vdo);
  266. bool vdo_set_compressing(struct vdo *vdo, bool enable);
  267. bool vdo_get_compressing(struct vdo *vdo);
  268. void vdo_fetch_statistics(struct vdo *vdo, struct vdo_statistics *stats);
  269. thread_id_t vdo_get_callback_thread_id(void);
  270. enum vdo_state __must_check vdo_get_state(const struct vdo *vdo);
  271. void vdo_set_state(struct vdo *vdo, enum vdo_state state);
  272. void vdo_save_components(struct vdo *vdo, struct vdo_completion *parent);
  273. int vdo_register_read_only_listener(struct vdo *vdo, void *listener,
  274. vdo_read_only_notification_fn notification,
  275. thread_id_t thread_id);
  276. int vdo_enable_read_only_entry(struct vdo *vdo);
  277. void vdo_wait_until_not_entering_read_only_mode(struct vdo_completion *parent);
  278. void vdo_allow_read_only_mode_entry(struct vdo_completion *parent);
  279. void vdo_enter_read_only_mode(struct vdo *vdo, int error_code);
  280. bool __must_check vdo_is_read_only(struct vdo *vdo);
  281. bool __must_check vdo_in_read_only_mode(const struct vdo *vdo);
  282. bool __must_check vdo_in_recovery_mode(const struct vdo *vdo);
  283. void vdo_enter_recovery_mode(struct vdo *vdo);
  284. void vdo_assert_on_admin_thread(const struct vdo *vdo, const char *name);
  285. void vdo_assert_on_logical_zone_thread(const struct vdo *vdo, zone_count_t logical_zone,
  286. const char *name);
  287. void vdo_assert_on_physical_zone_thread(const struct vdo *vdo, zone_count_t physical_zone,
  288. const char *name);
  289. int __must_check vdo_get_physical_zone(const struct vdo *vdo, physical_block_number_t pbn,
  290. struct physical_zone **zone_ptr);
  291. void vdo_dump_status(const struct vdo *vdo);
  292. #endif /* VDO_H */