4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of the device-mapper userspace tools.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #ifndef LIB_DEVICE_MAPPER_H
19 #define LIB_DEVICE_MAPPER_H
23 #include <sys/types.h>
26 # include <linux/types.h>
34 /*****************************************************************
35 * The first section of this file provides direct access to the
36 * individual device-mapper ioctls. Since it is quite laborious to
37 * build the ioctl arguments for the device-mapper, people are
38 * encouraged to use this library.
39 ****************************************************************/
42 * The library user may wish to register their own
43 * logging function. By default errors go to stderr.
44 * Use dm_log_with_errno_init(NULL) to restore the default log fn.
47 typedef void (*dm_log_with_errno_fn
) (int level
, const char *file
, int line
,
48 int dm_errno
, const char *f
, ...)
49 __attribute__ ((format(printf
, 5, 6)));
51 void dm_log_with_errno_init(dm_log_with_errno_fn fn
);
52 void dm_log_init_verbose(int level
);
55 * Original version of this function.
56 * dm_errno is set to 0.
58 * Deprecated: Use the _with_errno_ versions above instead.
60 typedef void (*dm_log_fn
) (int level
, const char *file
, int line
,
62 __attribute__ ((format(printf
, 4, 5)));
63 void dm_log_init(dm_log_fn fn
);
65 * For backward-compatibility, indicate that dm_log_init() was used
66 * to set a non-default value of dm_log().
68 int dm_log_is_non_default(void);
95 DM_DEVICE_LIST_VERSIONS
,
99 DM_DEVICE_SET_GEOMETRY
103 * You will need to build a struct dm_task for
104 * each ioctl command you want to execute.
109 struct dm_task
*dm_task_create(int type
);
110 void dm_task_destroy(struct dm_task
*dmt
);
112 int dm_task_set_name(struct dm_task
*dmt
, const char *name
);
113 int dm_task_set_uuid(struct dm_task
*dmt
, const char *uuid
);
116 * Retrieve attributes after an info.
126 uint32_t minor
; /* minor device number */
127 int read_only
; /* 0:read-write; 1:read-only */
129 int32_t target_count
;
140 uint32_t next
; /* Offset to next struct from start of this struct */
145 uint32_t next
; /* Offset to next struct from start of this struct */
151 int dm_get_library_version(char *version
, size_t size
);
152 int dm_task_get_driver_version(struct dm_task
*dmt
, char *version
, size_t size
);
153 int dm_task_get_info(struct dm_task
*dmt
, struct dm_info
*dmi
);
154 const char *dm_task_get_name(const struct dm_task
*dmt
);
155 const char *dm_task_get_uuid(const struct dm_task
*dmt
);
157 struct dm_deps
*dm_task_get_deps(struct dm_task
*dmt
);
158 struct dm_names
*dm_task_get_names(struct dm_task
*dmt
);
159 struct dm_versions
*dm_task_get_versions(struct dm_task
*dmt
);
161 int dm_task_set_ro(struct dm_task
*dmt
);
162 int dm_task_set_newname(struct dm_task
*dmt
, const char *newname
);
163 int dm_task_set_minor(struct dm_task
*dmt
, int minor
);
164 int dm_task_set_major(struct dm_task
*dmt
, int major
);
165 int dm_task_set_major_minor(struct dm_task
*dmt
, int major
, int minor
, int allow_default_major_fallback
);
166 int dm_task_set_uid(struct dm_task
*dmt
, uid_t uid
);
167 int dm_task_set_gid(struct dm_task
*dmt
, gid_t gid
);
168 int dm_task_set_mode(struct dm_task
*dmt
, mode_t mode
);
169 int dm_task_set_cookie(struct dm_task
*dmt
, uint32_t *cookie
, uint16_t flags
);
170 int dm_task_set_event_nr(struct dm_task
*dmt
, uint32_t event_nr
);
171 int dm_task_set_geometry(struct dm_task
*dmt
, const char *cylinders
, const char *heads
, const char *sectors
, const char *start
);
172 int dm_task_set_message(struct dm_task
*dmt
, const char *message
);
173 int dm_task_set_sector(struct dm_task
*dmt
, uint64_t sector
);
174 int dm_task_no_flush(struct dm_task
*dmt
);
175 int dm_task_no_open_count(struct dm_task
*dmt
);
176 int dm_task_skip_lockfs(struct dm_task
*dmt
);
177 int dm_task_query_inactive_table(struct dm_task
*dmt
);
178 int dm_task_suppress_identical_reload(struct dm_task
*dmt
);
181 * Control read_ahead.
183 #define DM_READ_AHEAD_AUTO UINT32_MAX /* Use kernel default readahead */
184 #define DM_READ_AHEAD_NONE 0 /* Disable readahead */
186 #define DM_READ_AHEAD_MINIMUM_FLAG 0x1 /* Value supplied is minimum */
189 * Read ahead is set with DM_DEVICE_CREATE with a table or DM_DEVICE_RESUME.
191 int dm_task_set_read_ahead(struct dm_task
*dmt
, uint32_t read_ahead
,
192 uint32_t read_ahead_flags
);
193 uint32_t dm_task_get_read_ahead(const struct dm_task
*dmt
,
194 uint32_t *read_ahead
);
197 * Use these to prepare for a create or reload.
199 int dm_task_add_target(struct dm_task
*dmt
,
201 uint64_t size
, const char *ttype
, const char *params
);
204 * Format major/minor numbers correctly for input to driver.
206 #define DM_FORMAT_DEV_BUFSIZE 13 /* Minimum bufsize to handle worst case. */
207 int dm_format_dev(char *buf
, int bufsize
, uint32_t dev_major
, uint32_t dev_minor
);
209 /* Use this to retrive target information returned from a STATUS call */
210 void *dm_get_next_target(struct dm_task
*dmt
,
211 void *next
, uint64_t *start
, uint64_t *length
,
212 char **target_type
, char **params
);
215 * Call this to actually run the ioctl.
217 int dm_task_run(struct dm_task
*dmt
);
220 * Call this to make or remove the device nodes associated with previously
223 void dm_task_update_nodes(void);
226 * Configure the device-mapper directory
228 int dm_set_dev_dir(const char *dir
);
229 const char *dm_dir(void);
232 * Determine whether a major number belongs to device-mapper or not.
234 int dm_is_dm_major(uint32_t major
);
237 * Release library resources
239 void dm_lib_release(void);
240 void dm_lib_exit(void) __attribute((destructor
));
243 * Use NULL for all devices.
245 int dm_mknodes(const char *name
);
246 int dm_driver_version(char *version
, size_t size
);
248 /******************************************************
249 * Functions to build and manipulate trees of devices *
250 ******************************************************/
255 * Initialise an empty dependency tree.
257 * The tree consists of a root node together with one node for each mapped
258 * device which has child nodes for each device referenced in its table.
260 * Every node in the tree has one or more children and one or more parents.
262 * The root node is the parent/child of every node that doesn't have other
265 struct dm_tree
*dm_tree_create(void);
266 void dm_tree_free(struct dm_tree
*tree
);
269 * Add nodes to the tree for a given device and all the devices it uses.
271 int dm_tree_add_dev(struct dm_tree
*tree
, uint32_t major
, uint32_t minor
);
274 * Add a new node to the tree if it doesn't already exist.
276 struct dm_tree_node
*dm_tree_add_new_dev(struct dm_tree
*tree
,
279 uint32_t major
, uint32_t minor
,
283 struct dm_tree_node
*dm_tree_add_new_dev_with_udev_flags(struct dm_tree
*tree
,
291 uint16_t udev_flags
);
294 * Search for a node in the tree.
295 * Set major and minor to 0 or uuid to NULL to get the root node.
297 struct dm_tree_node
*dm_tree_find_node(struct dm_tree
*tree
,
300 struct dm_tree_node
*dm_tree_find_node_by_uuid(struct dm_tree
*tree
,
304 * Use this to walk through all children of a given node.
305 * Set handle to NULL in first call.
306 * Returns NULL after the last child.
307 * Set inverted to use inverted tree.
309 struct dm_tree_node
*dm_tree_next_child(void **handle
,
310 struct dm_tree_node
*parent
,
314 * Get properties of a node.
316 const char *dm_tree_node_get_name(struct dm_tree_node
*node
);
317 const char *dm_tree_node_get_uuid(struct dm_tree_node
*node
);
318 const struct dm_info
*dm_tree_node_get_info(struct dm_tree_node
*node
);
319 void *dm_tree_node_get_context(struct dm_tree_node
*node
);
320 int dm_tree_node_size_changed(struct dm_tree_node
*dnode
);
323 * Returns the number of children of the given node (excluding the root node).
324 * Set inverted for the number of parents.
326 int dm_tree_node_num_children(struct dm_tree_node
*node
, uint32_t inverted
);
329 * Deactivate a device plus all dependencies.
330 * Ignores devices that don't have a uuid starting with uuid_prefix.
332 int dm_tree_deactivate_children(struct dm_tree_node
*dnode
,
333 const char *uuid_prefix
,
334 size_t uuid_prefix_len
);
336 * Preload/create a device plus all dependencies.
337 * Ignores devices that don't have a uuid starting with uuid_prefix.
339 int dm_tree_preload_children(struct dm_tree_node
*dnode
,
340 const char *uuid_prefix
,
341 size_t uuid_prefix_len
);
344 * Resume a device plus all dependencies.
345 * Ignores devices that don't have a uuid starting with uuid_prefix.
347 int dm_tree_activate_children(struct dm_tree_node
*dnode
,
348 const char *uuid_prefix
,
349 size_t uuid_prefix_len
);
352 * Suspend a device plus all dependencies.
353 * Ignores devices that don't have a uuid starting with uuid_prefix.
355 int dm_tree_suspend_children(struct dm_tree_node
*dnode
,
356 const char *uuid_prefix
,
357 size_t uuid_prefix_len
);
360 * Skip the filesystem sync when suspending.
361 * Does nothing with other functions.
362 * Use this when no snapshots are involved.
364 void dm_tree_skip_lockfs(struct dm_tree_node
*dnode
);
367 * Set the 'noflush' flag when suspending devices.
368 * If the kernel supports it, instead of erroring outstanding I/O that
369 * cannot be completed, the I/O is queued and resubmitted when the
370 * device is resumed. This affects multipath devices when all paths
371 * have failed and queue_if_no_path is set, and mirror devices when
372 * block_on_error is set and the mirror log has failed.
374 void dm_tree_use_no_flush_suspend(struct dm_tree_node
*dnode
);
377 * Is the uuid prefix present in the tree?
378 * Only returns 0 if every node was checked successfully.
379 * Returns 1 if the tree walk has to be aborted.
381 int dm_tree_children_use_uuid(struct dm_tree_node
*dnode
,
382 const char *uuid_prefix
,
383 size_t uuid_prefix_len
);
386 * Construct tables for new nodes before activating them.
388 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node
*dnode
,
390 const char *origin_uuid
);
391 int dm_tree_node_add_snapshot_target(struct dm_tree_node
*node
,
393 const char *origin_uuid
,
394 const char *cow_uuid
,
396 uint32_t chunk_size
);
397 int dm_tree_node_add_error_target(struct dm_tree_node
*node
,
399 int dm_tree_node_add_zero_target(struct dm_tree_node
*node
,
401 int dm_tree_node_add_linear_target(struct dm_tree_node
*node
,
403 int dm_tree_node_add_striped_target(struct dm_tree_node
*node
,
405 uint32_t stripe_size
);
407 #define DM_CRYPT_IV_DEFAULT UINT64_C(-1) /* iv_offset == seg offset */
409 * Function accepts one string in cipher specification
410 * (chainmode and iv should be NULL because included in cipher string)
412 * separate arguments which will be joined to "cipher-chainmode-iv"
414 int dm_tree_node_add_crypt_target(struct dm_tree_node
*node
,
417 const char *chainmode
,
421 int dm_tree_node_add_mirror_target(struct dm_tree_node
*node
,
424 /* Mirror log flags */
425 #define DM_NOSYNC 0x00000001 /* Known already in sync */
426 #define DM_FORCESYNC 0x00000002 /* Force resync */
427 #define DM_BLOCK_ON_ERROR 0x00000004 /* On error, suspend I/O */
428 #define DM_CORELOG 0x00000008 /* In-memory log */
430 int dm_tree_node_add_mirror_target_log(struct dm_tree_node
*node
,
431 uint32_t region_size
,
433 const char *log_uuid
,
436 int dm_tree_node_add_target_area(struct dm_tree_node
*node
,
437 const char *dev_name
,
442 * Set readahead (in sectors) after loading the node.
444 void dm_tree_node_set_read_ahead(struct dm_tree_node
*dnode
,
446 uint32_t read_ahead_flags
);
448 void dm_tree_set_cookie(struct dm_tree_node
*node
, uint32_t cookie
);
449 uint32_t dm_tree_get_cookie(struct dm_tree_node
*node
);
451 /*****************************************************************************
453 *****************************************************************************/
459 void *dm_malloc_aux(size_t s
, const char *file
, int line
);
460 void *dm_malloc_aux_debug(size_t s
, const char *file
, int line
);
461 char *dm_strdup_aux(const char *str
, const char *file
, int line
);
462 void dm_free_aux(void *p
);
463 void *dm_realloc_aux(void *p
, unsigned int s
, const char *file
, int line
);
464 int dm_dump_memory_debug(void);
465 void dm_bounds_check_debug(void);
469 # define dm_malloc(s) dm_malloc_aux_debug((s), __FILE__, __LINE__)
470 # define dm_strdup(s) dm_strdup_aux((s), __FILE__, __LINE__)
471 # define dm_free(p) dm_free_aux(p)
472 # define dm_realloc(p, s) dm_realloc_aux(p, s, __FILE__, __LINE__)
473 # define dm_dump_memory() dm_dump_memory_debug()
474 # define dm_bounds_check() dm_bounds_check_debug()
478 # define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__)
479 # define dm_strdup(s) strdup(s)
480 # define dm_free(p) free(p)
481 # define dm_realloc(p, s) realloc(p, s)
482 # define dm_dump_memory() {}
483 # define dm_bounds_check() {}
489 * The pool allocator is useful when you are going to allocate
490 * lots of memory, use the memory for a bit, and then free the
491 * memory in one go. A surprising amount of code has this usage
494 * You should think of the pool as an infinite, contiguous chunk
495 * of memory. The front of this chunk of memory contains
496 * allocated objects, the second half is free. dm_pool_alloc grabs
497 * the next 'size' bytes from the free half, in effect moving it
498 * into the allocated half. This operation is very efficient.
500 * dm_pool_free frees the allocated object *and* all objects
501 * allocated after it. It is important to note this semantic
502 * difference from malloc/free. This is also extremely
503 * efficient, since a single dm_pool_free can dispose of a large
506 * dm_pool_destroy frees all allocated memory.
508 * eg, If you are building a binary tree in your program, and
509 * know that you are only ever going to insert into your tree,
510 * and not delete (eg, maintaining a symbol table for a
511 * compiler). You can create yourself a pool, allocate the nodes
512 * from it, and when the tree becomes redundant call dm_pool_destroy
513 * (no nasty iterating through the tree to free nodes).
515 * eg, On the other hand if you wanted to repeatedly insert and
516 * remove objects into the tree, you would be better off
517 * allocating the nodes from a free list; you cannot free a
518 * single arbitrary node with pool.
523 /* constructor and destructor */
524 struct dm_pool
*dm_pool_create(const char *name
, size_t chunk_hint
);
525 void dm_pool_destroy(struct dm_pool
*p
);
527 /* simple allocation/free routines */
528 void *dm_pool_alloc(struct dm_pool
*p
, size_t s
);
529 void *dm_pool_alloc_aligned(struct dm_pool
*p
, size_t s
, unsigned alignment
);
530 void dm_pool_empty(struct dm_pool
*p
);
531 void dm_pool_free(struct dm_pool
*p
, void *ptr
);
534 * Object building routines:
536 * These allow you to 'grow' an object, useful for
537 * building strings, or filling in dynamic
540 * It's probably best explained with an example:
542 * char *build_string(struct dm_pool *mem)
547 * if (!dm_pool_begin_object(mem, 128))
550 * for (i = 0; i < 50; i++) {
551 * snprintf(buffer, sizeof(buffer), "%d, ", i);
552 * if (!dm_pool_grow_object(mem, buffer, 0))
557 * if (!dm_pool_grow_object(mem, "\0", 1))
560 * return dm_pool_end_object(mem);
564 * dm_pool_abandon_object(mem);
568 * So start an object by calling dm_pool_begin_object
569 * with a guess at the final object size - if in
570 * doubt make the guess too small.
572 * Then append chunks of data to your object with
573 * dm_pool_grow_object. Finally get your object with
574 * a call to dm_pool_end_object.
576 * Setting delta to 0 means it will use strlen(extra).
578 int dm_pool_begin_object(struct dm_pool
*p
, size_t hint
);
579 int dm_pool_grow_object(struct dm_pool
*p
, const void *extra
, size_t delta
);
580 void *dm_pool_end_object(struct dm_pool
*p
);
581 void dm_pool_abandon_object(struct dm_pool
*p
);
584 char *dm_pool_strdup(struct dm_pool
*p
, const char *str
);
585 char *dm_pool_strndup(struct dm_pool
*p
, const char *str
, size_t n
);
586 void *dm_pool_zalloc(struct dm_pool
*p
, size_t s
);
592 typedef uint32_t *dm_bitset_t
;
594 dm_bitset_t
dm_bitset_create(struct dm_pool
*mem
, unsigned num_bits
);
595 void dm_bitset_destroy(dm_bitset_t bs
);
597 void dm_bit_union(dm_bitset_t out
, dm_bitset_t in1
, dm_bitset_t in2
);
598 int dm_bit_get_first(dm_bitset_t bs
);
599 int dm_bit_get_next(dm_bitset_t bs
, int last_bit
);
601 #define DM_BITS_PER_INT (sizeof(int) * CHAR_BIT)
603 #define dm_bit(bs, i) \
604 (bs[(i / DM_BITS_PER_INT) + 1] & (0x1 << (i & (DM_BITS_PER_INT - 1))))
606 #define dm_bit_set(bs, i) \
607 (bs[(i / DM_BITS_PER_INT) + 1] |= (0x1 << (i & (DM_BITS_PER_INT - 1))))
609 #define dm_bit_clear(bs, i) \
610 (bs[(i / DM_BITS_PER_INT) + 1] &= ~(0x1 << (i & (DM_BITS_PER_INT - 1))))
612 #define dm_bit_set_all(bs) \
613 memset(bs + 1, -1, ((*bs / DM_BITS_PER_INT) + 1) * sizeof(int))
615 #define dm_bit_clear_all(bs) \
616 memset(bs + 1, 0, ((*bs / DM_BITS_PER_INT) + 1) * sizeof(int))
618 #define dm_bit_copy(bs1, bs2) \
619 memcpy(bs1 + 1, bs2 + 1, ((*bs1 / DM_BITS_PER_INT) + 1) * sizeof(int))
621 /* Returns number of set bits */
622 static inline unsigned hweight32(uint32_t i
)
624 unsigned r
= (i
& 0x55555555) + ((i
>> 1) & 0x55555555);
626 r
= (r
& 0x33333333) + ((r
>> 2) & 0x33333333);
627 r
= (r
& 0x0F0F0F0F) + ((r
>> 4) & 0x0F0F0F0F);
628 r
= (r
& 0x00FF00FF) + ((r
>> 8) & 0x00FF00FF);
629 return (r
& 0x0000FFFF) + ((r
>> 16) & 0x0000FFFF);
636 struct dm_hash_table
;
639 typedef void (*dm_hash_iterate_fn
) (void *data
);
641 struct dm_hash_table
*dm_hash_create(unsigned size_hint
);
642 void dm_hash_destroy(struct dm_hash_table
*t
);
643 void dm_hash_wipe(struct dm_hash_table
*t
);
645 void *dm_hash_lookup(struct dm_hash_table
*t
, const char *key
);
646 int dm_hash_insert(struct dm_hash_table
*t
, const char *key
, void *data
);
647 void dm_hash_remove(struct dm_hash_table
*t
, const char *key
);
649 void *dm_hash_lookup_binary(struct dm_hash_table
*t
, const char *key
, uint32_t len
);
650 int dm_hash_insert_binary(struct dm_hash_table
*t
, const char *key
, uint32_t len
,
652 void dm_hash_remove_binary(struct dm_hash_table
*t
, const char *key
, uint32_t len
);
654 unsigned dm_hash_get_num_entries(struct dm_hash_table
*t
);
655 void dm_hash_iter(struct dm_hash_table
*t
, dm_hash_iterate_fn f
);
657 char *dm_hash_get_key(struct dm_hash_table
*t
, struct dm_hash_node
*n
);
658 void *dm_hash_get_data(struct dm_hash_table
*t
, struct dm_hash_node
*n
);
659 struct dm_hash_node
*dm_hash_get_first(struct dm_hash_table
*t
);
660 struct dm_hash_node
*dm_hash_get_next(struct dm_hash_table
*t
, struct dm_hash_node
*n
);
662 #define dm_hash_iterate(v, h) \
663 for (v = dm_hash_get_first(h); v; \
664 v = dm_hash_get_next(h, v))
671 * A list consists of a list head plus elements.
672 * Each element has 'next' and 'previous' pointers.
673 * The list head's pointers point to the first and the last element.
677 struct dm_list
*n
, *p
;
681 * Initialise a list before use.
682 * The list head's next and previous pointers point back to itself.
684 #define DM_LIST_INIT(name) struct dm_list name = { &(name), &(name) }
685 void dm_list_init(struct dm_list
*head
);
688 * Insert an element before 'head'.
689 * If 'head' is the list head, this adds an element to the end of the list.
691 void dm_list_add(struct dm_list
*head
, struct dm_list
*elem
);
694 * Insert an element after 'head'.
695 * If 'head' is the list head, this adds an element to the front of the list.
697 void dm_list_add_h(struct dm_list
*head
, struct dm_list
*elem
);
700 * Delete an element from its list.
701 * Note that this doesn't change the element itself - it may still be safe
702 * to follow its pointers.
704 void dm_list_del(struct dm_list
*elem
);
707 * Remove an element from existing list and insert before 'head'.
709 void dm_list_move(struct dm_list
*head
, struct dm_list
*elem
);
714 int dm_list_empty(const struct dm_list
*head
);
717 * Is this the first element of the list?
719 int dm_list_start(const struct dm_list
*head
, const struct dm_list
*elem
);
722 * Is this the last element of the list?
724 int dm_list_end(const struct dm_list
*head
, const struct dm_list
*elem
);
727 * Return first element of the list or NULL if empty
729 struct dm_list
*dm_list_first(const struct dm_list
*head
);
732 * Return last element of the list or NULL if empty
734 struct dm_list
*dm_list_last(const struct dm_list
*head
);
737 * Return the previous element of the list, or NULL if we've reached the start.
739 struct dm_list
*dm_list_prev(const struct dm_list
*head
, const struct dm_list
*elem
);
742 * Return the next element of the list, or NULL if we've reached the end.
744 struct dm_list
*dm_list_next(const struct dm_list
*head
, const struct dm_list
*elem
);
747 * Given the address v of an instance of 'struct dm_list' called 'head'
748 * contained in a structure of type t, return the containing structure.
750 #define dm_list_struct_base(v, t, head) \
751 ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->head))
754 * Given the address v of an instance of 'struct dm_list list' contained in
755 * a structure of type t, return the containing structure.
757 #define dm_list_item(v, t) dm_list_struct_base((v), t, list)
760 * Given the address v of one known element e in a known structure of type t,
761 * return another element f.
763 #define dm_struct_field(v, t, e, f) \
764 (((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
767 * Given the address v of a known element e in a known structure of type t,
768 * return the list head 'list'
770 #define dm_list_head(v, t, e) dm_struct_field(v, t, e, list)
773 * Set v to each element of a list in turn.
775 #define dm_list_iterate(v, head) \
776 for (v = (head)->n; v != head; v = v->n)
779 * Set v to each element in a list in turn, starting from the element
780 * in front of 'start'.
781 * You can use this to 'unwind' a list_iterate and back out actions on
782 * already-processed elements.
783 * If 'start' is 'head' it walks the list backwards.
785 #define dm_list_uniterate(v, head, start) \
786 for (v = (start)->p; v != head; v = v->p)
789 * A safe way to walk a list and delete and free some elements along
791 * t must be defined as a temporary variable of the same type as v.
793 #define dm_list_iterate_safe(v, t, head) \
794 for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
797 * Walk a list, setting 'v' in turn to the containing structure of each item.
798 * The containing structure should be the same type as 'v'.
799 * The 'struct dm_list' variable within the containing structure is 'field'.
801 #define dm_list_iterate_items_gen(v, head, field) \
802 for (v = dm_list_struct_base((head)->n, typeof(*v), field); \
803 &v->field != (head); \
804 v = dm_list_struct_base(v->field.n, typeof(*v), field))
807 * Walk a list, setting 'v' in turn to the containing structure of each item.
808 * The containing structure should be the same type as 'v'.
809 * The list should be 'struct dm_list list' within the containing structure.
811 #define dm_list_iterate_items(v, head) dm_list_iterate_items_gen(v, (head), list)
814 * Walk a list, setting 'v' in turn to the containing structure of each item.
815 * The containing structure should be the same type as 'v'.
816 * The 'struct dm_list' variable within the containing structure is 'field'.
817 * t must be defined as a temporary variable of the same type as v.
819 #define dm_list_iterate_items_gen_safe(v, t, head, field) \
820 for (v = dm_list_struct_base((head)->n, typeof(*v), field), \
821 t = dm_list_struct_base(v->field.n, typeof(*v), field); \
822 &v->field != (head); \
823 v = t, t = dm_list_struct_base(v->field.n, typeof(*v), field))
825 * Walk a list, setting 'v' in turn to the containing structure of each item.
826 * The containing structure should be the same type as 'v'.
827 * The list should be 'struct dm_list list' within the containing structure.
828 * t must be defined as a temporary variable of the same type as v.
830 #define dm_list_iterate_items_safe(v, t, head) \
831 dm_list_iterate_items_gen_safe(v, t, (head), list)
834 * Walk a list backwards, setting 'v' in turn to the containing structure
836 * The containing structure should be the same type as 'v'.
837 * The 'struct dm_list' variable within the containing structure is 'field'.
839 #define dm_list_iterate_back_items_gen(v, head, field) \
840 for (v = dm_list_struct_base((head)->p, typeof(*v), field); \
841 &v->field != (head); \
842 v = dm_list_struct_base(v->field.p, typeof(*v), field))
845 * Walk a list backwards, setting 'v' in turn to the containing structure
847 * The containing structure should be the same type as 'v'.
848 * The list should be 'struct dm_list list' within the containing structure.
850 #define dm_list_iterate_back_items(v, head) dm_list_iterate_back_items_gen(v, (head), list)
853 * Return the number of elements in a list by walking it.
855 unsigned int dm_list_size(const struct dm_list
*head
);
860 int dm_set_selinux_context(const char *path
, mode_t mode
);
862 /*********************
863 * string manipulation
864 *********************/
867 * Break up the name of a mapped device into its constituent
868 * Volume Group, Logical Volume and Layer (if present).
869 * If mem is supplied, the result is allocated from the mempool.
870 * Otherwise the strings are changed in situ.
872 int dm_split_lvm_name(struct dm_pool
*mem
, const char *dmname
,
873 char **vgname
, char **lvname
, char **layer
);
876 * Destructively split buffer into NULL-separated words in argv.
877 * Returns number of words.
879 int dm_split_words(char *buffer
, unsigned max
,
880 unsigned ignore_comments
, /* Not implemented */
884 * Returns -1 if buffer too small
886 int dm_snprintf(char *buf
, size_t bufsize
, const char *format
, ...);
889 * Returns pointer to the last component of the path.
891 char *dm_basename(const char *path
);
893 /**************************
894 * file/stream manipulation
895 **************************/
898 * Create a directory (with parent directories if necessary).
899 * Returns 1 on success, 0 on failure.
901 int dm_create_dir(const char *dir
);
904 * Close a stream, with nicer error checking than fclose's.
905 * Derived from gnulib's close-stream.c.
907 * Close "stream". Return 0 if successful, and EOF (setting errno)
908 * otherwise. Upon failure, set errno to 0 if the error number
909 * cannot be determined. Useful mainly for writable streams.
911 int dm_fclose(FILE *stream
);
914 * Returns size of a buffer which is allocated with dm_malloc.
915 * Pointer to the buffer is stored in *buf.
916 * Returns -1 on failure leaving buf undefined.
918 int dm_asprintf(char **buf
, const char *format
, ...);
920 /*********************
921 * regular expressions
922 *********************/
926 * Initialise an array of num patterns for matching.
927 * Uses memory from mem.
929 struct dm_regex
*dm_regex_create(struct dm_pool
*mem
, const char **patterns
,
930 unsigned num_patterns
);
933 * Match string s against the patterns.
934 * Returns the index of the highest pattern in the array that matches,
935 * or -1 if none match.
937 int dm_regex_match(struct dm_regex
*regex
, const char *s
);
939 /*********************
940 * reporting functions
941 *********************/
943 struct dm_report_object_type
{
944 uint32_t id
; /* Powers of 2 */
946 const char *prefix
; /* field id string prefix (optional) */
947 void *(*data_fn
)(void *object
); /* callback from report_object() */
950 struct dm_report_field
;
953 * dm_report_field_type flags
955 #define DM_REPORT_FIELD_MASK 0x000000FF
956 #define DM_REPORT_FIELD_ALIGN_MASK 0x0000000F
957 #define DM_REPORT_FIELD_ALIGN_LEFT 0x00000001
958 #define DM_REPORT_FIELD_ALIGN_RIGHT 0x00000002
959 #define DM_REPORT_FIELD_TYPE_MASK 0x000000F0
960 #define DM_REPORT_FIELD_TYPE_STRING 0x00000010
961 #define DM_REPORT_FIELD_TYPE_NUMBER 0x00000020
964 struct dm_report_field_type
{
965 uint32_t type
; /* object type id */
966 uint32_t flags
; /* DM_REPORT_FIELD_* */
967 uint32_t offset
; /* byte offset in the object */
968 int32_t width
; /* default width */
969 const char id
[32]; /* string used to specify the field */
970 const char heading
[32]; /* string printed in header */
971 int (*report_fn
)(struct dm_report
*rh
, struct dm_pool
*mem
,
972 struct dm_report_field
*field
, const void *data
,
974 const char *desc
; /* description of the field */
978 * dm_report_init output_flags
980 #define DM_REPORT_OUTPUT_MASK 0x000000FF
981 #define DM_REPORT_OUTPUT_ALIGNED 0x00000001
982 #define DM_REPORT_OUTPUT_BUFFERED 0x00000002
983 #define DM_REPORT_OUTPUT_HEADINGS 0x00000004
984 #define DM_REPORT_OUTPUT_FIELD_NAME_PREFIX 0x00000008
985 #define DM_REPORT_OUTPUT_FIELD_UNQUOTED 0x00000010
986 #define DM_REPORT_OUTPUT_COLUMNS_AS_ROWS 0x00000020
988 struct dm_report
*dm_report_init(uint32_t *report_types
,
989 const struct dm_report_object_type
*types
,
990 const struct dm_report_field_type
*fields
,
991 const char *output_fields
,
992 const char *output_separator
,
993 uint32_t output_flags
,
994 const char *sort_keys
,
996 int dm_report_object(struct dm_report
*rh
, void *object
);
997 int dm_report_output(struct dm_report
*rh
);
998 void dm_report_free(struct dm_report
*rh
);
1001 * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
1003 int dm_report_set_output_field_name_prefix(struct dm_report
*rh
,
1004 const char *report_prefix
);
1007 * Report functions are provided for simple data types.
1008 * They take care of allocating copies of the data.
1010 int dm_report_field_string(struct dm_report
*rh
, struct dm_report_field
*field
,
1012 int dm_report_field_int32(struct dm_report
*rh
, struct dm_report_field
*field
,
1013 const int32_t *data
);
1014 int dm_report_field_uint32(struct dm_report
*rh
, struct dm_report_field
*field
,
1015 const uint32_t *data
);
1016 int dm_report_field_int(struct dm_report
*rh
, struct dm_report_field
*field
,
1018 int dm_report_field_uint64(struct dm_report
*rh
, struct dm_report_field
*field
,
1019 const uint64_t *data
);
1022 * For custom fields, allocate the data in 'mem' and use
1023 * dm_report_field_set_value().
1024 * 'sortvalue' may be NULL if it matches 'value'
1026 void dm_report_field_set_value(struct dm_report_field
*field
, const void *value
,
1027 const void *sortvalue
);
1030 * The cookie value consists of a prefix (16 bits) and a base (16 bits).
1031 * We can use the prefix to store the flags. These flags are sent to
1032 * kernel within given dm task. When returned back to userspace in
1033 * DM_COOKIE udev environment variable, we can control several aspects
1034 * of udev rules we use by decoding the cookie prefix. When doing the
1035 * notification, we replace the cookie prefix with DM_COOKIE_MAGIC,
1036 * so we notify the right semaphore.
1037 * It is still possible to use cookies for passing the flags to udev
1038 * rules even when udev_sync is disabled. The base part of the cookie
1039 * will be zero (there's no notification semaphore) and prefix will be
1040 * set then. However, having udev_sync enabled is highly recommended.
1042 #define DM_COOKIE_MAGIC 0x0D4D
1043 #define DM_UDEV_FLAGS_MASK 0xFFFF0000
1044 #define DM_UDEV_FLAGS_SHIFT 16
1047 * DM_UDEV_DISABLE_DM_RULES_FLAG is set in case we need to disable
1048 * basic device-mapper udev rules that create symlinks in /dev/<DM_DIR>
1049 * directory. However, we can't reliably prevent creating default
1050 * nodes by udev (commonly /dev/dm-X, where X is a number).
1052 #define DM_UDEV_DISABLE_DM_RULES_FLAG 0x0001
1054 * DM_UDEV_DISABLE_SUBSYTEM_RULES_FLAG is set in case we need to disable
1055 * subsystem udev rules, but still we need the general DM udev rules to
1056 * be applied (to create the nodes and symlinks under /dev and /dev/disk).
1058 #define DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG 0x0002
1060 * DM_UDEV_DISABLE_DISK_RULES_FLAG is set in case we need to disable
1061 * general DM rules that set symlinks in /dev/disk directory.
1063 #define DM_UDEV_DISABLE_DISK_RULES_FLAG 0x0004
1065 * DM_UDEV_DISABLE_OTHER_RULES_FLAG is set in case we need to disable
1066 * all the other rules that are not general device-mapper nor subsystem
1067 * related (the rules belong to other software or packages). All foreign
1068 * rules should check this flag directly and they should ignore further
1069 * rule processing for such event.
1071 #define DM_UDEV_DISABLE_OTHER_RULES_FLAG 0x0008
1073 * DM_UDEV_LOW_PRIORITY_FLAG is set in case we need to instruct the
1074 * udev rules to give low priority to the device that is currently
1075 * processed. For example, this provides a way to select which symlinks
1076 * could be overwritten by high priority ones if their names are equal.
1077 * Common situation is a name based on FS UUID while using origin and
1080 #define DM_UDEV_LOW_PRIORITY_FLAG 0x0010
1082 int dm_cookie_supported(void);
1085 * Udev synchronisation functions.
1087 void dm_udev_set_sync_support(int sync_with_udev
);
1088 int dm_udev_get_sync_support(void);
1089 int dm_udev_complete(uint32_t cookie
);
1090 int dm_udev_wait(uint32_t cookie
);
1092 #define DM_DEV_DIR_UMASK 0022
1094 #endif /* LIB_DEVICE_MAPPER_H */