^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2011 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This file is released under the GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #ifndef _LINUX_DM_TRANSACTION_MANAGER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define _LINUX_DM_TRANSACTION_MANAGER_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "dm-block-manager.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct dm_transaction_manager;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct dm_space_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /*----------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * This manages the scope of a transaction. It also enforces immutability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * of the on-disk data structures by limiting access to writeable blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Clients should not fiddle with the block manager directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void dm_tm_destroy(struct dm_transaction_manager *tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * The non-blocking version of a transaction manager is intended for use in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * fast path code that needs to do lookups e.g. a dm mapping function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * You create the non-blocking variant from a normal tm. The interface is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * the same, except that most functions will just return -EWOULDBLOCK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Methods that return void yet may block should not be called on a clone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * viz. dm_tm_inc, dm_tm_dec. Call dm_tm_destroy() as you would with a normal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * tm when you've finished with it. You may not destroy the original prior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * to clones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct dm_transaction_manager *dm_tm_create_non_blocking_clone(struct dm_transaction_manager *real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * We use a 2-phase commit here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * i) Make all changes for the transaction *except* for the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Then call dm_tm_pre_commit() to flush them to disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * ii) Lock your superblock. Update. Then call dm_tm_commit() which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * unlock the superblock and flush it. No other blocks should be updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * during this period. Care should be taken to never unlock a partially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * updated superblock; perform any operations that could fail *before* you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * take the superblock lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int dm_tm_pre_commit(struct dm_transaction_manager *tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *superblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * These methods are the only way to get hold of a writeable block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * dm_tm_new_block() is pretty self-explanatory. Make sure you do actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * write to the whole of @data before you unlock, otherwise you could get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * a data leak. (The other option is for tm_new_block() to zero new blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * before handing them out, which will be redundant in most, if not all,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * cases).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * Zeroes the new block and returns with write lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int dm_tm_new_block(struct dm_transaction_manager *tm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct dm_block **result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * dm_tm_shadow_block() allocates a new block and copies the data from @orig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * to it. It then decrements the reference count on original block. Use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * this to update the contents of a block in a data structure, don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * confuse this with a clone - you shouldn't access the orig block after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * this operation. Because the tm knows the scope of the transaction it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * can optimise requests for a shadow of a shadow to a no-op. Don't forget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * to unlock when you've finished with the shadow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * The @inc_children flag is used to tell the caller whether it needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * adjust reference counts for children. (Data in the block may refer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * other blocks.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Shadowing implicitly drops a reference on @orig so you must not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * it locked when you call this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int dm_tm_shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct dm_block **result, int *inc_children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * Read access. You can lock any block you want. If there's a write lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * on it outstanding then it'll block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int dm_tm_read_lock(struct dm_transaction_manager *tm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct dm_block_validator *v,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct dm_block **result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) void dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Functions for altering the reference count of a block directly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) void dm_tm_inc(struct dm_transaction_manager *tm, dm_block_t b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) void dm_tm_dec(struct dm_transaction_manager *tm, dm_block_t b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int dm_tm_ref(struct dm_transaction_manager *tm, dm_block_t b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) uint32_t *result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct dm_block_manager *dm_tm_get_bm(struct dm_transaction_manager *tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * If you're using a non-blocking clone the tm will build up a list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * requested blocks that weren't in core. This call will request those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * blocks to be prefetched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) void dm_tm_issue_prefetches(struct dm_transaction_manager *tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * A little utility that ties the knot by producing a transaction manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * that has a space map managed by the transaction manager...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Returns a tm that has an open transaction to write the new disk sm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * Caller should store the new sm root and commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * The superblock location is passed so the metadata space map knows it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * shouldn't be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int dm_tm_create_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct dm_transaction_manager **tm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct dm_space_map **sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int dm_tm_open_with_sm(struct dm_block_manager *bm, dm_block_t sb_location,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) void *sm_root, size_t root_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct dm_transaction_manager **tm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct dm_space_map **sm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #endif /* _LINUX_DM_TRANSACTION_MANAGER_H */