blob: b03988667740e6cb3e8d68d61bd82c01cebff622 [file] [log] [blame]
Mike Snitzer4f81a412012-10-12 21:02:13 +01001/*
2 * Copyright (C) 2011-2012 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#ifndef DM_BIO_PRISON_H
8#define DM_BIO_PRISON_H
9
10#include "persistent-data/dm-block-manager.h" /* FIXME: for dm_block_t */
11#include "dm-thin-metadata.h" /* FIXME: for dm_thin_id */
12
Mike Snitzer4f81a412012-10-12 21:02:13 +010013#include <linux/bio.h>
Joe Thornbera195db22014-10-06 16:30:06 -040014#include <linux/rbtree.h>
Mike Snitzer4f81a412012-10-12 21:02:13 +010015
16/*----------------------------------------------------------------*/
17
18/*
19 * Sometimes we can't deal with a bio straight away. We put them in prison
20 * where they can't cause any mischief. Bios are put in a cell identified
21 * by a key, multiple bios can be in the same cell. When the cell is
22 * subsequently unlocked the bios become available.
23 */
24struct dm_bio_prison;
Mike Snitzer4f81a412012-10-12 21:02:13 +010025
26/* FIXME: this needs to be more abstract */
27struct dm_cell_key {
28 int virtual;
29 dm_thin_id dev;
30 dm_block_t block;
31};
32
Joe Thornber025b9682013-03-01 22:45:50 +000033/*
34 * Treat this as opaque, only in header so callers can manage allocation
35 * themselves.
36 */
37struct dm_bio_prison_cell {
Joe Thornbera374bb22014-10-10 13:43:14 +010038 struct list_head user_list; /* for client use */
Joe Thornbera195db22014-10-06 16:30:06 -040039 struct rb_node node;
40
Joe Thornber025b9682013-03-01 22:45:50 +000041 struct dm_cell_key key;
42 struct bio *holder;
43 struct bio_list bios;
44};
45
Joe Thornbera195db22014-10-06 16:30:06 -040046struct dm_bio_prison *dm_bio_prison_create(void);
Mike Snitzer4f81a412012-10-12 21:02:13 +010047void dm_bio_prison_destroy(struct dm_bio_prison *prison);
48
49/*
Joe Thornber6beca5e2013-03-01 22:45:50 +000050 * These two functions just wrap a mempool. This is a transitory step:
51 * Eventually all bio prison clients should manage their own cell memory.
52 *
53 * Like mempool_alloc(), dm_bio_prison_alloc_cell() can only fail if called
54 * in interrupt context or passed GFP_NOWAIT.
55 */
56struct dm_bio_prison_cell *dm_bio_prison_alloc_cell(struct dm_bio_prison *prison,
57 gfp_t gfp);
58void dm_bio_prison_free_cell(struct dm_bio_prison *prison,
59 struct dm_bio_prison_cell *cell);
60
61/*
Joe Thornberc6b4fcb2013-03-01 22:45:51 +000062 * Creates, or retrieves a cell for the given key.
63 *
64 * Returns 1 if pre-existing cell returned, zero if new cell created using
65 * @cell_prealloc.
66 */
67int dm_get_cell(struct dm_bio_prison *prison,
68 struct dm_cell_key *key,
69 struct dm_bio_prison_cell *cell_prealloc,
70 struct dm_bio_prison_cell **cell_result);
71
72/*
Joe Thornber6beca5e2013-03-01 22:45:50 +000073 * An atomic op that combines retrieving a cell, and adding a bio to it.
Mike Snitzer4f81a412012-10-12 21:02:13 +010074 *
75 * Returns 1 if the cell was already held, 0 if @inmate is the new holder.
76 */
Joe Thornber6beca5e2013-03-01 22:45:50 +000077int dm_bio_detain(struct dm_bio_prison *prison,
78 struct dm_cell_key *key,
79 struct bio *inmate,
80 struct dm_bio_prison_cell *cell_prealloc,
81 struct dm_bio_prison_cell **cell_result);
Mike Snitzer4f81a412012-10-12 21:02:13 +010082
Joe Thornber6beca5e2013-03-01 22:45:50 +000083void dm_cell_release(struct dm_bio_prison *prison,
84 struct dm_bio_prison_cell *cell,
85 struct bio_list *bios);
86void dm_cell_release_no_holder(struct dm_bio_prison *prison,
87 struct dm_bio_prison_cell *cell,
88 struct bio_list *inmates);
89void dm_cell_error(struct dm_bio_prison *prison,
Mike Snitzeraf918052014-05-22 14:32:51 -040090 struct dm_bio_prison_cell *cell, int error);
Mike Snitzer4f81a412012-10-12 21:02:13 +010091
Joe Thornber2d759a42014-10-10 15:27:16 +010092/*
93 * Visits the cell and then releases. Guarantees no new inmates are
94 * inserted between the visit and release.
95 */
96void dm_cell_visit_release(struct dm_bio_prison *prison,
97 void (*visit_fn)(void *, struct dm_bio_prison_cell *),
98 void *context, struct dm_bio_prison_cell *cell);
99
Mike Snitzer4f81a412012-10-12 21:02:13 +0100100/*----------------------------------------------------------------*/
101
102/*
103 * We use the deferred set to keep track of pending reads to shared blocks.
104 * We do this to ensure the new mapping caused by a write isn't performed
105 * until these prior reads have completed. Otherwise the insertion of the
106 * new mapping could free the old block that the read bios are mapped to.
107 */
108
109struct dm_deferred_set;
110struct dm_deferred_entry;
111
112struct dm_deferred_set *dm_deferred_set_create(void);
113void dm_deferred_set_destroy(struct dm_deferred_set *ds);
114
115struct dm_deferred_entry *dm_deferred_entry_inc(struct dm_deferred_set *ds);
116void dm_deferred_entry_dec(struct dm_deferred_entry *entry, struct list_head *head);
117int dm_deferred_set_add_work(struct dm_deferred_set *ds, struct list_head *work);
118
119/*----------------------------------------------------------------*/
120
121#endif