dm-block-manager.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2011 Red Hat, Inc.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #ifndef _LINUX_DM_BLOCK_MANAGER_H
  8. #define _LINUX_DM_BLOCK_MANAGER_H
  9. #include <linux/types.h>
  10. #include <linux/blkdev.h>
  11. /*----------------------------------------------------------------*/
  12. /*
  13. * Block number.
  14. */
  15. typedef uint64_t dm_block_t;
  16. struct dm_block;
  17. dm_block_t dm_block_location(struct dm_block *b);
  18. void *dm_block_data(struct dm_block *b);
  19. /*----------------------------------------------------------------*/
  20. /*
  21. * @name should be a unique identifier for the block manager, no longer
  22. * than 32 chars.
  23. *
  24. * @max_held_per_thread should be the maximum number of locks, read or
  25. * write, that an individual thread holds at any one time.
  26. */
  27. struct dm_block_manager;
  28. struct dm_block_manager *dm_block_manager_create(
  29. struct block_device *bdev, unsigned int block_size,
  30. unsigned int max_held_per_thread);
  31. void dm_block_manager_destroy(struct dm_block_manager *bm);
  32. void dm_block_manager_reset(struct dm_block_manager *bm);
  33. unsigned int dm_bm_block_size(struct dm_block_manager *bm);
  34. dm_block_t dm_bm_nr_blocks(struct dm_block_manager *bm);
  35. /*----------------------------------------------------------------*/
  36. /*
  37. * The validator allows the caller to verify newly-read data and modify
  38. * the data just before writing, e.g. to calculate checksums. It's
  39. * important to be consistent with your use of validators. The only time
  40. * you can change validators is if you call dm_bm_write_lock_zero.
  41. */
  42. struct dm_block_validator {
  43. const char *name;
  44. void (*prepare_for_write)(const struct dm_block_validator *v,
  45. struct dm_block *b, size_t block_size);
  46. /*
  47. * Return 0 if the checksum is valid or < 0 on error.
  48. */
  49. int (*check)(const struct dm_block_validator *v,
  50. struct dm_block *b, size_t block_size);
  51. };
  52. /*----------------------------------------------------------------*/
  53. /*
  54. * You can have multiple concurrent readers or a single writer holding a
  55. * block lock.
  56. */
  57. /*
  58. * dm_bm_lock() locks a block and returns through @result a pointer to
  59. * memory that holds a copy of that block. If you have write-locked the
  60. * block then any changes you make to memory pointed to by @result will be
  61. * written back to the disk sometime after dm_bm_unlock is called.
  62. */
  63. int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
  64. const struct dm_block_validator *v,
  65. struct dm_block **result);
  66. int dm_bm_write_lock(struct dm_block_manager *bm, dm_block_t b,
  67. const struct dm_block_validator *v,
  68. struct dm_block **result);
  69. /*
  70. * The *_try_lock variants return -EWOULDBLOCK if the block isn't
  71. * available immediately.
  72. */
  73. int dm_bm_read_try_lock(struct dm_block_manager *bm, dm_block_t b,
  74. const struct dm_block_validator *v,
  75. struct dm_block **result);
  76. /*
  77. * Use dm_bm_write_lock_zero() when you know you're going to
  78. * overwrite the block completely. It saves a disk read.
  79. */
  80. int dm_bm_write_lock_zero(struct dm_block_manager *bm, dm_block_t b,
  81. const struct dm_block_validator *v,
  82. struct dm_block **result);
  83. void dm_bm_unlock(struct dm_block *b);
  84. /*
  85. * It's a common idiom to have a superblock that should be committed last.
  86. *
  87. * @superblock should be write-locked on entry. It will be unlocked during
  88. * this function. All dirty blocks are guaranteed to be written and flushed
  89. * before the superblock.
  90. *
  91. * This method always blocks.
  92. */
  93. int dm_bm_flush(struct dm_block_manager *bm);
  94. /*
  95. * Request data is prefetched into the cache.
  96. */
  97. void dm_bm_prefetch(struct dm_block_manager *bm, dm_block_t b);
  98. /*
  99. * Switches the bm to a read only mode. Once read-only mode
  100. * has been entered the following functions will return -EPERM.
  101. *
  102. * dm_bm_write_lock
  103. * dm_bm_write_lock_zero
  104. * dm_bm_flush_and_unlock
  105. *
  106. * Additionally you should not use dm_bm_unlock_move, however no error will
  107. * be returned if you do.
  108. */
  109. bool dm_bm_is_read_only(struct dm_block_manager *bm);
  110. void dm_bm_set_read_only(struct dm_block_manager *bm);
  111. void dm_bm_set_read_write(struct dm_block_manager *bm);
  112. u32 dm_bm_checksum(const void *data, size_t len, u32 init_xor);
  113. /*----------------------------------------------------------------*/
  114. #endif /* _LINUX_DM_BLOCK_MANAGER_H */