xref: /openbmc/linux/include/linux/device-mapper.h (revision 3cb4021453a69585e458ec2177677c0c1300dccf)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2001 Sistina Software (UK) Limited.
31da177e4SLinus Torvalds  * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the LGPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #ifndef _LINUX_DEVICE_MAPPER_H
91da177e4SLinus Torvalds #define _LINUX_DEVICE_MAPPER_H
101da177e4SLinus Torvalds 
1117b2f66fSAlasdair G Kergon #ifdef __KERNEL__
1217b2f66fSAlasdair G Kergon 
131da177e4SLinus Torvalds struct dm_target;
141da177e4SLinus Torvalds struct dm_table;
151da177e4SLinus Torvalds struct dm_dev;
1617b2f66fSAlasdair G Kergon struct mapped_device;
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds union map_info {
211da177e4SLinus Torvalds 	void *ptr;
221da177e4SLinus Torvalds 	unsigned long long ll;
231da177e4SLinus Torvalds };
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /*
261da177e4SLinus Torvalds  * In the constructor the target parameter will already have the
271da177e4SLinus Torvalds  * table, type, begin and len fields filled in.
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds typedef int (*dm_ctr_fn) (struct dm_target *target,
301da177e4SLinus Torvalds 			  unsigned int argc, char **argv);
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /*
331da177e4SLinus Torvalds  * The destructor doesn't need to free the dm_target, just
341da177e4SLinus Torvalds  * anything hidden ti->private.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds typedef void (*dm_dtr_fn) (struct dm_target *ti);
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * The map function must return:
401da177e4SLinus Torvalds  * < 0: error
411da177e4SLinus Torvalds  * = 0: The target will handle the io by resubmitting it later
421da177e4SLinus Torvalds  * > 0: simple remap complete
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio,
451da177e4SLinus Torvalds 			  union map_info *map_context);
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /*
481da177e4SLinus Torvalds  * Returns:
491da177e4SLinus Torvalds  * < 0 : error (currently ignored)
501da177e4SLinus Torvalds  * 0   : ended successfully
511da177e4SLinus Torvalds  * 1   : for some reason the io has still not completed (eg,
521da177e4SLinus Torvalds  *       multipath target might want to requeue a failed io).
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds typedef int (*dm_endio_fn) (struct dm_target *ti,
551da177e4SLinus Torvalds 			    struct bio *bio, int error,
561da177e4SLinus Torvalds 			    union map_info *map_context);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds typedef void (*dm_presuspend_fn) (struct dm_target *ti);
591da177e4SLinus Torvalds typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
608757b776SMilan Broz typedef int (*dm_preresume_fn) (struct dm_target *ti);
611da177e4SLinus Torvalds typedef void (*dm_resume_fn) (struct dm_target *ti);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds typedef int (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
641da177e4SLinus Torvalds 			     char *result, unsigned int maxlen);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv);
671da177e4SLinus Torvalds 
68aa129a22SMilan Broz typedef int (*dm_ioctl_fn) (struct dm_target *ti, struct inode *inode,
69aa129a22SMilan Broz 			    struct file *filp, unsigned int cmd,
70aa129a22SMilan Broz 			    unsigned long arg);
71aa129a22SMilan Broz 
721da177e4SLinus Torvalds void dm_error(const char *message);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
75*3cb40214SBryn Reeves  * Combine device limits.
76*3cb40214SBryn Reeves  */
77*3cb40214SBryn Reeves void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev);
78*3cb40214SBryn Reeves 
79*3cb40214SBryn Reeves /*
801da177e4SLinus Torvalds  * Constructors should call these functions to ensure destination devices
811da177e4SLinus Torvalds  * are opened/closed correctly.
821da177e4SLinus Torvalds  * FIXME: too many arguments.
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
851da177e4SLinus Torvalds 		  sector_t len, int mode, struct dm_dev **result);
861da177e4SLinus Torvalds void dm_put_device(struct dm_target *ti, struct dm_dev *d);
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * Information about a target type
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds struct target_type {
921da177e4SLinus Torvalds 	const char *name;
931da177e4SLinus Torvalds 	struct module *module;
941da177e4SLinus Torvalds 	unsigned version[3];
951da177e4SLinus Torvalds 	dm_ctr_fn ctr;
961da177e4SLinus Torvalds 	dm_dtr_fn dtr;
971da177e4SLinus Torvalds 	dm_map_fn map;
981da177e4SLinus Torvalds 	dm_endio_fn end_io;
991da177e4SLinus Torvalds 	dm_presuspend_fn presuspend;
1001da177e4SLinus Torvalds 	dm_postsuspend_fn postsuspend;
1018757b776SMilan Broz 	dm_preresume_fn preresume;
1021da177e4SLinus Torvalds 	dm_resume_fn resume;
1031da177e4SLinus Torvalds 	dm_status_fn status;
1041da177e4SLinus Torvalds 	dm_message_fn message;
105aa129a22SMilan Broz 	dm_ioctl_fn ioctl;
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds struct io_restrictions {
1093ee247ebSAlasdair G Kergon 	unsigned int		max_sectors;
1101da177e4SLinus Torvalds 	unsigned short		max_phys_segments;
1111da177e4SLinus Torvalds 	unsigned short		max_hw_segments;
1121da177e4SLinus Torvalds 	unsigned short		hardsect_size;
1131da177e4SLinus Torvalds 	unsigned int		max_segment_size;
1141da177e4SLinus Torvalds 	unsigned long		seg_boundary_mask;
115969429b5SNeilBrown 	unsigned char		no_cluster; /* inverted so that 0 is default */
1161da177e4SLinus Torvalds };
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds struct dm_target {
1191da177e4SLinus Torvalds 	struct dm_table *table;
1201da177e4SLinus Torvalds 	struct target_type *type;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	/* target limits */
1231da177e4SLinus Torvalds 	sector_t begin;
1241da177e4SLinus Torvalds 	sector_t len;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	/* FIXME: turn this into a mask, and merge with io_restrictions */
1271da177e4SLinus Torvalds 	/* Always a power of 2 */
1281da177e4SLinus Torvalds 	sector_t split_io;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	/*
1311da177e4SLinus Torvalds 	 * These are automatically filled in by
1321da177e4SLinus Torvalds 	 * dm_table_get_device.
1331da177e4SLinus Torvalds 	 */
1341da177e4SLinus Torvalds 	struct io_restrictions limits;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	/* target specific data */
1371da177e4SLinus Torvalds 	void *private;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Used to provide an error string from the ctr */
1401da177e4SLinus Torvalds 	char *error;
1411da177e4SLinus Torvalds };
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds int dm_register_target(struct target_type *t);
1441da177e4SLinus Torvalds int dm_unregister_target(struct target_type *t);
1451da177e4SLinus Torvalds 
14617b2f66fSAlasdair G Kergon 
14717b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
14817b2f66fSAlasdair G Kergon  * Functions for creating and manipulating mapped devices.
14917b2f66fSAlasdair G Kergon  * Drop the reference with dm_put when you finish with the object.
15017b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
15117b2f66fSAlasdair G Kergon 
15217b2f66fSAlasdair G Kergon /*
15317b2f66fSAlasdair G Kergon  * DM_ANY_MINOR chooses the next available minor number.
15417b2f66fSAlasdair G Kergon  */
15517b2f66fSAlasdair G Kergon #define DM_ANY_MINOR (-1)
15617b2f66fSAlasdair G Kergon int dm_create(int minor, struct mapped_device **md);
15717b2f66fSAlasdair G Kergon 
15817b2f66fSAlasdair G Kergon /*
15917b2f66fSAlasdair G Kergon  * Reference counting for md.
16017b2f66fSAlasdair G Kergon  */
16117b2f66fSAlasdair G Kergon struct mapped_device *dm_get_md(dev_t dev);
16217b2f66fSAlasdair G Kergon void dm_get(struct mapped_device *md);
16317b2f66fSAlasdair G Kergon void dm_put(struct mapped_device *md);
16417b2f66fSAlasdair G Kergon 
16517b2f66fSAlasdair G Kergon /*
16617b2f66fSAlasdair G Kergon  * An arbitrary pointer may be stored alongside a mapped device.
16717b2f66fSAlasdair G Kergon  */
16817b2f66fSAlasdair G Kergon void dm_set_mdptr(struct mapped_device *md, void *ptr);
16917b2f66fSAlasdair G Kergon void *dm_get_mdptr(struct mapped_device *md);
17017b2f66fSAlasdair G Kergon 
17117b2f66fSAlasdair G Kergon /*
17217b2f66fSAlasdair G Kergon  * A device can still be used while suspended, but I/O is deferred.
17317b2f66fSAlasdair G Kergon  */
17417b2f66fSAlasdair G Kergon int dm_suspend(struct mapped_device *md, int with_lockfs);
17517b2f66fSAlasdair G Kergon int dm_resume(struct mapped_device *md);
17617b2f66fSAlasdair G Kergon 
17717b2f66fSAlasdair G Kergon /*
17817b2f66fSAlasdair G Kergon  * Event functions.
17917b2f66fSAlasdair G Kergon  */
18017b2f66fSAlasdair G Kergon uint32_t dm_get_event_nr(struct mapped_device *md);
18117b2f66fSAlasdair G Kergon int dm_wait_event(struct mapped_device *md, int event_nr);
18217b2f66fSAlasdair G Kergon 
18317b2f66fSAlasdair G Kergon /*
18417b2f66fSAlasdair G Kergon  * Info functions.
18517b2f66fSAlasdair G Kergon  */
18672d94861SAlasdair G Kergon const char *dm_device_name(struct mapped_device *md);
18717b2f66fSAlasdair G Kergon struct gendisk *dm_disk(struct mapped_device *md);
18817b2f66fSAlasdair G Kergon int dm_suspended(struct mapped_device *md);
18917b2f66fSAlasdair G Kergon 
19017b2f66fSAlasdair G Kergon /*
19117b2f66fSAlasdair G Kergon  * Geometry functions.
19217b2f66fSAlasdair G Kergon  */
19317b2f66fSAlasdair G Kergon int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
19417b2f66fSAlasdair G Kergon int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
19517b2f66fSAlasdair G Kergon 
19617b2f66fSAlasdair G Kergon 
19717b2f66fSAlasdair G Kergon /*-----------------------------------------------------------------
19817b2f66fSAlasdair G Kergon  * Functions for manipulating device-mapper tables.
19917b2f66fSAlasdair G Kergon  *---------------------------------------------------------------*/
20017b2f66fSAlasdair G Kergon 
20117b2f66fSAlasdair G Kergon /*
20217b2f66fSAlasdair G Kergon  * First create an empty table.
20317b2f66fSAlasdair G Kergon  */
20417b2f66fSAlasdair G Kergon int dm_table_create(struct dm_table **result, int mode,
20517b2f66fSAlasdair G Kergon 		    unsigned num_targets, struct mapped_device *md);
20617b2f66fSAlasdair G Kergon 
20717b2f66fSAlasdair G Kergon /*
20817b2f66fSAlasdair G Kergon  * Then call this once for each target.
20917b2f66fSAlasdair G Kergon  */
21017b2f66fSAlasdair G Kergon int dm_table_add_target(struct dm_table *t, const char *type,
21117b2f66fSAlasdair G Kergon 			sector_t start, sector_t len, char *params);
21217b2f66fSAlasdair G Kergon 
21317b2f66fSAlasdair G Kergon /*
21417b2f66fSAlasdair G Kergon  * Finally call this to make the table ready for use.
21517b2f66fSAlasdair G Kergon  */
21617b2f66fSAlasdair G Kergon int dm_table_complete(struct dm_table *t);
21717b2f66fSAlasdair G Kergon 
21817b2f66fSAlasdair G Kergon /*
21917b2f66fSAlasdair G Kergon  * Table reference counting.
22017b2f66fSAlasdair G Kergon  */
22117b2f66fSAlasdair G Kergon struct dm_table *dm_get_table(struct mapped_device *md);
22217b2f66fSAlasdair G Kergon void dm_table_get(struct dm_table *t);
22317b2f66fSAlasdair G Kergon void dm_table_put(struct dm_table *t);
22417b2f66fSAlasdair G Kergon 
22517b2f66fSAlasdair G Kergon /*
22617b2f66fSAlasdair G Kergon  * Queries
22717b2f66fSAlasdair G Kergon  */
22817b2f66fSAlasdair G Kergon sector_t dm_table_get_size(struct dm_table *t);
22917b2f66fSAlasdair G Kergon unsigned int dm_table_get_num_targets(struct dm_table *t);
23017b2f66fSAlasdair G Kergon int dm_table_get_mode(struct dm_table *t);
23117b2f66fSAlasdair G Kergon struct mapped_device *dm_table_get_md(struct dm_table *t);
23217b2f66fSAlasdair G Kergon 
23317b2f66fSAlasdair G Kergon /*
23417b2f66fSAlasdair G Kergon  * Trigger an event.
23517b2f66fSAlasdair G Kergon  */
23617b2f66fSAlasdair G Kergon void dm_table_event(struct dm_table *t);
23717b2f66fSAlasdair G Kergon 
23817b2f66fSAlasdair G Kergon /*
23917b2f66fSAlasdair G Kergon  * The device must be suspended before calling this method.
24017b2f66fSAlasdair G Kergon  */
24117b2f66fSAlasdair G Kergon int dm_swap_table(struct mapped_device *md, struct dm_table *t);
24217b2f66fSAlasdair G Kergon 
243c2ade42dSDavid Teigland /*
244c2ade42dSDavid Teigland  * Prepare a table for a device that will error all I/O.
245c2ade42dSDavid Teigland  * To make it active, call dm_suspend(), dm_swap_table() then dm_resume().
246c2ade42dSDavid Teigland  */
247c2ade42dSDavid Teigland int dm_create_error_table(struct dm_table **result, struct mapped_device *md);
248c2ade42dSDavid Teigland 
24917b2f66fSAlasdair G Kergon #endif	/* __KERNEL__ */
2501da177e4SLinus Torvalds #endif	/* _LINUX_DEVICE_MAPPER_H */
251