dm-block-manager.h 3.8 KB

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