2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
5 * This file is part of the device-mapper userspace tools.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #ifndef LIB_DEVICE_MAPPER_H
17 #define LIB_DEVICE_MAPPER_H
21 #include <sys/types.h>
24 # include <linux/types.h>
33 # define __typeof__ typeof
40 /*****************************************************************
41 * The first section of this file provides direct access to the
42 * individual device-mapper ioctls. Since it is quite laborious to
43 * build the ioctl arguments for the device-mapper, people are
44 * encouraged to use this library.
45 ****************************************************************/
48 * The library user may wish to register their own
49 * logging function. By default errors go to stderr.
50 * Use dm_log_with_errno_init(NULL) to restore the default log fn.
53 typedef void (*dm_log_with_errno_fn) (int level, const char *file, int line,
54 int dm_errno, const char *f, ...)
55 __attribute__ ((format(printf, 5, 6)));
57 void dm_log_with_errno_init(dm_log_with_errno_fn fn);
58 void dm_log_init_verbose(int level);
61 * Original version of this function.
62 * dm_errno is set to 0.
64 * Deprecated: Use the _with_errno_ versions above instead.
66 typedef void (*dm_log_fn) (int level, const char *file, int line,
68 __attribute__ ((format(printf, 4, 5)));
70 void dm_log_init(dm_log_fn fn);
72 * For backward-compatibility, indicate that dm_log_init() was used
73 * to set a non-default value of dm_log().
75 int dm_log_is_non_default(void);
102 DM_DEVICE_LIST_VERSIONS,
104 DM_DEVICE_TARGET_MSG,
106 DM_DEVICE_SET_GEOMETRY
110 * You will need to build a struct dm_task for
111 * each ioctl command you want to execute.
116 struct dm_task *dm_task_create(int type);
117 void dm_task_destroy(struct dm_task *dmt);
119 int dm_task_set_name(struct dm_task *dmt, const char *name);
120 int dm_task_set_uuid(struct dm_task *dmt, const char *uuid);
123 * Retrieve attributes after an info.
133 uint32_t minor; /* minor device number */
134 int read_only; /* 0:read-write; 1:read-only */
136 int32_t target_count;
147 uint32_t next; /* Offset to next struct from start of this struct */
152 uint32_t next; /* Offset to next struct from start of this struct */
158 int dm_get_library_version(char *version, size_t size);
159 int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size);
160 int dm_task_get_info(struct dm_task *dmt, struct dm_info *dmi);
161 const char *dm_task_get_name(const struct dm_task *dmt);
162 const char *dm_task_get_uuid(const struct dm_task *dmt);
164 struct dm_deps *dm_task_get_deps(struct dm_task *dmt);
165 struct dm_names *dm_task_get_names(struct dm_task *dmt);
166 struct dm_versions *dm_task_get_versions(struct dm_task *dmt);
168 int dm_task_set_ro(struct dm_task *dmt);
169 int dm_task_set_newname(struct dm_task *dmt, const char *newname);
170 int dm_task_set_newuuid(struct dm_task *dmt, const char *newuuid);
171 int dm_task_set_minor(struct dm_task *dmt, int minor);
172 int dm_task_set_major(struct dm_task *dmt, int major);
173 int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor, int allow_default_major_fallback);
174 int dm_task_set_uid(struct dm_task *dmt, uid_t uid);
175 int dm_task_set_gid(struct dm_task *dmt, gid_t gid);
176 int dm_task_set_mode(struct dm_task *dmt, mode_t mode);
177 int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags);
178 int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr);
179 int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders, const char *heads, const char *sectors, const char *start);
180 int dm_task_set_message(struct dm_task *dmt, const char *message);
181 int dm_task_set_sector(struct dm_task *dmt, uint64_t sector);
182 int dm_task_no_flush(struct dm_task *dmt);
183 int dm_task_no_open_count(struct dm_task *dmt);
184 int dm_task_skip_lockfs(struct dm_task *dmt);
185 int dm_task_query_inactive_table(struct dm_task *dmt);
186 int dm_task_suppress_identical_reload(struct dm_task *dmt);
189 * Control read_ahead.
191 #define DM_READ_AHEAD_AUTO UINT32_MAX /* Use kernel default readahead */
192 #define DM_READ_AHEAD_NONE 0 /* Disable readahead */
194 #define DM_READ_AHEAD_MINIMUM_FLAG 0x1 /* Value supplied is minimum */
197 * Read ahead is set with DM_DEVICE_CREATE with a table or DM_DEVICE_RESUME.
199 int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead,
200 uint32_t read_ahead_flags);
201 uint32_t dm_task_get_read_ahead(const struct dm_task *dmt,
202 uint32_t *read_ahead);
205 * Use these to prepare for a create or reload.
207 int dm_task_add_target(struct dm_task *dmt,
209 uint64_t size, const char *ttype, const char *params);
212 * Format major/minor numbers correctly for input to driver.
214 #define DM_FORMAT_DEV_BUFSIZE 13 /* Minimum bufsize to handle worst case. */
215 int dm_format_dev(char *buf, int bufsize, uint32_t dev_major, uint32_t dev_minor);
217 /* Use this to retrive target information returned from a STATUS call */
218 void *dm_get_next_target(struct dm_task *dmt,
219 void *next, uint64_t *start, uint64_t *length,
220 char **target_type, char **params);
223 * Call this to actually run the ioctl.
225 int dm_task_run(struct dm_task *dmt);
228 * Call this to make or remove the device nodes associated with previously
231 void dm_task_update_nodes(void);
234 * Configure the device-mapper directory
236 int dm_set_dev_dir(const char *dir);
237 const char *dm_dir(void);
240 * Determine whether a major number belongs to device-mapper or not.
242 int dm_is_dm_major(uint32_t major);
245 * Release library resources
247 void dm_lib_release(void);
248 void dm_lib_exit(void) __attribute__((destructor));
251 * Use NULL for all devices.
253 int dm_mknodes(const char *name);
254 int dm_driver_version(char *version, size_t size);
256 /******************************************************
257 * Functions to build and manipulate trees of devices *
258 ******************************************************/
263 * Initialise an empty dependency tree.
265 * The tree consists of a root node together with one node for each mapped
266 * device which has child nodes for each device referenced in its table.
268 * Every node in the tree has one or more children and one or more parents.
270 * The root node is the parent/child of every node that doesn't have other
273 struct dm_tree *dm_tree_create(void);
274 void dm_tree_free(struct dm_tree *tree);
277 * Add nodes to the tree for a given device and all the devices it uses.
279 int dm_tree_add_dev(struct dm_tree *tree, uint32_t major, uint32_t minor);
280 int dm_tree_add_dev_with_udev_flags(struct dm_tree *tree, uint32_t major,
281 uint32_t minor, uint16_t udev_flags);
284 * Add a new node to the tree if it doesn't already exist.
286 struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *tree,
289 uint32_t major, uint32_t minor,
293 struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *tree,
301 uint16_t udev_flags);
304 * Search for a node in the tree.
305 * Set major and minor to 0 or uuid to NULL to get the root node.
307 struct dm_tree_node *dm_tree_find_node(struct dm_tree *tree,
310 struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *tree,
314 * Use this to walk through all children of a given node.
315 * Set handle to NULL in first call.
316 * Returns NULL after the last child.
317 * Set inverted to use inverted tree.
319 struct dm_tree_node *dm_tree_next_child(void **handle,
320 const struct dm_tree_node *parent,
324 * Get properties of a node.
326 const char *dm_tree_node_get_name(const struct dm_tree_node *node);
327 const char *dm_tree_node_get_uuid(const struct dm_tree_node *node);
328 const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node);
329 void *dm_tree_node_get_context(const struct dm_tree_node *node);
330 int dm_tree_node_size_changed(const struct dm_tree_node *dnode);
333 * Returns the number of children of the given node (excluding the root node).
334 * Set inverted for the number of parents.
336 int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted);
339 * Deactivate a device plus all dependencies.
340 * Ignores devices that don't have a uuid starting with uuid_prefix.
342 int dm_tree_deactivate_children(struct dm_tree_node *dnode,
343 const char *uuid_prefix,
344 size_t uuid_prefix_len);
346 * Preload/create a device plus all dependencies.
347 * Ignores devices that don't have a uuid starting with uuid_prefix.
349 int dm_tree_preload_children(struct dm_tree_node *dnode,
350 const char *uuid_prefix,
351 size_t uuid_prefix_len);
354 * Resume a device plus all dependencies.
355 * Ignores devices that don't have a uuid starting with uuid_prefix.
357 int dm_tree_activate_children(struct dm_tree_node *dnode,
358 const char *uuid_prefix,
359 size_t uuid_prefix_len);
362 * Suspend a device plus all dependencies.
363 * Ignores devices that don't have a uuid starting with uuid_prefix.
365 int dm_tree_suspend_children(struct dm_tree_node *dnode,
366 const char *uuid_prefix,
367 size_t uuid_prefix_len);
370 * Skip the filesystem sync when suspending.
371 * Does nothing with other functions.
372 * Use this when no snapshots are involved.
374 void dm_tree_skip_lockfs(struct dm_tree_node *dnode);
377 * Set the 'noflush' flag when suspending devices.
378 * If the kernel supports it, instead of erroring outstanding I/O that
379 * cannot be completed, the I/O is queued and resubmitted when the
380 * device is resumed. This affects multipath devices when all paths
381 * have failed and queue_if_no_path is set, and mirror devices when
382 * block_on_error is set and the mirror log has failed.
384 void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode);
387 * Is the uuid prefix present in the tree?
388 * Only returns 0 if every node was checked successfully.
389 * Returns 1 if the tree walk has to be aborted.
391 int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
392 const char *uuid_prefix,
393 size_t uuid_prefix_len);
396 * Construct tables for new nodes before activating them.
398 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
400 const char *origin_uuid);
401 int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
403 const char *origin_uuid,
404 const char *cow_uuid,
406 uint32_t chunk_size);
407 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
409 const char *origin_uuid,
410 const char *cow_uuid,
411 const char *merge_uuid,
412 uint32_t chunk_size);
413 int dm_tree_node_add_error_target(struct dm_tree_node *node,
415 int dm_tree_node_add_zero_target(struct dm_tree_node *node,
417 int dm_tree_node_add_linear_target(struct dm_tree_node *node,
419 int dm_tree_node_add_striped_target(struct dm_tree_node *node,
421 uint32_t stripe_size);
423 #define DM_CRYPT_IV_DEFAULT UINT64_C(-1) /* iv_offset == seg offset */
425 * Function accepts one string in cipher specification
426 * (chainmode and iv should be NULL because included in cipher string)
428 * separate arguments which will be joined to "cipher-chainmode-iv"
430 int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
433 const char *chainmode,
437 int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
440 /* Mirror log flags */
441 #define DM_NOSYNC 0x00000001 /* Known already in sync */
442 #define DM_FORCESYNC 0x00000002 /* Force resync */
443 #define DM_BLOCK_ON_ERROR 0x00000004 /* On error, suspend I/O */
444 #define DM_CORELOG 0x00000008 /* In-memory log */
446 int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
447 uint32_t region_size,
449 const char *log_uuid,
454 * Replicator operation mode
455 * Note: API for Replicator is not yet stable
458 DM_REPLICATOR_SYNC, /* Synchronous replication */
459 DM_REPLICATOR_ASYNC_WARN, /* Warn if async replicator is slow */
460 DM_REPLICATOR_ASYNC_STALL, /* Stall replicator if not fast enough */
461 DM_REPLICATOR_ASYNC_DROP, /* Drop sites out of sync */
462 DM_REPLICATOR_ASYNC_FAIL, /* Fail replicator if slow */
463 NUM_DM_REPLICATOR_MODES
464 } dm_replicator_mode_t;
466 int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
468 const char *rlog_uuid,
469 const char *rlog_type,
470 unsigned rsite_index,
471 dm_replicator_mode_t mode,
472 uint32_t async_timeout,
473 uint64_t fall_behind_data,
474 uint32_t fall_behind_ios);
476 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
478 const char *replicator_uuid, /* Replicator control device */
479 uint64_t rdevice_index,
480 const char *rdev_uuid, /* Rimage device name/uuid */
481 unsigned rsite_index,
482 const char *slog_uuid,
483 uint32_t slog_flags, /* Mirror log flags */
484 uint32_t slog_region_size);
485 /* End of Replicator API */
487 void dm_tree_node_set_presuspend_node(struct dm_tree_node *node,
488 struct dm_tree_node *presuspend_node);
490 int dm_tree_node_add_target_area(struct dm_tree_node *node,
491 const char *dev_name,
496 * Set readahead (in sectors) after loading the node.
498 void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
500 uint32_t read_ahead_flags);
502 void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie);
503 uint32_t dm_tree_get_cookie(struct dm_tree_node *node);
505 /*****************************************************************************
507 *****************************************************************************/
513 void *dm_malloc_aux(size_t s, const char *file, int line);
514 void *dm_malloc_aux_debug(size_t s, const char *file, int line);
515 void *dm_zalloc_aux(size_t s, const char *file, int line);
516 void *dm_zalloc_aux_debug(size_t s, const char *file, int line);
517 char *dm_strdup_aux(const char *str, const char *file, int line);
518 void dm_free_aux(void *p);
519 void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line);
520 int dm_dump_memory_debug(void);
521 void dm_bounds_check_debug(void);
525 # define dm_malloc(s) dm_malloc_aux_debug((s), __FILE__, __LINE__)
526 # define dm_zalloc(s) dm_zalloc_aux_debug((s), __FILE__, __LINE__)
527 # define dm_strdup(s) dm_strdup_aux((s), __FILE__, __LINE__)
528 # define dm_free(p) dm_free_aux(p)
529 # define dm_realloc(p, s) dm_realloc_aux(p, s, __FILE__, __LINE__)
530 # define dm_dump_memory() dm_dump_memory_debug()
531 # define dm_bounds_check() dm_bounds_check_debug()
535 # define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__)
536 # define dm_zalloc(s) dm_zalloc_aux((s), __FILE__, __LINE__)
537 # define dm_strdup(s) strdup(s)
538 # define dm_free(p) free(p)
539 # define dm_realloc(p, s) realloc(p, s)
540 # define dm_dump_memory() {}
541 # define dm_bounds_check() {}
547 * The pool allocator is useful when you are going to allocate
548 * lots of memory, use the memory for a bit, and then free the
549 * memory in one go. A surprising amount of code has this usage
552 * You should think of the pool as an infinite, contiguous chunk
553 * of memory. The front of this chunk of memory contains
554 * allocated objects, the second half is free. dm_pool_alloc grabs
555 * the next 'size' bytes from the free half, in effect moving it
556 * into the allocated half. This operation is very efficient.
558 * dm_pool_free frees the allocated object *and* all objects
559 * allocated after it. It is important to note this semantic
560 * difference from malloc/free. This is also extremely
561 * efficient, since a single dm_pool_free can dispose of a large
564 * dm_pool_destroy frees all allocated memory.
566 * eg, If you are building a binary tree in your program, and
567 * know that you are only ever going to insert into your tree,
568 * and not delete (eg, maintaining a symbol table for a
569 * compiler). You can create yourself a pool, allocate the nodes
570 * from it, and when the tree becomes redundant call dm_pool_destroy
571 * (no nasty iterating through the tree to free nodes).
573 * eg, On the other hand if you wanted to repeatedly insert and
574 * remove objects into the tree, you would be better off
575 * allocating the nodes from a free list; you cannot free a
576 * single arbitrary node with pool.
581 /* constructor and destructor */
582 struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint);
583 void dm_pool_destroy(struct dm_pool *p);
585 /* simple allocation/free routines */
586 void *dm_pool_alloc(struct dm_pool *p, size_t s);
587 void *dm_pool_alloc_aligned(struct dm_pool *p, size_t s, unsigned alignment);
588 void dm_pool_empty(struct dm_pool *p);
589 void dm_pool_free(struct dm_pool *p, void *ptr);
592 * Object building routines:
594 * These allow you to 'grow' an object, useful for
595 * building strings, or filling in dynamic
598 * It's probably best explained with an example:
600 * char *build_string(struct dm_pool *mem)
605 * if (!dm_pool_begin_object(mem, 128))
608 * for (i = 0; i < 50; i++) {
609 * snprintf(buffer, sizeof(buffer), "%d, ", i);
610 * if (!dm_pool_grow_object(mem, buffer, 0))
615 * if (!dm_pool_grow_object(mem, "\0", 1))
618 * return dm_pool_end_object(mem);
622 * dm_pool_abandon_object(mem);
626 * So start an object by calling dm_pool_begin_object
627 * with a guess at the final object size - if in
628 * doubt make the guess too small.
630 * Then append chunks of data to your object with
631 * dm_pool_grow_object. Finally get your object with
632 * a call to dm_pool_end_object.
634 * Setting delta to 0 means it will use strlen(extra).
636 int dm_pool_begin_object(struct dm_pool *p, size_t hint);
637 int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta);
638 void *dm_pool_end_object(struct dm_pool *p);
639 void dm_pool_abandon_object(struct dm_pool *p);
642 char *dm_pool_strdup(struct dm_pool *p, const char *str);
643 char *dm_pool_strndup(struct dm_pool *p, const char *str, size_t n);
644 void *dm_pool_zalloc(struct dm_pool *p, size_t s);
650 typedef uint32_t *dm_bitset_t;
652 dm_bitset_t dm_bitset_create(struct dm_pool *mem, unsigned num_bits);
653 void dm_bitset_destroy(dm_bitset_t bs);
655 int dm_bitset_equal(dm_bitset_t in1, dm_bitset_t in2);
657 void dm_bit_and(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2);
658 void dm_bit_union(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2);
659 int dm_bit_get_first(dm_bitset_t bs);
660 int dm_bit_get_next(dm_bitset_t bs, int last_bit);
662 #define DM_BITS_PER_INT (sizeof(int) * CHAR_BIT)
664 #define dm_bit(bs, i) \
665 ((bs)[((i) / DM_BITS_PER_INT) + 1] & (0x1 << ((i) & (DM_BITS_PER_INT - 1))))
667 #define dm_bit_set(bs, i) \
668 ((bs)[((i) / DM_BITS_PER_INT) + 1] |= (0x1 << ((i) & (DM_BITS_PER_INT - 1))))
670 #define dm_bit_clear(bs, i) \
671 ((bs)[((i) / DM_BITS_PER_INT) + 1] &= ~(0x1 << ((i) & (DM_BITS_PER_INT - 1))))
673 #define dm_bit_set_all(bs) \
674 memset((bs) + 1, -1, ((*(bs) / DM_BITS_PER_INT) + 1) * sizeof(int))
676 #define dm_bit_clear_all(bs) \
677 memset((bs) + 1, 0, ((*(bs) / DM_BITS_PER_INT) + 1) * sizeof(int))
679 #define dm_bit_copy(bs1, bs2) \
680 memcpy((bs1) + 1, (bs2) + 1, ((*(bs1) / DM_BITS_PER_INT) + 1) * sizeof(int))
682 /* Returns number of set bits */
683 static inline unsigned hweight32(uint32_t i)
685 unsigned r = (i & 0x55555555) + ((i >> 1) & 0x55555555);
687 r = (r & 0x33333333) + ((r >> 2) & 0x33333333);
688 r = (r & 0x0F0F0F0F) + ((r >> 4) & 0x0F0F0F0F);
689 r = (r & 0x00FF00FF) + ((r >> 8) & 0x00FF00FF);
690 return (r & 0x0000FFFF) + ((r >> 16) & 0x0000FFFF);
697 struct dm_hash_table;
700 typedef void (*dm_hash_iterate_fn) (void *data);
702 struct dm_hash_table *dm_hash_create(unsigned size_hint);
703 void dm_hash_destroy(struct dm_hash_table *t);
704 void dm_hash_wipe(struct dm_hash_table *t);
706 void *dm_hash_lookup(struct dm_hash_table *t, const char *key);
707 int dm_hash_insert(struct dm_hash_table *t, const char *key, void *data);
708 void dm_hash_remove(struct dm_hash_table *t, const char *key);
710 void *dm_hash_lookup_binary(struct dm_hash_table *t, const char *key, uint32_t len);
711 int dm_hash_insert_binary(struct dm_hash_table *t, const char *key, uint32_t len,
713 void dm_hash_remove_binary(struct dm_hash_table *t, const char *key, uint32_t len);
715 unsigned dm_hash_get_num_entries(struct dm_hash_table *t);
716 void dm_hash_iter(struct dm_hash_table *t, dm_hash_iterate_fn f);
718 char *dm_hash_get_key(struct dm_hash_table *t, struct dm_hash_node *n);
719 void *dm_hash_get_data(struct dm_hash_table *t, struct dm_hash_node *n);
720 struct dm_hash_node *dm_hash_get_first(struct dm_hash_table *t);
721 struct dm_hash_node *dm_hash_get_next(struct dm_hash_table *t, struct dm_hash_node *n);
723 #define dm_hash_iterate(v, h) \
724 for (v = dm_hash_get_first((h)); v; \
725 v = dm_hash_get_next((h), v))
732 * A list consists of a list head plus elements.
733 * Each element has 'next' and 'previous' pointers.
734 * The list head's pointers point to the first and the last element.
738 struct dm_list *n, *p;
742 * Initialise a list before use.
743 * The list head's next and previous pointers point back to itself.
745 #define DM_LIST_INIT(name) struct dm_list name = { &(name), &(name) }
746 void dm_list_init(struct dm_list *head);
749 * Insert an element before 'head'.
750 * If 'head' is the list head, this adds an element to the end of the list.
752 void dm_list_add(struct dm_list *head, struct dm_list *elem);
755 * Insert an element after 'head'.
756 * If 'head' is the list head, this adds an element to the front of the list.
758 void dm_list_add_h(struct dm_list *head, struct dm_list *elem);
761 * Delete an element from its list.
762 * Note that this doesn't change the element itself - it may still be safe
763 * to follow its pointers.
765 void dm_list_del(struct dm_list *elem);
768 * Remove an element from existing list and insert before 'head'.
770 void dm_list_move(struct dm_list *head, struct dm_list *elem);
773 * Join 'head1' to the of 'head'.
775 void dm_list_splice(struct dm_list *head, struct dm_list *head1);
780 int dm_list_empty(const struct dm_list *head);
783 * Is this the first element of the list?
785 int dm_list_start(const struct dm_list *head, const struct dm_list *elem);
788 * Is this the last element of the list?
790 int dm_list_end(const struct dm_list *head, const struct dm_list *elem);
793 * Return first element of the list or NULL if empty
795 struct dm_list *dm_list_first(const struct dm_list *head);
798 * Return last element of the list or NULL if empty
800 struct dm_list *dm_list_last(const struct dm_list *head);
803 * Return the previous element of the list, or NULL if we've reached the start.
805 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem);
808 * Return the next element of the list, or NULL if we've reached the end.
810 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem);
813 * Given the address v of an instance of 'struct dm_list' called 'head'
814 * contained in a structure of type t, return the containing structure.
816 #define dm_list_struct_base(v, t, head) \
817 ((t *)((const char *)(v) - (const char *)&((t *) 0)->head))
820 * Given the address v of an instance of 'struct dm_list list' contained in
821 * a structure of type t, return the containing structure.
823 #define dm_list_item(v, t) dm_list_struct_base((v), t, list)
826 * Given the address v of one known element e in a known structure of type t,
827 * return another element f.
829 #define dm_struct_field(v, t, e, f) \
830 (((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
833 * Given the address v of a known element e in a known structure of type t,
834 * return the list head 'list'
836 #define dm_list_head(v, t, e) dm_struct_field(v, t, e, list)
839 * Set v to each element of a list in turn.
841 #define dm_list_iterate(v, head) \
842 for (v = (head)->n; v != head; v = v->n)
845 * Set v to each element in a list in turn, starting from the element
846 * in front of 'start'.
847 * You can use this to 'unwind' a list_iterate and back out actions on
848 * already-processed elements.
849 * If 'start' is 'head' it walks the list backwards.
851 #define dm_list_uniterate(v, head, start) \
852 for (v = (start)->p; v != head; v = v->p)
855 * A safe way to walk a list and delete and free some elements along
857 * t must be defined as a temporary variable of the same type as v.
859 #define dm_list_iterate_safe(v, t, head) \
860 for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
863 * Walk a list, setting 'v' in turn to the containing structure of each item.
864 * The containing structure should be the same type as 'v'.
865 * The 'struct dm_list' variable within the containing structure is 'field'.
867 #define dm_list_iterate_items_gen(v, head, field) \
868 for (v = dm_list_struct_base((head)->n, __typeof__(*v), field); \
869 &v->field != (head); \
870 v = dm_list_struct_base(v->field.n, __typeof__(*v), field))
873 * Walk a list, setting 'v' in turn to the containing structure of each item.
874 * The containing structure should be the same type as 'v'.
875 * The list should be 'struct dm_list list' within the containing structure.
877 #define dm_list_iterate_items(v, head) dm_list_iterate_items_gen(v, (head), list)
880 * Walk a list, setting 'v' in turn to the containing structure of each item.
881 * The containing structure should be the same type as 'v'.
882 * The 'struct dm_list' variable within the containing structure is 'field'.
883 * t must be defined as a temporary variable of the same type as v.
885 #define dm_list_iterate_items_gen_safe(v, t, head, field) \
886 for (v = dm_list_struct_base((head)->n, __typeof__(*v), field), \
887 t = dm_list_struct_base(v->field.n, __typeof__(*v), field); \
888 &v->field != (head); \
889 v = t, t = dm_list_struct_base(v->field.n, __typeof__(*v), field))
891 * Walk a list, setting 'v' in turn to the containing structure of each item.
892 * The containing structure should be the same type as 'v'.
893 * The list should be 'struct dm_list list' within the containing structure.
894 * t must be defined as a temporary variable of the same type as v.
896 #define dm_list_iterate_items_safe(v, t, head) \
897 dm_list_iterate_items_gen_safe(v, t, (head), list)
900 * Walk a list backwards, setting 'v' in turn to the containing structure
902 * The containing structure should be the same type as 'v'.
903 * The 'struct dm_list' variable within the containing structure is 'field'.
905 #define dm_list_iterate_back_items_gen(v, head, field) \
906 for (v = dm_list_struct_base((head)->p, __typeof__(*v), field); \
907 &v->field != (head); \
908 v = dm_list_struct_base(v->field.p, __typeof__(*v), field))
911 * Walk a list backwards, setting 'v' in turn to the containing structure
913 * The containing structure should be the same type as 'v'.
914 * The list should be 'struct dm_list list' within the containing structure.
916 #define dm_list_iterate_back_items(v, head) dm_list_iterate_back_items_gen(v, (head), list)
919 * Return the number of elements in a list by walking it.
921 unsigned int dm_list_size(const struct dm_list *head);
928 * Obtain SELinux security context assigned for the path and set this
929 * context for creating a new file system object. This security context
930 * is global and it is used until reset to default policy behaviour
931 * by calling 'dm_prepare_selinux_context(NULL, 0)'.
933 int dm_prepare_selinux_context(const char *path, mode_t mode);
935 * Set SELinux context for existing file system object.
937 int dm_set_selinux_context(const char *path, mode_t mode);
939 /*********************
940 * string manipulation
941 *********************/
944 * Break up the name of a mapped device into its constituent
945 * Volume Group, Logical Volume and Layer (if present).
946 * If mem is supplied, the result is allocated from the mempool.
947 * Otherwise the strings are changed in situ.
949 int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,
950 char **vgname, char **lvname, char **layer);
953 * Destructively split buffer into NULL-separated words in argv.
954 * Returns number of words.
956 int dm_split_words(char *buffer, unsigned max,
957 unsigned ignore_comments, /* Not implemented */
961 * Returns -1 if buffer too small
963 int dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
964 __attribute__ ((format(printf, 3, 4)));
967 * Returns pointer to the last component of the path.
969 const char *dm_basename(const char *path);
971 /**************************
972 * file/stream manipulation
973 **************************/
976 * Create a directory (with parent directories if necessary).
977 * Returns 1 on success, 0 on failure.
979 int dm_create_dir(const char *dir);
982 * Close a stream, with nicer error checking than fclose's.
983 * Derived from gnulib's close-stream.c.
985 * Close "stream". Return 0 if successful, and EOF (setting errno)
986 * otherwise. Upon failure, set errno to 0 if the error number
987 * cannot be determined. Useful mainly for writable streams.
989 int dm_fclose(FILE *stream);
992 * Returns size of a buffer which is allocated with dm_malloc.
993 * Pointer to the buffer is stored in *buf.
994 * Returns -1 on failure leaving buf undefined.
996 int dm_asprintf(char **buf, const char *format, ...)
997 __attribute__ ((format(printf, 2, 3)));
1000 * create lockfile (pidfile) - create and lock a lock file
1001 * @lockfile: location of lock file
1003 * Returns: 1 on success, 0 otherwise, errno is handled internally
1005 int dm_create_lockfile(const char* lockfile);
1008 * Query whether a daemon is running based on its lockfile
1010 * Returns: 1 if running, 0 if not
1012 int dm_daemon_is_running(const char* lockfile);
1014 /*********************
1015 * regular expressions
1016 *********************/
1020 * Initialise an array of num patterns for matching.
1021 * Uses memory from mem.
1023 struct dm_regex *dm_regex_create(struct dm_pool *mem, const char * const *patterns,
1024 unsigned num_patterns);
1027 * Match string s against the patterns.
1028 * Returns the index of the highest pattern in the array that matches,
1029 * or -1 if none match.
1031 int dm_regex_match(struct dm_regex *regex, const char *s);
1034 * This is useful for regression testing only. The idea is if two
1035 * fingerprints are different, then the two dfas are certainly not
1036 * isomorphic. If two fingerprints _are_ the same then it's very likely
1037 * that the dfas are isomorphic.
1039 * This function must be called before any matching is done.
1041 uint32_t dm_regex_fingerprint(struct dm_regex *regex);
1043 /*********************
1044 * reporting functions
1045 *********************/
1047 struct dm_report_object_type {
1048 uint32_t id; /* Powers of 2 */
1050 const char *prefix; /* field id string prefix (optional) */
1051 void *(*data_fn)(void *object); /* callback from report_object() */
1054 struct dm_report_field;
1057 * dm_report_field_type flags
1059 #define DM_REPORT_FIELD_MASK 0x000000FF
1060 #define DM_REPORT_FIELD_ALIGN_MASK 0x0000000F
1061 #define DM_REPORT_FIELD_ALIGN_LEFT 0x00000001
1062 #define DM_REPORT_FIELD_ALIGN_RIGHT 0x00000002
1063 #define DM_REPORT_FIELD_TYPE_MASK 0x000000F0
1064 #define DM_REPORT_FIELD_TYPE_STRING 0x00000010
1065 #define DM_REPORT_FIELD_TYPE_NUMBER 0x00000020
1067 #define DM_REPORT_FIELD_TYPE_ID_LEN 32
1068 #define DM_REPORT_FIELD_TYPE_HEADING_LEN 32
1071 struct dm_report_field_type {
1072 uint32_t type; /* object type id */
1073 uint32_t flags; /* DM_REPORT_FIELD_* */
1074 uint32_t offset; /* byte offset in the object */
1075 int32_t width; /* default width */
1076 /* string used to specify the field */
1077 const char id[DM_REPORT_FIELD_TYPE_ID_LEN];
1078 /* string printed in header */
1079 const char heading[DM_REPORT_FIELD_TYPE_HEADING_LEN];
1080 int (*report_fn)(struct dm_report *rh, struct dm_pool *mem,
1081 struct dm_report_field *field, const void *data,
1082 void *private_data);
1083 const char *desc; /* description of the field */
1087 * dm_report_init output_flags
1089 #define DM_REPORT_OUTPUT_MASK 0x000000FF
1090 #define DM_REPORT_OUTPUT_ALIGNED 0x00000001
1091 #define DM_REPORT_OUTPUT_BUFFERED 0x00000002
1092 #define DM_REPORT_OUTPUT_HEADINGS 0x00000004
1093 #define DM_REPORT_OUTPUT_FIELD_NAME_PREFIX 0x00000008
1094 #define DM_REPORT_OUTPUT_FIELD_UNQUOTED 0x00000010
1095 #define DM_REPORT_OUTPUT_COLUMNS_AS_ROWS 0x00000020
1097 struct dm_report *dm_report_init(uint32_t *report_types,
1098 const struct dm_report_object_type *types,
1099 const struct dm_report_field_type *fields,
1100 const char *output_fields,
1101 const char *output_separator,
1102 uint32_t output_flags,
1103 const char *sort_keys,
1104 void *private_data);
1105 int dm_report_object(struct dm_report *rh, void *object);
1106 int dm_report_output(struct dm_report *rh);
1107 void dm_report_free(struct dm_report *rh);
1110 * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
1112 int dm_report_set_output_field_name_prefix(struct dm_report *rh,
1113 const char *report_prefix);
1116 * Report functions are provided for simple data types.
1117 * They take care of allocating copies of the data.
1119 int dm_report_field_string(struct dm_report *rh, struct dm_report_field *field,
1121 int dm_report_field_int32(struct dm_report *rh, struct dm_report_field *field,
1122 const int32_t *data);
1123 int dm_report_field_uint32(struct dm_report *rh, struct dm_report_field *field,
1124 const uint32_t *data);
1125 int dm_report_field_int(struct dm_report *rh, struct dm_report_field *field,
1127 int dm_report_field_uint64(struct dm_report *rh, struct dm_report_field *field,
1128 const uint64_t *data);
1131 * For custom fields, allocate the data in 'mem' and use
1132 * dm_report_field_set_value().
1133 * 'sortvalue' may be NULL if it matches 'value'
1135 void dm_report_field_set_value(struct dm_report_field *field, const void *value,
1136 const void *sortvalue);
1139 * The cookie value consists of a prefix (16 bits) and a base (16 bits).
1140 * We can use the prefix to store the flags. These flags are sent to
1141 * kernel within given dm task. When returned back to userspace in
1142 * DM_COOKIE udev environment variable, we can control several aspects
1143 * of udev rules we use by decoding the cookie prefix. When doing the
1144 * notification, we replace the cookie prefix with DM_COOKIE_MAGIC,
1145 * so we notify the right semaphore.
1146 * It is still possible to use cookies for passing the flags to udev
1147 * rules even when udev_sync is disabled. The base part of the cookie
1148 * will be zero (there's no notification semaphore) and prefix will be
1149 * set then. However, having udev_sync enabled is highly recommended.
1151 #define DM_COOKIE_MAGIC 0x0D4D
1152 #define DM_UDEV_FLAGS_MASK 0xFFFF0000
1153 #define DM_UDEV_FLAGS_SHIFT 16
1156 * DM_UDEV_DISABLE_DM_RULES_FLAG is set in case we need to disable
1157 * basic device-mapper udev rules that create symlinks in /dev/<DM_DIR>
1158 * directory. However, we can't reliably prevent creating default
1159 * nodes by udev (commonly /dev/dm-X, where X is a number).
1161 #define DM_UDEV_DISABLE_DM_RULES_FLAG 0x0001
1163 * DM_UDEV_DISABLE_SUBSYTEM_RULES_FLAG is set in case we need to disable
1164 * subsystem udev rules, but still we need the general DM udev rules to
1165 * be applied (to create the nodes and symlinks under /dev and /dev/disk).
1167 #define DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG 0x0002
1169 * DM_UDEV_DISABLE_DISK_RULES_FLAG is set in case we need to disable
1170 * general DM rules that set symlinks in /dev/disk directory.
1172 #define DM_UDEV_DISABLE_DISK_RULES_FLAG 0x0004
1174 * DM_UDEV_DISABLE_OTHER_RULES_FLAG is set in case we need to disable
1175 * all the other rules that are not general device-mapper nor subsystem
1176 * related (the rules belong to other software or packages). All foreign
1177 * rules should check this flag directly and they should ignore further
1178 * rule processing for such event.
1180 #define DM_UDEV_DISABLE_OTHER_RULES_FLAG 0x0008
1182 * DM_UDEV_LOW_PRIORITY_FLAG is set in case we need to instruct the
1183 * udev rules to give low priority to the device that is currently
1184 * processed. For example, this provides a way to select which symlinks
1185 * could be overwritten by high priority ones if their names are equal.
1186 * Common situation is a name based on FS UUID while using origin and
1189 #define DM_UDEV_LOW_PRIORITY_FLAG 0x0010
1191 * DM_UDEV_DISABLE_LIBRARY_FALLBACK is set in case we need to disable
1192 * libdevmapper's node management. We will rely on udev completely
1193 * and there will be no fallback action provided by libdevmapper if
1194 * udev does something improperly.
1196 #define DM_UDEV_DISABLE_LIBRARY_FALLBACK 0x0020
1198 * DM_UDEV_PRIMARY_SOURCE_FLAG is automatically appended by
1199 * libdevmapper for all ioctls generating udev uevents. Once used in
1200 * udev rules, we know if this is a real "primary sourced" event or not.
1201 * We need to distinguish real events originated in libdevmapper from
1202 * any spurious events to gather all missing information (e.g. events
1203 * generated as a result of "udevadm trigger" command or as a result
1204 * of the "watch" udev rule).
1206 #define DM_UDEV_PRIMARY_SOURCE_FLAG 0x0040
1208 int dm_cookie_supported(void);
1211 * Udev synchronisation functions.
1213 void dm_udev_set_sync_support(int sync_with_udev);
1214 int dm_udev_get_sync_support(void);
1215 void dm_udev_set_checking(int checking);
1216 int dm_udev_get_checking(void);
1217 int dm_udev_create_cookie(uint32_t *cookie);
1218 int dm_udev_complete(uint32_t cookie);
1219 int dm_udev_wait(uint32_t cookie);
1221 #define DM_DEV_DIR_UMASK 0022
1226 #endif /* LIB_DEVICE_MAPPER_H */