Tizen 2.1 base
[external/device-mapper.git] / libdm / libdevmapper.h
1 /*
2  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2010 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of the device-mapper userspace tools.
6  *
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.
10  *
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
14  */
15
16 #ifndef LIB_DEVICE_MAPPER_H
17 #define LIB_DEVICE_MAPPER_H
18
19 #include <inttypes.h>
20 #include <stdarg.h>
21 #include <sys/types.h>
22
23 #ifdef linux
24 #  include <linux/types.h>
25 #endif
26
27 #include <limits.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #ifndef __GNUC__
33 # define __typeof__ typeof
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
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  ****************************************************************/
46
47 /*
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.
51  */
52
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)));
56
57 void dm_log_with_errno_init(dm_log_with_errno_fn fn);
58 void dm_log_init_verbose(int level);
59
60 /*
61  * Original version of this function.
62  * dm_errno is set to 0.
63  *
64  * Deprecated: Use the _with_errno_ versions above instead.
65  */
66 typedef void (*dm_log_fn) (int level, const char *file, int line,
67                            const char *f, ...)
68     __attribute__ ((format(printf, 4, 5)));
69
70 void dm_log_init(dm_log_fn fn);
71 /*
72  * For backward-compatibility, indicate that dm_log_init() was used
73  * to set a non-default value of dm_log().
74  */
75 int dm_log_is_non_default(void);
76
77 enum {
78         DM_DEVICE_CREATE,
79         DM_DEVICE_RELOAD,
80         DM_DEVICE_REMOVE,
81         DM_DEVICE_REMOVE_ALL,
82
83         DM_DEVICE_SUSPEND,
84         DM_DEVICE_RESUME,
85
86         DM_DEVICE_INFO,
87         DM_DEVICE_DEPS,
88         DM_DEVICE_RENAME,
89
90         DM_DEVICE_VERSION,
91
92         DM_DEVICE_STATUS,
93         DM_DEVICE_TABLE,
94         DM_DEVICE_WAITEVENT,
95
96         DM_DEVICE_LIST,
97
98         DM_DEVICE_CLEAR,
99
100         DM_DEVICE_MKNODES,
101
102         DM_DEVICE_LIST_VERSIONS,
103         
104         DM_DEVICE_TARGET_MSG,
105
106         DM_DEVICE_SET_GEOMETRY
107 };
108
109 /*
110  * You will need to build a struct dm_task for
111  * each ioctl command you want to execute.
112  */
113
114 struct dm_task;
115
116 struct dm_task *dm_task_create(int type);
117 void dm_task_destroy(struct dm_task *dmt);
118
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);
121
122 /*
123  * Retrieve attributes after an info.
124  */
125 struct dm_info {
126         int exists;
127         int suspended;
128         int live_table;
129         int inactive_table;
130         int32_t open_count;
131         uint32_t event_nr;
132         uint32_t major;
133         uint32_t minor;         /* minor device number */
134         int read_only;          /* 0:read-write; 1:read-only */
135
136         int32_t target_count;
137 };
138
139 struct dm_deps {
140         uint32_t count;
141         uint32_t filler;
142         uint64_t device[0];
143 };
144
145 struct dm_names {
146         uint64_t dev;
147         uint32_t next;          /* Offset to next struct from start of this struct */
148         char name[0];
149 };
150
151 struct dm_versions {
152         uint32_t next;          /* Offset to next struct from start of this struct */
153         uint32_t version[3];
154
155         char name[0];
156 };
157
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);
163
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);
167
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);
187
188 /*
189  * Control read_ahead.
190  */
191 #define DM_READ_AHEAD_AUTO UINT32_MAX   /* Use kernel default readahead */
192 #define DM_READ_AHEAD_NONE 0            /* Disable readahead */
193
194 #define DM_READ_AHEAD_MINIMUM_FLAG      0x1     /* Value supplied is minimum */
195
196 /*
197  * Read ahead is set with DM_DEVICE_CREATE with a table or DM_DEVICE_RESUME.
198  */
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);
203
204 /*
205  * Use these to prepare for a create or reload.
206  */
207 int dm_task_add_target(struct dm_task *dmt,
208                        uint64_t start,
209                        uint64_t size, const char *ttype, const char *params);
210
211 /*
212  * Format major/minor numbers correctly for input to driver.
213  */
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);
216
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);
221
222 /*
223  * Call this to actually run the ioctl.
224  */
225 int dm_task_run(struct dm_task *dmt);
226
227 /*
228  * Call this to make or remove the device nodes associated with previously
229  * issued commands.
230  */
231 void dm_task_update_nodes(void);
232
233 /*
234  * Configure the device-mapper directory
235  */
236 int dm_set_dev_dir(const char *dir);
237 const char *dm_dir(void);
238
239 /*
240  * Determine whether a major number belongs to device-mapper or not.
241  */
242 int dm_is_dm_major(uint32_t major);
243
244 /*
245  * Release library resources
246  */
247 void dm_lib_release(void);
248 void dm_lib_exit(void) __attribute__((destructor));
249
250 /*
251  * Use NULL for all devices.
252  */
253 int dm_mknodes(const char *name);
254 int dm_driver_version(char *version, size_t size);
255
256 /******************************************************
257  * Functions to build and manipulate trees of devices *
258  ******************************************************/
259 struct dm_tree;
260 struct dm_tree_node;
261
262 /*
263  * Initialise an empty dependency tree.
264  *
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.
267  *
268  * Every node in the tree has one or more children and one or more parents.
269  *
270  * The root node is the parent/child of every node that doesn't have other 
271  * parents/children.
272  */
273 struct dm_tree *dm_tree_create(void);
274 void dm_tree_free(struct dm_tree *tree);
275
276 /*
277  * Add nodes to the tree for a given device and all the devices it uses.
278  */
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);
282
283 /*
284  * Add a new node to the tree if it doesn't already exist.
285  */
286 struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *tree,
287                                          const char *name,
288                                          const char *uuid,
289                                          uint32_t major, uint32_t minor,
290                                          int read_only,
291                                          int clear_inactive,
292                                          void *context);
293 struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *tree,
294                                                          const char *name,
295                                                          const char *uuid,
296                                                          uint32_t major,
297                                                          uint32_t minor,
298                                                          int read_only,
299                                                          int clear_inactive,
300                                                          void *context,
301                                                          uint16_t udev_flags);
302
303 /*
304  * Search for a node in the tree.
305  * Set major and minor to 0 or uuid to NULL to get the root node.
306  */
307 struct dm_tree_node *dm_tree_find_node(struct dm_tree *tree,
308                                           uint32_t major,
309                                           uint32_t minor);
310 struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *tree,
311                                                   const char *uuid);
312
313 /*
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.
318  */
319 struct dm_tree_node *dm_tree_next_child(void **handle,
320                                         const struct dm_tree_node *parent,
321                                         uint32_t inverted);
322
323 /*
324  * Get properties of a node.
325  */
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);
331
332 /*
333  * Returns the number of children of the given node (excluding the root node).
334  * Set inverted for the number of parents.
335  */
336 int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted);
337
338 /*
339  * Deactivate a device plus all dependencies.
340  * Ignores devices that don't have a uuid starting with uuid_prefix.
341  */
342 int dm_tree_deactivate_children(struct dm_tree_node *dnode,
343                                    const char *uuid_prefix,
344                                    size_t uuid_prefix_len);
345 /*
346  * Preload/create a device plus all dependencies.
347  * Ignores devices that don't have a uuid starting with uuid_prefix.
348  */
349 int dm_tree_preload_children(struct dm_tree_node *dnode,
350                              const char *uuid_prefix,
351                              size_t uuid_prefix_len);
352
353 /*
354  * Resume a device plus all dependencies.
355  * Ignores devices that don't have a uuid starting with uuid_prefix.
356  */
357 int dm_tree_activate_children(struct dm_tree_node *dnode,
358                               const char *uuid_prefix,
359                               size_t uuid_prefix_len);
360
361 /*
362  * Suspend a device plus all dependencies.
363  * Ignores devices that don't have a uuid starting with uuid_prefix.
364  */
365 int dm_tree_suspend_children(struct dm_tree_node *dnode,
366                                    const char *uuid_prefix,
367                                    size_t uuid_prefix_len);
368
369 /*
370  * Skip the filesystem sync when suspending.
371  * Does nothing with other functions.
372  * Use this when no snapshots are involved.
373  */ 
374 void dm_tree_skip_lockfs(struct dm_tree_node *dnode);
375
376 /*
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.
383  */
384 void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode);
385
386 /*
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.
390  */
391 int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
392                                  const char *uuid_prefix,
393                                  size_t uuid_prefix_len);
394
395 /*
396  * Construct tables for new nodes before activating them.
397  */
398 int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
399                                                uint64_t size,
400                                                const char *origin_uuid);
401 int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
402                                         uint64_t size,
403                                         const char *origin_uuid,
404                                         const char *cow_uuid,
405                                         int persistent,
406                                         uint32_t chunk_size);
407 int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
408                                              uint64_t size,
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,
414                                      uint64_t size);
415 int dm_tree_node_add_zero_target(struct dm_tree_node *node,
416                                     uint64_t size);
417 int dm_tree_node_add_linear_target(struct dm_tree_node *node,
418                                       uint64_t size);
419 int dm_tree_node_add_striped_target(struct dm_tree_node *node,
420                                        uint64_t size,
421                                        uint32_t stripe_size);
422
423 #define DM_CRYPT_IV_DEFAULT     UINT64_C(-1)    /* iv_offset == seg offset */
424 /*
425  * Function accepts one string in cipher specification
426  * (chainmode and iv should be NULL because included in cipher string)
427  *   or
428  * separate arguments which will be joined to "cipher-chainmode-iv"
429  */
430 int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
431                                   uint64_t size,
432                                   const char *cipher,
433                                   const char *chainmode,
434                                   const char *iv,
435                                   uint64_t iv_offset,
436                                   const char *key);
437 int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
438                                       uint64_t size);
439  
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 */
445
446 int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
447                                           uint32_t region_size,
448                                           unsigned clustered,
449                                           const char *log_uuid,
450                                           unsigned area_count,
451                                           uint32_t flags);
452
453 /*
454  * Replicator operation mode
455  * Note: API for Replicator is not yet stable
456  */
457 typedef enum {
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;
465
466 int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
467                                        uint64_t size,
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);
475
476 int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
477                                            uint64_t size,
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 */
486
487 void dm_tree_node_set_presuspend_node(struct dm_tree_node *node,
488                                       struct dm_tree_node *presuspend_node);
489
490 int dm_tree_node_add_target_area(struct dm_tree_node *node,
491                                     const char *dev_name,
492                                     const char *dlid,
493                                     uint64_t offset);
494
495 /*
496  * Set readahead (in sectors) after loading the node.
497  */
498 void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
499                                  uint32_t read_ahead,
500                                  uint32_t read_ahead_flags);
501
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);
504
505 /*****************************************************************************
506  * Library functions
507  *****************************************************************************/
508
509 /*******************
510  * Memory management
511  *******************/
512
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);
522
523 #ifdef DEBUG_MEM
524
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()
532
533 #else
534
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() {}
542
543 #endif
544
545
546 /*
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
550  * profile.
551  *
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.
557  *
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
562  * complex object.
563  *
564  * dm_pool_destroy frees all allocated memory.
565  *
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).
572  *
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.
577  */
578
579 struct dm_pool;
580
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);
584
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);
590
591 /*
592  * Object building routines:
593  *
594  * These allow you to 'grow' an object, useful for
595  * building strings, or filling in dynamic
596  * arrays.
597  *
598  * It's probably best explained with an example:
599  *
600  * char *build_string(struct dm_pool *mem)
601  * {
602  *      int i;
603  *      char buffer[16];
604  *
605  *      if (!dm_pool_begin_object(mem, 128))
606  *              return NULL;
607  *
608  *      for (i = 0; i < 50; i++) {
609  *              snprintf(buffer, sizeof(buffer), "%d, ", i);
610  *              if (!dm_pool_grow_object(mem, buffer, 0))
611  *                      goto bad;
612  *      }
613  *
614  *      // add null
615  *      if (!dm_pool_grow_object(mem, "\0", 1))
616  *              goto bad;
617  *
618  *      return dm_pool_end_object(mem);
619  *
620  * bad:
621  *
622  *      dm_pool_abandon_object(mem);
623  *      return NULL;
624  *}
625  *
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.
629  *
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.
633  *
634  * Setting delta to 0 means it will use strlen(extra).
635  */
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);
640
641 /* utilities */
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);
645
646 /******************
647  * bitset functions
648  ******************/
649
650 typedef uint32_t *dm_bitset_t;
651
652 dm_bitset_t dm_bitset_create(struct dm_pool *mem, unsigned num_bits);
653 void dm_bitset_destroy(dm_bitset_t bs);
654
655 int dm_bitset_equal(dm_bitset_t in1, dm_bitset_t in2);
656
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);
661
662 #define DM_BITS_PER_INT (sizeof(int) * CHAR_BIT)
663
664 #define dm_bit(bs, i) \
665    ((bs)[((i) / DM_BITS_PER_INT) + 1] & (0x1 << ((i) & (DM_BITS_PER_INT - 1))))
666
667 #define dm_bit_set(bs, i) \
668    ((bs)[((i) / DM_BITS_PER_INT) + 1] |= (0x1 << ((i) & (DM_BITS_PER_INT - 1))))
669
670 #define dm_bit_clear(bs, i) \
671    ((bs)[((i) / DM_BITS_PER_INT) + 1] &= ~(0x1 << ((i) & (DM_BITS_PER_INT - 1))))
672
673 #define dm_bit_set_all(bs) \
674    memset((bs) + 1, -1, ((*(bs) / DM_BITS_PER_INT) + 1) * sizeof(int))
675
676 #define dm_bit_clear_all(bs) \
677    memset((bs) + 1, 0, ((*(bs) / DM_BITS_PER_INT) + 1) * sizeof(int))
678
679 #define dm_bit_copy(bs1, bs2) \
680    memcpy((bs1) + 1, (bs2) + 1, ((*(bs1) / DM_BITS_PER_INT) + 1) * sizeof(int))
681
682 /* Returns number of set bits */
683 static inline unsigned hweight32(uint32_t i)
684 {
685         unsigned r = (i & 0x55555555) + ((i >> 1) & 0x55555555);
686
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);
691 }
692
693 /****************
694  * hash functions
695  ****************/
696
697 struct dm_hash_table;
698 struct dm_hash_node;
699
700 typedef void (*dm_hash_iterate_fn) (void *data);
701
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);
705
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);
709
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,
712                        void *data);
713 void dm_hash_remove_binary(struct dm_hash_table *t, const char *key, uint32_t len);
714
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);
717
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);
722
723 #define dm_hash_iterate(v, h) \
724         for (v = dm_hash_get_first((h)); v; \
725              v = dm_hash_get_next((h), v))
726
727 /****************
728  * list functions
729  ****************/
730
731 /*
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.
735  */
736
737 struct dm_list {
738         struct dm_list *n, *p;
739 };
740
741 /*
742  * Initialise a list before use.
743  * The list head's next and previous pointers point back to itself.
744  */
745 #define DM_LIST_INIT(name)      struct dm_list name = { &(name), &(name) }
746 void dm_list_init(struct dm_list *head);
747
748 /*
749  * Insert an element before 'head'.
750  * If 'head' is the list head, this adds an element to the end of the list.
751  */
752 void dm_list_add(struct dm_list *head, struct dm_list *elem);
753
754 /*
755  * Insert an element after 'head'.
756  * If 'head' is the list head, this adds an element to the front of the list.
757  */
758 void dm_list_add_h(struct dm_list *head, struct dm_list *elem);
759
760 /*
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.
764  */
765 void dm_list_del(struct dm_list *elem);
766
767 /*
768  * Remove an element from existing list and insert before 'head'.
769  */
770 void dm_list_move(struct dm_list *head, struct dm_list *elem);
771
772 /*
773  * Join 'head1' to the of 'head'.
774  */
775 void dm_list_splice(struct dm_list *head, struct dm_list *head1);
776
777 /*
778  * Is the list empty?
779  */
780 int dm_list_empty(const struct dm_list *head);
781
782 /*
783  * Is this the first element of the list?
784  */
785 int dm_list_start(const struct dm_list *head, const struct dm_list *elem);
786
787 /*
788  * Is this the last element of the list?
789  */
790 int dm_list_end(const struct dm_list *head, const struct dm_list *elem);
791
792 /*
793  * Return first element of the list or NULL if empty
794  */
795 struct dm_list *dm_list_first(const struct dm_list *head);
796
797 /*
798  * Return last element of the list or NULL if empty
799  */
800 struct dm_list *dm_list_last(const struct dm_list *head);
801
802 /*
803  * Return the previous element of the list, or NULL if we've reached the start.
804  */
805 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem);
806
807 /*
808  * Return the next element of the list, or NULL if we've reached the end.
809  */
810 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem);
811
812 /*
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.
815  */
816 #define dm_list_struct_base(v, t, head) \
817     ((t *)((const char *)(v) - (const char *)&((t *) 0)->head))
818
819 /*
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.
822  */
823 #define dm_list_item(v, t) dm_list_struct_base((v), t, list)
824
825 /*
826  * Given the address v of one known element e in a known structure of type t,
827  * return another element f.
828  */
829 #define dm_struct_field(v, t, e, f) \
830     (((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
831
832 /*
833  * Given the address v of a known element e in a known structure of type t,
834  * return the list head 'list'
835  */
836 #define dm_list_head(v, t, e) dm_struct_field(v, t, e, list)
837
838 /*
839  * Set v to each element of a list in turn.
840  */
841 #define dm_list_iterate(v, head) \
842         for (v = (head)->n; v != head; v = v->n)
843
844 /*
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.
850  */
851 #define dm_list_uniterate(v, head, start) \
852         for (v = (start)->p; v != head; v = v->p)
853
854 /*
855  * A safe way to walk a list and delete and free some elements along
856  * the way.
857  * t must be defined as a temporary variable of the same type as v.
858  */
859 #define dm_list_iterate_safe(v, t, head) \
860         for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
861
862 /*
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'.
866  */
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))
871
872 /*
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.
876  */
877 #define dm_list_iterate_items(v, head) dm_list_iterate_items_gen(v, (head), list)
878
879 /*
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.
884  */
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))
890 /*
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.
895  */
896 #define dm_list_iterate_items_safe(v, t, head) \
897         dm_list_iterate_items_gen_safe(v, t, (head), list)
898
899 /*
900  * Walk a list backwards, setting 'v' in turn to the containing structure 
901  * of each item.
902  * The containing structure should be the same type as 'v'.
903  * The 'struct dm_list' variable within the containing structure is 'field'.
904  */
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))
909
910 /*
911  * Walk a list backwards, setting 'v' in turn to the containing structure 
912  * of each item.
913  * The containing structure should be the same type as 'v'.
914  * The list should be 'struct dm_list list' within the containing structure.
915  */
916 #define dm_list_iterate_back_items(v, head) dm_list_iterate_back_items_gen(v, (head), list)
917
918 /*
919  * Return the number of elements in a list by walking it.
920  */
921 unsigned int dm_list_size(const struct dm_list *head);
922
923 /*********
924  * selinux
925  *********/
926
927 /*
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)'.
932  */
933 int dm_prepare_selinux_context(const char *path, mode_t mode);
934 /*
935  * Set SELinux context for existing file system object.
936  */
937 int dm_set_selinux_context(const char *path, mode_t mode);
938
939 /*********************
940  * string manipulation
941  *********************/
942
943 /*
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.
948  */
949 int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,
950                       char **vgname, char **lvname, char **layer);
951
952 /*
953  * Destructively split buffer into NULL-separated words in argv.
954  * Returns number of words.
955  */
956 int dm_split_words(char *buffer, unsigned max,
957                    unsigned ignore_comments, /* Not implemented */
958                    char **argv);
959
960 /* 
961  * Returns -1 if buffer too small
962  */
963 int dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
964     __attribute__ ((format(printf, 3, 4)));
965
966 /*
967  * Returns pointer to the last component of the path.
968  */
969 const char *dm_basename(const char *path);
970
971 /**************************
972  * file/stream manipulation
973  **************************/
974
975 /*
976  * Create a directory (with parent directories if necessary).
977  * Returns 1 on success, 0 on failure.
978  */
979 int dm_create_dir(const char *dir);
980
981 /*
982  * Close a stream, with nicer error checking than fclose's.
983  * Derived from gnulib's close-stream.c.
984  *
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.
988  */
989 int dm_fclose(FILE *stream);
990
991 /*
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.
995  */
996 int dm_asprintf(char **buf, const char *format, ...)
997     __attribute__ ((format(printf, 2, 3)));
998
999 /*
1000  * create lockfile (pidfile) - create and lock a lock file
1001  * @lockfile: location of lock file
1002  *
1003  * Returns: 1 on success, 0 otherwise, errno is handled internally
1004  */
1005 int dm_create_lockfile(const char* lockfile);
1006
1007 /*
1008  * Query whether a daemon is running based on its lockfile
1009  *
1010  * Returns: 1 if running, 0 if not
1011  */
1012 int dm_daemon_is_running(const char* lockfile);
1013
1014 /*********************
1015  * regular expressions
1016  *********************/
1017 struct dm_regex;
1018
1019 /*
1020  * Initialise an array of num patterns for matching.
1021  * Uses memory from mem.
1022  */
1023 struct dm_regex *dm_regex_create(struct dm_pool *mem, const char * const *patterns,
1024                                  unsigned num_patterns);
1025
1026 /*
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.
1030  */
1031 int dm_regex_match(struct dm_regex *regex, const char *s);
1032
1033 /*
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.
1038  *
1039  * This function must be called before any matching is done.
1040  */
1041 uint32_t dm_regex_fingerprint(struct dm_regex *regex);
1042
1043 /*********************
1044  * reporting functions
1045  *********************/
1046
1047 struct dm_report_object_type {
1048         uint32_t id;                    /* Powers of 2 */
1049         const char *desc;
1050         const char *prefix;             /* field id string prefix (optional) */
1051         void *(*data_fn)(void *object); /* callback from report_object() */
1052 };
1053
1054 struct dm_report_field;
1055
1056 /*
1057  * dm_report_field_type flags
1058  */
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
1066
1067 #define DM_REPORT_FIELD_TYPE_ID_LEN 32
1068 #define DM_REPORT_FIELD_TYPE_HEADING_LEN 32
1069
1070 struct dm_report;
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 */
1084 };
1085
1086 /*
1087  * dm_report_init output_flags
1088  */
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
1096
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);
1108
1109 /*
1110  * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
1111  */
1112 int dm_report_set_output_field_name_prefix(struct dm_report *rh,
1113                                            const char *report_prefix);
1114
1115 /*
1116  * Report functions are provided for simple data types.
1117  * They take care of allocating copies of the data.
1118  */
1119 int dm_report_field_string(struct dm_report *rh, struct dm_report_field *field,
1120                            const char **data);
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,
1126                         const int *data);
1127 int dm_report_field_uint64(struct dm_report *rh, struct dm_report_field *field,
1128                            const uint64_t *data);
1129
1130 /*
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'
1134  */
1135 void dm_report_field_set_value(struct dm_report_field *field, const void *value,
1136                                const void *sortvalue);
1137
1138 /* Cookie prefixes.
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.
1150  */
1151 #define DM_COOKIE_MAGIC 0x0D4D
1152 #define DM_UDEV_FLAGS_MASK 0xFFFF0000
1153 #define DM_UDEV_FLAGS_SHIFT 16
1154
1155 /*
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).
1160  */
1161 #define DM_UDEV_DISABLE_DM_RULES_FLAG 0x0001
1162 /*
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).
1166  */
1167 #define DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG 0x0002
1168 /*
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.
1171  */
1172 #define DM_UDEV_DISABLE_DISK_RULES_FLAG 0x0004
1173 /*
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.
1179  */
1180 #define DM_UDEV_DISABLE_OTHER_RULES_FLAG 0x0008
1181 /*
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
1187  * snapshot devices.
1188  */
1189 #define DM_UDEV_LOW_PRIORITY_FLAG 0x0010
1190 /*
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.
1195  */
1196 #define DM_UDEV_DISABLE_LIBRARY_FALLBACK 0x0020
1197 /*
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).
1205  */
1206 #define DM_UDEV_PRIMARY_SOURCE_FLAG 0x0040
1207
1208 int dm_cookie_supported(void);
1209
1210 /*
1211  * Udev synchronisation functions.
1212  */
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);
1220
1221 #define DM_DEV_DIR_UMASK 0022
1222
1223 #ifdef __cplusplus
1224 }
1225 #endif
1226 #endif                          /* LIB_DEVICE_MAPPER_H */