blob: 2ce339212b6e6c87474c395856d430aa2cdf6200 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Sistina Software (UK) Limited.
Alasdair G Kergon0da336e2008-04-24 21:43:52 +01003 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file is released under the LGPL.
6 */
7
8#ifndef _LINUX_DEVICE_MAPPER_H
9#define _LINUX_DEVICE_MAPPER_H
10
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010011#include <linux/bio.h>
Milan Brozf6fccb12008-07-21 12:00:37 +010012#include <linux/blkdev.h>
Mikulas Patockafd2ed4d2013-08-16 10:54:23 -040013#include <linux/math64.h>
Namhyung Kim71a16732011-10-31 20:18:54 +000014#include <linux/ratelimit.h>
Heinz Mauelshagen416cd172008-04-24 21:43:35 +010015
Mike Snitzeraf4874e2009-06-22 10:12:33 +010016struct dm_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct dm_target;
18struct dm_table;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -070019struct mapped_device;
Milan Brozf6fccb12008-07-21 12:00:37 +010020struct bio_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Mike Snitzere83068a2016-05-24 21:16:51 -040022/*
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 Torvalds1da177e2005-04-16 15:20:36 -070030typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
31
32union map_info {
33 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
36/*
37 * In the constructor the target parameter will already have the
38 * table, type, begin and len fields filled in.
39 */
40typedef 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 */
47typedef 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 Ueda45cbcd72006-12-08 02:41:05 -080053 * = 1: simple remap complete
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -080054 * = 2: The target wants to push back the io
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
Mikulas Patocka7de3ee52012-12-21 20:23:41 +000056typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
Kiyoshi Ueda7d763452009-01-06 03:05:07 +000057typedef int (*dm_map_request_fn) (struct dm_target *ti, struct request *clone,
58 union map_info *map_context);
Mike Snitzere5863d92014-12-17 21:08:12 -050059typedef 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);
63typedef void (*dm_release_clone_request_fn) (struct request *clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
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 Ueda2e93ccc2006-12-08 02:41:09 -080071 * 2 : The target wants to push back the io
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73typedef int (*dm_endio_fn) (struct dm_target *ti,
Mikulas Patocka7de3ee52012-12-21 20:23:41 +000074 struct bio *bio, int error);
Kiyoshi Ueda7d763452009-01-06 03:05:07 +000075typedef int (*dm_request_endio_fn) (struct dm_target *ti,
76 struct request *clone, int error,
77 union map_info *map_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79typedef void (*dm_presuspend_fn) (struct dm_target *ti);
Mike Snitzerd67ee212014-10-28 20:13:31 -040080typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
Milan Broz8757b772006-10-03 01:15:36 -070082typedef int (*dm_preresume_fn) (struct dm_target *ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083typedef void (*dm_resume_fn) (struct dm_target *ti);
84
Mikulas Patockafd7c0922013-03-01 22:45:44 +000085typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
86 unsigned status_flags, char *result, unsigned maxlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
89
Christoph Hellwige56f81e2015-10-15 14:10:50 +020090typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti,
91 struct block_device **bdev, fmode_t *mode);
Milan Brozaa129a22006-10-03 01:15:15 -070092
Alasdair G Kergon058ce5c2013-05-10 14:37:17 +010093/*
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 Snitzeraf4874e2009-06-22 10:12:33 +0100103typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
104 struct dm_dev *dev,
Mike Snitzer5dea2712009-07-23 20:30:42 +0100105 sector_t start, sector_t len,
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100106 void *data);
107
Alasdair G Kergon058ce5c2013-05-10 14:37:17 +0100108/*
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 Snitzeraf4874e2009-06-22 10:12:33 +0100113typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
114 iterate_devices_callout_fn fn,
115 void *data);
116
Mike Snitzer40bea432009-09-04 20:40:25 +0100117typedef void (*dm_io_hints_fn) (struct dm_target *ti,
118 struct queue_limits *limits);
119
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000120/*
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 */
125typedef int (*dm_busy_fn) (struct dm_target *ti);
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127void dm_error(const char *message);
128
Mikulas Patocka82b15192008-10-10 13:37:09 +0100129struct dm_dev {
130 struct block_device *bdev;
Al Viroaeb5d722008-09-02 15:28:45 -0400131 fmode_t mode;
Mikulas Patocka82b15192008-10-10 13:37:09 +0100132 char name[16];
133};
134
DingXiang4df2bf42016-02-02 12:29:18 +0800135dev_t dm_get_dev_t(const char *path);
136
Bryn Reeves3cb40212006-10-03 01:15:42 -0700137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Constructors should call these functions to ensure destination devices
139 * are opened/closed correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000141int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
Mike Snitzer11f04312014-06-03 10:30:28 -0400142 struct dm_dev **result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143void dm_put_device(struct dm_target *ti, struct dm_dev *d);
144
145/*
146 * Information about a target type
147 */
Andi Kleenab4c142482009-01-06 03:05:09 +0000148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct target_type {
Andi Kleenab4c142482009-01-06 03:05:09 +0000150 uint64_t features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 const char *name;
152 struct module *module;
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700153 unsigned version[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 dm_ctr_fn ctr;
155 dm_dtr_fn dtr;
156 dm_map_fn map;
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000157 dm_map_request_fn map_rq;
Mike Snitzere5863d92014-12-17 21:08:12 -0500158 dm_clone_and_map_request_fn clone_and_map_rq;
159 dm_release_clone_request_fn release_clone_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 dm_endio_fn end_io;
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000161 dm_request_endio_fn rq_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dm_presuspend_fn presuspend;
Mike Snitzerd67ee212014-10-28 20:13:31 -0400163 dm_presuspend_undo_fn presuspend_undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dm_postsuspend_fn postsuspend;
Milan Broz8757b772006-10-03 01:15:36 -0700165 dm_preresume_fn preresume;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dm_resume_fn resume;
167 dm_status_fn status;
168 dm_message_fn message;
Christoph Hellwige56f81e2015-10-15 14:10:50 +0200169 dm_prepare_ioctl_fn prepare_ioctl;
Kiyoshi Ueda7d763452009-01-06 03:05:07 +0000170 dm_busy_fn busy;
Mike Snitzeraf4874e2009-06-22 10:12:33 +0100171 dm_iterate_devices_fn iterate_devices;
Mike Snitzer40bea432009-09-04 20:40:25 +0100172 dm_io_hints_fn io_hints;
Cheng Renquan45194e42009-04-02 19:55:28 +0100173
174 /* For internal device-mapper use. */
175 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Alasdair G Kergon3791e2f2011-10-31 20:19:00 +0000178/*
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 Kergoncc6cbe12011-10-31 20:19:02 +0000188/*
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 Kergon36a04562011-10-31 20:19:04 +0000195/*
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 Kergonb0d8ed42013-03-01 22:45:49 +0000202/*
Mike Snitzerf083b092016-02-06 18:38:46 -0500203 * 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 Kergonb0d8ed42013-03-01 22:45:49 +0000210 * 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 */
215typedef unsigned (*dm_num_write_bios_fn) (struct dm_target *ti, struct bio *bio);
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217struct 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 Snitzer542f9032012-07-27 15:08:00 +0100225 /* If non-zero, maximum size of I/O submitted to a target. */
226 uint32_t max_io_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /*
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000229 * A number of zero-length barrier bios that will be submitted
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +0100230 * to the target for the purpose of flushing cache.
231 *
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000232 * 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 Patockaf9ab94c2009-06-22 10:12:20 +0100234 * to the real underlying devices.
235 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000236 unsigned num_flush_bios;
Mikulas Patockaf9ab94c2009-06-22 10:12:20 +0100237
Mike Snitzer5ae89a82010-08-12 04:14:08 +0100238 /*
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000239 * 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 Snitzer5ae89a82010-08-12 04:14:08 +0100241 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000242 unsigned num_discard_bios;
Mike Snitzer5ae89a82010-08-12 04:14:08 +0100243
Mike Snitzerd54eaa52012-12-21 20:23:36 +0000244 /*
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000245 * 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 Snitzerd54eaa52012-12-21 20:23:36 +0000247 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000248 unsigned num_write_same_bios;
Mike Snitzerd54eaa52012-12-21 20:23:36 +0000249
Mikulas Patockac0820cf2012-12-21 20:23:38 +0000250 /*
Mike Snitzer30187e12016-01-31 13:28:26 -0500251 * The minimum number of extra bytes allocated in each io for the
252 * target to use.
Mikulas Patockac0820cf2012-12-21 20:23:38 +0000253 */
Mike Snitzer30187e12016-01-31 13:28:26 -0500254 unsigned per_io_data_size;
Mikulas Patockac0820cf2012-12-21 20:23:38 +0000255
Alasdair G Kergonb0d8ed42013-03-01 22:45:49 +0000256 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700263 /* target specific data */
264 void *private;
265
266 /* Used to provide an error string from the ctr */
267 char *error;
Mike Snitzer4c259322011-05-29 12:52:55 +0100268
269 /*
Joe Thornber0e9c24e2012-07-27 15:08:07 +0100270 * 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 Snitzer4c259322011-05-29 12:52:55 +0100276 * Set if this target needs to receive discards regardless of
277 * whether or not its underlying devices have support.
278 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +0100279 bool discards_supported:1;
Milan Broz983c7db2011-09-25 23:26:21 +0100280
281 /*
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000282 * Set if the target required discard bios to be split
Mikulas Patocka7acf0272012-07-27 15:08:03 +0100283 * on max_io_len boundary.
284 */
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000285 bool split_discard_bios:1;
Mikulas Patocka7acf0272012-07-27 15:08:03 +0100286
287 /*
Milan Broz983c7db2011-09-25 23:26:21 +0100288 * Set if this target does not return zeroes on discarded blocks.
289 */
Alasdair G Kergon0ac55482012-07-27 15:08:08 +0100290 bool discard_zeroes_data_unsupported:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292
NeilBrown9d357b02011-01-13 20:00:01 +0000293/* Each target can link one of these into the table */
294struct dm_target_callbacks {
295 struct list_head list;
296 int (*congested_fn) (struct dm_target_callbacks *, int);
297};
298
Mikulas Patockac0820cf2012-12-21 20:23:38 +0000299/*
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 */
306struct dm_target_io {
307 struct dm_io *io;
308 struct dm_target *ti;
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000309 unsigned target_bio_nr;
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400310 unsigned *len_ptr;
Mikulas Patockac0820cf2012-12-21 20:23:38 +0000311 struct bio clone;
312};
313
314static 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
319static 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 Kergon55a62ee2013-03-01 22:45:47 +0000324static inline unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
Mikulas Patockaddbd6582012-12-21 20:23:39 +0000325{
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000326 return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
Mikulas Patockaddbd6582012-12-21 20:23:39 +0000327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329int dm_register_target(struct target_type *t);
Mikulas Patocka10d3bd02009-01-06 03:04:58 +0000330void dm_unregister_target(struct target_type *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700331
Mike Snitzer498f0102011-08-02 12:32:04 +0100332/*
333 * Target argument parsing.
334 */
335struct 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 */
344struct 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 */
354int 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 */
362int 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 */
368const char *dm_shift_arg(struct dm_arg_set *as);
369
370/*
371 * Move through num_args arguments.
372 */
373void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
374
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700375/*-----------------------------------------------------------------
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)
384int dm_create(int minor, struct mapped_device **md);
385
386/*
387 * Reference counting for md.
388 */
389struct mapped_device *dm_get_md(dev_t dev);
390void dm_get(struct mapped_device *md);
Mikulas Patocka09ee96b2015-02-26 11:41:28 -0500391int dm_hold(struct mapped_device *md);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700392void dm_put(struct mapped_device *md);
393
394/*
395 * An arbitrary pointer may be stored alongside a mapped device.
396 */
397void dm_set_mdptr(struct mapped_device *md, void *ptr);
398void *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 Uedaa3d77d32006-12-08 02:41:04 -0800403int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700404int dm_resume(struct mapped_device *md);
405
406/*
407 * Event functions.
408 */
409uint32_t dm_get_event_nr(struct mapped_device *md);
410int dm_wait_event(struct mapped_device *md, int event_nr);
Mike Anderson7a8c3d32007-10-19 22:48:01 +0100411uint32_t dm_next_uevent_seq(struct mapped_device *md);
412void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700413
414/*
415 * Info functions.
416 */
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700417const char *dm_device_name(struct mapped_device *md);
Mike Anderson96a1f7d2007-10-19 22:47:59 +0100418int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700419struct gendisk *dm_disk(struct mapped_device *md);
Kiyoshi Ueda64dbce52009-12-10 23:52:27 +0000420int dm_suspended(struct dm_target *ti);
Kiyoshi Ueda2e93ccc2006-12-08 02:41:09 -0800421int dm_noflush_suspending(struct dm_target *ti);
Mikulas Patocka1dd40c32014-03-14 18:41:24 -0400422void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
Kiyoshi Uedacec47e32009-06-22 10:12:35 +0100423union map_info *dm_get_rq_mapinfo(struct request *rq);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700424
Mike Snitzerf84cb8a2013-09-19 12:13:58 -0400425struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
426
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700427/*
428 * Geometry functions.
429 */
430int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
431int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
432
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700433/*-----------------------------------------------------------------
434 * Functions for manipulating device-mapper tables.
435 *---------------------------------------------------------------*/
436
437/*
438 * First create an empty table.
439 */
Al Viroaeb5d722008-09-02 15:28:45 -0400440int dm_table_create(struct dm_table **result, fmode_t mode,
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700441 unsigned num_targets, struct mapped_device *md);
442
443/*
444 * Then call this once for each target.
445 */
446int dm_table_add_target(struct dm_table *t, const char *type,
447 sector_t start, sector_t len, char *params);
448
449/*
NeilBrown9d357b02011-01-13 20:00:01 +0000450 * Target_ctr should call this if it needs to add any callbacks.
451 */
452void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb);
453
454/*
Mike Snitzere83068a2016-05-24 21:16:51 -0400455 * 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 */
460void dm_table_set_type(struct dm_table *t, unsigned type);
461
462/*
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700463 * Finally call this to make the table ready for use.
464 */
465int dm_table_complete(struct dm_table *t);
466
467/*
Mike Snitzer542f9032012-07-27 15:08:00 +0100468 * Target may require that it is never sent I/O larger than len.
469 */
470int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
471
472/*
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700473 * Table reference counting.
474 */
Mikulas Patocka83d5e5b2013-07-10 23:41:18 +0100475struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
476void dm_put_live_table(struct mapped_device *md, int srcu_idx);
477void dm_sync_table(struct mapped_device *md);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700478
479/*
480 * Queries
481 */
482sector_t dm_table_get_size(struct dm_table *t);
483unsigned int dm_table_get_num_targets(struct dm_table *t);
Al Viroaeb5d722008-09-02 15:28:45 -0400484fmode_t dm_table_get_mode(struct dm_table *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700485struct mapped_device *dm_table_get_md(struct dm_table *t);
486
487/*
488 * Trigger an event.
489 */
490void dm_table_event(struct dm_table *t);
491
492/*
Mike Snitzer9974fa22014-02-28 15:33:43 +0100493 * Run the queue for request-based targets.
494 */
495void dm_table_run_md_queue_async(struct dm_table *t);
496
497/*
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700498 * The device must be suspended before calling this method.
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000499 * Returns the previous table, which the caller must destroy.
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700500 */
Alasdair G Kergon042d2a92009-12-10 23:52:24 +0000501struct dm_table *dm_swap_table(struct mapped_device *md,
502 struct dm_table *t);
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700503
Mikulas Patocka54160902008-10-10 13:37:12 +0100504/*
505 * A wrapper around vmalloc.
506 */
507void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
508
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100509/*-----------------------------------------------------------------
510 * Macros.
511 *---------------------------------------------------------------*/
512#define DM_NAME "device-mapper"
513
Namhyung Kim71a16732011-10-31 20:18:54 +0000514#ifdef CONFIG_PRINTK
515extern 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 Patocka10d3bd02009-01-06 03:04:58 +0000522#define DMCRIT(f, arg...) \
523 printk(KERN_CRIT DM_NAME ": " DM_MSG_PREFIX ": " f "\n", ## arg)
524
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100525#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 Kim71a16732011-10-31 20:18:54 +0000529 if (dm_ratelimit()) \
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100530 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 Kim71a16732011-10-31 20:18:54 +0000538 if (dm_ratelimit()) \
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100539 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 Kim71a16732011-10-31 20:18:54 +0000547 if (dm_ratelimit()) \
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100548 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 Kim71a16732011-10-31 20:18:54 +0000557 if (dm_ratelimit()) \
Alasdair G Kergon0da336e2008-04-24 21:43:52 +0100558 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 Patockafd2ed4d2013-08-16 10:54:23 -0400584#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 Kergon0da336e2008-04-24 21:43:52 +0100592/*
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 Patockad63a5ce2008-10-21 17:44:57 +0100610#define dm_array_too_big(fixed, obj, num) \
611 ((num) > (UINT_MAX - (fixed)) / (obj))
612
Mike Snitzer56a67df2010-08-12 04:14:10 +0100613/*
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 Kergon0da336e2008-04-24 21:43:52 +0100619static inline sector_t to_sector(unsigned long n)
620{
621 return (n >> SECTOR_SHIFT);
622}
623
624static inline unsigned long to_bytes(sector_t n)
625{
626 return (n << SECTOR_SHIFT);
627}
628
Alasdair G Kergon17b2f662006-06-26 00:27:33 -0700629#endif /* _LINUX_DEVICE_MAPPER_H */