1 /*
2  * Copyright (C) 2001 Sistina Software (UK) Limited.
3  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
4  *
5  * This file is released under the LGPL.
6  */
7 
8 #ifndef _LINUX_DEVICE_MAPPER_H
9 #define _LINUX_DEVICE_MAPPER_H
10 
11 #include <linux/bio.h>
12 #include <linux/blkdev.h>
13 #include <linux/dm-ioctl.h>
14 #include <linux/math64.h>
15 #include <linux/ratelimit.h>
16 
17 struct dm_dev;
18 struct dm_target;
19 struct dm_table;
20 struct dm_report_zones_args;
21 struct mapped_device;
22 struct bio_vec;
23 
24 /*
25  * Type of table, mapped_device's mempool and request_queue
26  */
27 enum dm_queue_mode {
28 	DM_TYPE_NONE		 = 0,
29 	DM_TYPE_BIO_BASED	 = 1,
30 	DM_TYPE_REQUEST_BASED	 = 2,
31 	DM_TYPE_DAX_BIO_BASED	 = 3,
32 };
33 
34 typedef enum { STATUSTYPE_INFO, STATUSTYPE_TABLE } status_type_t;
35 
36 union map_info {
37 	void *ptr;
38 };
39 
40 /*
41  * In the constructor the target parameter will already have the
42  * table, type, begin and len fields filled in.
43  */
44 typedef int (*dm_ctr_fn) (struct dm_target *target,
45 			  unsigned int argc, char **argv);
46 
47 /*
48  * The destructor doesn't need to free the dm_target, just
49  * anything hidden ti->private.
50  */
51 typedef void (*dm_dtr_fn) (struct dm_target *ti);
52 
53 /*
54  * The map function must return:
55  * < 0: error
56  * = 0: The target will handle the io by resubmitting it later
57  * = 1: simple remap complete
58  * = 2: The target wants to push back the io
59  */
60 typedef int (*dm_map_fn) (struct dm_target *ti, struct bio *bio);
61 typedef int (*dm_clone_and_map_request_fn) (struct dm_target *ti,
62 					    struct request *rq,
63 					    union map_info *map_context,
64 					    struct request **clone);
65 typedef void (*dm_release_clone_request_fn) (struct request *clone,
66 					     union map_info *map_context);
67 
68 /*
69  * Returns:
70  * < 0 : error (currently ignored)
71  * 0   : ended successfully
72  * 1   : for some reason the io has still not completed (eg,
73  *       multipath target might want to requeue a failed io).
74  * 2   : The target wants to push back the io
75  */
76 typedef int (*dm_endio_fn) (struct dm_target *ti,
77 			    struct bio *bio, blk_status_t *error);
78 typedef int (*dm_request_endio_fn) (struct dm_target *ti,
79 				    struct request *clone, blk_status_t error,
80 				    union map_info *map_context);
81 
82 typedef void (*dm_presuspend_fn) (struct dm_target *ti);
83 typedef void (*dm_presuspend_undo_fn) (struct dm_target *ti);
84 typedef void (*dm_postsuspend_fn) (struct dm_target *ti);
85 typedef int (*dm_preresume_fn) (struct dm_target *ti);
86 typedef void (*dm_resume_fn) (struct dm_target *ti);
87 
88 typedef void (*dm_status_fn) (struct dm_target *ti, status_type_t status_type,
89 			      unsigned status_flags, char *result, unsigned maxlen);
90 
91 typedef int (*dm_message_fn) (struct dm_target *ti, unsigned argc, char **argv,
92 			      char *result, unsigned maxlen);
93 
94 typedef int (*dm_prepare_ioctl_fn) (struct dm_target *ti, struct block_device **bdev);
95 
96 typedef int (*dm_report_zones_fn) (struct dm_target *ti,
97 				   struct dm_report_zones_args *args,
98 				   unsigned int nr_zones);
99 
100 /*
101  * These iteration functions are typically used to check (and combine)
102  * properties of underlying devices.
103  * E.g. Does at least one underlying device support flush?
104  *      Does any underlying device not support WRITE_SAME?
105  *
106  * The callout function is called once for each contiguous section of
107  * an underlying device.  State can be maintained in *data.
108  * Return non-zero to stop iterating through any further devices.
109  */
110 typedef int (*iterate_devices_callout_fn) (struct dm_target *ti,
111 					   struct dm_dev *dev,
112 					   sector_t start, sector_t len,
113 					   void *data);
114 
115 /*
116  * This function must iterate through each section of device used by the
117  * target until it encounters a non-zero return code, which it then returns.
118  * Returns zero if no callout returned non-zero.
119  */
120 typedef int (*dm_iterate_devices_fn) (struct dm_target *ti,
121 				      iterate_devices_callout_fn fn,
122 				      void *data);
123 
124 typedef void (*dm_io_hints_fn) (struct dm_target *ti,
125 				struct queue_limits *limits);
126 
127 /*
128  * Returns:
129  *    0: The target can handle the next I/O immediately.
130  *    1: The target can't handle the next I/O immediately.
131  */
132 typedef int (*dm_busy_fn) (struct dm_target *ti);
133 
134 /*
135  * Returns:
136  *  < 0 : error
137  * >= 0 : the number of bytes accessible at the address
138  */
139 typedef long (*dm_dax_direct_access_fn) (struct dm_target *ti, pgoff_t pgoff,
140 		long nr_pages, void **kaddr, pfn_t *pfn);
141 typedef size_t (*dm_dax_copy_iter_fn)(struct dm_target *ti, pgoff_t pgoff,
142 		void *addr, size_t bytes, struct iov_iter *i);
143 typedef int (*dm_dax_zero_page_range_fn)(struct dm_target *ti, pgoff_t pgoff,
144 		size_t nr_pages);
145 #define PAGE_SECTORS (PAGE_SIZE / 512)
146 
147 void dm_error(const char *message);
148 
149 struct dm_dev {
150 	struct block_device *bdev;
151 	struct dax_device *dax_dev;
152 	fmode_t mode;
153 	char name[16];
154 };
155 
156 dev_t dm_get_dev_t(const char *path);
157 
158 /*
159  * Constructors should call these functions to ensure destination devices
160  * are opened/closed correctly.
161  */
162 int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
163 		  struct dm_dev **result);
164 void dm_put_device(struct dm_target *ti, struct dm_dev *d);
165 
166 /*
167  * Information about a target type
168  */
169 
170 struct target_type {
171 	uint64_t features;
172 	const char *name;
173 	struct module *module;
174 	unsigned version[3];
175 	dm_ctr_fn ctr;
176 	dm_dtr_fn dtr;
177 	dm_map_fn map;
178 	dm_clone_and_map_request_fn clone_and_map_rq;
179 	dm_release_clone_request_fn release_clone_rq;
180 	dm_endio_fn end_io;
181 	dm_request_endio_fn rq_end_io;
182 	dm_presuspend_fn presuspend;
183 	dm_presuspend_undo_fn presuspend_undo;
184 	dm_postsuspend_fn postsuspend;
185 	dm_preresume_fn preresume;
186 	dm_resume_fn resume;
187 	dm_status_fn status;
188 	dm_message_fn message;
189 	dm_prepare_ioctl_fn prepare_ioctl;
190 #ifdef CONFIG_BLK_DEV_ZONED
191 	dm_report_zones_fn report_zones;
192 #endif
193 	dm_busy_fn busy;
194 	dm_iterate_devices_fn iterate_devices;
195 	dm_io_hints_fn io_hints;
196 	dm_dax_direct_access_fn direct_access;
197 	dm_dax_copy_iter_fn dax_copy_from_iter;
198 	dm_dax_copy_iter_fn dax_copy_to_iter;
199 	dm_dax_zero_page_range_fn dax_zero_page_range;
200 
201 	/* For internal device-mapper use. */
202 	struct list_head list;
203 };
204 
205 /*
206  * Target features
207  */
208 
209 /*
210  * Any table that contains an instance of this target must have only one.
211  */
212 #define DM_TARGET_SINGLETON		0x00000001
213 #define dm_target_needs_singleton(type)	((type)->features & DM_TARGET_SINGLETON)
214 
215 /*
216  * Indicates that a target does not support read-only devices.
217  */
218 #define DM_TARGET_ALWAYS_WRITEABLE	0x00000002
219 #define dm_target_always_writeable(type) \
220 		((type)->features & DM_TARGET_ALWAYS_WRITEABLE)
221 
222 /*
223  * Any device that contains a table with an instance of this target may never
224  * have tables containing any different target type.
225  */
226 #define DM_TARGET_IMMUTABLE		0x00000004
227 #define dm_target_is_immutable(type)	((type)->features & DM_TARGET_IMMUTABLE)
228 
229 /*
230  * Indicates that a target may replace any target; even immutable targets.
231  * .map, .map_rq, .clone_and_map_rq and .release_clone_rq are all defined.
232  */
233 #define DM_TARGET_WILDCARD		0x00000008
234 #define dm_target_is_wildcard(type)	((type)->features & DM_TARGET_WILDCARD)
235 
236 /*
237  * A target implements own bio data integrity.
238  */
239 #define DM_TARGET_INTEGRITY		0x00000010
240 #define dm_target_has_integrity(type)	((type)->features & DM_TARGET_INTEGRITY)
241 
242 /*
243  * A target passes integrity data to the lower device.
244  */
245 #define DM_TARGET_PASSES_INTEGRITY	0x00000020
246 #define dm_target_passes_integrity(type) ((type)->features & DM_TARGET_PASSES_INTEGRITY)
247 
248 /*
249  * Indicates that a target supports host-managed zoned block devices.
250  */
251 #define DM_TARGET_ZONED_HM		0x00000040
252 #define dm_target_supports_zoned_hm(type) ((type)->features & DM_TARGET_ZONED_HM)
253 
254 /*
255  * A target handles REQ_NOWAIT
256  */
257 #define DM_TARGET_NOWAIT		0x00000080
258 #define dm_target_supports_nowait(type) ((type)->features & DM_TARGET_NOWAIT)
259 
260 struct dm_target {
261 	struct dm_table *table;
262 	struct target_type *type;
263 
264 	/* target limits */
265 	sector_t begin;
266 	sector_t len;
267 
268 	/* If non-zero, maximum size of I/O submitted to a target. */
269 	uint32_t max_io_len;
270 
271 	/*
272 	 * A number of zero-length barrier bios that will be submitted
273 	 * to the target for the purpose of flushing cache.
274 	 *
275 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
276 	 * It is a responsibility of the target driver to remap these bios
277 	 * to the real underlying devices.
278 	 */
279 	unsigned num_flush_bios;
280 
281 	/*
282 	 * The number of discard bios that will be submitted to the target.
283 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
284 	 */
285 	unsigned num_discard_bios;
286 
287 	/*
288 	 * The number of secure erase bios that will be submitted to the target.
289 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
290 	 */
291 	unsigned num_secure_erase_bios;
292 
293 	/*
294 	 * The number of WRITE SAME bios that will be submitted to the target.
295 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
296 	 */
297 	unsigned num_write_same_bios;
298 
299 	/*
300 	 * The number of WRITE ZEROES bios that will be submitted to the target.
301 	 * The bio number can be accessed with dm_bio_get_target_bio_nr.
302 	 */
303 	unsigned num_write_zeroes_bios;
304 
305 	/*
306 	 * The minimum number of extra bytes allocated in each io for the
307 	 * target to use.
308 	 */
309 	unsigned per_io_data_size;
310 
311 	/* target specific data */
312 	void *private;
313 
314 	/* Used to provide an error string from the ctr */
315 	char *error;
316 
317 	/*
318 	 * Set if this target needs to receive flushes regardless of
319 	 * whether or not its underlying devices have support.
320 	 */
321 	bool flush_supported:1;
322 
323 	/*
324 	 * Set if this target needs to receive discards regardless of
325 	 * whether or not its underlying devices have support.
326 	 */
327 	bool discards_supported:1;
328 };
329 
330 void *dm_per_bio_data(struct bio *bio, size_t data_size);
331 struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size);
332 unsigned dm_bio_get_target_bio_nr(const struct bio *bio);
333 
334 u64 dm_start_time_ns_from_clone(struct bio *bio);
335 
336 int dm_register_target(struct target_type *t);
337 void dm_unregister_target(struct target_type *t);
338 
339 /*
340  * Target argument parsing.
341  */
342 struct dm_arg_set {
343 	unsigned argc;
344 	char **argv;
345 };
346 
347 /*
348  * The minimum and maximum value of a numeric argument, together with
349  * the error message to use if the number is found to be outside that range.
350  */
351 struct dm_arg {
352 	unsigned min;
353 	unsigned max;
354 	char *error;
355 };
356 
357 /*
358  * Validate the next argument, either returning it as *value or, if invalid,
359  * returning -EINVAL and setting *error.
360  */
361 int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
362 		unsigned *value, char **error);
363 
364 /*
365  * Process the next argument as the start of a group containing between
366  * arg->min and arg->max further arguments. Either return the size as
367  * *num_args or, if invalid, return -EINVAL and set *error.
368  */
369 int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
370 		      unsigned *num_args, char **error);
371 
372 /*
373  * Return the current argument and shift to the next.
374  */
375 const char *dm_shift_arg(struct dm_arg_set *as);
376 
377 /*
378  * Move through num_args arguments.
379  */
380 void dm_consume_args(struct dm_arg_set *as, unsigned num_args);
381 
382 /*-----------------------------------------------------------------
383  * Functions for creating and manipulating mapped devices.
384  * Drop the reference with dm_put when you finish with the object.
385  *---------------------------------------------------------------*/
386 
387 /*
388  * DM_ANY_MINOR chooses the next available minor number.
389  */
390 #define DM_ANY_MINOR (-1)
391 int dm_create(int minor, struct mapped_device **md);
392 
393 /*
394  * Reference counting for md.
395  */
396 struct mapped_device *dm_get_md(dev_t dev);
397 void dm_get(struct mapped_device *md);
398 int dm_hold(struct mapped_device *md);
399 void dm_put(struct mapped_device *md);
400 
401 /*
402  * An arbitrary pointer may be stored alongside a mapped device.
403  */
404 void dm_set_mdptr(struct mapped_device *md, void *ptr);
405 void *dm_get_mdptr(struct mapped_device *md);
406 
407 /*
408  * A device can still be used while suspended, but I/O is deferred.
409  */
410 int dm_suspend(struct mapped_device *md, unsigned suspend_flags);
411 int dm_resume(struct mapped_device *md);
412 
413 /*
414  * Event functions.
415  */
416 uint32_t dm_get_event_nr(struct mapped_device *md);
417 int dm_wait_event(struct mapped_device *md, int event_nr);
418 uint32_t dm_next_uevent_seq(struct mapped_device *md);
419 void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
420 
421 /*
422  * Info functions.
423  */
424 const char *dm_device_name(struct mapped_device *md);
425 int dm_copy_name_and_uuid(struct mapped_device *md, char *name, char *uuid);
426 struct gendisk *dm_disk(struct mapped_device *md);
427 int dm_suspended(struct dm_target *ti);
428 int dm_post_suspending(struct dm_target *ti);
429 int dm_noflush_suspending(struct dm_target *ti);
430 void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors);
431 union map_info *dm_get_rq_mapinfo(struct request *rq);
432 
433 #ifdef CONFIG_BLK_DEV_ZONED
434 struct dm_report_zones_args {
435 	struct dm_target *tgt;
436 	sector_t next_sector;
437 
438 	void *orig_data;
439 	report_zones_cb orig_cb;
440 	unsigned int zone_idx;
441 
442 	/* must be filled by ->report_zones before calling dm_report_zones_cb */
443 	sector_t start;
444 };
445 int dm_report_zones_cb(struct blk_zone *zone, unsigned int idx, void *data);
446 #endif /* CONFIG_BLK_DEV_ZONED */
447 
448 /*
449  * Device mapper functions to parse and create devices specified by the
450  * parameter "dm-mod.create="
451  */
452 int __init dm_early_create(struct dm_ioctl *dmi,
453 			   struct dm_target_spec **spec_array,
454 			   char **target_params_array);
455 
456 struct queue_limits *dm_get_queue_limits(struct mapped_device *md);
457 
458 /*
459  * Geometry functions.
460  */
461 int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo);
462 int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo);
463 
464 /*-----------------------------------------------------------------
465  * Functions for manipulating device-mapper tables.
466  *---------------------------------------------------------------*/
467 
468 /*
469  * First create an empty table.
470  */
471 int dm_table_create(struct dm_table **result, fmode_t mode,
472 		    unsigned num_targets, struct mapped_device *md);
473 
474 /*
475  * Then call this once for each target.
476  */
477 int dm_table_add_target(struct dm_table *t, const char *type,
478 			sector_t start, sector_t len, char *params);
479 
480 /*
481  * Target can use this to set the table's type.
482  * Can only ever be called from a target's ctr.
483  * Useful for "hybrid" target (supports both bio-based
484  * and request-based).
485  */
486 void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type);
487 
488 /*
489  * Finally call this to make the table ready for use.
490  */
491 int dm_table_complete(struct dm_table *t);
492 
493 /*
494  * Destroy the table when finished.
495  */
496 void dm_table_destroy(struct dm_table *t);
497 
498 /*
499  * Target may require that it is never sent I/O larger than len.
500  */
501 int __must_check dm_set_target_max_io_len(struct dm_target *ti, sector_t len);
502 
503 /*
504  * Table reference counting.
505  */
506 struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx);
507 void dm_put_live_table(struct mapped_device *md, int srcu_idx);
508 void dm_sync_table(struct mapped_device *md);
509 
510 /*
511  * Queries
512  */
513 sector_t dm_table_get_size(struct dm_table *t);
514 unsigned int dm_table_get_num_targets(struct dm_table *t);
515 fmode_t dm_table_get_mode(struct dm_table *t);
516 struct mapped_device *dm_table_get_md(struct dm_table *t);
517 const char *dm_table_device_name(struct dm_table *t);
518 
519 /*
520  * Trigger an event.
521  */
522 void dm_table_event(struct dm_table *t);
523 
524 /*
525  * Run the queue for request-based targets.
526  */
527 void dm_table_run_md_queue_async(struct dm_table *t);
528 
529 /*
530  * The device must be suspended before calling this method.
531  * Returns the previous table, which the caller must destroy.
532  */
533 struct dm_table *dm_swap_table(struct mapped_device *md,
534 			       struct dm_table *t);
535 
536 /*
537  * A wrapper around vmalloc.
538  */
539 void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size);
540 
541 /*-----------------------------------------------------------------
542  * Macros.
543  *---------------------------------------------------------------*/
544 #define DM_NAME "device-mapper"
545 
546 #define DM_FMT(fmt) DM_NAME ": " DM_MSG_PREFIX ": " fmt "\n"
547 
548 #define DMCRIT(fmt, ...) pr_crit(DM_FMT(fmt), ##__VA_ARGS__)
549 
550 #define DMERR(fmt, ...) pr_err(DM_FMT(fmt), ##__VA_ARGS__)
551 #define DMERR_LIMIT(fmt, ...) pr_err_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
552 #define DMWARN(fmt, ...) pr_warn(DM_FMT(fmt), ##__VA_ARGS__)
553 #define DMWARN_LIMIT(fmt, ...) pr_warn_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
554 #define DMINFO(fmt, ...) pr_info(DM_FMT(fmt), ##__VA_ARGS__)
555 #define DMINFO_LIMIT(fmt, ...) pr_info_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
556 
557 #define DMDEBUG(fmt, ...) pr_debug(DM_FMT(fmt), ##__VA_ARGS__)
558 #define DMDEBUG_LIMIT(fmt, ...) pr_debug_ratelimited(DM_FMT(fmt), ##__VA_ARGS__)
559 
560 #define DMEMIT(x...) sz += ((sz >= maxlen) ? \
561 			  0 : scnprintf(result + sz, maxlen - sz, x))
562 
563 /*
564  * Definitions of return values from target end_io function.
565  */
566 #define DM_ENDIO_DONE		0
567 #define DM_ENDIO_INCOMPLETE	1
568 #define DM_ENDIO_REQUEUE	2
569 #define DM_ENDIO_DELAY_REQUEUE	3
570 
571 /*
572  * Definitions of return values from target map function.
573  */
574 #define DM_MAPIO_SUBMITTED	0
575 #define DM_MAPIO_REMAPPED	1
576 #define DM_MAPIO_REQUEUE	DM_ENDIO_REQUEUE
577 #define DM_MAPIO_DELAY_REQUEUE	DM_ENDIO_DELAY_REQUEUE
578 #define DM_MAPIO_KILL		4
579 
580 #define dm_sector_div64(x, y)( \
581 { \
582 	u64 _res; \
583 	(x) = div64_u64_rem(x, y, &_res); \
584 	_res; \
585 } \
586 )
587 
588 /*
589  * Ceiling(n / sz)
590  */
591 #define dm_div_up(n, sz) (((n) + (sz) - 1) / (sz))
592 
593 #define dm_sector_div_up(n, sz) ( \
594 { \
595 	sector_t _r = ((n) + (sz) - 1); \
596 	sector_div(_r, (sz)); \
597 	_r; \
598 } \
599 )
600 
601 /*
602  * ceiling(n / size) * size
603  */
604 #define dm_round_up(n, sz) (dm_div_up((n), (sz)) * (sz))
605 
606 /*
607  * Sector offset taken relative to the start of the target instead of
608  * relative to the start of the device.
609  */
610 #define dm_target_offset(ti, sector) ((sector) - (ti)->begin)
611 
to_sector(unsigned long long n)612 static inline sector_t to_sector(unsigned long long n)
613 {
614 	return (n >> SECTOR_SHIFT);
615 }
616 
to_bytes(sector_t n)617 static inline unsigned long to_bytes(sector_t n)
618 {
619 	return (n << SECTOR_SHIFT);
620 }
621 
622 #endif	/* _LINUX_DEVICE_MAPPER_H */
623