Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Sistina Software (UK) Limited. |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the LGPL. |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LINUX_DEVICE_MAPPER_H |
| 9 | #define _LINUX_DEVICE_MAPPER_H |
| 10 | |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 11 | #include <linux/bio.h> |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 12 | #include <linux/blkdev.h> |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 13 | #include <linux/math64.h> |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 14 | #include <linux/ratelimit.h> |
Heinz Mauelshagen | 416cd17 | 2008-04-24 21:43:35 +0100 | [diff] [blame] | 15 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 16 | struct dm_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | struct dm_target; |
| 18 | struct dm_table; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 19 | struct mapped_device; |
Milan Broz | f6fccb1 | 2008-07-21 12:00:37 +0100 | [diff] [blame] | 20 | struct bio_vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Mike Snitzer | e83068a | 2016-05-24 21:16:51 -0400 | [diff] [blame^] | 22 | /* |
| 23 | * Type of table, mapped_device's mempool and request_queue |
| 24 | */ |
| 25 | #define DM_TYPE_NONE 0 |
| 26 | #define DM_TYPE_BIO_BASED 1 |
| 27 | #define DM_TYPE_REQUEST_BASED 2 |
| 28 | #define DM_TYPE_MQ_REQUEST_BASED 3 |
| 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t; |
| 31 | |
| 32 | union map_info { |
| 33 | void *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * In the constructor the target parameter will already have the |
| 38 | * table, type, begin and len fields filled in. |
| 39 | */ |
| 40 | typedef int (*dm_ctr_fn) (struct dm_target *target, |
| 41 | unsigned int argc, char **argv); |
| 42 | |
| 43 | /* |
| 44 | * The destructor doesn't need to free the dm_target, just |
| 45 | * anything hidden ti->private. |
| 46 | */ |
| 47 | typedef void (*dm_dtr_fn) (struct dm_target *ti); |
| 48 | |
| 49 | /* |
| 50 | * The map function must return: |
| 51 | * < 0: error |
| 52 | * = 0: The target will handle the io by resubmitting it later |
Kiyoshi Ueda | 45cbcd7 | 2006-12-08 02:41:05 -0800 | [diff] [blame] | 53 | * = 1: simple remap complete |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 54 | * = 2: The target wants to push back the io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | */ |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 56 | typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio); |
Kiyoshi Ueda | 7d76345 | 2009-01-06 03:05:07 +0000 | [diff] [blame] | 57 | typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone, |
| 58 | union map_info *map_context); |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 59 | typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti, |
| 60 | struct request *rq, |
| 61 | union map_info *map_context, |
| 62 | struct request **clone); |
| 63 | typedef void (*dm_release_clone_request_fn) (struct request *clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Returns: |
| 67 | * < 0 : error (currently ignored) |
| 68 | * 0 : ended successfully |
| 69 | * 1 : for some reason the io has still not completed (eg, |
| 70 | * multipath target might want to requeue a failed io). |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 71 | * 2 : The target wants to push back the io |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | */ |
| 73 | typedef int (*dm_endio_fn) (struct dm_target *ti, |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 74 | struct bio *bio, int error); |
Kiyoshi Ueda | 7d76345 | 2009-01-06 03:05:07 +0000 | [diff] [blame] | 75 | typedef int (*dm_request_endio_fn) (struct dm_target *ti, |
| 76 | struct request *clone, int error, |
| 77 | union map_info *map_context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | typedef void (*dm_presuspend_fn) (struct dm_target *ti); |
Mike Snitzer | d67ee21 | 2014-10-28 20:13:31 -0400 | [diff] [blame] | 80 | typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | typedef void (*dm_postsuspend_fn) (struct dm_target *ti); |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 82 | typedef int (*dm_preresume_fn) (struct dm_target *ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | typedef void (*dm_resume_fn) (struct dm_target *ti); |
| 84 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 85 | typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type, |
| 86 | unsigned status_flags, char *result, unsigned maxlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv); |
| 89 | |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 90 | typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti, |
| 91 | struct block_device **bdev, fmode_t *mode); |
Milan Broz | aa129a2 | 2006-10-03 01:15:15 -0700 | [diff] [blame] | 92 | |
Alasdair G Kergon | 058ce5c | 2013-05-10 14:37:17 +0100 | [diff] [blame] | 93 | /* |
| 94 | * These iteration functions are typically used to check (and combine) |
| 95 | * properties of underlying devices. |
| 96 | * E.g. Does at least one underlying device support flush? |
| 97 | * Does any underlying device not support WRITE_SAME? |
| 98 | * |
| 99 | * The callout function is called once for each contiguous section of |
| 100 | * an underlying device. State can be maintained in *data. |
| 101 | * Return non-zero to stop iterating through any further devices. |
| 102 | */ |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 103 | typedef int (*iterate_devices_callout_fn) (struct dm_target *ti, |
| 104 | struct dm_dev *dev, |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 105 | sector_t start, sector_t len, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 106 | void *data); |
| 107 | |
Alasdair G Kergon | 058ce5c | 2013-05-10 14:37:17 +0100 | [diff] [blame] | 108 | /* |
| 109 | * This function must iterate through each section of device used by the |
| 110 | * target until it encounters a non-zero return code, which it then returns. |
| 111 | * Returns zero if no callout returned non-zero. |
| 112 | */ |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 113 | typedef int (*dm_iterate_devices_fn) (struct dm_target *ti, |
| 114 | iterate_devices_callout_fn fn, |
| 115 | void *data); |
| 116 | |
Mike Snitzer | 40bea43 | 2009-09-04 20:40:25 +0100 | [diff] [blame] | 117 | typedef void (*dm_io_hints_fn) (struct dm_target *ti, |
| 118 | struct queue_limits *limits); |
| 119 | |
Kiyoshi Ueda | 7d76345 | 2009-01-06 03:05:07 +0000 | [diff] [blame] | 120 | /* |
| 121 | * Returns: |
| 122 | * 0: The target can handle the next I/O immediately. |
| 123 | * 1: The target can't handle the next I/O immediately. |
| 124 | */ |
| 125 | typedef int (*dm_busy_fn) (struct dm_target *ti); |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | void dm_error(const char *message); |
| 128 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 129 | struct dm_dev { |
| 130 | struct block_device *bdev; |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 131 | fmode_t mode; |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 132 | char name[16]; |
| 133 | }; |
| 134 | |
DingXiang | 4df2bf4 | 2016-02-02 12:29:18 +0800 | [diff] [blame] | 135 | dev_t dm_get_dev_t(const char *path); |
| 136 | |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 137 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | * Constructors should call these functions to ensure destination devices |
| 139 | * are opened/closed correctly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | */ |
Nikanth Karthikesan | 8215d6e | 2010-03-06 02:32:27 +0000 | [diff] [blame] | 141 | int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode, |
Mike Snitzer | 11f0431 | 2014-06-03 10:30:28 -0400 | [diff] [blame] | 142 | struct dm_dev **result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | void dm_put_device(struct dm_target *ti, struct dm_dev *d); |
| 144 | |
| 145 | /* |
| 146 | * Information about a target type |
| 147 | */ |
Andi Kleen | ab4c14248 | 2009-01-06 03:05:09 +0000 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | struct target_type { |
Andi Kleen | ab4c14248 | 2009-01-06 03:05:09 +0000 | [diff] [blame] | 150 | uint64_t features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | const char *name; |
| 152 | struct module *module; |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 153 | unsigned version[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | dm_ctr_fn ctr; |
| 155 | dm_dtr_fn dtr; |
| 156 | dm_map_fn map; |
Kiyoshi Ueda | 7d76345 | 2009-01-06 03:05:07 +0000 | [diff] [blame] | 157 | dm_map_request_fn map_rq; |
Mike Snitzer | e5863d9 | 2014-12-17 21:08:12 -0500 | [diff] [blame] | 158 | dm_clone_and_map_request_fn clone_and_map_rq; |
| 159 | dm_release_clone_request_fn release_clone_rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | dm_endio_fn end_io; |
Kiyoshi Ueda | 7d76345 | 2009-01-06 03:05:07 +0000 | [diff] [blame] | 161 | dm_request_endio_fn rq_end_io; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | dm_presuspend_fn presuspend; |
Mike Snitzer | d67ee21 | 2014-10-28 20:13:31 -0400 | [diff] [blame] | 163 | dm_presuspend_undo_fn presuspend_undo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | dm_postsuspend_fn postsuspend; |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 165 | dm_preresume_fn preresume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | dm_resume_fn resume; |
| 167 | dm_status_fn status; |
| 168 | dm_message_fn message; |
Christoph Hellwig | e56f81e | 2015-10-15 14:10:50 +0200 | [diff] [blame] | 169 | dm_prepare_ioctl_fn prepare_ioctl; |
Kiyoshi Ueda | 7d76345 | 2009-01-06 03:05:07 +0000 | [diff] [blame] | 170 | dm_busy_fn busy; |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 171 | dm_iterate_devices_fn iterate_devices; |
Mike Snitzer | 40bea43 | 2009-09-04 20:40:25 +0100 | [diff] [blame] | 172 | dm_io_hints_fn io_hints; |
Cheng Renquan | 45194e4 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 173 | |
| 174 | /* For internal device-mapper use. */ |
| 175 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Alasdair G Kergon | 3791e2f | 2011-10-31 20:19:00 +0000 | [diff] [blame] | 178 | /* |
| 179 | * Target features |
| 180 | */ |
| 181 | |
| 182 | /* |
| 183 | * Any table that contains an instance of this target must have only one. |
| 184 | */ |
| 185 | #define DM_TARGET_SINGLETON 0x00000001 |
| 186 | #define dm_target_needs_singleton(type) ((type)->features & DM_TARGET_SINGLETON) |
| 187 | |
Alasdair G Kergon | cc6cbe1 | 2011-10-31 20:19:02 +0000 | [diff] [blame] | 188 | /* |
| 189 | * Indicates that a target does not support read-only devices. |
| 190 | */ |
| 191 | #define DM_TARGET_ALWAYS_WRITEABLE 0x00000002 |
| 192 | #define dm_target_always_writeable(type) \ |
| 193 | ((type)->features & DM_TARGET_ALWAYS_WRITEABLE) |
| 194 | |
Alasdair G Kergon | 36a0456 | 2011-10-31 20:19:04 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Any device that contains a table with an instance of this target may never |
| 197 | * have tables containing any different target type. |
| 198 | */ |
| 199 | #define DM_TARGET_IMMUTABLE 0x00000004 |
| 200 | #define dm_target_is_immutable(type) ((type)->features & DM_TARGET_IMMUTABLE) |
| 201 | |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 202 | /* |
Mike Snitzer | f083b09 | 2016-02-06 18:38:46 -0500 | [diff] [blame] | 203 | * Indicates that a target may replace any target; even immutable targets. |
| 204 | * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined. |
| 205 | */ |
| 206 | #define DM_TARGET_WILDCARD 0x00000008 |
| 207 | #define dm_target_is_wildcard(type) ((type)->features & DM_TARGET_WILDCARD) |
| 208 | |
| 209 | /* |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 210 | * Some targets need to be sent the same WRITE bio severals times so |
| 211 | * that they can send copies of it to different devices. This function |
| 212 | * examines any supplied bio and returns the number of copies of it the |
| 213 | * target requires. |
| 214 | */ |
| 215 | typedef unsigned (*dm_num_write_bios_fn) (struct dm_target *ti, struct bio *bio); |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | struct dm_target { |
| 218 | struct dm_table *table; |
| 219 | struct target_type *type; |
| 220 | |
| 221 | /* target limits */ |
| 222 | sector_t begin; |
| 223 | sector_t len; |
| 224 | |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 225 | /* If non-zero, maximum size of I/O submitted to a target. */ |
| 226 | uint32_t max_io_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 229 | * A number of zero-length barrier bios that will be submitted |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 230 | * to the target for the purpose of flushing cache. |
| 231 | * |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 232 | * The bio number can be accessed with dm_bio_get_target_bio_nr. |
| 233 | * It is a responsibility of the target driver to remap these bios |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 234 | * to the real underlying devices. |
| 235 | */ |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 236 | unsigned num_flush_bios; |
Mikulas Patocka | f9ab94c | 2009-06-22 10:12:20 +0100 | [diff] [blame] | 237 | |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 238 | /* |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 239 | * The number of discard bios that will be submitted to the target. |
| 240 | * The bio number can be accessed with dm_bio_get_target_bio_nr. |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 241 | */ |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 242 | unsigned num_discard_bios; |
Mike Snitzer | 5ae89a8 | 2010-08-12 04:14:08 +0100 | [diff] [blame] | 243 | |
Mike Snitzer | d54eaa5 | 2012-12-21 20:23:36 +0000 | [diff] [blame] | 244 | /* |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 245 | * The number of WRITE SAME bios that will be submitted to the target. |
| 246 | * The bio number can be accessed with dm_bio_get_target_bio_nr. |
Mike Snitzer | d54eaa5 | 2012-12-21 20:23:36 +0000 | [diff] [blame] | 247 | */ |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 248 | unsigned num_write_same_bios; |
Mike Snitzer | d54eaa5 | 2012-12-21 20:23:36 +0000 | [diff] [blame] | 249 | |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 250 | /* |
Mike Snitzer | 30187e1 | 2016-01-31 13:28:26 -0500 | [diff] [blame] | 251 | * The minimum number of extra bytes allocated in each io for the |
| 252 | * target to use. |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 253 | */ |
Mike Snitzer | 30187e1 | 2016-01-31 13:28:26 -0500 | [diff] [blame] | 254 | unsigned per_io_data_size; |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 255 | |
Alasdair G Kergon | b0d8ed4 | 2013-03-01 22:45:49 +0000 | [diff] [blame] | 256 | /* |
| 257 | * If defined, this function is called to find out how many |
| 258 | * duplicate bios should be sent to the target when writing |
| 259 | * data. |
| 260 | */ |
| 261 | dm_num_write_bios_fn num_write_bios; |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* target specific data */ |
| 264 | void *private; |
| 265 | |
| 266 | /* Used to provide an error string from the ctr */ |
| 267 | char *error; |
Mike Snitzer | 4c25932 | 2011-05-29 12:52:55 +0100 | [diff] [blame] | 268 | |
| 269 | /* |
Joe Thornber | 0e9c24e | 2012-07-27 15:08:07 +0100 | [diff] [blame] | 270 | * Set if this target needs to receive flushes regardless of |
| 271 | * whether or not its underlying devices have support. |
| 272 | */ |
| 273 | bool flush_supported:1; |
| 274 | |
| 275 | /* |
Mike Snitzer | 4c25932 | 2011-05-29 12:52:55 +0100 | [diff] [blame] | 276 | * Set if this target needs to receive discards regardless of |
| 277 | * whether or not its underlying devices have support. |
| 278 | */ |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 279 | bool discards_supported:1; |
Milan Broz | 983c7db | 2011-09-25 23:26:21 +0100 | [diff] [blame] | 280 | |
| 281 | /* |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 282 | * Set if the target required discard bios to be split |
Mikulas Patocka | 7acf027 | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 283 | * on max_io_len boundary. |
| 284 | */ |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 285 | bool split_discard_bios:1; |
Mikulas Patocka | 7acf027 | 2012-07-27 15:08:03 +0100 | [diff] [blame] | 286 | |
| 287 | /* |
Milan Broz | 983c7db | 2011-09-25 23:26:21 +0100 | [diff] [blame] | 288 | * Set if this target does not return zeroes on discarded blocks. |
| 289 | */ |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 290 | bool discard_zeroes_data_unsupported:1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
NeilBrown | 9d357b0 | 2011-01-13 20:00:01 +0000 | [diff] [blame] | 293 | /* Each target can link one of these into the table */ |
| 294 | struct dm_target_callbacks { |
| 295 | struct list_head list; |
| 296 | int (*congested_fn) (struct dm_target_callbacks *, int); |
| 297 | }; |
| 298 | |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 299 | /* |
| 300 | * For bio-based dm. |
| 301 | * One of these is allocated for each bio. |
| 302 | * This structure shouldn't be touched directly by target drivers. |
| 303 | * It is here so that we can inline dm_per_bio_data and |
| 304 | * dm_bio_from_per_bio_data |
| 305 | */ |
| 306 | struct dm_target_io { |
| 307 | struct dm_io *io; |
| 308 | struct dm_target *ti; |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 309 | unsigned target_bio_nr; |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 310 | unsigned *len_ptr; |
Mikulas Patocka | c0820cf | 2012-12-21 20:23:38 +0000 | [diff] [blame] | 311 | struct bio clone; |
| 312 | }; |
| 313 | |
| 314 | static inline void *dm_per_bio_data(struct bio *bio, size_t data_size) |
| 315 | { |
| 316 | return (char *)bio - offsetof(struct dm_target_io, clone) - data_size; |
| 317 | } |
| 318 | |
| 319 | static inline struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size) |
| 320 | { |
| 321 | return (struct bio *)((char *)data + data_size + offsetof(struct dm_target_io, clone)); |
| 322 | } |
| 323 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 324 | static inline unsigned dm_bio_get_target_bio_nr(const struct bio *bio) |
Mikulas Patocka | ddbd658 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 325 | { |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 326 | return container_of(bio, struct dm_target_io, clone)->target_bio_nr; |
Mikulas Patocka | ddbd658 | 2012-12-21 20:23:39 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | int dm_register_target(struct target_type *t); |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 330 | void dm_unregister_target(struct target_type *t); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 331 | |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 332 | /* |
| 333 | * Target argument parsing. |
| 334 | */ |
| 335 | struct dm_arg_set { |
| 336 | unsigned argc; |
| 337 | char **argv; |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * The minimum and maximum value of a numeric argument, together with |
| 342 | * the error message to use if the number is found to be outside that range. |
| 343 | */ |
| 344 | struct dm_arg { |
| 345 | unsigned min; |
| 346 | unsigned max; |
| 347 | char *error; |
| 348 | }; |
| 349 | |
| 350 | /* |
| 351 | * Validate the next argument, either returning it as *value or, if invalid, |
| 352 | * returning -EINVAL and setting *error. |
| 353 | */ |
| 354 | int dm_read_arg(struct dm_arg *arg, struct dm_arg_set *arg_set, |
| 355 | unsigned *value, char **error); |
| 356 | |
| 357 | /* |
| 358 | * Process the next argument as the start of a group containing between |
| 359 | * arg->min and arg->max further arguments. Either return the size as |
| 360 | * *num_args or, if invalid, return -EINVAL and set *error. |
| 361 | */ |
| 362 | int dm_read_arg_group(struct dm_arg *arg, struct dm_arg_set *arg_set, |
| 363 | unsigned *num_args, char **error); |
| 364 | |
| 365 | /* |
| 366 | * Return the current argument and shift to the next. |
| 367 | */ |
| 368 | const char *dm_shift_arg(struct dm_arg_set *as); |
| 369 | |
| 370 | /* |
| 371 | * Move through num_args arguments. |
| 372 | */ |
| 373 | void dm_consume_args(struct dm_arg_set *as, unsigned num_args); |
| 374 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 375 | /*----------------------------------------------------------------- |
| 376 | * Functions for creating and manipulating mapped devices. |
| 377 | * Drop the reference with dm_put when you finish with the object. |
| 378 | *---------------------------------------------------------------*/ |
| 379 | |
| 380 | /* |
| 381 | * DM_ANY_MINOR chooses the next available minor number. |
| 382 | */ |
| 383 | #define DM_ANY_MINOR (-1) |
| 384 | int dm_create(int minor, struct mapped_device **md); |
| 385 | |
| 386 | /* |
| 387 | * Reference counting for md. |
| 388 | */ |
| 389 | struct mapped_device *dm_get_md(dev_t dev); |
| 390 | void dm_get(struct mapped_device *md); |
Mikulas Patocka | 09ee96b | 2015-02-26 11:41:28 -0500 | [diff] [blame] | 391 | int dm_hold(struct mapped_device *md); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 392 | void dm_put(struct mapped_device *md); |
| 393 | |
| 394 | /* |
| 395 | * An arbitrary pointer may be stored alongside a mapped device. |
| 396 | */ |
| 397 | void dm_set_mdptr(struct mapped_device *md, void *ptr); |
| 398 | void *dm_get_mdptr(struct mapped_device *md); |
| 399 | |
| 400 | /* |
| 401 | * A device can still be used while suspended, but I/O is deferred. |
| 402 | */ |
Kiyoshi Ueda | a3d77d3 | 2006-12-08 02:41:04 -0800 | [diff] [blame] | 403 | int dm_suspend(struct mapped_device *md, unsigned suspend_flags); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 404 | int dm_resume(struct mapped_device *md); |
| 405 | |
| 406 | /* |
| 407 | * Event functions. |
| 408 | */ |
| 409 | uint32_t dm_get_event_nr(struct mapped_device *md); |
| 410 | int dm_wait_event(struct mapped_device *md, int event_nr); |
Mike Anderson | 7a8c3d3 | 2007-10-19 22:48:01 +0100 | [diff] [blame] | 411 | uint32_t dm_next_uevent_seq(struct mapped_device *md); |
| 412 | void dm_uevent_add(struct mapped_device *md, struct list_head *elist); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * Info functions. |
| 416 | */ |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 417 | const char *dm_device_name(struct mapped_device *md); |
Mike Anderson | 96a1f7d | 2007-10-19 22:47:59 +0100 | [diff] [blame] | 418 | int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 419 | struct gendisk *dm_disk(struct mapped_device *md); |
Kiyoshi Ueda | 64dbce5 | 2009-12-10 23:52:27 +0000 | [diff] [blame] | 420 | int dm_suspended(struct dm_target *ti); |
Kiyoshi Ueda | 2e93ccc | 2006-12-08 02:41:09 -0800 | [diff] [blame] | 421 | int dm_noflush_suspending(struct dm_target *ti); |
Mikulas Patocka | 1dd40c3 | 2014-03-14 18:41:24 -0400 | [diff] [blame] | 422 | void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors); |
Kiyoshi Ueda | cec47e3 | 2009-06-22 10:12:35 +0100 | [diff] [blame] | 423 | union map_info *dm_get_rq_mapinfo(struct request *rq); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 424 | |
Mike Snitzer | f84cb8a | 2013-09-19 12:13:58 -0400 | [diff] [blame] | 425 | struct queue_limits *dm_get_queue_limits(struct mapped_device *md); |
| 426 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 427 | /* |
| 428 | * Geometry functions. |
| 429 | */ |
| 430 | int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo); |
| 431 | int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo); |
| 432 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 433 | /*----------------------------------------------------------------- |
| 434 | * Functions for manipulating device-mapper tables. |
| 435 | *---------------------------------------------------------------*/ |
| 436 | |
| 437 | /* |
| 438 | * First create an empty table. |
| 439 | */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 440 | int dm_table_create(struct dm_table **result, fmode_t mode, |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 441 | unsigned num_targets, struct mapped_device *md); |
| 442 | |
| 443 | /* |
| 444 | * Then call this once for each target. |
| 445 | */ |
| 446 | int dm_table_add_target(struct dm_table *t, const char *type, |
| 447 | sector_t start, sector_t len, char *params); |
| 448 | |
| 449 | /* |
NeilBrown | 9d357b0 | 2011-01-13 20:00:01 +0000 | [diff] [blame] | 450 | * Target_ctr should call this if it needs to add any callbacks. |
| 451 | */ |
| 452 | void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb); |
| 453 | |
| 454 | /* |
Mike Snitzer | e83068a | 2016-05-24 21:16:51 -0400 | [diff] [blame^] | 455 | * Target can use this to set the table's type. |
| 456 | * Can only ever be called from a target's ctr. |
| 457 | * Useful for "hybrid" target (supports both bio-based |
| 458 | * and request-based). |
| 459 | */ |
| 460 | void dm_table_set_type(struct dm_table *t, unsigned type); |
| 461 | |
| 462 | /* |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 463 | * Finally call this to make the table ready for use. |
| 464 | */ |
| 465 | int dm_table_complete(struct dm_table *t); |
| 466 | |
| 467 | /* |
Mike Snitzer | 542f903 | 2012-07-27 15:08:00 +0100 | [diff] [blame] | 468 | * Target may require that it is never sent I/O larger than len. |
| 469 | */ |
| 470 | int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len); |
| 471 | |
| 472 | /* |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 473 | * Table reference counting. |
| 474 | */ |
Mikulas Patocka | 83d5e5b | 2013-07-10 23:41:18 +0100 | [diff] [blame] | 475 | struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx); |
| 476 | void dm_put_live_table(struct mapped_device *md, int srcu_idx); |
| 477 | void dm_sync_table(struct mapped_device *md); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Queries |
| 481 | */ |
| 482 | sector_t dm_table_get_size(struct dm_table *t); |
| 483 | unsigned int dm_table_get_num_targets(struct dm_table *t); |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 484 | fmode_t dm_table_get_mode(struct dm_table *t); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 485 | struct mapped_device *dm_table_get_md(struct dm_table *t); |
| 486 | |
| 487 | /* |
| 488 | * Trigger an event. |
| 489 | */ |
| 490 | void dm_table_event(struct dm_table *t); |
| 491 | |
| 492 | /* |
Mike Snitzer | 9974fa2 | 2014-02-28 15:33:43 +0100 | [diff] [blame] | 493 | * Run the queue for request-based targets. |
| 494 | */ |
| 495 | void dm_table_run_md_queue_async(struct dm_table *t); |
| 496 | |
| 497 | /* |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 498 | * The device must be suspended before calling this method. |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 499 | * Returns the previous table, which the caller must destroy. |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 500 | */ |
Alasdair G Kergon | 042d2a9 | 2009-12-10 23:52:24 +0000 | [diff] [blame] | 501 | struct dm_table *dm_swap_table(struct mapped_device *md, |
| 502 | struct dm_table *t); |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 503 | |
Mikulas Patocka | 5416090 | 2008-10-10 13:37:12 +0100 | [diff] [blame] | 504 | /* |
| 505 | * A wrapper around vmalloc. |
| 506 | */ |
| 507 | void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size); |
| 508 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 509 | /*----------------------------------------------------------------- |
| 510 | * Macros. |
| 511 | *---------------------------------------------------------------*/ |
| 512 | #define DM_NAME "device-mapper" |
| 513 | |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 514 | #ifdef CONFIG_PRINTK |
| 515 | extern struct ratelimit_state dm_ratelimit_state; |
| 516 | |
| 517 | #define dm_ratelimit() __ratelimit(&dm_ratelimit_state) |
| 518 | #else |
| 519 | #define dm_ratelimit() 0 |
| 520 | #endif |
| 521 | |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 522 | #define DMCRIT(f, arg...) \ |
| 523 | printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 524 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 525 | #define DMERR(f, arg...) \ |
| 526 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 527 | #define DMERR_LIMIT(f, arg...) \ |
| 528 | do { \ |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 529 | if (dm_ratelimit()) \ |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 530 | printk(KERN_ERR DM_NAME ": " DM_MSG_PREFIX ": " \ |
| 531 | f "\n", ## arg); \ |
| 532 | } while (0) |
| 533 | |
| 534 | #define DMWARN(f, arg...) \ |
| 535 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 536 | #define DMWARN_LIMIT(f, arg...) \ |
| 537 | do { \ |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 538 | if (dm_ratelimit()) \ |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 539 | printk(KERN_WARNING DM_NAME ": " DM_MSG_PREFIX ": " \ |
| 540 | f "\n", ## arg); \ |
| 541 | } while (0) |
| 542 | |
| 543 | #define DMINFO(f, arg...) \ |
| 544 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg) |
| 545 | #define DMINFO_LIMIT(f, arg...) \ |
| 546 | do { \ |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 547 | if (dm_ratelimit()) \ |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 548 | printk(KERN_INFO DM_NAME ": " DM_MSG_PREFIX ": " f \ |
| 549 | "\n", ## arg); \ |
| 550 | } while (0) |
| 551 | |
| 552 | #ifdef CONFIG_DM_DEBUG |
| 553 | # define DMDEBUG(f, arg...) \ |
| 554 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX " DEBUG: " f "\n", ## arg) |
| 555 | # define DMDEBUG_LIMIT(f, arg...) \ |
| 556 | do { \ |
Namhyung Kim | 71a1673 | 2011-10-31 20:18:54 +0000 | [diff] [blame] | 557 | if (dm_ratelimit()) \ |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 558 | printk(KERN_DEBUG DM_NAME ": " DM_MSG_PREFIX ": " f \ |
| 559 | "\n", ## arg); \ |
| 560 | } while (0) |
| 561 | #else |
| 562 | # define DMDEBUG(f, arg...) do {} while (0) |
| 563 | # define DMDEBUG_LIMIT(f, arg...) do {} while (0) |
| 564 | #endif |
| 565 | |
| 566 | #define DMEMIT(x...) sz += ((sz >= maxlen) ? \ |
| 567 | 0 : scnprintf(result + sz, maxlen - sz, x)) |
| 568 | |
| 569 | #define SECTOR_SHIFT 9 |
| 570 | |
| 571 | /* |
| 572 | * Definitions of return values from target end_io function. |
| 573 | */ |
| 574 | #define DM_ENDIO_INCOMPLETE 1 |
| 575 | #define DM_ENDIO_REQUEUE 2 |
| 576 | |
| 577 | /* |
| 578 | * Definitions of return values from target map function. |
| 579 | */ |
| 580 | #define DM_MAPIO_SUBMITTED 0 |
| 581 | #define DM_MAPIO_REMAPPED 1 |
| 582 | #define DM_MAPIO_REQUEUE DM_ENDIO_REQUEUE |
| 583 | |
Mikulas Patocka | fd2ed4d | 2013-08-16 10:54:23 -0400 | [diff] [blame] | 584 | #define dm_sector_div64(x, y)( \ |
| 585 | { \ |
| 586 | u64 _res; \ |
| 587 | (x) = div64_u64_rem(x, y, &_res); \ |
| 588 | _res; \ |
| 589 | } \ |
| 590 | ) |
| 591 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 592 | /* |
| 593 | * Ceiling(n / sz) |
| 594 | */ |
| 595 | #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz)) |
| 596 | |
| 597 | #define dm_sector_div_up(n, sz) ( \ |
| 598 | { \ |
| 599 | sector_t _r = ((n) + (sz) - 1); \ |
| 600 | sector_div(_r, (sz)); \ |
| 601 | _r; \ |
| 602 | } \ |
| 603 | ) |
| 604 | |
| 605 | /* |
| 606 | * ceiling(n / size) * size |
| 607 | */ |
| 608 | #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz)) |
| 609 | |
Mikulas Patocka | d63a5ce | 2008-10-21 17:44:57 +0100 | [diff] [blame] | 610 | #define dm_array_too_big(fixed, obj, num) \ |
| 611 | ((num) > (UINT_MAX - (fixed)) / (obj)) |
| 612 | |
Mike Snitzer | 56a67df | 2010-08-12 04:14:10 +0100 | [diff] [blame] | 613 | /* |
| 614 | * Sector offset taken relative to the start of the target instead of |
| 615 | * relative to the start of the device. |
| 616 | */ |
| 617 | #define dm_target_offset(ti, sector) ((sector) - (ti)->begin) |
| 618 | |
Alasdair G Kergon | 0da336e | 2008-04-24 21:43:52 +0100 | [diff] [blame] | 619 | static inline sector_t to_sector(unsigned long n) |
| 620 | { |
| 621 | return (n >> SECTOR_SHIFT); |
| 622 | } |
| 623 | |
| 624 | static inline unsigned long to_bytes(sector_t n) |
| 625 | { |
| 626 | return (n << SECTOR_SHIFT); |
| 627 | } |
| 628 | |
Alasdair G Kergon | 17b2f66 | 2006-06-26 00:27:33 -0700 | [diff] [blame] | 629 | #endif /* _LINUX_DEVICE_MAPPER_H */ |