13bd94003SHeinz Mauelshagen /* SPDX-License-Identifier: GPL-2.0-only */ 2742c8fdcSJoe Thornber /* 3742c8fdcSJoe Thornber * Copyright (C) 2011-2017 Red Hat, Inc. 4742c8fdcSJoe Thornber * 5742c8fdcSJoe Thornber * This file is released under the GPL. 6742c8fdcSJoe Thornber */ 7742c8fdcSJoe Thornber 8742c8fdcSJoe Thornber #ifndef DM_BIO_PRISON_V2_H 9742c8fdcSJoe Thornber #define DM_BIO_PRISON_V2_H 10742c8fdcSJoe Thornber 11742c8fdcSJoe Thornber #include "persistent-data/dm-block-manager.h" /* FIXME: for dm_block_t */ 12742c8fdcSJoe Thornber #include "dm-thin-metadata.h" /* FIXME: for dm_thin_id */ 13742c8fdcSJoe Thornber 14742c8fdcSJoe Thornber #include <linux/bio.h> 15742c8fdcSJoe Thornber #include <linux/rbtree.h> 16742c8fdcSJoe Thornber #include <linux/workqueue.h> 17742c8fdcSJoe Thornber 18742c8fdcSJoe Thornber /*----------------------------------------------------------------*/ 19742c8fdcSJoe Thornber 20742c8fdcSJoe Thornber int dm_bio_prison_init_v2(void); 21742c8fdcSJoe Thornber void dm_bio_prison_exit_v2(void); 22742c8fdcSJoe Thornber 23742c8fdcSJoe Thornber /* 24742c8fdcSJoe Thornber * Sometimes we can't deal with a bio straight away. We put them in prison 25742c8fdcSJoe Thornber * where they can't cause any mischief. Bios are put in a cell identified 26742c8fdcSJoe Thornber * by a key, multiple bios can be in the same cell. When the cell is 27742c8fdcSJoe Thornber * subsequently unlocked the bios become available. 28742c8fdcSJoe Thornber */ 29742c8fdcSJoe Thornber struct dm_bio_prison_v2; 30742c8fdcSJoe Thornber 31742c8fdcSJoe Thornber /* 32742c8fdcSJoe Thornber * Keys define a range of blocks within either a virtual or physical 33742c8fdcSJoe Thornber * device. 34742c8fdcSJoe Thornber */ 35742c8fdcSJoe Thornber struct dm_cell_key_v2 { 36742c8fdcSJoe Thornber int virtual; 37742c8fdcSJoe Thornber dm_thin_id dev; 38742c8fdcSJoe Thornber dm_block_t block_begin, block_end; 39742c8fdcSJoe Thornber }; 40742c8fdcSJoe Thornber 41742c8fdcSJoe Thornber /* 42742c8fdcSJoe Thornber * Treat this as opaque, only in header so callers can manage allocation 43742c8fdcSJoe Thornber * themselves. 44742c8fdcSJoe Thornber */ 45742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 { 46742c8fdcSJoe Thornber // FIXME: pack these 47742c8fdcSJoe Thornber bool exclusive_lock; 48*86a3238cSHeinz Mauelshagen unsigned int exclusive_level; 49*86a3238cSHeinz Mauelshagen unsigned int shared_count; 50742c8fdcSJoe Thornber struct work_struct *quiesce_continuation; 51742c8fdcSJoe Thornber 52742c8fdcSJoe Thornber struct rb_node node; 53742c8fdcSJoe Thornber struct dm_cell_key_v2 key; 54742c8fdcSJoe Thornber struct bio_list bios; 55742c8fdcSJoe Thornber }; 56742c8fdcSJoe Thornber 57742c8fdcSJoe Thornber struct dm_bio_prison_v2 *dm_bio_prison_create_v2(struct workqueue_struct *wq); 58742c8fdcSJoe Thornber void dm_bio_prison_destroy_v2(struct dm_bio_prison_v2 *prison); 59742c8fdcSJoe Thornber 60742c8fdcSJoe Thornber /* 61742c8fdcSJoe Thornber * These two functions just wrap a mempool. This is a transitory step: 62742c8fdcSJoe Thornber * Eventually all bio prison clients should manage their own cell memory. 63742c8fdcSJoe Thornber * 64742c8fdcSJoe Thornber * Like mempool_alloc(), dm_bio_prison_alloc_cell_v2() can only fail if called 65742c8fdcSJoe Thornber * in interrupt context or passed GFP_NOWAIT. 66742c8fdcSJoe Thornber */ 67742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *dm_bio_prison_alloc_cell_v2(struct dm_bio_prison_v2 *prison, 68742c8fdcSJoe Thornber gfp_t gfp); 69742c8fdcSJoe Thornber void dm_bio_prison_free_cell_v2(struct dm_bio_prison_v2 *prison, 70742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell); 71742c8fdcSJoe Thornber 72742c8fdcSJoe Thornber /* 73742c8fdcSJoe Thornber * Shared locks have a bio associated with them. 74742c8fdcSJoe Thornber * 75742c8fdcSJoe Thornber * If the lock is granted the caller can continue to use the bio, and must 76742c8fdcSJoe Thornber * call dm_cell_put_v2() to drop the reference count when finished using it. 77742c8fdcSJoe Thornber * 78742c8fdcSJoe Thornber * If the lock cannot be granted then the bio will be tracked within the 79742c8fdcSJoe Thornber * cell, and later given to the holder of the exclusive lock. 80742c8fdcSJoe Thornber * 81742c8fdcSJoe Thornber * See dm_cell_lock_v2() for discussion of the lock_level parameter. 82742c8fdcSJoe Thornber * 83742c8fdcSJoe Thornber * Compare *cell_result with cell_prealloc to see if the prealloc was used. 84742c8fdcSJoe Thornber * If cell_prealloc was used then inmate wasn't added to it. 85742c8fdcSJoe Thornber * 86742c8fdcSJoe Thornber * Returns true if the lock is granted. 87742c8fdcSJoe Thornber */ 88742c8fdcSJoe Thornber bool dm_cell_get_v2(struct dm_bio_prison_v2 *prison, 89742c8fdcSJoe Thornber struct dm_cell_key_v2 *key, 90*86a3238cSHeinz Mauelshagen unsigned int lock_level, 91742c8fdcSJoe Thornber struct bio *inmate, 92742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell_prealloc, 93742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 **cell_result); 94742c8fdcSJoe Thornber 95742c8fdcSJoe Thornber /* 96742c8fdcSJoe Thornber * Decrement the shared reference count for the lock. Returns true if 97742c8fdcSJoe Thornber * returning ownership of the cell (ie. you should free it). 98742c8fdcSJoe Thornber */ 99742c8fdcSJoe Thornber bool dm_cell_put_v2(struct dm_bio_prison_v2 *prison, 100742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell); 101742c8fdcSJoe Thornber 102742c8fdcSJoe Thornber /* 103742c8fdcSJoe Thornber * Locks a cell. No associated bio. Exclusive locks get priority. These 104742c8fdcSJoe Thornber * locks constrain whether the io locks are granted according to level. 105742c8fdcSJoe Thornber * 106742c8fdcSJoe Thornber * Shared locks will still be granted if the lock_level is > (not = to) the 107742c8fdcSJoe Thornber * exclusive lock level. 108742c8fdcSJoe Thornber * 109742c8fdcSJoe Thornber * If an _exclusive_ lock is already held then -EBUSY is returned. 110742c8fdcSJoe Thornber * 111742c8fdcSJoe Thornber * Return values: 112742c8fdcSJoe Thornber * < 0 - error 113742c8fdcSJoe Thornber * 0 - locked; no quiescing needed 114742c8fdcSJoe Thornber * 1 - locked; quiescing needed 115742c8fdcSJoe Thornber */ 116742c8fdcSJoe Thornber int dm_cell_lock_v2(struct dm_bio_prison_v2 *prison, 117742c8fdcSJoe Thornber struct dm_cell_key_v2 *key, 118*86a3238cSHeinz Mauelshagen unsigned int lock_level, 119742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell_prealloc, 120742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 **cell_result); 121742c8fdcSJoe Thornber 122742c8fdcSJoe Thornber void dm_cell_quiesce_v2(struct dm_bio_prison_v2 *prison, 123742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell, 124742c8fdcSJoe Thornber struct work_struct *continuation); 125742c8fdcSJoe Thornber 126742c8fdcSJoe Thornber /* 127742c8fdcSJoe Thornber * Promotes an _exclusive_ lock to a higher lock level. 128742c8fdcSJoe Thornber * 129742c8fdcSJoe Thornber * Return values: 130742c8fdcSJoe Thornber * < 0 - error 131742c8fdcSJoe Thornber * 0 - promoted; no quiescing needed 132742c8fdcSJoe Thornber * 1 - promoted; quiescing needed 133742c8fdcSJoe Thornber */ 134742c8fdcSJoe Thornber int dm_cell_lock_promote_v2(struct dm_bio_prison_v2 *prison, 135742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell, 136*86a3238cSHeinz Mauelshagen unsigned int new_lock_level); 137742c8fdcSJoe Thornber 138742c8fdcSJoe Thornber /* 139742c8fdcSJoe Thornber * Adds any held bios to the bio list. 140742c8fdcSJoe Thornber * 141742c8fdcSJoe Thornber * There may be shared locks still held at this point even if you quiesced 142742c8fdcSJoe Thornber * (ie. different lock levels). 143742c8fdcSJoe Thornber * 144742c8fdcSJoe Thornber * Returns true if returning ownership of the cell (ie. you should free 145742c8fdcSJoe Thornber * it). 146742c8fdcSJoe Thornber */ 147742c8fdcSJoe Thornber bool dm_cell_unlock_v2(struct dm_bio_prison_v2 *prison, 148742c8fdcSJoe Thornber struct dm_bio_prison_cell_v2 *cell, 149742c8fdcSJoe Thornber struct bio_list *bios); 150742c8fdcSJoe Thornber 151742c8fdcSJoe Thornber /*----------------------------------------------------------------*/ 152742c8fdcSJoe Thornber 153742c8fdcSJoe Thornber #endif 154