Coverage Report

Created: 2026-04-28 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/static_lib_deps/include/libdevmapper.h
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
 * Copyright (C) 2004-2026 Red Hat, Inc. All rights reserved.
4
 * Copyright (C) 2006 Rackable Systems All rights reserved.
5
 *
6
 * This file is part of the device-mapper userspace tools.
7
 *
8
 * This copyrighted material is made available to anyone wishing to use,
9
 * modify, copy, or redistribute it subject to the terms and conditions
10
 * of the GNU Lesser General Public License v.2.1.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public License
13
 * along with this program; if not, write to the Free Software Foundation,
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15
 */
16
17
#ifndef LIB_DEVICE_MAPPER_H
18
#define LIB_DEVICE_MAPPER_H
19
20
#include <inttypes.h>
21
#include <stdarg.h>
22
#include <sys/types.h>
23
#include <sys/stat.h>
24
25
#ifdef __linux__
26
#  include <linux/types.h>
27
#endif
28
29
#include <limits.h>
30
#include <string.h>
31
#include <stdlib.h>
32
#include <stdio.h>
33
#include <stddef.h> /* offsetof */
34
35
#ifndef __GNUC__
36
# define __typeof__ typeof
37
#endif
38
39
/* Macros to make string defines */
40
#define DM_TO_STRING_EXP(A) #A
41
#define DM_TO_STRING(A) DM_TO_STRING_EXP(A)
42
43
#define DM_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
44
45
#ifdef __cplusplus
46
extern "C" {
47
#endif
48
49
/*****************************************************************
50
 * The first section of this file provides direct access to the
51
 * individual device-mapper ioctls.  Since it is quite laborious to
52
 * build the ioctl arguments for the device-mapper, people are
53
 * encouraged to use this library.
54
 ****************************************************************/
55
56
/*
57
 * The library user may wish to register their own
58
 * logging function.  By default errors go to stderr.
59
 * Use dm_log_with_errno_init(NULL) to restore the default log fn.
60
 * Error messages may have a non-zero errno.
61
 * Debug messages may have a non-zero class.
62
 * Aborts on internal error when env DM_ABORT_ON_INTERNAL_ERRORS is 1
63
 */
64
65
typedef void (*dm_log_with_errno_fn) (int level, const char *file, int line,
66
              int dm_errno_or_class, const char *f, ...)
67
    __attribute__ ((format(printf, 5, 6)));
68
69
void dm_log_with_errno_init(dm_log_with_errno_fn fn);
70
void dm_log_init_verbose(int level);
71
72
/*
73
 * Original version of this function.
74
 * dm_errno is set to 0.
75
 *
76
 * Deprecated: Use the _with_errno_ versions above instead.
77
 */
78
typedef void (*dm_log_fn) (int level, const char *file, int line,
79
         const char *f, ...)
80
    __attribute__ ((format(printf, 4, 5)));
81
82
void dm_log_init(dm_log_fn fn);
83
/*
84
 * For backward-compatibility, indicate that dm_log_init() was used
85
 * to set a non-default value of dm_log().
86
 */
87
int dm_log_is_non_default(void);
88
89
/*
90
 * Number of devices currently in suspended state (via the library).
91
 */
92
int dm_get_suspended_counter(void);
93
94
enum {
95
  DM_DEVICE_CREATE,
96
  DM_DEVICE_RELOAD,
97
  DM_DEVICE_REMOVE,
98
  DM_DEVICE_REMOVE_ALL,
99
100
  DM_DEVICE_SUSPEND,
101
  DM_DEVICE_RESUME,
102
103
  DM_DEVICE_INFO,
104
  DM_DEVICE_DEPS,
105
  DM_DEVICE_RENAME,
106
107
  DM_DEVICE_VERSION,
108
109
  DM_DEVICE_STATUS,
110
  DM_DEVICE_TABLE,
111
  DM_DEVICE_WAITEVENT,
112
113
  DM_DEVICE_LIST,
114
115
  DM_DEVICE_CLEAR,
116
117
  DM_DEVICE_MKNODES,
118
119
  DM_DEVICE_LIST_VERSIONS,
120
121
  DM_DEVICE_TARGET_MSG,
122
123
  DM_DEVICE_SET_GEOMETRY,
124
125
  DM_DEVICE_ARM_POLL,
126
127
  DM_DEVICE_GET_TARGET_VERSION
128
};
129
130
/*
131
 * You will need to build a struct dm_task for
132
 * each ioctl command you want to execute.
133
 */
134
135
struct dm_pool;
136
struct dm_task;
137
struct dm_timestamp;
138
139
struct dm_task *dm_task_create(int type);
140
void dm_task_destroy(struct dm_task *dmt);
141
142
int dm_task_set_name(struct dm_task *dmt, const char *name);
143
int dm_task_set_uuid(struct dm_task *dmt, const char *uuid);
144
145
/*
146
 * Retrieve attributes after an info.
147
 */
148
struct dm_info {
149
  int exists;
150
  int suspended;
151
  int live_table;
152
  int inactive_table;
153
  int32_t open_count;
154
  uint32_t event_nr;
155
  uint32_t major;
156
  uint32_t minor;   /* minor device number */
157
  int read_only;    /* 0:read-write; 1:read-only */
158
159
  int32_t target_count;
160
161
  int deferred_remove;
162
  int internal_suspend;
163
};
164
165
struct dm_deps {
166
  uint32_t count;
167
  uint32_t filler;
168
  uint64_t device[];
169
};
170
171
struct dm_names {
172
  uint64_t dev;
173
  uint32_t next;    /* Offset to next struct from start of this struct */
174
  char name[];
175
};
176
177
struct dm_versions {
178
  uint32_t next;    /* Offset to next struct from start of this struct */
179
  uint32_t version[3];
180
181
  char name[];
182
};
183
184
int dm_get_library_version(char *version, size_t size);
185
int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size);
186
int dm_task_get_info(struct dm_task *dmt, struct dm_info *info);
187
188
/*
189
 * This function returns dm device's UUID based on the value
190
 * of the mangling mode set during preceding dm_task_run call:
191
 *   - unmangled UUID for DM_STRING_MANGLING_{AUTO, HEX},
192
 *   - UUID without any changes for DM_STRING_MANGLING_NONE.
193
 *
194
 * To get mangled or unmangled form of the UUID directly, use
195
 * dm_task_get_uuid_mangled or dm_task_get_uuid_unmangled function.
196
 */
197
const char *dm_task_get_uuid(const struct dm_task *dmt);
198
199
struct dm_deps *dm_task_get_deps(struct dm_task *dmt);
200
struct dm_versions *dm_task_get_versions(struct dm_task *dmt);
201
const char *dm_task_get_message_response(struct dm_task *dmt);
202
203
/*
204
 * These functions return device-mapper names based on the value
205
 * of the mangling mode set during preceding dm_task_run call:
206
 *   - unmangled name for DM_STRING_MANGLING_{AUTO, HEX},
207
 *   - name without any changes for DM_STRING_MANGLING_NONE.
208
 *
209
 * To get mangled or unmangled form of the name directly, use
210
 * dm_task_get_name_mangled or dm_task_get_name_unmangled function.
211
 */
212
const char *dm_task_get_name(const struct dm_task *dmt);
213
struct dm_names *dm_task_get_names(struct dm_task *dmt);
214
215
int dm_task_set_ro(struct dm_task *dmt);
216
int dm_task_set_newname(struct dm_task *dmt, const char *newname);
217
int dm_task_set_newuuid(struct dm_task *dmt, const char *newuuid);
218
int dm_task_set_minor(struct dm_task *dmt, int minor);
219
int dm_task_set_major(struct dm_task *dmt, int major);
220
int dm_task_set_major_minor(struct dm_task *dmt, int major, int minor, int allow_default_major_fallback);
221
int dm_task_set_uid(struct dm_task *dmt, uid_t uid);
222
int dm_task_set_gid(struct dm_task *dmt, gid_t gid);
223
int dm_task_set_mode(struct dm_task *dmt, mode_t mode);
224
/* See also description for DM_UDEV_DISABLE_LIBRARY_FALLBACK flag! */
225
int dm_task_set_cookie(struct dm_task *dmt, uint32_t *cookie, uint16_t flags);
226
int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr);
227
int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders, const char *heads, const char *sectors, const char *start);
228
int dm_task_set_message(struct dm_task *dmt, const char *message);
229
int dm_task_set_sector(struct dm_task *dmt, uint64_t sector);
230
int dm_task_no_flush(struct dm_task *dmt);
231
int dm_task_no_open_count(struct dm_task *dmt);
232
int dm_task_skip_lockfs(struct dm_task *dmt);
233
int dm_task_query_inactive_table(struct dm_task *dmt);
234
int dm_task_suppress_identical_reload(struct dm_task *dmt);
235
int dm_task_secure_data(struct dm_task *dmt);
236
int dm_task_retry_remove(struct dm_task *dmt);
237
int dm_task_deferred_remove(struct dm_task *dmt);
238
int dm_task_ima_measurement(struct dm_task *dmt);
239
240
/*
241
 * Record timestamp immediately after the ioctl returns.
242
 */
243
int dm_task_set_record_timestamp(struct dm_task *dmt);
244
struct dm_timestamp *dm_task_get_ioctl_timestamp(struct dm_task *dmt);
245
246
/*
247
 * Enable checks for common mistakes such as issuing ioctls in an unsafe order.
248
 */
249
int dm_task_enable_checks(struct dm_task *dmt);
250
251
typedef enum dm_add_node_e {
252
  DM_ADD_NODE_ON_RESUME, /* add /dev/mapper node with dmsetup resume */
253
  DM_ADD_NODE_ON_CREATE  /* add /dev/mapper node with dmsetup create */
254
} dm_add_node_t;
255
int dm_task_set_add_node(struct dm_task *dmt, dm_add_node_t add_node);
256
257
/*
258
 * Control read_ahead.
259
 */
260
#define DM_READ_AHEAD_AUTO UINT32_MAX /* Use kernel default readahead */
261
#define DM_READ_AHEAD_NONE 0    /* Disable readahead */
262
263
0
#define DM_READ_AHEAD_MINIMUM_FLAG  0x1  /* Value supplied is minimum */
264
265
/*
266
 * Read ahead is set with DM_DEVICE_CREATE with a table or DM_DEVICE_RESUME.
267
 */
268
int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead,
269
         uint32_t read_ahead_flags);
270
uint32_t dm_task_get_read_ahead(const struct dm_task *dmt,
271
        uint32_t *read_ahead);
272
273
/*
274
 * Use these to prepare for a create or reload.
275
 */
276
int dm_task_add_target(struct dm_task *dmt,
277
           uint64_t start,
278
           uint64_t size, const char *ttype, const char *params);
279
280
/*
281
 * Format major/minor numbers correctly for input to driver.
282
 */
283
#define DM_FORMAT_DEV_BUFSIZE 13  /* Minimum bufsize to handle worst case. */
284
int dm_format_dev(char *buf, int bufsize, uint32_t dev_major, uint32_t dev_minor);
285
286
/* Use this to retrieve target information returned from a STATUS call */
287
void *dm_get_next_target(struct dm_task *dmt,
288
       void *next, uint64_t *start, uint64_t *length,
289
       char **target_type, char **params);
290
291
/*
292
 * Following dm_get_status_* functions will allocate appropriate status structure
293
 * from passed mempool together with the necessary character arrays.
294
 * Destroying the mempool will release all associated allocation.
295
 */
296
297
/* Parse params from STATUS call for mirror target */
298
typedef enum dm_status_mirror_health_e {
299
  DM_STATUS_MIRROR_ALIVE        = 'A',/* No failures */
300
  DM_STATUS_MIRROR_FLUSH_FAILED = 'F',/* Mirror out-of-sync */
301
  DM_STATUS_MIRROR_WRITE_FAILED = 'D',/* Mirror out-of-sync */
302
  DM_STATUS_MIRROR_SYNC_FAILED  = 'S',/* Mirror out-of-sync */
303
  DM_STATUS_MIRROR_READ_FAILED  = 'R',/* Mirror data unaffected */
304
  DM_STATUS_MIRROR_UNCLASSIFIED = 'U' /* Bug */
305
} dm_status_mirror_health_t;
306
307
struct dm_status_mirror {
308
  uint64_t total_regions;
309
  uint64_t insync_regions;
310
  uint32_t dev_count;             /* # of devs[] elements (<= 8) */
311
  struct dm_dev_leg_health_s {
312
    dm_status_mirror_health_t health;
313
    uint32_t major;
314
    uint32_t minor;
315
  } *devs;                        /* array with individual legs */
316
  const char *log_type;           /* core, disk,.... */
317
  uint32_t log_count;   /* # of logs[] elements */
318
  struct dm_dev_log_health_s {
319
    dm_status_mirror_health_t health;
320
    uint32_t major;
321
    uint32_t minor;
322
  } *logs;      /* array with individual logs */
323
};
324
325
int dm_get_status_mirror(struct dm_pool *mem, const char *params,
326
       struct dm_status_mirror **status);
327
328
/* Parse params from STATUS call for raid target */
329
struct dm_status_raid {
330
  uint64_t reserved;
331
  uint64_t total_regions;   /* sectors */
332
  uint64_t insync_regions;  /* sectors */
333
  uint64_t mismatch_count;
334
  uint32_t dev_count;
335
  char *raid_type;
336
  /* A - alive,  a - alive not in-sync,  D - dead/failed */
337
  char *dev_health;
338
  /* idle, frozen, resync, recover, check, repair */
339
  char *sync_action;
340
  uint64_t data_offset; /* RAID out-of-place reshaping */
341
};
342
343
int dm_get_status_raid(struct dm_pool *mem, const char *params,
344
           struct dm_status_raid **status);
345
346
/* Parse params from STATUS call for cache target */
347
struct dm_status_cache {
348
  uint64_t version;  /* zero for now */
349
350
  uint32_t metadata_block_size;   /* in 512B sectors */
351
  uint32_t block_size;            /* AKA 'chunk_size' */
352
353
  uint64_t metadata_used_blocks;
354
  uint64_t metadata_total_blocks;
355
356
  uint64_t used_blocks;
357
  uint64_t dirty_blocks;
358
  uint64_t total_blocks;
359
360
  uint64_t read_hits;
361
  uint64_t read_misses;
362
  uint64_t write_hits;
363
  uint64_t write_misses;
364
365
  uint64_t demotions;
366
  uint64_t promotions;
367
368
  uint64_t feature_flags;   /* DM_CACHE_FEATURE_? */
369
370
  int core_argc;
371
  char **core_argv;
372
373
  char *policy_name;
374
  int policy_argc;
375
  char **policy_argv;
376
377
  unsigned error : 1;   /* detected error (switches to fail soon) */
378
  unsigned fail : 1;    /* all I/O fails */
379
  unsigned needs_check : 1; /* metadata needs check */
380
  unsigned read_only : 1;   /* metadata may not be changed */
381
  uint32_t reserved : 28;
382
};
383
384
int dm_get_status_cache(struct dm_pool *mem, const char *params,
385
      struct dm_status_cache **status);
386
387
struct dm_status_writecache {
388
  uint64_t error;
389
  uint64_t total_blocks;
390
  uint64_t free_blocks;
391
  uint64_t writeback_blocks;
392
};
393
394
int dm_get_status_writecache(struct dm_pool *mem, const char *params,
395
           struct dm_status_writecache **status);
396
397
struct dm_status_integrity {
398
  uint64_t number_of_mismatches;
399
  uint64_t provided_data_sectors;
400
  uint64_t recalc_sector;
401
};
402
403
int dm_get_status_integrity(struct dm_pool *mem, const char *params,
404
          struct dm_status_integrity **status);
405
406
/*
407
 * RAID target support
408
 */
409
int dm_raid_count_failed_devices(const char *dev_path, uint32_t *nr_failed);
410
int dm_raid_clear_failed_devices(const char *dev_path, uint32_t *nr_failed);
411
412
/*
413
 * Parse params from STATUS call for snapshot target
414
 *
415
 * Snapshot target's format:
416
 * <= 1.7.0: <used_sectors>/<total_sectors>
417
 * >= 1.8.0: <used_sectors>/<total_sectors> <metadata_sectors>
418
 */
419
struct dm_status_snapshot {
420
  uint64_t used_sectors;          /* in 512b units */
421
  uint64_t total_sectors;
422
  uint64_t metadata_sectors;
423
  unsigned has_metadata_sectors : 1; /* set when metadata_sectors is present */
424
  unsigned invalid : 1;   /* set when snapshot is invalidated */
425
  unsigned merge_failed : 1;  /* set when snapshot merge failed */
426
  unsigned overflow : 1;    /* set when snapshot overflows */
427
};
428
429
int dm_get_status_snapshot(struct dm_pool *mem, const char *params,
430
         struct dm_status_snapshot **status);
431
432
/* Parse params from STATUS call for thin_pool target */
433
typedef enum dm_thin_discards_e {
434
  DM_THIN_DISCARDS_IGNORE,
435
  DM_THIN_DISCARDS_NO_PASSDOWN,
436
  DM_THIN_DISCARDS_PASSDOWN
437
} dm_thin_discards_t;
438
439
struct dm_status_thin_pool {
440
  uint64_t transaction_id;
441
  uint64_t used_metadata_blocks;
442
  uint64_t total_metadata_blocks;
443
  uint64_t used_data_blocks;
444
  uint64_t total_data_blocks;
445
  uint64_t held_metadata_root;
446
  uint32_t read_only;   /* metadata may not be changed */
447
  dm_thin_discards_t discards;
448
  uint32_t fail : 1;    /* all I/O fails */
449
  uint32_t error_if_no_space : 1; /* otherwise queue_if_no_space */
450
  uint32_t out_of_data_space : 1; /* metadata may be changed, but data may not be allocated (no rw) */
451
  uint32_t needs_check : 1; /* metadata needs check */
452
  uint32_t error : 1;   /* detected error (switches to fail soon) */
453
  uint32_t reserved : 27;
454
};
455
456
int dm_get_status_thin_pool(struct dm_pool *mem, const char *params,
457
          struct dm_status_thin_pool **status);
458
459
/* Parse params from STATUS call for thin target */
460
struct dm_status_thin {
461
  uint64_t mapped_sectors;
462
  uint64_t highest_mapped_sector;
463
  uint32_t fail : 1;              /* Thin volume fails I/O */
464
  uint32_t reserved : 31;
465
};
466
467
int dm_get_status_thin(struct dm_pool *mem, const char *params,
468
           struct dm_status_thin **status);
469
470
/*
471
 * device-mapper statistics support
472
 */
473
474
/*
475
 * Statistics handle.
476
 *
477
 * Operations on dm_stats objects include managing statistics regions
478
 * and obtaining and manipulating current counter values from the
479
 * kernel. Methods are provided to return basic count values and to
480
 * derive time-based metrics when a suitable interval estimate is
481
 * provided.
482
 *
483
 * Internally the dm_stats handle contains a pointer to a table of one
484
 * or more dm_stats_region objects representing the regions registered
485
 * with the dm_stats_create_region() method. These in turn point to a
486
 * table of one or more dm_stats_counters objects containing the
487
 * counter sets for each defined area within the region:
488
 *
489
 * dm_stats->dm_stats_region[nr_regions]->dm_stats_counters[nr_areas]
490
 *
491
 * This structure is private to the library and may change in future
492
 * versions: all users should make use of the public interface and treat
493
 * the dm_stats type as an opaque handle.
494
 *
495
 * Regions and counter sets are stored in order of increasing region_id.
496
 * Depending on region specifications and the sequence of create and
497
 * delete operations this may not correspond to increasing sector
498
 * number: users of the library should not assume that this is the case
499
 * unless region creation is deliberately managed to ensure this (by
500
 * always creating regions in strict order of ascending sector address).
501
 *
502
 * Regions may also overlap so the same sector range may be included in
503
 * more than one region or area: applications should be prepared to deal
504
 * with this or manage regions such that it does not occur.
505
 */
506
struct dm_stats;
507
508
/*
509
 * Histogram handle.
510
 *
511
 * A histogram object represents the latency histogram values and bin
512
 * boundaries of the histogram associated with a particular area.
513
 *
514
 * Operations on the handle allow the number of bins, bin boundaries,
515
 * counts and relative proportions to be obtained as well as the
516
 * conversion of a histogram or its bounds to a compact string
517
 * representation.
518
 */
519
struct dm_histogram;
520
521
/*
522
 * Allocate a dm_stats handle to use for subsequent device-mapper
523
 * statistics operations. A program_id may be specified and will be
524
 * used by default for subsequent operations on this handle.
525
 *
526
 * If program_id is NULL or the empty string a program_id will be
527
 * automatically set to the value contained in /proc/self/comm.
528
 */
529
struct dm_stats *dm_stats_create(const char *program_id);
530
531
/*
532
 * Bind a dm_stats handle to the specified device major and minor
533
 * values. Any previous binding is cleared and any preexisting counter
534
 * data contained in the handle is released.
535
 */
536
int dm_stats_bind_devno(struct dm_stats *dms, int major, int minor);
537
538
/*
539
 * Bind a dm_stats handle to the specified device name.
540
 * Any previous binding is cleared and any preexisting counter
541
 * data contained in the handle is released.
542
 */
543
int dm_stats_bind_name(struct dm_stats *dms, const char *name);
544
545
/*
546
 * Bind a dm_stats handle to the specified device UUID.
547
 * Any previous binding is cleared and any preexisting counter
548
 * data contained in the handle is released.
549
 */
550
int dm_stats_bind_uuid(struct dm_stats *dms, const char *uuid);
551
552
/*
553
 * Bind a dm_stats handle to the device backing the file referenced
554
 * by the specified file descriptor.
555
 *
556
 * File descriptor fd must reference a regular file, open for reading,
557
 * in a local file system, backed by a device-mapper device, that
558
 * supports the FIEMAP ioctl, and that returns data describing the
559
 * physical location of extents.
560
 */
561
int dm_stats_bind_from_fd(struct dm_stats *dms, int fd);
562
/*
563
 * Test whether the running kernel supports the precise_timestamps
564
 * feature. Presence of this feature also implies histogram support.
565
 * The library will check this call internally and fails any attempt
566
 * to use nanosecond counters or histograms on kernels that fail to
567
 * meet this check.
568
 */
569
int dm_message_supports_precise_timestamps(void);
570
571
/*
572
 * Precise timestamps and histogram support.
573
 *
574
 * Test for the presence of precise_timestamps and histogram support.
575
 */
576
int dm_stats_driver_supports_precise(void);
577
int dm_stats_driver_supports_histogram(void);
578
579
/*
580
 * Returns 1 if the specified region has the precise_timestamps feature
581
 * enabled (i.e. produces nanosecond-precision counter values) or 0 for
582
 * a region using the default millisecond precision.
583
 */
584
int dm_stats_get_region_precise_timestamps(const struct dm_stats *dms,
585
             uint64_t region_id);
586
587
/*
588
 * Returns 1 if the region at the current cursor location has the
589
 * precise_timestamps feature enabled (i.e. produces
590
 * nanosecond-precision counter values) or 0 for a region using the
591
 * default millisecond precision.
592
 */
593
int dm_stats_get_current_region_precise_timestamps(const struct dm_stats *dms);
594
595
#define DM_STATS_ALL_PROGRAMS ""
596
/*
597
 * Parse the response from a @stats_list message. dm_stats_list will
598
 * allocate the necessary dm_stats and dm_stats region structures from
599
 * the embedded dm_pool. No counter data will be obtained (the counters
600
 * members of dm_stats_region objects are set to NULL).
601
 *
602
 * A program_id may optionally be supplied; if the argument is non-NULL
603
 * only regions with a matching program_id value will be considered. If
604
 * the argument is NULL then the default program_id associated with the
605
 * dm_stats handle will be used. Passing the special value
606
 * DM_STATS_ALL_PROGRAMS will cause all regions to be queried
607
 * regardless of region program_id.
608
 */
609
int dm_stats_list(struct dm_stats *dms, const char *program_id);
610
611
#define DM_STATS_REGIONS_ALL UINT64_MAX
612
/*
613
 * Populate a dm_stats object with statistics for one or more regions of
614
 * the specified device.
615
 *
616
 * A program_id may optionally be supplied; if the argument is non-NULL
617
 * only regions with a matching program_id value will be considered. If
618
 * the argument is NULL then the default program_id associated with the
619
 * dm_stats handle will be used. Passing the special value
620
 * DM_STATS_ALL_PROGRAMS will cause all regions to be queried
621
 * regardless of region program_id.
622
 *
623
 * Passing the special value DM_STATS_REGIONS_ALL as the region_id
624
 * argument will attempt to retrieve all regions selected by the
625
 * program_id argument.
626
 *
627
 * If region_id is used to request a single region_id to be populated
628
 * the program_id is ignored.
629
 */
630
int dm_stats_populate(struct dm_stats *dms, const char *program_id,
631
          uint64_t region_id);
632
633
/*
634
 * Create a new statistics region on the device bound to dms.
635
 *
636
 * start and len specify the region start and length in 512b sectors.
637
 * Passing zero for both start and len will create a region spanning
638
 * the entire device.
639
 *
640
 * Step determines how to subdivide the region into discrete counter
641
 * sets: a positive value specifies the size of areas into which the
642
 * region should be split while a negative value will split the region
643
 * into a number of areas equal to the absolute value of step:
644
 *
645
 * - a region with one area spanning the entire device:
646
 *
647
 *   dm_stats_create_region(dms, 0, 0, -1, p, a);
648
 *
649
 * - a region with areas of 1MiB:
650
 *
651
 *   dm_stats_create_region(dms, 0, 0, 1 << 11, p, a);
652
 *
653
 * - one 1MiB region starting at 1024 sectors with two areas:
654
 *
655
 *   dm_stats_create_region(dms, 1024, 1 << 11, -2, p, a);
656
 *
657
 * If precise is non-zero attempt to create a region with nanosecond
658
 * precision counters using the kernel precise_timestamps feature.
659
 *
660
 * precise - A flag to request nanosecond precision counters
661
 * to be used for this region.
662
 *
663
 * histogram_bounds - specify the boundaries of a latency histogram to
664
 * be tracked for the region. The values are expressed as an array of
665
 * uint64_t terminated with a zero. Values must be in order of ascending
666
 * magnitude and specify the upper bounds of successive histogram bins
667
 * in nanoseconds (with an implicit lower bound of zero on the first bin
668
 * and an implicit upper bound of infinity on the final bin). For
669
 * example:
670
 *
671
 *   uint64_t bounds_ary[] = { 1000, 2000, 3000, 0 };
672
 *
673
 * Specifies a histogram with four bins: 0-1000ns, 1000-2000ns,
674
 * 2000-3000ns and >3000ns.
675
 *
676
 * The smallest latency value that can be tracked for a region not using
677
 * precise_timestamps is 1ms: attempting to create a region with
678
 * histogram boundaries < 1ms will cause the precise_timestamps feature
679
 * to be enabled for that region automatically if it was not requested
680
 * explicitly.
681
 *
682
 * program_id is an optional string argument that identifies the
683
 * program creating the region. If program_id is NULL or the empty
684
 * string the default program_id stored in the handle will be used.
685
 *
686
 * user_data is an optional string argument that is added to the
687
 * content of the aux_data field stored with the statistics region by
688
 * the kernel.
689
 *
690
 * The library may also use this space internally, for example, to
691
 * store a group descriptor or other metadata: in this case the
692
 * library will strip any internal data fields from the value before
693
 * it is returned via a call to dm_stats_get_region_aux_data().
694
 *
695
 * The user data stored is not accessed by the library or kernel and
696
 * may be used to store an arbitrary data word (embedded whitespace is
697
 * not permitted).
698
 *
699
 * An application using both the library and direct access to the
700
 * @stats_list device-mapper message may see the internal values stored
701
 * in this field by the library. In such cases any string up to and
702
 * including the first '#' in the field must be treated as an opaque
703
 * value and preserved across any external modification of aux_data.
704
 *
705
 * The region_id of the newly-created region is returned in *region_id
706
 * if it is non-NULL.
707
 */
708
int dm_stats_create_region(struct dm_stats *dms, uint64_t *region_id,
709
         uint64_t start, uint64_t len, int64_t step,
710
         int precise, struct dm_histogram *bounds,
711
         const char *program_id, const char *user_data);
712
713
/*
714
 * Delete the specified statistics region. This will also mark the
715
 * region as not-present and discard any existing statistics data.
716
 */
717
int dm_stats_delete_region(struct dm_stats *dms, uint64_t region_id);
718
719
/*
720
 * Clear the specified statistics region. This requests the kernel to
721
 * zero all counter values (except in-flight I/O). Note that this
722
 * operation is not atomic with respect to reads of the counters; any IO
723
 * events occurring between the last print operation and the clear will
724
 * be lost. This can be avoided by using the atomic print-and-clear
725
 * function of the dm_stats_print_region() call or by using the higher
726
 * level dm_stats_populate() interface.
727
 */
728
int dm_stats_clear_region(struct dm_stats *dms, uint64_t region_id);
729
730
/*
731
 * Print the current counter values for the specified statistics region
732
 * and return them as a string. The memory for the string buffer will
733
 * be allocated from the dm_stats handle's private pool and should be
734
 * returned by calling dm_stats_buffer_destroy() when no longer
735
 * required. The pointer will become invalid following any call that
736
 * clears or reinitializes the handle (destroy, list, populate, bind).
737
 *
738
 * This allows applications that wish to access the raw message response
739
 * to obtain it via a dm_stats handle; no parsing of the textual counter
740
 * data is carried out by this function.
741
 *
742
 * Most users are recommended to use the dm_stats_populate() call
743
 * instead since this will automatically parse the statistics data into
744
 * numeric form accessible via the dm_stats_get_*() counter access
745
 * methods.
746
 *
747
 * A subset of the data lines may be requested by setting the
748
 * start_line and num_lines parameters. If both are zero all data
749
 * lines are returned.
750
 *
751
 * If the clear parameter is non-zero the operation will also
752
 * atomically reset all counter values to zero (except in-flight IO).
753
 */
754
char *dm_stats_print_region(struct dm_stats *dms, uint64_t region_id,
755
          unsigned start_line, unsigned num_lines,
756
          unsigned clear);
757
758
/*
759
 * Destroy a statistics response buffer obtained from a call to
760
 * dm_stats_print_region().
761
 */
762
void dm_stats_buffer_destroy(struct dm_stats *dms, char *buffer);
763
764
/*
765
 * Determine the number of regions contained in a dm_stats handle
766
 * following a dm_stats_list() or dm_stats_populate() call.
767
 *
768
 * The value returned is the number of registered regions visible with the
769
 * program_id value used for the list or populate operation and may not be
770
 * equal to the highest present region_id (either due to program_id
771
 * filtering or gaps in the sequence of region_id values).
772
 *
773
 * Always returns zero on an empty handle.
774
 */
775
uint64_t dm_stats_get_nr_regions(const struct dm_stats *dms);
776
777
/*
778
 * Determine the number of groups contained in a dm_stats handle
779
 * following a dm_stats_list() or dm_stats_populate() call.
780
 *
781
 * The value returned is the number of registered groups visible with the
782
 * program_id value used for the list or populate operation and may not be
783
 * equal to the highest present group_id (either due to program_id
784
 * filtering or gaps in the sequence of group_id values).
785
 *
786
 * Always returns zero on an empty handle.
787
 */
788
uint64_t dm_stats_get_nr_groups(const struct dm_stats *dms);
789
790
/*
791
 * Test whether region_id is present in this dm_stats handle.
792
 */
793
int dm_stats_region_present(const struct dm_stats *dms, uint64_t region_id);
794
795
/*
796
 * Returns the number of areas (counter sets) contained in the specified
797
 * region_id of the supplied dm_stats handle.
798
 */
799
uint64_t dm_stats_get_region_nr_areas(const struct dm_stats *dms,
800
              uint64_t region_id);
801
802
/*
803
 * Returns the total number of areas (counter sets) in all regions of the
804
 * given dm_stats object.
805
 */
806
uint64_t dm_stats_get_nr_areas(const struct dm_stats *dms);
807
808
/*
809
 * Test whether group_id is present in this dm_stats handle.
810
 */
811
int dm_stats_group_present(const struct dm_stats *dms, uint64_t group_id);
812
813
/*
814
 * Return the number of bins in the histogram configuration for the
815
 * specified region or zero if no histogram specification is configured.
816
 * Valid following a dm_stats_list() or dm_stats_populate() operation.
817
 */
818
int dm_stats_get_region_nr_histogram_bins(const struct dm_stats *dms,
819
            uint64_t region_id);
820
821
/*
822
 * Parse a histogram string with optional unit suffixes into a
823
 * dm_histogram bounds description.
824
 *
825
 * A histogram string is a string of numbers "n1,n2,n3,..." that
826
 * represent the boundaries of a histogram. The first and final bins
827
 * have implicit lower and upper bounds of zero and infinity
828
 * respectively and boundary values must occur in order of ascending
829
 * magnitude.  Unless a unit suffix is given all values are specified in
830
 * nanoseconds.
831
 *
832
 * For example, if bounds_str="300,600,900", the region will be created
833
 * with a histogram containing four bins. Each report will include four
834
 * numbers a:b:c:d. a is the number of requests that took between 0 and
835
 * 300ns to complete, b is the number of requests that took 300-600ns to
836
 * complete, c is the number of requests that took 600-900ns to complete
837
 * and d is the number of requests that took more than 900ns to
838
 * complete.
839
 *
840
 * An optional unit suffix of 's', 'ms', 'us', or 'ns' may be used to
841
 * specify units of seconds, milliseconds, microseconds, or nanoseconds:
842
 *
843
 *   bounds_str="1ns,1us,1ms,1s"
844
 *   bounds_str="500us,1ms,1500us,2ms"
845
 *   bounds_str="200ms,400ms,600ms,800ms,1s"
846
 *
847
 * The smallest valid unit of time for a histogram specification depends
848
 * on whether the region uses precise timestamps: for a region with the
849
 * default millisecond precision the smallest possible histogram boundary
850
 * magnitude is one millisecond: attempting to use a histogram with a
851
 * boundary less than one millisecond when creating a region will cause
852
 * the region to be created with the precise_timestamps feature enabled.
853
 *
854
 * On success a pointer to the struct dm_histogram representing the
855
 * bounds values is returned, or NULL in the case of error. The returned
856
 * pointer should be freed using dm_free() when no longer required.
857
 */
858
struct dm_histogram *dm_histogram_bounds_from_string(const char *bounds_str);
859
860
/*
861
 * Parse a zero terminated array of uint64_t into a dm_histogram bounds
862
 * description.
863
 *
864
 * Each value in the array specifies the upper bound of a bin in the
865
 * latency histogram in nanoseconds. Values must appear in ascending
866
 * order of magnitude.
867
 *
868
 * The smallest valid unit of time for a histogram specification depends
869
 * on whether the region uses precise timestamps: for a region with the
870
 * default millisecond precision the smallest possible histogram boundary
871
 * magnitude is one millisecond: attempting to use a histogram with a
872
 * boundary less than one millisecond when creating a region will cause
873
 * the region to be created with the precise_timestamps feature enabled.
874
 */
875
struct dm_histogram *dm_histogram_bounds_from_uint64(const uint64_t *bounds);
876
877
/*
878
 * Destroy the histogram bounds array obtained from a call to
879
 * dm_histogram_bounds_from_string().
880
 */
881
void dm_histogram_bounds_destroy(struct dm_histogram *bounds);
882
883
/*
884
 * Destroy a dm_stats object and all associated regions, counter
885
 * sets and histograms.
886
 */
887
void dm_stats_destroy(struct dm_stats *dms);
888
889
/*
890
 * Counter sampling interval
891
 */
892
893
/*
894
 * Set the sampling interval for counter data to the specified value in
895
 * either nanoseconds or milliseconds.
896
 *
897
 * The interval is used to calculate time-based metrics from the basic
898
 * counter data: an interval must be set before calling any of the
899
 * metric methods.
900
 *
901
 * For best accuracy the duration should be measured and updated at the
902
 * end of each interval.
903
 *
904
 * All values are stored internally with nanosecond precision and are
905
 * converted to or from ms when the millisecond interfaces are used.
906
 */
907
void dm_stats_set_sampling_interval_ns(struct dm_stats *dms,
908
               uint64_t interval_ns);
909
910
void dm_stats_set_sampling_interval_ms(struct dm_stats *dms,
911
               uint64_t interval_ms);
912
913
/*
914
 * Retrieve the configured sampling interval in either nanoseconds or
915
 * milliseconds.
916
 */
917
uint64_t dm_stats_get_sampling_interval_ns(const struct dm_stats *dms);
918
uint64_t dm_stats_get_sampling_interval_ms(const struct dm_stats *dms);
919
920
/*
921
 * Override program_id. This may be used to change the default
922
 * program_id value for an existing handle. If the allow_empty argument
923
 * is non-zero a NULL or empty program_id is permitted.
924
 *
925
 * Use with caution! Most users of the library should set a valid,
926
 * non-NULL program_id for every statistics region created. Failing to
927
 * do so may result in confusing state when multiple programs are
928
 * creating and managing statistics regions.
929
 *
930
 * All users of the library are encouraged to choose an unambiguous,
931
 * unique program_id: this could be based on PID (for programs that
932
 * create, report, and delete regions in a single process), session id,
933
 * executable name, or some other distinguishing string.
934
 *
935
 * Use of the empty string as a program_id does not simplify use of the
936
 * library or the command line tools and use of this value is strongly
937
 * discouraged.
938
 */
939
int dm_stats_set_program_id(struct dm_stats *dms, int allow_empty,
940
          const char *program_id);
941
942
/*
943
 * Region properties: size, length & area_len.
944
 *
945
 * Region start and length are returned in units of 512b as specified
946
 * at region creation time. The area_len value gives the size of areas
947
 * into which the region has been subdivided. For regions with a single
948
 * area spanning the range this value is equal to the region length.
949
 *
950
 * For regions created with a specified number of areas the value
951
 * represents the size of the areas into which the kernel divided the
952
 * region excluding any rounding of the last area size. The number of
953
 * areas may be obtained using the dm_stats_nr_areas_region() call.
954
 *
955
 * All values are returned in units of 512b sectors.
956
 */
957
int dm_stats_get_region_start(const struct dm_stats *dms, uint64_t *start,
958
            uint64_t region_id);
959
960
int dm_stats_get_region_len(const struct dm_stats *dms, uint64_t *len,
961
          uint64_t region_id);
962
963
int dm_stats_get_region_area_len(const struct dm_stats *dms,
964
         uint64_t *len, uint64_t region_id);
965
966
/*
967
 * Area properties: start, offset and length.
968
 *
969
 * The area length is always equal to the area length of the region
970
 * that contains it and is obtained from dm_stats_get_region_area_len().
971
 *
972
 * The start of an area is a function of the area_id and the containing
973
 * region's start and area length: it gives the absolute offset into the
974
 * containing device of the beginning of the area.
975
 *
976
 * The offset expresses the area's relative offset into the current
977
 * region. I.e. the area start minus the start offset of the containing
978
 * region.
979
 *
980
 * All values are returned in units of 512b sectors.
981
 */
982
int dm_stats_get_area_start(const struct dm_stats *dms, uint64_t *start,
983
          uint64_t region_id, uint64_t area_id);
984
985
int dm_stats_get_area_offset(const struct dm_stats *dms, uint64_t *offset,
986
           uint64_t region_id, uint64_t area_id);
987
988
/*
989
 * Retrieve program_id and user aux_data for a specific region.
990
 *
991
 * Only valid following a call to dm_stats_list().
992
 */
993
994
/*
995
 * Retrieve program_id for the specified region.
996
 *
997
 * The returned pointer does not need to be freed separately from the
998
 * dm_stats handle but will become invalid after a dm_stats_destroy(),
999
 * dm_stats_list(), dm_stats_populate(), or dm_stats_bind*() of the
1000
 * handle from which it was obtained.
1001
 */
1002
const char *dm_stats_get_region_program_id(const struct dm_stats *dms,
1003
             uint64_t region_id);
1004
1005
/*
1006
 * Retrieve user aux_data set for the specified region. This function
1007
 * will return any stored user aux_data as a string in the memory
1008
 * pointed to by the aux_data argument.
1009
 *
1010
 * Any library internal aux_data fields, such as DMS_GROUP descriptors,
1011
 * are stripped before the value is returned.
1012
 *
1013
 * The returned pointer does not need to be freed separately from the
1014
 * dm_stats handle but will become invalid after a dm_stats_destroy(),
1015
 * dm_stats_list(), dm_stats_populate(), or dm_stats_bind*() of the
1016
 * handle from which it was obtained.
1017
 */
1018
const char *dm_stats_get_region_aux_data(const struct dm_stats *dms,
1019
           uint64_t region_id);
1020
1021
typedef enum dm_stats_obj_type_e {
1022
  DM_STATS_OBJECT_TYPE_NONE,
1023
  DM_STATS_OBJECT_TYPE_AREA,
1024
  DM_STATS_OBJECT_TYPE_REGION,
1025
  DM_STATS_OBJECT_TYPE_GROUP
1026
} dm_stats_obj_type_t;
1027
1028
/*
1029
 * Statistics cursor
1030
 *
1031
 * A dm_stats handle maintains an optional cursor into the statistics
1032
 * tables that it stores. Iterators are provided to visit each region,
1033
 * area, or group in a handle and accessor methods are provided to
1034
 * obtain properties and values for the object at the current cursor
1035
 * position.
1036
 *
1037
 * Using the cursor simplifies walking all regions or groups when
1038
 * the tables are sparse (i.e. contains some present and some
1039
 * non-present region_id or group_id values either due to program_id
1040
 * filtering or the ordering of region and group creation and deletion).
1041
 *
1042
 * Simple macros are provided to visit each area, region, or group,
1043
 * contained in a handle and applications are encouraged to use these
1044
 * where possible.
1045
 */
1046
1047
/*
1048
 * Walk flags are used to initialise a dm_stats handle's cursor control
1049
 * and to select region or group aggregation when calling a metric or
1050
 * counter property method with immediate group, region, and area ID
1051
 * values.
1052
 *
1053
 * Walk flags are stored in the uppermost word of a uint64_t so that
1054
 * a region_id or group_id may be encoded in the lower bits. This
1055
 * allows an aggregate region_id or group_id to be specified when
1056
 * retrieving counter or metric values.
1057
 *
1058
 * Flags may be ORred together when used to initialise a dm_stats_walk:
1059
 * the resulting walk will visit instance of each type specified by
1060
 * the flag combination.
1061
 */
1062
#define DM_STATS_WALK_AREA   0x1000000000000ULL
1063
#define DM_STATS_WALK_REGION 0x2000000000000ULL
1064
#define DM_STATS_WALK_GROUP  0x4000000000000ULL
1065
1066
#define DM_STATS_WALK_ALL    0x7000000000000ULL
1067
#define DM_STATS_WALK_DEFAULT (DM_STATS_WALK_AREA | DM_STATS_WALK_REGION)
1068
1069
/*
1070
 * Skip regions from a DM_STATS_WALK_REGION that contain only a single
1071
 * area: in this case the region's aggregate values are identical to
1072
 * the values of the single contained area. Setting this flag will
1073
 * suppress these duplicate entries during a dm_stats_walk_* with the
1074
 * DM_STATS_WALK_REGION flag set.
1075
 */
1076
#define DM_STATS_WALK_SKIP_SINGLE_AREA   0x8000000000000ULL
1077
1078
/*
1079
 * Initialise the cursor control of a dm_stats handle for the specified
1080
 * walk type(s). Including a walk flag in the flags argument will cause
1081
 * any subsequent walk to visit that type of object (until the next
1082
 * call to dm_stats_walk_init()).
1083
 */
1084
int dm_stats_walk_init(struct dm_stats *dms, uint64_t flags);
1085
1086
/*
1087
 * Set the cursor of a dm_stats handle to address the first present
1088
 * group, region, or area of the currently configured walk. It is
1089
 * valid to attempt to walk a NULL stats handle or a handle containing
1090
 * no present regions; in this case any call to dm_stats_walk_next()
1091
 * becomes a no-op and all calls to dm_stats_walk_end() return true.
1092
 */
1093
void dm_stats_walk_start(struct dm_stats *dms);
1094
1095
/*
1096
 * Advance the statistics cursor to the next area, or to the next
1097
 * present region if at the end of the current region. If the end of
1098
 * the region, area, or group tables is reached a subsequent call to
1099
 * dm_stats_walk_end() will return 1 and dm_stats_object_type() called
1100
 * on the location will return DM_STATS_OBJECT_TYPE_NONE,
1101
 */
1102
void dm_stats_walk_next(struct dm_stats *dms);
1103
1104
/*
1105
 * Force the statistics cursor to advance to the next region. This will
1106
 * stop any in-progress area walk (by clearing DM_STATS_WALK_AREA) and
1107
 * advance the cursor to the next present region, the first present
1108
 * group (if DM_STATS_GROUP_WALK is set), or to the end. In this case a
1109
 * subsequent call to dm_stats_walk_end() will return 1 and a call to
1110
 * dm_stats_object_type() for the location will return
1111
 * DM_STATS_OBJECT_TYPE_NONE.
1112
 */
1113
void dm_stats_walk_next_region(struct dm_stats *dms);
1114
1115
/*
1116
 * Test whether the end of a statistics walk has been reached.
1117
 */
1118
int dm_stats_walk_end(struct dm_stats *dms);
1119
1120
/*
1121
 * Return the type of object at the location specified by region_id
1122
 * and area_id. If either region_id or area_id uses one of the special
1123
 * values DM_STATS_REGION_CURRENT or DM_STATS_AREA_CURRENT the
1124
 * corresponding region or area identifier will be taken from the
1125
 * current cursor location. If the cursor location or the value encoded
1126
 * by region_id and area_id indicates an aggregate region or group,
1127
 * this will be reflected in the value returned.
1128
 */
1129
dm_stats_obj_type_t dm_stats_object_type(const struct dm_stats *dms,
1130
           uint64_t region_id,
1131
           uint64_t area_id);
1132
1133
/*
1134
 * Return the type of object at the current stats cursor location.
1135
 */
1136
dm_stats_obj_type_t dm_stats_current_object_type(const struct dm_stats *dms);
1137
1138
/*
1139
 * Stats iterators
1140
 *
1141
 * C 'for' and 'do'/'while' style iterators for dm_stats data.
1142
 *
1143
 * It is not safe to call any function that modifies the region table
1144
 * within the loop body (i.e. dm_stats_list(), dm_stats_populate(),
1145
 * dm_stats_init(), or dm_stats_destroy()).
1146
 *
1147
 * All counter and property (dm_stats_get_*) access methods, as well as
1148
 * dm_stats_populate_region() can be safely called from loops.
1149
 *
1150
 */
1151
1152
/*
1153
 * Iterate over the regions table visiting each region.
1154
 *
1155
 * If the region table is empty or unpopulated the loop body will not be
1156
 * executed.
1157
 */
1158
#define dm_stats_foreach_region(dms)        \
1159
for (dm_stats_walk_init((dms), DM_STATS_WALK_REGION),   \
1160
     dm_stats_walk_start((dms));        \
1161
     !dm_stats_walk_end((dms)); dm_stats_walk_next_region((dms)))
1162
1163
/*
1164
 * Iterate over the regions table visiting each area.
1165
 *
1166
 * If the region table is empty or unpopulated the loop body will not
1167
 * be executed.
1168
 */
1169
#define dm_stats_foreach_area(dms)        \
1170
for (dm_stats_walk_init((dms), DM_STATS_WALK_AREA),   \
1171
     dm_stats_walk_start((dms));        \
1172
     !dm_stats_walk_end((dms)); dm_stats_walk_next((dms)))
1173
1174
/*
1175
 * Iterate over the regions table visiting each group. Metric and
1176
 * counter methods will return values for the group.
1177
 *
1178
 * If the group table is empty or unpopulated the loop body will not
1179
 * be executed.
1180
 */
1181
#define dm_stats_foreach_group(dms)       \
1182
for (dm_stats_walk_init((dms), DM_STATS_WALK_GROUP),    \
1183
     dm_stats_walk_start(dms);          \
1184
     !dm_stats_walk_end(dms);         \
1185
     dm_stats_walk_next(dms))
1186
1187
/*
1188
 * Start a walk iterating over the regions contained in dm_stats handle
1189
 * 'dms'.
1190
 *
1191
 * The body of the loop should call dm_stats_walk_next() or
1192
 * dm_stats_walk_next_region() to advance to the next element.
1193
 *
1194
 * The loop body is executed at least once even if the stats handle is
1195
 * empty.
1196
 */
1197
#define dm_stats_walk_do(dms)         \
1198
do {                \
1199
  dm_stats_walk_start((dms));       \
1200
  do
1201
1202
/*
1203
 * Start a 'while' style loop or end a 'do..while' loop iterating over the
1204
 * regions contained in dm_stats handle 'dms'.
1205
 */
1206
#define dm_stats_walk_while(dms)        \
1207
  while(!dm_stats_walk_end((dms)));     \
1208
} while (0)
1209
1210
/*
1211
 * Cursor relative property methods
1212
 *
1213
 * Calls with the prefix dm_stats_get_current_* operate relative to the
1214
 * current cursor location, returning properties for the current region
1215
 * or area of the supplied dm_stats handle.
1216
 *
1217
 */
1218
1219
/*
1220
 * Returns the number of areas (counter sets) contained in the current
1221
 * region of the supplied dm_stats handle.
1222
 */
1223
uint64_t dm_stats_get_current_nr_areas(const struct dm_stats *dms);
1224
1225
/*
1226
 * Retrieve the current values of the stats cursor.
1227
 */
1228
uint64_t dm_stats_get_current_region(const struct dm_stats *dms);
1229
uint64_t dm_stats_get_current_area(const struct dm_stats *dms);
1230
1231
/*
1232
 * Current region properties: size, length & area_len.
1233
 *
1234
 * See the comments for the equivalent dm_stats_get_* versions for a
1235
 * complete description of these methods.
1236
 *
1237
 * All values are returned in units of 512b sectors.
1238
 */
1239
int dm_stats_get_current_region_start(const struct dm_stats *dms,
1240
              uint64_t *start);
1241
1242
int dm_stats_get_current_region_len(const struct dm_stats *dms,
1243
            uint64_t *len);
1244
1245
int dm_stats_get_current_region_area_len(const struct dm_stats *dms,
1246
           uint64_t *area_len);
1247
1248
/*
1249
 * Current area properties: start and length.
1250
 *
1251
 * See the comments for the equivalent dm_stats_get_* versions for a
1252
 * complete description of these methods.
1253
 *
1254
 * All values are returned in units of 512b sectors.
1255
 */
1256
int dm_stats_get_current_area_start(const struct dm_stats *dms,
1257
            uint64_t *start);
1258
1259
int dm_stats_get_current_area_offset(const struct dm_stats *dms,
1260
             uint64_t *offset);
1261
1262
int dm_stats_get_current_area_len(const struct dm_stats *dms,
1263
               uint64_t *len);
1264
1265
/*
1266
 * Return a pointer to the program_id string for region at the current
1267
 * cursor location.
1268
 */
1269
const char *dm_stats_get_current_region_program_id(const struct dm_stats *dms);
1270
1271
/*
1272
 * Return a pointer to the user aux_data string for the region at the
1273
 * current cursor location.
1274
 */
1275
const char *dm_stats_get_current_region_aux_data(const struct dm_stats *dms);
1276
1277
/*
1278
 * Statistics groups and data aggregation.
1279
 */
1280
1281
/*
1282
 * Create a new group in stats handle dms from the group descriptor
1283
 * passed in group. The group descriptor is a string containing a list
1284
 * of region_id values that will be included in the group. The first
1285
 * region_id found will be the group leader. Ranges of identifiers may
1286
 * be expressed as "M-N", where M and N are the start and end region_id
1287
 * values for the range.
1288
 */
1289
int dm_stats_create_group(struct dm_stats *dms, const char *members,
1290
        const char *alias, uint64_t *group_id);
1291
1292
/*
1293
 * Remove the specified group_id. If the remove argument is zero the
1294
 * group will be removed but the regions that it contained will remain.
1295
 * If remove is non-zero then all regions that belong to the group will
1296
 * also be removed.
1297
 */
1298
int dm_stats_delete_group(struct dm_stats *dms, uint64_t group_id, int remove);
1299
1300
/*
1301
 * Set an alias for this group or region. The alias will be returned
1302
 * instead of the normal dm-stats name for this region or group.
1303
 */
1304
int dm_stats_set_alias(struct dm_stats *dms, uint64_t group_id,
1305
           const char *alias);
1306
1307
/*
1308
 * Returns a pointer to the currently configured alias for id, or the
1309
 * name of the dm device the handle is bound to if no alias has been
1310
 * set. The pointer will be freed automatically when a new alias is set
1311
 * or when the stats handle is cleared.
1312
 */
1313
const char *dm_stats_get_alias(const struct dm_stats *dms, uint64_t id);
1314
1315
#define DM_STATS_GROUP_NONE UINT64_MAX
1316
/*
1317
 * Return the group_id that the specified region_id belongs to, or the
1318
 * special value DM_STATS_GROUP_NONE if the region does not belong
1319
 * to any group.
1320
 */
1321
uint64_t dm_stats_get_group_id(const struct dm_stats *dms, uint64_t region_id);
1322
1323
/*
1324
 * Store a pointer to a string describing the regions that are members
1325
 * of the group specified by group_id in the memory pointed to by buf.
1326
 * The string is in the same format as the 'group' argument to
1327
 * dm_stats_create_group().
1328
 *
1329
 * The pointer does not need to be freed explicitly by the caller: it
1330
 * will become invalid following a subsequent dm_stats_list(),
1331
 * dm_stats_populate() or dm_stats_destroy() of the corresponding
1332
 * dm_stats handle.
1333
 */
1334
int dm_stats_get_group_descriptor(const struct dm_stats *dms,
1335
          uint64_t group_id, char **buf);
1336
1337
/*
1338
 * Create regions that correspond to the extents of a file in the
1339
 * filesystem and optionally place them into a group.
1340
 *
1341
 * File descriptor fd must reference a regular file, open for reading,
1342
 * in a local file system that supports the FIEMAP ioctl, and that
1343
 * returns data describing the physical location of extents.
1344
 *
1345
 * The file descriptor can be closed by the caller following the call
1346
 * to dm_stats_create_regions_from_fd().
1347
 *
1348
 * Unless nogroup is non-zero the regions will be placed into a group
1349
 * and the group alias set to the value supplied (if alias is NULL no
1350
 * group alias will be assigned).
1351
 *
1352
 * On success the function returns a pointer to an array of uint64_t
1353
 * containing the IDs of the newly created regions. The region_id
1354
 * array is terminated by the value DM_STATS_REGION_NOT_PRESENT and
1355
 * should be freed using dm_free() when no longer required.
1356
 *
1357
 * On error NULL is returned.
1358
 *
1359
 * Following a call to dm_stats_create_regions_from_fd() the handle
1360
 * is guaranteed to be in a listed state, and to contain any region
1361
 * and group identifiers created by the operation.
1362
 *
1363
 * The group_id for the new group is equal to the region_id value in
1364
 * the first array element.
1365
 */
1366
uint64_t *dm_stats_create_regions_from_fd(struct dm_stats *dms, int fd,
1367
            int group, int precise,
1368
            struct dm_histogram *bounds,
1369
            const char *alias);
1370
/*
1371
 * Update a group of regions that correspond to the extents of a file
1372
 * in the filesystem, adding and removing regions to account for
1373
 * allocation changes in the underlying file.
1374
 *
1375
 * File descriptor fd must reference a regular file, open for reading,
1376
 * in a local file system that supports the FIEMAP ioctl, and that
1377
 * returns data describing the physical location of extents.
1378
 *
1379
 * The file descriptor can be closed by the caller following the call
1380
 * to dm_stats_update_regions_from_fd().
1381
 *
1382
 * On success the function returns a pointer to an array of uint64_t
1383
 * containing the IDs of the updated regions (including any existing
1384
 * regions that were not modified by the call).
1385
 *
1386
 * The region_id array is terminated by the special value
1387
 * DM_STATS_REGION_NOT_PRESENT and should be freed using dm_free()
1388
 * when no longer required.
1389
 *
1390
 * On error NULL is returned.
1391
 *
1392
 * Following a call to dm_stats_update_regions_from_fd() the handle
1393
 * is guaranteed to be in a listed state, and to contain any region
1394
 * and group identifiers created by the operation.
1395
 *
1396
 * This function cannot be used with file mapped regions that are
1397
 * not members of a group: either group the regions, or remove them
1398
 * and re-map them with dm_stats_create_regions_from_fd().
1399
 */
1400
uint64_t *dm_stats_update_regions_from_fd(struct dm_stats *dms, int fd,
1401
            uint64_t group_id);
1402
1403
1404
/*
1405
 * The file map monitoring daemon can monitor files in two distinct
1406
 * ways: the mode affects the behaviour of the daemon when a file
1407
 * under monitoring is renamed or unlinked, and the conditions which
1408
 * cause the daemon to terminate.
1409
 *
1410
 * In both modes, the daemon will always shut down when the group
1411
 * being monitored is deleted.
1412
 *
1413
 * Follow inode:
1414
 * The daemon follows the inode of the file, as it was at the time the
1415
 * daemon started. The file descriptor referencing the file is kept
1416
 * open at all times, and the daemon will exit when it detects that
1417
 * the file has been unlinked and it is the last holder of a reference
1418
 * to the file.
1419
 *
1420
 * This mode is useful if the file is expected to be renamed, or moved
1421
 * within the file system, while it is being monitored.
1422
 *
1423
 * Follow path:
1424
 * The daemon follows the path that was given on the daemon command
1425
 * line. The file descriptor referencing the file is re-opened on each
1426
 * iteration of the daemon, and the daemon will exit if no file exists
1427
 * at this location (a tolerance is allowed so that a brief delay
1428
 * between unlink() and creat() is permitted).
1429
 *
1430
 * This mode is useful if the file is updated by unlinking the original
1431
 * and placing a new file at the same path.
1432
 */
1433
1434
typedef enum dm_filemapd_mode_e {
1435
  DM_FILEMAPD_FOLLOW_INODE,
1436
  DM_FILEMAPD_FOLLOW_PATH,
1437
  DM_FILEMAPD_FOLLOW_NONE
1438
} dm_filemapd_mode_t;
1439
1440
/*
1441
 * Parse a string representation of a dmfilemapd mode.
1442
 *
1443
 * Returns a valid dm_filemapd_mode_t value on success, or
1444
 * DM_FILEMAPD_FOLLOW_NONE on error.
1445
 */
1446
dm_filemapd_mode_t dm_filemapd_mode_from_string(const char *mode_str);
1447
1448
/*
1449
 * Start the dmfilemapd filemap monitoring daemon for the specified
1450
 * file descriptor, group, and file system path. The daemon will
1451
 * monitor the file for allocation changes, and when a change is
1452
 * detected, call dm_stats_update_regions_from_fd() to update the
1453
 * mapped regions for the file.
1454
 *
1455
 * The path provided to dm_stats_start_filemapd() must be an absolute
1456
 * path, and should reflect the path of 'fd' at the time that it was
1457
 * opened.
1458
 *
1459
 * The mode parameter controls the behaviour of the daemon when the
1460
 * file being monitored is unlinked or moved: see the comments for
1461
 * dm_filemapd_mode_t for a full description and possible values.
1462
 *
1463
 * The daemon can be stopped at any time by sending SIGTERM to the
1464
 * daemon pid.
1465
 */
1466
int dm_stats_start_filemapd(int fd, uint64_t group_id, const char *path,
1467
          dm_filemapd_mode_t mode, unsigned foreground,
1468
          unsigned verbose);
1469
1470
/*
1471
 * Call this to actually run the ioctl.
1472
 */
1473
int dm_task_run(struct dm_task *dmt);
1474
1475
/*
1476
 * The errno from the last device-mapper ioctl performed by dm_task_run.
1477
 */
1478
int dm_task_get_errno(struct dm_task *dmt);
1479
1480
/*
1481
 * Call this to make or remove the device nodes associated with previously
1482
 * issued commands.
1483
 */
1484
void dm_task_update_nodes(void);
1485
1486
/*
1487
 * Mangling support
1488
 *
1489
 * Character whitelist: 0-9, A-Z, a-z, #+-.:=@_
1490
 * HEX mangling format: \xNN, NN being the hex value of the character.
1491
 * (whitelist and format supported by udev)
1492
*/
1493
typedef enum dm_string_mangling_e {
1494
  DM_STRING_MANGLING_NONE, /* do not mangle at all */
1495
  DM_STRING_MANGLING_AUTO, /* mangle only if not already mangled with hex, error when mixed */
1496
  DM_STRING_MANGLING_HEX   /* always mangle with hex encoding, no matter what the input is */
1497
} dm_string_mangling_t;
1498
1499
/*
1500
 * Set/get mangling mode used for device-mapper names and uuids.
1501
 */
1502
int dm_set_name_mangling_mode(dm_string_mangling_t name_mangling);
1503
dm_string_mangling_t dm_get_name_mangling_mode(void);
1504
1505
/*
1506
 * Get mangled/unmangled form of the device-mapper name or uuid
1507
 * irrespective of the global setting (set by dm_set_name_mangling_mode).
1508
 * The name or uuid returned needs to be freed after use by calling dm_free!
1509
 */
1510
char *dm_task_get_name_mangled(const struct dm_task *dmt);
1511
char *dm_task_get_name_unmangled(const struct dm_task *dmt);
1512
char *dm_task_get_uuid_mangled(const struct dm_task *dmt);
1513
char *dm_task_get_uuid_unmangled(const struct dm_task *dmt);
1514
1515
/*
1516
 * Configure the device-mapper directory
1517
 */
1518
int dm_set_dev_dir(const char *dir);
1519
const char *dm_dir(void);
1520
1521
/*
1522
 * Configure sysfs directory, /sys by default
1523
 */
1524
int dm_set_sysfs_dir(const char *dir);
1525
const char *dm_sysfs_dir(void);
1526
1527
/*
1528
 * Configure default UUID prefix string.
1529
 * Conventionally this is a short capitalized prefix indicating the subsystem
1530
 * that is managing the devices, e.g. "LVM-" or "MPATH-".
1531
 * To support stacks of devices from different subsystems, recursive functions
1532
 * stop recursing if they reach a device with a different prefix.
1533
 */
1534
int dm_set_uuid_prefix(const char *uuid_prefix);
1535
const char *dm_uuid_prefix(void);
1536
1537
/*
1538
 * Determine whether a major number belongs to device-mapper or not.
1539
 */
1540
int dm_is_dm_major(uint32_t major);
1541
1542
/*
1543
 * Get associated device name for given major and minor number by reading
1544
 * the sysfs content. If this is a dm device, get associated dm name, the one
1545
 * that appears in /dev/mapper. DM names could be resolved this way only if
1546
 * kernel used >= 2.6.29, kernel name is found otherwise (e.g. dm-0).
1547
 * If prefer_kernel_name is set, the kernel name is always preferred over
1548
 * device-mapper name for dm devices no matter what the kernel version is.
1549
 * For non-dm devices, we always get associated kernel name, e.g sda, md0 etc.
1550
 * Returns 0 on error or if sysfs is not used (or configured incorrectly),
1551
 * otherwise returns 1 and the supplied buffer holds the device name.
1552
 */
1553
int dm_device_get_name(uint32_t major, uint32_t minor,
1554
           int prefer_kernel_name,
1555
           char *buf, size_t buf_size);
1556
1557
/*
1558
 * Determine whether a device has any holders (devices
1559
 * using this device). If sysfs is not used (or configured
1560
 * incorrectly), returns 0.
1561
 */
1562
int dm_device_has_holders(uint32_t major, uint32_t minor);
1563
1564
/*
1565
 * Determine whether a device contains mounted filesystem.
1566
 * If sysfs is not used (or configured incorrectly), returns 0.
1567
 */
1568
int dm_device_has_mounted_fs(uint32_t major, uint32_t minor);
1569
1570
1571
/*
1572
 * Callback is invoked for individual mountinfo lines,
1573
 * minor, major and mount target are parsed and unmangled.
1574
 */
1575
typedef int (*dm_mountinfo_line_callback_fn) (char *line, unsigned maj, unsigned min,
1576
                char *target, void *cb_data);
1577
1578
/*
1579
 * Read all lines from /proc/self/mountinfo,
1580
 * for each line calls read_fn callback.
1581
 */
1582
int dm_mountinfo_read(dm_mountinfo_line_callback_fn read_fn, void *cb_data);
1583
1584
/*
1585
 * Initialise library
1586
 */
1587
void dm_lib_init(void) __attribute__((constructor));
1588
1589
/*
1590
 * Release library resources
1591
 */
1592
void dm_lib_release(void);
1593
void dm_lib_exit(void) __attribute__((destructor));
1594
1595
/* An optimisation for clients making repeated calls involving dm ioctls */
1596
void dm_hold_control_dev(int hold_open);
1597
1598
/*
1599
 * Use NULL for all devices.
1600
 */
1601
int dm_mknodes(const char *name);
1602
int dm_driver_version(char *version, size_t size);
1603
1604
/******************************************************
1605
 * Functions to build and manipulate trees of devices *
1606
 ******************************************************/
1607
struct dm_tree;
1608
struct dm_tree_node;
1609
1610
/*
1611
 * Initialise an empty dependency tree.
1612
 *
1613
 * The tree consists of a root node together with one node for each mapped
1614
 * device which has child nodes for each device referenced in its table.
1615
 *
1616
 * Every node in the tree has one or more children and one or more parents.
1617
 *
1618
 * The root node is the parent/child of every node that doesn't have other
1619
 * parents/children.
1620
 */
1621
struct dm_tree *dm_tree_create(void);
1622
void dm_tree_free(struct dm_tree *tree);
1623
1624
/*
1625
 * List of suffixes to be ignored when matching uuids against existing devices.
1626
 */
1627
void dm_tree_set_optional_uuid_suffixes(struct dm_tree *dtree, const char **optional_uuid_suffixes);
1628
1629
/*
1630
 * Add nodes to the tree for a given device and all the devices it uses.
1631
 */
1632
int dm_tree_add_dev(struct dm_tree *tree, uint32_t major, uint32_t minor);
1633
int dm_tree_add_dev_with_udev_flags(struct dm_tree *tree, uint32_t major,
1634
            uint32_t minor, uint16_t udev_flags);
1635
1636
/*
1637
 * Add a new node to the tree if it doesn't already exist.
1638
 */
1639
struct dm_tree_node *dm_tree_add_new_dev(struct dm_tree *tree,
1640
           const char *name,
1641
           const char *uuid,
1642
           uint32_t major, uint32_t minor,
1643
           int read_only,
1644
           int clear_inactive,
1645
           void *context);
1646
struct dm_tree_node *dm_tree_add_new_dev_with_udev_flags(struct dm_tree *tree,
1647
               const char *name,
1648
               const char *uuid,
1649
               uint32_t major,
1650
               uint32_t minor,
1651
               int read_only,
1652
               int clear_inactive,
1653
               void *context,
1654
               uint16_t udev_flags);
1655
1656
/*
1657
 * Search for a node in the tree.
1658
 * Set major and minor to 0 or uuid to NULL to get the root node.
1659
 */
1660
struct dm_tree_node *dm_tree_find_node(struct dm_tree *tree,
1661
               uint32_t major,
1662
               uint32_t minor);
1663
struct dm_tree_node *dm_tree_find_node_by_uuid(struct dm_tree *tree,
1664
                 const char *uuid);
1665
1666
/*
1667
 * Use this to walk through all children of a given node.
1668
 * Set handle to NULL in first call.
1669
 * Returns NULL after the last child.
1670
 * Set inverted to use inverted tree.
1671
 */
1672
struct dm_tree_node *dm_tree_next_child(void **handle,
1673
          const struct dm_tree_node *parent,
1674
          uint32_t inverted);
1675
1676
/*
1677
 * Get properties of a node.
1678
 */
1679
const char *dm_tree_node_get_name(const struct dm_tree_node *node);
1680
const char *dm_tree_node_get_uuid(const struct dm_tree_node *node);
1681
const struct dm_info *dm_tree_node_get_info(const struct dm_tree_node *node);
1682
void *dm_tree_node_get_context(const struct dm_tree_node *node);
1683
/*
1684
 * Returns  0 when node size and its children is unchanged.
1685
 * Returns  1 when node or any of its children has increased size.
1686
 * Returns -1 when node or any of its children has reduced size.
1687
 */
1688
int dm_tree_node_size_changed(const struct dm_tree_node *dnode);
1689
1690
/*
1691
 * Returns the number of children of the given node (excluding the root node).
1692
 * Set inverted for the number of parents.
1693
 */
1694
int dm_tree_node_num_children(const struct dm_tree_node *node, uint32_t inverted);
1695
1696
/*
1697
 * Deactivate a device plus all dependencies.
1698
 * Ignores devices that don't have a uuid starting with uuid_prefix.
1699
 */
1700
int dm_tree_deactivate_children(struct dm_tree_node *dnode,
1701
        const char *uuid_prefix,
1702
        size_t uuid_prefix_len);
1703
/*
1704
 * Preload/create a device plus all dependencies.
1705
 * Ignores devices that don't have a uuid starting with uuid_prefix.
1706
 */
1707
int dm_tree_preload_children(struct dm_tree_node *dnode,
1708
           const char *uuid_prefix,
1709
           size_t uuid_prefix_len);
1710
1711
/*
1712
 * Resume a device plus all dependencies.
1713
 * Ignores devices that don't have a uuid starting with uuid_prefix.
1714
 */
1715
int dm_tree_activate_children(struct dm_tree_node *dnode,
1716
            const char *uuid_prefix,
1717
            size_t uuid_prefix_len);
1718
1719
/*
1720
 * Suspend a device plus all dependencies.
1721
 * Ignores devices that don't have a uuid starting with uuid_prefix.
1722
 */
1723
int dm_tree_suspend_children(struct dm_tree_node *dnode,
1724
           const char *uuid_prefix,
1725
           size_t uuid_prefix_len);
1726
1727
/*
1728
 * Skip the filesystem sync when suspending.
1729
 * Does nothing with other functions.
1730
 * Use this when no snapshots are involved.
1731
 */
1732
void dm_tree_skip_lockfs(struct dm_tree_node *dnode);
1733
1734
/*
1735
 * Set the 'noflush' flag when suspending devices.
1736
 * If the kernel supports it, instead of erroring outstanding I/O that
1737
 * cannot be completed, the I/O is queued and resubmitted when the
1738
 * device is resumed.  This affects multipath devices when all paths
1739
 * have failed and queue_if_no_path is set, and mirror devices when
1740
 * block_on_error is set and the mirror log has failed.
1741
 */
1742
void dm_tree_use_no_flush_suspend(struct dm_tree_node *dnode);
1743
1744
/*
1745
 * Retry removal of each device if not successful.
1746
 */
1747
void dm_tree_retry_remove(struct dm_tree_node *dnode);
1748
1749
/*
1750
 * Is the uuid prefix present in the tree?
1751
 * Only returns 0 if every node was checked successfully.
1752
 * Returns 1 if the tree walk has to be aborted.
1753
 */
1754
int dm_tree_children_use_uuid(struct dm_tree_node *dnode,
1755
            const char *uuid_prefix,
1756
            size_t uuid_prefix_len);
1757
1758
/*
1759
 * Construct tables for new nodes before activating them.
1760
 */
1761
int dm_tree_node_add_snapshot_origin_target(struct dm_tree_node *dnode,
1762
              uint64_t size,
1763
              const char *origin_uuid);
1764
int dm_tree_node_add_snapshot_target(struct dm_tree_node *node,
1765
             uint64_t size,
1766
             const char *origin_uuid,
1767
             const char *cow_uuid,
1768
             int persistent,
1769
             uint32_t chunk_size);
1770
int dm_tree_node_add_snapshot_merge_target(struct dm_tree_node *node,
1771
             uint64_t size,
1772
             const char *origin_uuid,
1773
             const char *cow_uuid,
1774
             const char *merge_uuid,
1775
             uint32_t chunk_size);
1776
int dm_tree_node_add_error_target(struct dm_tree_node *node,
1777
          uint64_t size);
1778
int dm_tree_node_add_zero_target(struct dm_tree_node *node,
1779
         uint64_t size);
1780
int dm_tree_node_add_linear_target(struct dm_tree_node *node,
1781
           uint64_t size);
1782
int dm_tree_node_add_striped_target(struct dm_tree_node *node,
1783
            uint64_t size,
1784
            uint32_t stripe_size);
1785
1786
#define DM_CRYPT_IV_DEFAULT UINT64_C(-1)  /* iv_offset == seg offset */
1787
/*
1788
 * Function accepts one string in cipher specification
1789
 * (chainmode and iv should be NULL because included in cipher string)
1790
 *   or
1791
 * separate arguments which will be joined to "cipher-chainmode-iv"
1792
 */
1793
int dm_tree_node_add_crypt_target(struct dm_tree_node *node,
1794
          uint64_t size,
1795
          const char *cipher,
1796
          const char *chainmode,
1797
          const char *iv,
1798
          uint64_t iv_offset,
1799
          const char *key);
1800
int dm_tree_node_add_mirror_target(struct dm_tree_node *node,
1801
           uint64_t size);
1802
1803
/* Mirror log flags */
1804
#define DM_NOSYNC   0x00000001  /* Known already in sync */
1805
#define DM_FORCESYNC    0x00000002  /* Force resync */
1806
#define DM_BLOCK_ON_ERROR 0x00000004  /* On error, suspend I/O */
1807
#define DM_CORELOG    0x00000008  /* In-memory log */
1808
1809
int dm_tree_node_add_mirror_target_log(struct dm_tree_node *node,
1810
               uint32_t region_size,
1811
               unsigned clustered,
1812
               const char *log_uuid,
1813
               unsigned area_count,
1814
               uint32_t flags);
1815
1816
int dm_tree_node_add_raid_target(struct dm_tree_node *node,
1817
         uint64_t size,
1818
         const char *raid_type,
1819
         uint32_t region_size,
1820
         uint32_t stripe_size,
1821
         uint64_t rebuilds,
1822
         uint64_t flags);
1823
1824
/*
1825
 * Defines below are based on kernel's dm-cache.c defines
1826
 * DM_CACHE_MIN_DATA_BLOCK_SIZE (32 * 1024 >> SECTOR_SHIFT)
1827
 * DM_CACHE_MAX_DATA_BLOCK_SIZE (1024 * 1024 * 1024 >> SECTOR_SHIFT)
1828
 */
1829
#define DM_CACHE_MIN_DATA_BLOCK_SIZE (UINT32_C(64))
1830
#define DM_CACHE_MAX_DATA_BLOCK_SIZE (UINT32_C(2097152))
1831
/*
1832
 * Max supported size for cache pool metadata device.
1833
 * Limitation is hardcoded into the kernel and bigger device sizes
1834
 * are not accepted.
1835
 *
1836
 * Limit defined in drivers/md/dm-cache-metadata.h
1837
 */
1838
#define DM_CACHE_METADATA_MAX_SECTORS DM_THIN_METADATA_MAX_SECTORS
1839
1840
/*
1841
 * Define number of elements in rebuild and writemostly arrays
1842
 * 'of struct dm_tree_node_raid_params'.
1843
 */
1844
1845
struct dm_tree_node_raid_params {
1846
  const char *raid_type;
1847
1848
  uint32_t stripes;
1849
  uint32_t mirrors;
1850
  uint32_t region_size;
1851
  uint32_t stripe_size;
1852
1853
  /*
1854
   * 'rebuilds' and 'writemostly' are bitfields that signify
1855
   * which devices in the array are to be rebuilt or marked
1856
   * writemostly.  The kernel supports up to 253 legs.
1857
   * We limit ourselves by choosing a lower value
1858
   * for DEFAULT_RAID{1}_MAX_IMAGES in defaults.h.
1859
   */
1860
  uint64_t rebuilds;
1861
  uint64_t writemostly;
1862
  uint32_t writebehind;     /* I/Os (kernel default COUNTER_MAX / 2) */
1863
  uint32_t sync_daemon_sleep; /* ms (kernel default = 5sec) */
1864
  uint32_t max_recovery_rate; /* kB/sec/disk */
1865
  uint32_t min_recovery_rate; /* kB/sec/disk */
1866
  uint32_t stripe_cache;      /* sectors */
1867
1868
  uint64_t flags;             /* [no]sync */
1869
  uint32_t reserved2;
1870
};
1871
1872
/*
1873
 * Version 2 of above node raid params struct to keep API compatibility.
1874
 *
1875
 * Extended for more than 64 legs (max 253 in the MD kernel runtime!),
1876
 * delta_disks for disk add/remove reshaping,
1877
 * data_offset for out-of-place reshaping
1878
 * and data_copies for odd number of raid10 legs.
1879
 */
1880
#define RAID_BITMAP_SIZE 4 /* 4 * 64 bit elements in rebuilds/writemostly arrays */
1881
struct dm_tree_node_raid_params_v2 {
1882
  const char *raid_type;
1883
1884
  uint32_t stripes;
1885
  uint32_t mirrors;
1886
  uint32_t region_size;
1887
  uint32_t stripe_size;
1888
1889
  int delta_disks; /* +/- number of disks to add/remove (reshaping) */
1890
  int data_offset; /* data offset to set (out-of-place reshaping) */
1891
1892
  /*
1893
   * 'rebuilds' and 'writemostly' are bitfields that signify
1894
   * which devices in the array are to be rebuilt or marked
1895
   * writemostly.  The kernel supports up to 253 legs.
1896
   * We limit ourselves by choosing a lower value
1897
   * for DEFAULT_RAID_MAX_IMAGES.
1898
   */
1899
  uint64_t rebuilds[RAID_BITMAP_SIZE];
1900
  uint64_t writemostly[RAID_BITMAP_SIZE];
1901
  uint32_t writebehind;     /* I/Os (kernel default COUNTER_MAX / 2) */
1902
  uint32_t data_copies;     /* RAID # of data copies */
1903
  uint32_t sync_daemon_sleep; /* ms (kernel default = 5sec) */
1904
  uint32_t max_recovery_rate; /* kB/sec/disk */
1905
  uint32_t min_recovery_rate; /* kB/sec/disk */
1906
  uint32_t stripe_cache;      /* sectors */
1907
1908
  uint64_t flags;             /* [no]sync */
1909
};
1910
1911
int dm_tree_node_add_raid_target_with_params(struct dm_tree_node *node,
1912
               uint64_t size,
1913
               const struct dm_tree_node_raid_params *p);
1914
1915
/* Version 2 API function taking dm_tree_node_raid_params_v2 for aforementioned extensions. */
1916
int dm_tree_node_add_raid_target_with_params_v2(struct dm_tree_node *node,
1917
            uint64_t size,
1918
            const struct dm_tree_node_raid_params_v2 *p);
1919
1920
/* Cache feature_flags */
1921
#define DM_CACHE_FEATURE_WRITEBACK    0x00000001
1922
#define DM_CACHE_FEATURE_WRITETHROUGH 0x00000002
1923
#define DM_CACHE_FEATURE_PASSTHROUGH  0x00000004
1924
#define DM_CACHE_FEATURE_METADATA2    0x00000008 /* cache v1.10 */
1925
#define DM_CACHE_FEATURE_NO_DISCARD_PASSDOWN 0x00000010
1926
1927
struct dm_config_node;
1928
/*
1929
 * Use for passing cache policy and all its args e.g.:
1930
 *
1931
 * policy_settings {
1932
 *    migration_threshold=2048
1933
 *    sequential_threshold=100
1934
 *    ...
1935
 * }
1936
 *
1937
 * For policy without any parameters use NULL.
1938
 */
1939
int dm_tree_node_add_cache_target(struct dm_tree_node *node,
1940
          uint64_t size,
1941
          uint64_t feature_flags, /* DM_CACHE_FEATURE_* */
1942
          const char *metadata_uuid,
1943
          const char *data_uuid,
1944
          const char *origin_uuid,
1945
          const char *policy_name,
1946
          const struct dm_config_node *policy_settings,
1947
          uint32_t data_block_size);
1948
1949
/*
1950
 * Add a cache target using a cachevol (single LV with metadata and data).
1951
 * The cachevol_uuid refers to a single device containing both metadata and data,
1952
 * with metadata_start/metadata_len and data_start/data_len specifying the regions.
1953
 */
1954
int dm_tree_node_add_cachevol_target(struct dm_tree_node *node,
1955
             uint64_t size,
1956
             uint64_t feature_flags, /* DM_CACHE_FEATURE_* */
1957
             const char *metadata_uuid,
1958
             const char *data_uuid,
1959
             const char *cachevol_uuid,
1960
             const char *origin_uuid,
1961
             const char *policy_name,
1962
             const struct dm_config_node *policy_settings,
1963
             uint64_t metadata_start,
1964
             uint64_t metadata_len,
1965
             uint64_t data_start,
1966
             uint64_t data_len,
1967
             uint32_t data_block_size);
1968
1969
struct dm_writecache_settings {
1970
  /*
1971
   * Allow an unrecognized key and its val to be passed to the kernel for
1972
   * cases where a new kernel setting is added but lvm doesn't know about
1973
   * it yet.
1974
   */
1975
  char *new_key;
1976
  char *new_val;
1977
1978
  /*
1979
   * Flag is 1 if a value has been set.
1980
   */
1981
  unsigned high_watermark_set:1;
1982
  unsigned low_watermark_set:1;
1983
  unsigned writeback_jobs_set:1;
1984
  unsigned autocommit_blocks_set:1;
1985
  unsigned autocommit_time_set:1;
1986
  unsigned fua_set:1;
1987
  unsigned nofua_set:1;
1988
  unsigned cleaner_set:1;
1989
  unsigned max_age_set:1;
1990
  unsigned metadata_only_set:1;
1991
  unsigned pause_writeback_set:1;
1992
  uint32_t reserved : 21;
1993
1994
  uint64_t high_watermark;
1995
  uint64_t low_watermark;
1996
  uint64_t writeback_jobs;
1997
  uint64_t autocommit_blocks;
1998
  uint64_t autocommit_time; /* in milliseconds */
1999
  uint32_t fua;
2000
  uint32_t nofua;
2001
  uint32_t cleaner;
2002
  uint32_t max_age;         /* in milliseconds */
2003
  uint32_t metadata_only;
2004
  uint32_t pause_writeback; /* in milliseconds */
2005
};
2006
2007
int dm_tree_node_add_writecache_target(struct dm_tree_node *node,
2008
        uint64_t size,
2009
        const char *origin_uuid,
2010
        const char *cache_uuid,
2011
        int pmem,
2012
        uint32_t writecache_block_size,
2013
        struct dm_writecache_settings *settings);
2014
2015
struct dm_integrity_settings {
2016
  char mode[8];
2017
  uint32_t tag_size;
2018
  uint32_t block_size;       /* optional table param always set by lvm */
2019
  const char *internal_hash; /* optional table param always set by lvm */
2020
2021
  uint32_t journal_sectors;
2022
  uint32_t interleave_sectors;
2023
  uint32_t buffer_sectors;
2024
  uint32_t journal_watermark;
2025
  uint32_t commit_time;
2026
  uint32_t bitmap_flush_interval;
2027
  uint64_t sectors_per_bit;
2028
  uint32_t allow_discards;
2029
2030
  unsigned journal_sectors_set:1;
2031
  unsigned interleave_sectors_set:1;
2032
  unsigned buffer_sectors_set:1;
2033
  unsigned journal_watermark_set:1;
2034
  unsigned commit_time_set:1;
2035
  unsigned bitmap_flush_interval_set:1;
2036
  unsigned sectors_per_bit_set:1;
2037
  unsigned allow_discards_set:1;
2038
};
2039
2040
int dm_tree_node_add_integrity_target(struct dm_tree_node *node,
2041
        uint64_t size,
2042
        const char *origin_uuid,
2043
        const char *meta_uuid,
2044
        struct dm_integrity_settings *settings,
2045
        int recalculate);
2046
2047
/*
2048
 * VDO target support
2049
 */
2050
2051
#define DM_SECTOR_SHIFT 9L
2052
2053
#define DM_VDO_BLOCK_SIZE     UINT64_C(8)   // 4KiB in sectors
2054
#define DM_VDO_BLOCK_SIZE_KB      (DM_VDO_BLOCK_SIZE << DM_SECTOR_SHIFT)
2055
2056
#define DM_VDO_BLOCK_MAP_CACHE_SIZE_MINIMUM_MB  (128)     // 128MiB
2057
#define DM_VDO_BLOCK_MAP_CACHE_SIZE_MAXIMUM_MB  (16 * 1024 * 1024 - 1)  // 16TiB - 1
2058
#define DM_VDO_BLOCK_MAP_CACHE_SIZE_MINIMUM_PER_LOGICAL_THREAD  (4096 * DM_VDO_BLOCK_SIZE_KB)
2059
2060
#define DM_VDO_BLOCK_MAP_ERA_LENGTH_MINIMUM 1
2061
#define DM_VDO_BLOCK_MAP_ERA_LENGTH_MAXIMUM 16380
2062
2063
#define DM_VDO_INDEX_MEMORY_SIZE_MINIMUM_MB 256     // 0.25 GiB
2064
#define DM_VDO_INDEX_MEMORY_SIZE_MAXIMUM_MB (1024 * 1024 * 1024)  // 1TiB
2065
2066
#define DM_VDO_SLAB_SIZE_MINIMUM_MB   128     // 128MiB
2067
#define DM_VDO_SLAB_SIZE_MAXIMUM_MB   (32 * 1024)   // 32GiB
2068
#define DM_VDO_SLABS_MAXIMUM      8192
2069
2070
#define DM_VDO_LOGICAL_SIZE_MAXIMUM (UINT64_C(4) * 1024 * 1024 * 1024 * 1024 * 1024 >> DM_SECTOR_SHIFT) // 4PiB
2071
#define DM_VDO_PHYSICAL_SIZE_MAXIMUM  (UINT64_C(64) * DM_VDO_BLOCK_SIZE_KB * 1024 * 1024 * 1024 >> DM_SECTOR_SHIFT) // 256TiB
2072
2073
#define DM_VDO_ACK_THREADS_MINIMUM    0
2074
#define DM_VDO_ACK_THREADS_MAXIMUM    100
2075
2076
#define DM_VDO_BIO_THREADS_MINIMUM    1
2077
#define DM_VDO_BIO_THREADS_MAXIMUM    100
2078
2079
#define DM_VDO_BIO_ROTATION_MINIMUM   1
2080
#define DM_VDO_BIO_ROTATION_MAXIMUM   1024
2081
2082
#define DM_VDO_CPU_THREADS_MINIMUM    1
2083
#define DM_VDO_CPU_THREADS_MAXIMUM    100
2084
2085
#define DM_VDO_HASH_ZONE_THREADS_MINIMUM  0
2086
#define DM_VDO_HASH_ZONE_THREADS_MAXIMUM  100
2087
2088
#define DM_VDO_LOGICAL_THREADS_MINIMUM    0
2089
#define DM_VDO_LOGICAL_THREADS_MAXIMUM    60
2090
2091
#define DM_VDO_PHYSICAL_THREADS_MINIMUM   0
2092
#define DM_VDO_PHYSICAL_THREADS_MAXIMUM   16
2093
2094
#define DM_VDO_MAX_DISCARD_MINIMUM    1
2095
#define DM_VDO_MAX_DISCARD_MAXIMUM    (UINT32_MAX / (uint32_t)(DM_VDO_BLOCK_SIZE_KB))
2096
2097
enum dm_vdo_operating_mode {
2098
  DM_VDO_MODE_RECOVERING,
2099
  DM_VDO_MODE_READ_ONLY,
2100
  DM_VDO_MODE_NORMAL
2101
};
2102
2103
enum dm_vdo_compression_state {
2104
  DM_VDO_COMPRESSION_ONLINE,
2105
  DM_VDO_COMPRESSION_OFFLINE
2106
};
2107
2108
enum dm_vdo_index_state {
2109
  DM_VDO_INDEX_ERROR,
2110
  DM_VDO_INDEX_CLOSED,
2111
  DM_VDO_INDEX_OPENING,
2112
  DM_VDO_INDEX_CLOSING,
2113
  DM_VDO_INDEX_OFFLINE,
2114
  DM_VDO_INDEX_ONLINE,
2115
  DM_VDO_INDEX_UNKNOWN
2116
};
2117
2118
struct dm_vdo_status {
2119
  char *device;
2120
  enum dm_vdo_operating_mode operating_mode;
2121
  int recovering;
2122
  enum dm_vdo_index_state index_state;
2123
  enum dm_vdo_compression_state compression_state;
2124
  uint64_t used_blocks;
2125
  uint64_t total_blocks;
2126
};
2127
2128
#define DM_VDO_MAX_ERROR 256
2129
2130
struct dm_vdo_status_parse_result {
2131
  char error[DM_VDO_MAX_ERROR];
2132
  struct dm_vdo_status *status;
2133
};
2134
2135
enum dm_vdo_write_policy {
2136
  DM_VDO_WRITE_POLICY_AUTO = 0,
2137
  DM_VDO_WRITE_POLICY_SYNC,
2138
  DM_VDO_WRITE_POLICY_ASYNC,
2139
  DM_VDO_WRITE_POLICY_ASYNC_UNSAFE
2140
};
2141
2142
struct dm_vdo_target_params {
2143
  uint32_t minimum_io_size;       /* in sectors */
2144
  uint32_t block_map_cache_size_mb;
2145
  union {
2146
    uint32_t block_map_era_length;  /* format period */
2147
    uint32_t block_map_period;      /* supported alias */
2148
  };
2149
  uint32_t index_memory_size_mb;  /* format */
2150
2151
  uint32_t slab_size_mb;          /* format */
2152
2153
  uint32_t max_discard;
2154
  /* threads */
2155
  uint32_t ack_threads;
2156
  uint32_t bio_threads;
2157
  uint32_t bio_rotation;
2158
  uint32_t cpu_threads;
2159
  uint32_t hash_zone_threads;
2160
  uint32_t logical_threads;
2161
  uint32_t physical_threads;
2162
2163
  int use_compression;
2164
  int use_deduplication;
2165
  int use_metadata_hints;
2166
  int use_sparse_index;          /* format */
2167
2168
  /* write policy */
2169
  enum dm_vdo_write_policy write_policy;
2170
};
2171
2172
int dm_vdo_validate_target_params(const struct dm_vdo_target_params *vtp,
2173
          uint64_t vdo_size);
2174
2175
int dm_tree_node_add_vdo_target(struct dm_tree_node *node,
2176
        uint64_t size,
2177
        uint32_t vdo_version,
2178
        const char *vdo_pool_name,
2179
        const char *data_uuid,
2180
        uint64_t data_size,
2181
        const struct dm_vdo_target_params *vtp);
2182
2183
int dm_vdo_parse_logical_size(const char *vdo_path, uint64_t *logical_blocks);
2184
2185
int dm_vdo_status_parse(struct dm_pool *mem, const char *input,
2186
      struct dm_vdo_status_parse_result *result);
2187
2188
struct dm_vdo_stats {
2189
  uint64_t physical_blocks;
2190
  uint64_t logical_blocks;
2191
  uint64_t bytes_per_physical_block;
2192
  uint64_t bytes_per_logical_block;
2193
  uint64_t data_blocks_used;
2194
  uint64_t overhead_blocks_used;
2195
  uint64_t logical_blocks_used;
2196
  uint64_t bios_in; /* write bios only (kernel: biosIn.write) */
2197
  uint64_t bios_out;  /* write bios only (kernel: biosOut.write) */
2198
  uint64_t bios_meta; /* write bios only (kernel: biosMeta.write) */
2199
  enum dm_vdo_operating_mode operating_mode;
2200
};
2201
2202
#define DM_VDO_STAT_FIELD_LEN 80
2203
2204
struct dm_vdo_stats_field {
2205
  char label[DM_VDO_STAT_FIELD_LEN];
2206
  char value[DM_VDO_STAT_FIELD_LEN];
2207
};
2208
2209
struct dm_vdo_stats_full {
2210
  struct dm_vdo_stats *stats;
2211
  unsigned field_count;
2212
  struct dm_vdo_stats_field fields[];
2213
};
2214
2215
#define DM_VDO_STATS_BASIC 0x0
2216
#define DM_VDO_STATS_FULL  0x1
2217
2218
struct dm_vdo_stats_full *dm_vdo_stats_parse(struct dm_pool *mem,
2219
               const char *stats_str,
2220
               unsigned flags);
2221
2222
/*
2223
 * FIXME Add individual cache policy pairs  <key> = value, like:
2224
 * int dm_tree_node_add_cache_policy_arg(struct dm_tree_node *dnode,
2225
 *              const char *key, uint64_t value);
2226
 */
2227
2228
/*
2229
 * Replicator operation mode
2230
 * Note: API for Replicator is not yet stable
2231
 */
2232
typedef enum dm_replicator_mode_e {
2233
  DM_REPLICATOR_SYNC,     /* Synchronous replication */
2234
  DM_REPLICATOR_ASYNC_WARN,   /* Warn if async replicator is slow */
2235
  DM_REPLICATOR_ASYNC_STALL,    /* Stall replicator if not fast enough */
2236
  DM_REPLICATOR_ASYNC_DROP,   /* Drop sites out of sync */
2237
  DM_REPLICATOR_ASYNC_FAIL,   /* Fail replicator if slow */
2238
  NUM_DM_REPLICATOR_MODES
2239
} dm_replicator_mode_t;
2240
2241
int dm_tree_node_add_replicator_target(struct dm_tree_node *node,
2242
               uint64_t size,
2243
               const char *rlog_uuid,
2244
               const char *rlog_type,
2245
               unsigned rsite_index,
2246
               dm_replicator_mode_t mode,
2247
               uint32_t async_timeout,
2248
               uint64_t fall_behind_data,
2249
               uint32_t fall_behind_ios);
2250
2251
int dm_tree_node_add_replicator_dev_target(struct dm_tree_node *node,
2252
             uint64_t size,
2253
             const char *replicator_uuid, /* Replicator control device */
2254
             uint64_t rdevice_index,
2255
             const char *rdev_uuid, /* Rimage device name/uuid */
2256
             unsigned rsite_index,
2257
             const char *slog_uuid,
2258
             uint32_t slog_flags,   /* Mirror log flags */
2259
             uint32_t slog_region_size);
2260
/* End of Replicator API */
2261
2262
/*
2263
 * FIXME: Defines below are based on kernel's dm-thin.c defines
2264
 * DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
2265
 * DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
2266
 */
2267
#define DM_THIN_MIN_DATA_BLOCK_SIZE (UINT32_C(128))
2268
#define DM_THIN_MAX_DATA_BLOCK_SIZE (UINT32_C(2097152))
2269
/*
2270
 * Max supported size for thin pool metadata device (17045913600 bytes)
2271
 * drivers/md/dm-thin-metadata.h THIN_METADATA_MAX_SECTORS
2272
 * But here DM_THIN_MAX_METADATA_SIZE got defined incorrectly
2273
 * Correct size is (UINT64_C(255) * ((1 << 14) - 64) * (4096 / (1 << 9)))
2274
 */
2275
#define DM_THIN_MAX_METADATA_SIZE   (UINT64_C(255) * (1 << 14) * (4096 / (1 << 9)) - 256 * 1024)
2276
2277
int dm_tree_node_add_thin_pool_target(struct dm_tree_node *node,
2278
              uint64_t size,
2279
              uint64_t transaction_id,
2280
              const char *metadata_uuid,
2281
              const char *pool_uuid,
2282
              uint32_t data_block_size,
2283
              uint64_t low_water_mark,
2284
              unsigned skip_block_zeroing);
2285
2286
int dm_tree_node_add_thin_pool_target_v1(struct dm_tree_node *node,
2287
           uint64_t size,
2288
           uint64_t transaction_id,
2289
           const char *metadata_uuid,
2290
           const char *pool_uuid,
2291
           uint32_t data_block_size,
2292
           uint64_t low_water_mark,
2293
           unsigned skip_block_zeroing,
2294
           unsigned crop_metadata);
2295
2296
/* Supported messages for thin provision target */
2297
typedef enum dm_thin_message_e {
2298
  DM_THIN_MESSAGE_CREATE_SNAP,    /* device_id, origin_id */
2299
  DM_THIN_MESSAGE_CREATE_THIN,    /* device_id */
2300
  DM_THIN_MESSAGE_DELETE,     /* device_id */
2301
  DM_THIN_MESSAGE_SET_TRANSACTION_ID, /* current_id, new_id */
2302
  DM_THIN_MESSAGE_RESERVE_METADATA_SNAP,  /* target version >= 1.1 */
2303
  DM_THIN_MESSAGE_RELEASE_METADATA_SNAP,  /* target version >= 1.1 */
2304
} dm_thin_message_t;
2305
2306
int dm_tree_node_add_thin_pool_message(struct dm_tree_node *node,
2307
               dm_thin_message_t type,
2308
               uint64_t id1, uint64_t id2);
2309
2310
/*
2311
 * Set thin pool discard features
2312
 *   ignore      - Disable support for discards
2313
 *   no_passdown - Don't pass discards down to underlying data device,
2314
 *                 just remove the mapping
2315
 * Feature is available since version 1.1 of the thin target.
2316
 */
2317
int dm_tree_node_set_thin_pool_discard(struct dm_tree_node *node,
2318
               unsigned ignore,
2319
               unsigned no_passdown);
2320
/*
2321
 * Set error if no space, instead of queueing for thin pool.
2322
 */
2323
int dm_tree_node_set_thin_pool_error_if_no_space(struct dm_tree_node *node,
2324
             unsigned error_if_no_space);
2325
/* Start thin pool with metadata in read-only mode */
2326
int dm_tree_node_set_thin_pool_read_only(struct dm_tree_node *node,
2327
           unsigned read_only);
2328
/*
2329
 * FIXME: Defines below are based on kernel's dm-thin.c defines
2330
 * MAX_DEV_ID ((1 << 24) - 1)
2331
 */
2332
#define DM_THIN_MAX_DEVICE_ID (UINT32_C((1 << 24) - 1))
2333
int dm_tree_node_add_thin_target(struct dm_tree_node *node,
2334
         uint64_t size,
2335
         const char *pool_uuid,
2336
         uint32_t device_id);
2337
2338
int dm_tree_node_set_thin_external_origin(struct dm_tree_node *node,
2339
            const char *external_uuid);
2340
2341
void dm_tree_node_set_udev_flags(struct dm_tree_node *node, uint16_t udev_flags);
2342
2343
void dm_tree_node_set_presuspend_node(struct dm_tree_node *node,
2344
              struct dm_tree_node *presuspend_node);
2345
2346
int dm_tree_node_add_target_area(struct dm_tree_node *node,
2347
         const char *dev_name,
2348
         const char *uuid,
2349
         uint64_t offset);
2350
2351
/*
2352
 * Only for temporarily-missing raid devices where changes are tracked.
2353
 */
2354
int dm_tree_node_add_null_area(struct dm_tree_node *node, uint64_t offset);
2355
2356
/*
2357
 * Set readahead (in sectors) after loading the node.
2358
 */
2359
void dm_tree_node_set_read_ahead(struct dm_tree_node *dnode,
2360
         uint32_t read_ahead,
2361
         uint32_t read_ahead_flags);
2362
2363
/*
2364
 * Set node callback hook before de/activation.
2365
 * Callback is called before 'activation' of node for activation tree,
2366
 * or 'deactivation' of node for deactivation tree.
2367
 */
2368
typedef enum dm_node_callback_e {
2369
  DM_NODE_CALLBACK_PRELOADED,   /* Node has preload deps */
2370
  DM_NODE_CALLBACK_DEACTIVATED, /* Node is deactivated */
2371
} dm_node_callback_t;
2372
typedef int (*dm_node_callback_fn) (struct dm_tree_node *node,
2373
            dm_node_callback_t type, void *cb_data);
2374
void dm_tree_node_set_callback(struct dm_tree_node *node,
2375
             dm_node_callback_fn cb, void *cb_data);
2376
2377
void dm_tree_set_cookie(struct dm_tree_node *node, uint32_t cookie);
2378
uint32_t dm_tree_get_cookie(struct dm_tree_node *node);
2379
2380
/*****************************************************************************
2381
 * Library functions
2382
 *****************************************************************************/
2383
2384
/*******************
2385
 * Memory management
2386
 *******************/
2387
2388
/*
2389
 * Never use these functions directly - use the macros following instead.
2390
 */
2391
void *dm_malloc_wrapper(size_t s, const char *file, int line)
2392
  __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
2393
void *dm_malloc_aligned_wrapper(size_t s, size_t a, const char *file, int line)
2394
  __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
2395
void *dm_zalloc_wrapper(size_t s, const char *file, int line)
2396
  __attribute__((__malloc__)) __attribute__((__warn_unused_result__));
2397
void *dm_realloc_wrapper(void *p, unsigned int s, const char *file, int line)
2398
  __attribute__((__warn_unused_result__));
2399
void dm_free_wrapper(void *ptr);
2400
char *dm_strdup_wrapper(const char *s, const char *file, int line)
2401
  __attribute__((__warn_unused_result__));
2402
int dm_dump_memory_wrapper(void);
2403
void dm_bounds_check_wrapper(void);
2404
2405
#define dm_malloc(s) dm_malloc_wrapper((s), __FILE__, __LINE__)
2406
#define dm_malloc_aligned(s, a) dm_malloc_aligned_wrapper((s), (a),  __FILE__, __LINE__)
2407
#define dm_zalloc(s) dm_zalloc_wrapper((s), __FILE__, __LINE__)
2408
#define dm_strdup(s) dm_strdup_wrapper((s), __FILE__, __LINE__)
2409
#define dm_free(p) dm_free_wrapper(p)
2410
#define dm_realloc(p, s) dm_realloc_wrapper((p), (s), __FILE__, __LINE__)
2411
#define dm_dump_memory() dm_dump_memory_wrapper()
2412
#define dm_bounds_check() dm_bounds_check_wrapper()
2413
2414
/*
2415
 * The pool allocator is useful when you are going to allocate
2416
 * lots of memory, use the memory for a bit, and then free the
2417
 * memory in one go.  A surprising amount of code has this usage
2418
 * profile.
2419
 *
2420
 * You should think of the pool as an infinite, contiguous chunk
2421
 * of memory.  The front of this chunk of memory contains
2422
 * allocated objects, the second half is free.  dm_pool_alloc grabs
2423
 * the next 'size' bytes from the free half, in effect moving it
2424
 * into the allocated half.  This operation is very efficient.
2425
 *
2426
 * dm_pool_free frees the allocated object *and* all objects
2427
 * allocated after it.  It is important to note this semantic
2428
 * difference from malloc/free.  This is also extremely
2429
 * efficient, since a single dm_pool_free can dispose of a large
2430
 * complex object.
2431
 *
2432
 * dm_pool_destroy frees all allocated memory.
2433
 *
2434
 * eg, If you are building a binary tree in your program, and
2435
 * know that you are only ever going to insert into your tree,
2436
 * and not delete (eg, maintaining a symbol table for a
2437
 * compiler).  You can create yourself a pool, allocate the nodes
2438
 * from it, and when the tree becomes redundant call dm_pool_destroy
2439
 * (no nasty iterating through the tree to free nodes).
2440
 *
2441
 * eg, On the other hand if you wanted to repeatedly insert and
2442
 * remove objects into the tree, you would be better off
2443
 * allocating the nodes from a free list; you cannot free a
2444
 * single arbitrary node with pool.
2445
 */
2446
2447
struct dm_pool;
2448
2449
/* constructor and destructor */
2450
struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint)
2451
  __attribute__((__warn_unused_result__));
2452
void dm_pool_destroy(struct dm_pool *p);
2453
2454
/* simple allocation/free routines */
2455
void *dm_pool_alloc(struct dm_pool *p, size_t s)
2456
  __attribute__((__warn_unused_result__));
2457
void *dm_pool_alloc_aligned(struct dm_pool *p, size_t s, unsigned alignment)
2458
  __attribute__((__warn_unused_result__));
2459
void dm_pool_empty(struct dm_pool *p);
2460
void dm_pool_free(struct dm_pool *p, void *ptr);
2461
2462
/*
2463
 * To aid debugging, a pool can be locked. Any modifications made
2464
 * to the content of the pool while it is locked can be detected.
2465
 * Default compilation is using a crc checksum to notice modifications.
2466
 * The pool locking is using the mprotect with the compilation flag
2467
 * DEBUG_ENFORCE_POOL_LOCKING to enforce the memory protection.
2468
 */
2469
/* query pool lock status */
2470
int dm_pool_locked(struct dm_pool *p);
2471
/* mark pool as locked */
2472
int dm_pool_lock(struct dm_pool *p, int crc)
2473
  __attribute__((__warn_unused_result__));
2474
/* mark pool as unlocked */
2475
int dm_pool_unlock(struct dm_pool *p, int crc)
2476
  __attribute__((__warn_unused_result__));
2477
2478
/*
2479
 * Object building routines:
2480
 *
2481
 * These allow you to 'grow' an object, useful for
2482
 * building strings, or filling in dynamic
2483
 * arrays.
2484
 *
2485
 * It's probably best explained with an example:
2486
 *
2487
 * char *build_string(struct dm_pool *mem)
2488
 * {
2489
 *      int i;
2490
 *      char buffer[16];
2491
 *
2492
 *      if (!dm_pool_begin_object(mem, 128))
2493
 *              return NULL;
2494
 *
2495
 *      for (i = 0; i < 50; i++) {
2496
 *              snprintf(buffer, sizeof(buffer), "%d, ", i);
2497
 *              if (!dm_pool_grow_object(mem, buffer, 0))
2498
 *                      goto bad;
2499
 *      }
2500
 *
2501
 *  // add null
2502
 *      if (!dm_pool_grow_object(mem, "\0", 1))
2503
 *              goto bad;
2504
 *
2505
 *      return dm_pool_end_object(mem);
2506
 *
2507
 * bad:
2508
 *
2509
 *      dm_pool_abandon_object(mem);
2510
 *      return NULL;
2511
 *}
2512
 *
2513
 * So start an object by calling dm_pool_begin_object
2514
 * with a guess at the final object size - if in
2515
 * doubt make the guess too small.
2516
 *
2517
 * Then append chunks of data to your object with
2518
 * dm_pool_grow_object.  Finally get your object with
2519
 * a call to dm_pool_end_object.
2520
 *
2521
 * Setting delta to 0 means it will use strlen(extra).
2522
 */
2523
int dm_pool_begin_object(struct dm_pool *p, size_t hint);
2524
int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta);
2525
void *dm_pool_end_object(struct dm_pool *p);
2526
void dm_pool_abandon_object(struct dm_pool *p);
2527
2528
/* utilities */
2529
char *dm_pool_strdup(struct dm_pool *p, const char *str)
2530
  __attribute__((__warn_unused_result__));
2531
char *dm_pool_strndup(struct dm_pool *p, const char *str, size_t n)
2532
  __attribute__((__warn_unused_result__));
2533
void *dm_pool_zalloc(struct dm_pool *p, size_t s)
2534
  __attribute__((__warn_unused_result__));
2535
2536
/******************
2537
 * bitset functions
2538
 ******************/
2539
2540
typedef uint32_t *dm_bitset_t;
2541
2542
dm_bitset_t dm_bitset_create(struct dm_pool *mem, unsigned num_bits);
2543
void dm_bitset_destroy(dm_bitset_t bs);
2544
2545
int dm_bitset_equal(dm_bitset_t in1, dm_bitset_t in2);
2546
2547
void dm_bit_and(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2);
2548
void dm_bit_union(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2);
2549
int dm_bit_get_first(dm_bitset_t bs);
2550
int dm_bit_get_next(dm_bitset_t bs, int last_bit);
2551
int dm_bit_get_last(dm_bitset_t bs);
2552
int dm_bit_get_prev(dm_bitset_t bs, int last_bit);
2553
2554
#define DM_BITS_PER_INT ((unsigned)sizeof(int) * CHAR_BIT)
2555
2556
#define dm_bit(bs, i) \
2557
   ((bs)[((i) / DM_BITS_PER_INT) + 1] & (0x1 << ((i) & (DM_BITS_PER_INT - 1))))
2558
2559
#define dm_bit_set(bs, i) \
2560
   ((bs)[((i) / DM_BITS_PER_INT) + 1] |= (0x1 << ((i) & (DM_BITS_PER_INT - 1))))
2561
2562
#define dm_bit_clear(bs, i) \
2563
   ((bs)[((i) / DM_BITS_PER_INT) + 1] &= ~(0x1 << ((i) & (DM_BITS_PER_INT - 1))))
2564
2565
#define dm_bit_set_all(bs) \
2566
   memset((bs) + 1, -1, ((*(bs) / DM_BITS_PER_INT) + 1) * sizeof(int))
2567
2568
#define dm_bit_clear_all(bs) \
2569
   memset((bs) + 1, 0, ((*(bs) / DM_BITS_PER_INT) + 1) * sizeof(int))
2570
2571
#define dm_bit_copy(bs1, bs2) \
2572
   memcpy((bs1) + 1, (bs2) + 1, ((*(bs2) / DM_BITS_PER_INT) + 1) * sizeof(int))
2573
2574
/*
2575
 * Parse a string representation of a bitset into a dm_bitset_t. The
2576
 * notation used is identical to the kernel bitmap parser (cpuset etc.)
2577
 * and supports both lists ("1,2,3") and ranges ("1-2,5-8"). If the mem
2578
 * parameter is NULL memory for the bitset will be allocated using
2579
 * dm_malloc(). Otherwise the bitset will be allocated using the supplied
2580
 * dm_pool.
2581
 */
2582
dm_bitset_t dm_bitset_parse_list(const char *str, struct dm_pool *mem,
2583
         size_t min_num_bits);
2584
2585
/* Returns number of set bits */
2586
static inline unsigned hweight32(uint32_t i)
2587
0
{
2588
0
  unsigned r = (i & 0x55555555) + ((i >> 1) & 0x55555555);
2589
0
2590
0
  r =    (r & 0x33333333) + ((r >>  2) & 0x33333333);
2591
0
  r =    (r & 0x0F0F0F0F) + ((r >>  4) & 0x0F0F0F0F);
2592
0
  r =    (r & 0x00FF00FF) + ((r >>  8) & 0x00FF00FF);
2593
0
  return (r & 0x0000FFFF) + ((r >> 16) & 0x0000FFFF);
2594
0
}
2595
2596
/****************
2597
 * hash functions
2598
 ****************/
2599
2600
struct dm_hash_table;
2601
struct dm_hash_node;
2602
2603
typedef void (*dm_hash_iterate_fn) (void *data);
2604
2605
struct dm_hash_table *dm_hash_create(unsigned size_hint)
2606
  __attribute__((__warn_unused_result__));
2607
void dm_hash_destroy(struct dm_hash_table *t);
2608
void dm_hash_wipe(struct dm_hash_table *t);
2609
2610
void *dm_hash_lookup(struct dm_hash_table *t, const char *key);
2611
int dm_hash_insert(struct dm_hash_table *t, const char *key, void *data);
2612
void dm_hash_remove(struct dm_hash_table *t, const char *key);
2613
2614
void *dm_hash_lookup_binary(struct dm_hash_table *t, const void *key, uint32_t len);
2615
int dm_hash_insert_binary(struct dm_hash_table *t, const void *key, uint32_t len,
2616
        void *data);
2617
void dm_hash_remove_binary(struct dm_hash_table *t, const void *key, uint32_t len);
2618
2619
unsigned dm_hash_get_num_entries(struct dm_hash_table *t);
2620
void dm_hash_iter(struct dm_hash_table *t, dm_hash_iterate_fn f);
2621
2622
char *dm_hash_get_key(struct dm_hash_table *t, struct dm_hash_node *n);
2623
void *dm_hash_get_data(struct dm_hash_table *t, struct dm_hash_node *n);
2624
struct dm_hash_node *dm_hash_get_first(struct dm_hash_table *t);
2625
struct dm_hash_node *dm_hash_get_next(struct dm_hash_table *t, struct dm_hash_node *n);
2626
2627
/*
2628
 * dm_hash_insert() replaces the value of an existing
2629
 * entry with a matching key if one exists.  Otherwise
2630
 * it adds a new entry.
2631
 *
2632
 * dm_hash_insert_with_val() inserts a new entry if
2633
 * another entry with the same key already exists.
2634
 * val_len is the size of the data being inserted.
2635
 *
2636
 * If two entries with the same key exist,
2637
 * (added using dm_hash_insert_allow_multiple), then:
2638
 * . dm_hash_lookup() returns the first one it finds, and
2639
 *   dm_hash_lookup_with_val() returns the one with a matching
2640
 *   val_len/val.
2641
 * . dm_hash_remove() removes the first one it finds, and
2642
 *   dm_hash_remove_with_val() removes the one with a matching
2643
 *   val_len/val.
2644
 *
2645
 * If a single entry with a given key exists, and it has
2646
 * zero val_len, then:
2647
 * . dm_hash_lookup() returns it
2648
 * . dm_hash_lookup_with_val(val_len=0) returns it
2649
 * . dm_hash_remove() removes it
2650
 * . dm_hash_remove_with_val(val_len=0) removes it
2651
 *
2652
 * dm_hash_lookup_with_count() is a single call that will
2653
 * both lookup a key's value and check if there is more
2654
 * than one entry with the given key.
2655
 *
2656
 * (It is not meant to retrieve all the entries with the
2657
 * given key.  In the common case where a single entry exists
2658
 * for the key, it is useful to have a single call that will
2659
 * both look up the value and indicate if multiple values
2660
 * exist for the key.)
2661
 *
2662
 * dm_hash_lookup_with_count:
2663
 * . If no entries exist, the function returns NULL, and
2664
 *   the count is set to 0.
2665
 * . If only one entry exists, the value of that entry is
2666
 *   returned and count is set to 1.
2667
 * . If N entries exists, the value of the first entry is
2668
 *   returned and count is set to N.
2669
 */
2670
2671
void *dm_hash_lookup_with_val(struct dm_hash_table *t, const char *key,
2672
            const void *val, uint32_t val_len);
2673
void dm_hash_remove_with_val(struct dm_hash_table *t, const char *key,
2674
           const void *val, uint32_t val_len);
2675
int dm_hash_insert_allow_multiple(struct dm_hash_table *t, const char *key,
2676
          const void *val, uint32_t val_len);
2677
void *dm_hash_lookup_with_count(struct dm_hash_table *t, const char *key, int *count);
2678
2679
2680
#define dm_hash_iterate(v, h) \
2681
  for (v = dm_hash_get_first((h)); v; \
2682
       v = dm_hash_get_next((h), v))
2683
2684
/****************
2685
 * list functions
2686
 ****************/
2687
2688
/*
2689
 * A list consists of a list head plus elements.
2690
 * Each element has 'next' and 'previous' pointers.
2691
 * The list head's pointers point to the first and the last element.
2692
 */
2693
2694
struct dm_list {
2695
  struct dm_list *n, *p;
2696
};
2697
2698
/*
2699
 * String list.
2700
 */
2701
struct dm_str_list {
2702
  struct dm_list list;
2703
  const char *str;
2704
};
2705
2706
/*
2707
 * Active device element returned dm_task_get_device_list()
2708
 * Only for accessing structure members.
2709
 * Do NOT allocate this structure locally.
2710
 * More elements can be added later (with DM_DEVICE_LIST_HAS_FLAG).
2711
 */
2712
struct dm_active_device {
2713
  struct dm_list list;
2714
  dev_t devno;
2715
  const char *name; /* device name */
2716
2717
  uint32_t event_nr;  /* valid when DM_DEVICE_LIST_HAS_EVENT_NR is set */
2718
  const char *uuid; /* valid uuid when DM_DEVICE_LIST_HAS_UUID is set */
2719
};
2720
2721
/*
2722
 * Initialise a list before use.
2723
 * The list head's next and previous pointers point back to itself.
2724
 */
2725
#define DM_LIST_HEAD_INIT(name)  { &(name), &(name) }
2726
#define DM_LIST_INIT(name)  struct dm_list name = DM_LIST_HEAD_INIT(name)
2727
void dm_list_init(struct dm_list *head);
2728
2729
/*
2730
 * Insert an element before 'head'.
2731
 * If 'head' is the list head, this adds an element to the end of the list.
2732
 */
2733
void dm_list_add(struct dm_list *head, struct dm_list *elem);
2734
2735
/*
2736
 * Insert an element after 'head'.
2737
 * If 'head' is the list head, this adds an element to the front of the list.
2738
 */
2739
void dm_list_add_h(struct dm_list *head, struct dm_list *elem);
2740
2741
/*
2742
 * Delete an element from its list.
2743
 * Note that this doesn't change the element itself - it may still be safe
2744
 * to follow its pointers.
2745
 */
2746
void dm_list_del(struct dm_list *elem);
2747
2748
/*
2749
 * Remove an element from existing list and insert before 'head'.
2750
 */
2751
void dm_list_move(struct dm_list *head, struct dm_list *elem);
2752
2753
/*
2754
 * Join 'head1' to the end of 'head'.
2755
 */
2756
void dm_list_splice(struct dm_list *head, struct dm_list *head1);
2757
2758
/*
2759
 * Is the list empty?
2760
 */
2761
int dm_list_empty(const struct dm_list *head);
2762
2763
/*
2764
 * Is this the first element of the list?
2765
 */
2766
int dm_list_start(const struct dm_list *head, const struct dm_list *elem);
2767
2768
/*
2769
 * Is this the last element of the list?
2770
 */
2771
int dm_list_end(const struct dm_list *head, const struct dm_list *elem);
2772
2773
/*
2774
 * Return first element of the list or NULL if empty
2775
 */
2776
struct dm_list *dm_list_first(const struct dm_list *head);
2777
2778
/*
2779
 * Return last element of the list or NULL if empty
2780
 */
2781
struct dm_list *dm_list_last(const struct dm_list *head);
2782
2783
/*
2784
 * Return the previous element of the list, or NULL if we've reached the start.
2785
 */
2786
struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem);
2787
2788
/*
2789
 * Return the next element of the list, or NULL if we've reached the end.
2790
 */
2791
struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem);
2792
2793
/*
2794
 * Given the address v of an instance of 'struct dm_list' called 'head'
2795
 * contained in a structure of type t, return the containing structure.
2796
 */
2797
#define dm_list_struct_base(v, t, head) \
2798
    ((t *)((char *)(v) - offsetof(t, head)))
2799
2800
/*
2801
 * Given the address v of an instance of 'struct dm_list list' contained in
2802
 * a structure of type t, return the containing structure.
2803
 */
2804
#define dm_list_item(v, t) dm_list_struct_base((v), t, list)
2805
2806
/*
2807
 * Given the address v of one known element e in a known structure of type t,
2808
 * return another element f.
2809
 */
2810
#define dm_struct_field(v, t, e, f) \
2811
    (((t *)((uintptr_t)(v) - offsetof(t, e))->f)
2812
2813
/*
2814
 * Given the address v of a known element e in a known structure of type t,
2815
 * return the list head 'list'
2816
 */
2817
#define dm_list_head(v, t, e) dm_struct_field(v, t, e, list)
2818
2819
/*
2820
 * Set v to each element of a list in turn.
2821
 */
2822
#define dm_list_iterate(v, head) \
2823
  for (v = (head)->n; v != head; v = v->n)
2824
2825
/*
2826
 * Set v to each element in a list in turn, starting from the element
2827
 * in front of 'start'.
2828
 * You can use this to 'unwind' a list_iterate and back out actions on
2829
 * already-processed elements.
2830
 * If 'start' is 'head' it walks the list backwards.
2831
 */
2832
#define dm_list_uniterate(v, head, start) \
2833
  for (v = (start)->p; v != head; v = v->p)
2834
2835
/*
2836
 * A safe way to walk a list and delete and free some elements along
2837
 * the way.
2838
 * t must be defined as a temporary variable of the same type as v.
2839
 */
2840
#define dm_list_iterate_safe(v, t, head) \
2841
  for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
2842
2843
/*
2844
 * Walk a list, setting 'v' in turn to the containing structure of each item.
2845
 * The containing structure should be the same type as 'v'.
2846
 * The 'struct dm_list' variable within the containing structure is 'field'.
2847
 */
2848
#define dm_list_iterate_items_gen(v, head, field) \
2849
  for (v = dm_list_struct_base((head)->n, __typeof__(*v), field); \
2850
       &v->field != (head); \
2851
       v = dm_list_struct_base(v->field.n, __typeof__(*v), field))
2852
2853
/*
2854
 * Walk a list, setting 'v' in turn to the containing structure of each item.
2855
 * The containing structure should be the same type as 'v'.
2856
 * The list should be 'struct dm_list list' within the containing structure.
2857
 */
2858
#define dm_list_iterate_items(v, head) dm_list_iterate_items_gen(v, (head), list)
2859
2860
/*
2861
 * Walk a list, setting 'v' in turn to the containing structure of each item.
2862
 * The containing structure should be the same type as 'v'.
2863
 * The 'struct dm_list' variable within the containing structure is 'field'.
2864
 * t must be defined as a temporary variable of the same type as v.
2865
 */
2866
#define dm_list_iterate_items_gen_safe(v, t, head, field) \
2867
  for (v = dm_list_struct_base((head)->n, __typeof__(*v), field), \
2868
       t = dm_list_struct_base(v->field.n, __typeof__(*v), field); \
2869
       &v->field != (head); \
2870
       v = t, t = dm_list_struct_base(v->field.n, __typeof__(*v), field))
2871
/*
2872
 * Walk a list, setting 'v' in turn to the containing structure of each item.
2873
 * The containing structure should be the same type as 'v'.
2874
 * The list should be 'struct dm_list list' within the containing structure.
2875
 * t must be defined as a temporary variable of the same type as v.
2876
 */
2877
#define dm_list_iterate_items_safe(v, t, head) \
2878
  dm_list_iterate_items_gen_safe(v, t, (head), list)
2879
2880
/*
2881
 * Walk a list backwards, setting 'v' in turn to the containing structure
2882
 * of each item.
2883
 * The containing structure should be the same type as 'v'.
2884
 * The 'struct dm_list' variable within the containing structure is 'field'.
2885
 */
2886
#define dm_list_iterate_back_items_gen(v, head, field) \
2887
  for (v = dm_list_struct_base((head)->p, __typeof__(*v), field); \
2888
       &v->field != (head); \
2889
       v = dm_list_struct_base(v->field.p, __typeof__(*v), field))
2890
2891
/*
2892
 * Walk a list backwards, setting 'v' in turn to the containing structure
2893
 * of each item.
2894
 * The containing structure should be the same type as 'v'.
2895
 * The list should be 'struct dm_list list' within the containing structure.
2896
 */
2897
#define dm_list_iterate_back_items(v, head) dm_list_iterate_back_items_gen(v, (head), list)
2898
2899
/*
2900
 * Return the number of elements in a list by walking it.
2901
 */
2902
unsigned int dm_list_size(const struct dm_list *head);
2903
2904
/*
2905
 * Retrieve the list of devices and put them into easily accessible
2906
 * struct dm_active_device list elements.
2907
 * devs_features provides flag-set with used features so it's easy to check
2908
 * whether the kernel provides i.e. UUID info together with DM names
2909
 */
2910
#define DM_DEVICE_LIST_HAS_EVENT_NR 1
2911
#define DM_DEVICE_LIST_HAS_UUID   2
2912
int dm_task_get_device_list(struct dm_task *dmt, struct dm_list **devs_list,
2913
          unsigned *devs_features);
2914
/* Release all associated memory with list of active DM devices */
2915
void dm_device_list_destroy(struct dm_list **devs_list);
2916
/*
2917
 * Compare two dm_list structures containing dm_active_device elements.
2918
 * Returns 1 if both lists contain identical devices (same devno and uuid in same order),
2919
 * 0 if lists differ.
2920
 */
2921
int dm_device_list_equal(const struct dm_list *list1, const struct dm_list *list2);
2922
2923
/*********
2924
 * selinux
2925
 *********/
2926
2927
/*
2928
 * Obtain SELinux security context assigned for the path and set this
2929
 * context for creating a new file system object. This security context
2930
 * is global and it is used until reset to default policy behaviour
2931
 * by calling 'dm_prepare_selinux_context(NULL, 0)'.
2932
 */
2933
int dm_prepare_selinux_context(const char *path, mode_t mode);
2934
/*
2935
 * Set SELinux context for existing file system object.
2936
 */
2937
int dm_set_selinux_context(const char *path, mode_t mode);
2938
2939
/*********************
2940
 * string manipulation
2941
 *********************/
2942
2943
/*
2944
 * Break up the name of a mapped device into its constituent
2945
 * Volume Group, Logical Volume and Layer (if present).
2946
 * If mem is supplied, the result is allocated from the mempool.
2947
 * Otherwise the strings are changed in situ.
2948
 */
2949
int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,
2950
          char **vgname, char **lvname, char **layer);
2951
2952
/*
2953
 * Destructively split buffer into NULL-separated words in argv.
2954
 * Returns number of words.
2955
 */
2956
int dm_split_words(char *buffer, unsigned max,
2957
       unsigned ignore_comments, /* Not implemented */
2958
       char **argv);
2959
2960
/*
2961
 * Returns -1 if buffer too small
2962
 */
2963
int dm_snprintf(char *buf, size_t bufsize, const char *format, ...)
2964
    __attribute__ ((format(printf, 3, 4)));
2965
2966
/*
2967
 * Returns pointer to the last component of the path.
2968
 */
2969
const char *dm_basename(const char *path);
2970
2971
/*
2972
 * Returns number of occurrences of 'c' in 'str' of length 'size'.
2973
 */
2974
unsigned dm_count_chars(const char *str, size_t len, const int c);
2975
2976
/*
2977
 * Length of string after escaping double quotes and backslashes.
2978
 */
2979
size_t dm_escaped_len(const char *str);
2980
2981
/*
2982
 * <vg>-<lv>-<layer> or if !layer just <vg>-<lv>.
2983
 */
2984
char *dm_build_dm_name(struct dm_pool *mem, const char *vgname,
2985
           const char *lvname, const char *layer);
2986
char *dm_build_dm_uuid(struct dm_pool *mem, const char *prefix, const char *lvid, const char *layer);
2987
2988
/*
2989
 * Copies a string, quoting double quotes with backslashes.
2990
 */
2991
char *dm_escape_double_quotes(char *out, const char *src);
2992
2993
/*
2994
 * Undo quoting in situ.
2995
 */
2996
void dm_unescape_double_quotes(char *src);
2997
2998
/*
2999
 * Unescape colons and "at" signs in situ and save the substrings
3000
 * starting at the position of the first unescaped colon and the
3001
 * first unescaped "at" sign. This is normally used to unescape
3002
 * device names used as PVs.
3003
 */
3004
void dm_unescape_colons_and_at_signs(char *src,
3005
             char **substr_first_unquoted_colon,
3006
             char **substr_first_unquoted_at_sign);
3007
3008
/*
3009
 * Replacement for strncpy() function.
3010
 *
3011
 * Copies no more than n bytes from string pointed by src to the buffer
3012
 * pointed by dest and ensure string is finished with '\0'.
3013
 * Returns 0 if the whole string does not fit.
3014
 */
3015
int dm_strncpy(char *dest, const char *src, size_t n);
3016
3017
/*
3018
 * Recognize unit specifier in the 'units' arg and return a factor
3019
 * representing that unit. If the 'units' contains a prefix with digits,
3020
 * the 'units' is considered to be a custom unit.
3021
 *
3022
 * Also, set 'unit_type' output arg to the character that represents
3023
 * the unit specified. The 'unit_type' character equals to the unit
3024
 * character itself recognized in the 'units' arg for canonical units.
3025
 * Otherwise, the 'unit_type' character is set to 'U' for custom unit.
3026
 *
3027
 * An example for k/K canonical units and 8k/8K custom units:
3028
 *
3029
 *   units  unit_type  return value (factor)
3030
 *   k      k          1024
3031
 *   K      K          1000
3032
 *   8k     U          1024*8
3033
 *   8K     U          1000*8
3034
 *   etc...
3035
 *
3036
 * Recognized units:
3037
 *
3038
 *   h/H - human readable (returns 1 for both)
3039
 *   b/B - byte (returns 1 for both)
3040
 *   s/S - sector (returns 512 for both)
3041
 *   k/K - kilo (returns 1024/1000 respectively)
3042
 *   m/M - mega (returns 1024^2/1000^2 respectively)
3043
 *   g/G - giga (returns 1024^3/1000^3 respectively)
3044
 *   t/T - tera (returns 1024^4/1000^4 respectively)
3045
 *   p/P - peta (returns 1024^5/1000^5 respectively)
3046
 *   e/E - exa (returns 1024^6/1000^6 respectively)
3047
 *
3048
 * Only one units character is allowed in the 'units' arg
3049
 * if strict mode is enabled by 'strict' arg.
3050
 *
3051
 * The 'endptr' output arg, if not NULL, saves the pointer
3052
 * in the 'units' string which follows the unit specifier
3053
 * recognized (IOW the position where the parsing of the
3054
 * unit specifier stopped).
3055
 *
3056
 * Returns the unit factor or 0 if no unit is recognized.
3057
 */
3058
uint64_t dm_units_to_factor(const char *units, char *unit_type,
3059
          int strict, const char **endptr);
3060
3061
/*
3062
 * Type of unit specifier used by dm_size_to_string().
3063
 */
3064
typedef enum dm_size_suffix_e {
3065
  DM_SIZE_LONG = 0, /* Megabyte */
3066
  DM_SIZE_SHORT = 1,  /* MB or MiB */
3067
  DM_SIZE_UNIT = 2  /* M or m */
3068
} dm_size_suffix_t;
3069
3070
/*
3071
 * Convert a size (in 512-byte sectors) into a printable string using units of unit_type.
3072
 * An upper-case unit_type indicates output units based on powers of 1000 are
3073
 * required; a lower-case unit_type indicates powers of 1024.
3074
 * For correct operation, unit_factor must be one of:
3075
 *  0 - the correct value will be calculated internally;
3076
 *   or the output from dm_units_to_factor() corresponding to unit_type;
3077
 *   or 'u' or 'U', an arbitrary number of bytes to use as the power base.
3078
 * Set include_suffix to 1 to include a suffix of suffix_type.
3079
 * Set use_si_units to 0 for suffixes that don't distinguish between 1000 and 1024.
3080
 * Set use_si_units to 1 for a suffix that does distinguish.
3081
 */
3082
const char *dm_size_to_string(struct dm_pool *mem, uint64_t size,
3083
            char unit_type, int use_si_units,
3084
            uint64_t unit_factor, int include_suffix,
3085
            dm_size_suffix_t suffix_type);
3086
3087
/**************************
3088
 * file/stream manipulation
3089
 **************************/
3090
3091
/*
3092
 * Create a directory (with parent directories if necessary).
3093
 * Returns 1 on success, 0 on failure.
3094
 */
3095
int dm_create_dir(const char *dir);
3096
3097
int dm_is_empty_dir(const char *dir);
3098
3099
/*
3100
 * Close a stream, with nicer error checking than fclose's.
3101
 * Derived from gnulib's close-stream.c.
3102
 *
3103
 * Close "stream".  Return 0 if successful, and EOF (setting errno)
3104
 * otherwise.  Upon failure, set errno to 0 if the error number
3105
 * cannot be determined.  Useful mainly for writable streams.
3106
 */
3107
int dm_fclose(FILE *stream);
3108
3109
/*
3110
 * Returns size of a buffer which is allocated with dm_malloc.
3111
 * Pointer to the buffer is stored in *buf.
3112
 * Returns -1 on failure leaving buf undefined.
3113
 */
3114
int dm_asprintf(char **result, const char *format, ...)
3115
    __attribute__ ((format(printf, 2, 3)));
3116
int dm_vasprintf(char **result, const char *format, va_list aq)
3117
    __attribute__ ((format(printf, 2, 0)));
3118
3119
/*
3120
 * create lockfile (pidfile) - create and lock a lock file
3121
 * @lockfile: location of lock file
3122
 *
3123
 * Returns: 1 on success, 0 otherwise, errno is handled internally
3124
 */
3125
int dm_create_lockfile(const char* lockfile);
3126
3127
/*
3128
 * Query whether a daemon is running based on its lockfile
3129
 *
3130
 * Returns: 1 if running, 0 if not
3131
 */
3132
int dm_daemon_is_running(const char* lockfile);
3133
3134
/*********************
3135
 * regular expressions
3136
 *********************/
3137
struct dm_regex;
3138
3139
/*
3140
 * Initialise an array of num patterns for matching.
3141
 * Uses memory from mem.
3142
 */
3143
struct dm_regex *dm_regex_create(struct dm_pool *mem, const char * const *patterns,
3144
         unsigned num_patterns);
3145
3146
/*
3147
 * Match string s against the patterns.
3148
 * Returns the index of the highest pattern in the array that matches,
3149
 * or -1 if none match.
3150
 */
3151
int dm_regex_match(struct dm_regex *regex, const char *s);
3152
3153
/*
3154
 * This is useful for regression testing only.  The idea is if two
3155
 * fingerprints are different, then the two dfas are certainly not
3156
 * isomorphic.  If two fingerprints _are_ the same then it's very likely
3157
 * that the dfas are isomorphic.
3158
 *
3159
 * This function must be called before any matching is done.
3160
 */
3161
uint32_t dm_regex_fingerprint(struct dm_regex *regex);
3162
3163
/******************
3164
 * percent handling
3165
 ******************/
3166
/*
3167
 * A fixed-point representation of percent values. One percent equals to
3168
 * DM_PERCENT_1 as defined below. Values that are not multiples of DM_PERCENT_1
3169
 * represent fractions, with precision of 1/1000000 of a percent. See
3170
 * dm_percent_to_float for a conversion to a floating-point representation.
3171
 *
3172
 * You should always use dm_make_percent when building dm_percent_t values. The
3173
 * implementation of dm_make_percent is biased towards the middle: it ensures that
3174
 * the result is DM_PERCENT_0 or DM_PERCENT_100 if and only if this is the actual
3175
 * value -- it never rounds any intermediate value (> 0 or < 100) to either 0
3176
 * or 100.
3177
*/
3178
#define DM_PERCENT_CHAR '%'
3179
3180
typedef enum dm_percent_range_e {
3181
  DM_PERCENT_0 = 0,
3182
  DM_PERCENT_1 = 1000000,
3183
  DM_PERCENT_100 = 100 * DM_PERCENT_1,
3184
  DM_PERCENT_INVALID = -1,
3185
  DM_PERCENT_FAILED = -2
3186
} dm_percent_range_t;
3187
3188
typedef int32_t dm_percent_t;
3189
3190
float dm_percent_to_float(dm_percent_t percent);
3191
/*
3192
 * Return adjusted/rounded float for better percent value printing.
3193
 * Function ensures for given precision of digits:
3194
 * 100.0% returns only when the value is DM_PERCENT_100
3195
 *        for close smaller values rounds to nearest smaller value
3196
 * 0.0% returns only for value DM_PERCENT_0
3197
 *        for close bigger values rounds to nearest bigger value
3198
 * In all other cases returns same value as dm_percent_to_float()
3199
 */
3200
float dm_percent_to_round_float(dm_percent_t percent, unsigned digits);
3201
dm_percent_t dm_make_percent(uint64_t numerator, uint64_t denominator);
3202
3203
/********************
3204
 * timestamp handling
3205
 ********************/
3206
3207
/*
3208
 * Create a dm_timestamp object to use with dm_timestamp_get.
3209
 */
3210
struct dm_timestamp *dm_timestamp_alloc(void);
3211
3212
/*
3213
 * Update dm_timestamp object to represent the current time.
3214
 */
3215
int dm_timestamp_get(struct dm_timestamp *ts);
3216
3217
/*
3218
 * Copy a timestamp from ts_old to ts_new.
3219
 */
3220
void dm_timestamp_copy(struct dm_timestamp *ts_new, struct dm_timestamp *ts_old);
3221
3222
/*
3223
 * Compare two timestamps.
3224
 *
3225
 * Return: -1 if ts1 is less than ts2
3226
 *        0 if ts1 is equal to ts2
3227
 *          1 if ts1 is greater than ts2
3228
 */
3229
int dm_timestamp_compare(struct dm_timestamp *ts1, struct dm_timestamp *ts2);
3230
3231
/*
3232
 * Return the absolute difference in nanoseconds between
3233
 * the dm_timestamp objects ts1 and ts2.
3234
 *
3235
 * Callers that need to know whether ts1 is before, equal to, or after ts2
3236
 * in addition to the magnitude should use dm_timestamp_compare.
3237
 */
3238
uint64_t dm_timestamp_delta(struct dm_timestamp *ts1, struct dm_timestamp *ts2);
3239
3240
/*
3241
 * Destroy a dm_timestamp object.
3242
 */
3243
void dm_timestamp_destroy(struct dm_timestamp *ts);
3244
3245
/*********************
3246
 * reporting functions
3247
 *********************/
3248
3249
struct dm_report_object_type {
3250
  uint32_t id;      /* Powers of 2 */
3251
  const char *desc;
3252
  const char *prefix;   /* field id string prefix (optional) */
3253
  /* FIXME: convert to proper usage of const pointers here */
3254
  void *(*data_fn)(void *object); /* callback from report_object() */
3255
};
3256
3257
struct dm_report_field;
3258
3259
/*
3260
 * dm_report_field_type flags
3261
 */
3262
#define DM_REPORT_FIELD_MASK        0x00000FFF
3263
#define DM_REPORT_FIELD_ALIGN_MASK      0x0000000F
3264
#define DM_REPORT_FIELD_ALIGN_LEFT      0x00000001
3265
#define DM_REPORT_FIELD_ALIGN_RIGHT     0x00000002
3266
#define DM_REPORT_FIELD_TYPE_MASK     0x00000FF0
3267
#define DM_REPORT_FIELD_TYPE_NONE     0x00000000
3268
#define DM_REPORT_FIELD_TYPE_STRING     0x00000010
3269
#define DM_REPORT_FIELD_TYPE_NUMBER     0x00000020
3270
#define DM_REPORT_FIELD_TYPE_SIZE     0x00000040
3271
#define DM_REPORT_FIELD_TYPE_PERCENT      0x00000080
3272
#define DM_REPORT_FIELD_TYPE_STRING_LIST    0x00000100
3273
#define DM_REPORT_FIELD_TYPE_TIME     0x00000200
3274
3275
/* For use with reserved values only! */
3276
#define DM_REPORT_FIELD_RESERVED_VALUE_MASK   0x0000000F
3277
#define DM_REPORT_FIELD_RESERVED_VALUE_NAMED    0x00000001 /* only named value, less strict form of reservation */
3278
#define DM_REPORT_FIELD_RESERVED_VALUE_RANGE    0x00000002 /* value is range - low and high value defined */
3279
#define DM_REPORT_FIELD_RESERVED_VALUE_DYNAMIC_VALUE  0x00000004 /* value is computed in runtime */
3280
#define DM_REPORT_FIELD_RESERVED_VALUE_FUZZY_NAMES  0x00000008 /* value names are recognized in runtime */
3281
3282
#define DM_REPORT_FIELD_TYPE_ID_LEN 32
3283
#define DM_REPORT_FIELD_TYPE_HEADING_LEN 32
3284
3285
struct dm_report;
3286
struct dm_report_field_type {
3287
  uint32_t type;    /* object type id */
3288
  uint32_t flags;   /* DM_REPORT_FIELD_* */
3289
  uint32_t offset;  /* byte offset in the object */
3290
  int32_t width;    /* default width */
3291
  /* string used to specify the field */
3292
  const char id[DM_REPORT_FIELD_TYPE_ID_LEN];
3293
  /* string printed in header */
3294
  const char heading[DM_REPORT_FIELD_TYPE_HEADING_LEN];
3295
  int (*report_fn)(struct dm_report *rh, struct dm_pool *mem,
3296
       struct dm_report_field *field, const void *data,
3297
       void *private_data);
3298
  const char *desc; /* description of the field */
3299
};
3300
3301
/*
3302
 * Per-field reserved value.
3303
 */
3304
struct dm_report_field_reserved_value {
3305
  /* field_num is the position of the field in 'fields'
3306
     array passed to dm_report_init_with_selection */
3307
  uint32_t field_num;
3308
  /* the value is of the same type as the field
3309
     identified by field_num */
3310
  const void *value;
3311
};
3312
3313
/*
3314
 * Reserved value is a 'value' that is used directly if any of the 'names' is hit
3315
 * or in case of fuzzy names, if such fuzzy name matches.
3316
 *
3317
 * If type is any of DM_REPORT_FIELD_TYPE_*, the reserved value is recognized
3318
 * for all fields of that type.
3319
 *
3320
 * If type is DM_REPORT_FIELD_TYPE_NONE, the reserved value is recognized
3321
 * for the exact field specified - hence the type of the value is automatically
3322
 * the same as the type of the field itself.
3323
 *
3324
 * The array of reserved values is used to initialize reporting with
3325
 * selection enabled (see also dm_report_init_with_selection function).
3326
 */
3327
struct dm_report_reserved_value {
3328
  const uint32_t type;    /* DM_REPORT_FIELD_RESERVED_VALUE_* and DM_REPORT_FIELD_TYPE_*  */
3329
  const void *value;    /* reserved value:
3330
            uint64_t for DM_REPORT_FIELD_TYPE_NUMBER
3331
            uint64_t for DM_REPORT_FIELD_TYPE_SIZE (number of 512-byte sectors)
3332
            uint64_t for DM_REPORT_FIELD_TYPE_PERCENT
3333
            const char* for DM_REPORT_FIELD_TYPE_STRING
3334
            struct dm_report_field_reserved_value for DM_REPORT_FIELD_TYPE_NONE
3335
            dm_report_reserved_handler* if DM_REPORT_FIELD_RESERVED_VALUE_{DYNAMIC_VALUE,FUZZY_NAMES} is used */
3336
  const char **names;   /* null-terminated array of static names for this reserved value */
3337
  const char *description;  /* description of the reserved value */
3338
};
3339
3340
/*
3341
 * Available actions for dm_report_reserved_value_handler.
3342
 */
3343
typedef enum dm_report_reserved_action_e {
3344
  DM_REPORT_RESERVED_PARSE_FUZZY_NAME,
3345
  DM_REPORT_RESERVED_GET_DYNAMIC_VALUE,
3346
} dm_report_reserved_action_t;
3347
3348
/*
3349
 * Generic reserved value handler to process reserved value names and/or values.
3350
 *
3351
 * Actions and their input/output:
3352
 *
3353
 *  DM_REPORT_RESERVED_PARSE_FUZZY_NAME
3354
 *    data_in:  const char *fuzzy_name
3355
 *    data_out: const char *canonical_name, NULL if fuzzy_name not recognized
3356
 *
3357
 *  DM_REPORT_RESERVED_GET_DYNAMIC_VALUE
3358
 *    data_in:  const char *canonical_name
3359
 *    data_out: void *value, NULL if canonical_name not recognized
3360
 *
3361
 * All actions return:
3362
 *
3363
 *  -1 if action not implemented
3364
 *  0 on error
3365
 *  1 on success
3366
 */
3367
typedef int (*dm_report_reserved_handler) (struct dm_report *rh,
3368
             struct dm_pool *mem,
3369
             uint32_t field_num,
3370
             dm_report_reserved_action_t action,
3371
             const void *data_in,
3372
             const void **data_out);
3373
3374
/*
3375
 * The dm_report_value_cache_{set,get} are helper functions to store and retrieve
3376
 * various values used during reporting (dm_report_field_type.report_fn) and/or
3377
 * selection processing (dm_report_reserved_handler instances) to avoid
3378
 * recalculation of these values or to share values among calls.
3379
 */
3380
int dm_report_value_cache_set(struct dm_report *rh, const char *name, const void *data);
3381
const void *dm_report_value_cache_get(struct dm_report *rh, const char *name);
3382
/*
3383
 * dm_report_init output_flags
3384
 */
3385
#define DM_REPORT_OUTPUT_MASK     0x000000FF
3386
#define DM_REPORT_OUTPUT_ALIGNED    0x00000001
3387
#define DM_REPORT_OUTPUT_BUFFERED   0x00000002
3388
#define DM_REPORT_OUTPUT_HEADINGS   0x00000004
3389
#define DM_REPORT_OUTPUT_FIELD_NAME_PREFIX  0x00000008
3390
#define DM_REPORT_OUTPUT_FIELD_UNQUOTED   0x00000010
3391
#define DM_REPORT_OUTPUT_COLUMNS_AS_ROWS  0x00000020
3392
#define DM_REPORT_OUTPUT_MULTIPLE_TIMES   0x00000040
3393
#define DM_REPORT_OUTPUT_FIELD_IDS_IN_HEADINGS  0x00000080
3394
3395
struct dm_report *dm_report_init(uint32_t *report_types,
3396
         const struct dm_report_object_type *types,
3397
         const struct dm_report_field_type *fields,
3398
         const char *output_fields,
3399
         const char *output_separator,
3400
         uint32_t output_flags,
3401
         const char *sort_keys,
3402
         void *private_data);
3403
struct dm_report *dm_report_init_with_selection(uint32_t *report_types,
3404
            const struct dm_report_object_type *types,
3405
            const struct dm_report_field_type *fields,
3406
            const char *output_fields,
3407
            const char *output_separator,
3408
            uint32_t output_flags,
3409
            const char *sort_keys,
3410
            const char *selection,
3411
            const struct dm_report_reserved_value reserved_values[],
3412
            void *private_data);
3413
/*
3414
 * Report an object, pass it through the selection criteria if they
3415
 * are present and display the result on output if it passes the criteria.
3416
 */
3417
int dm_report_object(struct dm_report *rh, void *object);
3418
/*
3419
 * The same as dm_report_object, but display the result on output only if
3420
 * 'do_output' arg is set. Also, save the result of selection in 'selected'
3421
 * arg if it's not NULL (either 1 if the object passes, otherwise 0).
3422
 */
3423
int dm_report_object_is_selected(struct dm_report *rh, void *object, int do_output, int *selected);
3424
3425
/*
3426
 * Compact report output so that if field value is empty for all rows in
3427
 * the report, drop the field from output completely (including headers).
3428
 * Compact output is applicable only if report is buffered, otherwise
3429
 * this function has no effect.
3430
 */
3431
int dm_report_compact_fields(struct dm_report *rh);
3432
3433
/*
3434
 * The same as dm_report_compact_fields, but for selected fields only.
3435
 * The "fields" arg is comma separated list of field names (the same format
3436
 * as used for "output_fields" arg in dm_report_init fn).
3437
 */
3438
int dm_report_compact_given_fields(struct dm_report *rh, const char *fields);
3439
3440
/*
3441
 * Returns 1 if there is no data waiting to be output.
3442
 */
3443
int dm_report_is_empty(struct dm_report *rh);
3444
3445
/*
3446
 * Destroy report content without doing output.
3447
 */
3448
void dm_report_destroy_rows(struct dm_report *rh);
3449
3450
int dm_report_output(struct dm_report *rh);
3451
3452
/*
3453
 * Output the report headings for a columns-based report, even if they
3454
 * have already been shown. Useful for repeating reports that wish to
3455
 * issue a periodic reminder of the column headings.
3456
 */
3457
int dm_report_column_headings(struct dm_report *rh);
3458
3459
void dm_report_free(struct dm_report *rh);
3460
3461
/*
3462
 * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
3463
 */
3464
int dm_report_set_output_field_name_prefix(struct dm_report *rh,
3465
             const char *output_field_name_prefix);
3466
3467
int dm_report_set_selection(struct dm_report *rh, const char *selection);
3468
3469
/*
3470
 * Report functions are provided for simple data types.
3471
 * They take care of allocating copies of the data.
3472
 */
3473
int dm_report_field_string(struct dm_report *rh, struct dm_report_field *field,
3474
         const char *const *data);
3475
int dm_report_field_string_list(struct dm_report *rh, struct dm_report_field *field,
3476
        const struct dm_list *data, const char *delimiter);
3477
int dm_report_field_string_list_unsorted(struct dm_report *rh, struct dm_report_field *field,
3478
           const struct dm_list *data, const char *delimiter);
3479
int dm_report_field_int32(struct dm_report *rh, struct dm_report_field *field,
3480
        const int32_t *data);
3481
int dm_report_field_uint32(struct dm_report *rh, struct dm_report_field *field,
3482
         const uint32_t *data);
3483
int dm_report_field_int(struct dm_report *rh, struct dm_report_field *field,
3484
      const int *data);
3485
int dm_report_field_uint64(struct dm_report *rh, struct dm_report_field *field,
3486
         const uint64_t *data);
3487
int dm_report_field_percent(struct dm_report *rh, struct dm_report_field *field,
3488
          const dm_percent_t *data);
3489
3490
/*
3491
 * For custom fields, allocate the data in 'mem' and use
3492
 * dm_report_field_set_value().
3493
 * 'sortvalue' may be NULL if it matches 'value'
3494
 */
3495
void dm_report_field_set_value(struct dm_report_field *field, const void *value,
3496
             const void *sortvalue);
3497
3498
/*
3499
 * Report group support.
3500
 */
3501
struct dm_report_group;
3502
3503
typedef enum dm_report_group_type_e {
3504
  DM_REPORT_GROUP_SINGLE,
3505
  DM_REPORT_GROUP_BASIC,
3506
  DM_REPORT_GROUP_JSON,
3507
  DM_REPORT_GROUP_JSON_STD
3508
} dm_report_group_type_t;
3509
3510
struct dm_report_group *dm_report_group_create(dm_report_group_type_t type, void *data);
3511
int dm_report_group_push(struct dm_report_group *group, struct dm_report *report, void *data);
3512
int dm_report_group_pop(struct dm_report_group *group);
3513
int dm_report_group_output_and_pop_all(struct dm_report_group *group);
3514
int dm_report_group_destroy(struct dm_report_group *group);
3515
3516
/*
3517
 * Stats counter access methods
3518
 *
3519
 * Each method returns the corresponding stats counter value from the
3520
 * supplied dm_stats handle for the specified region_id and area_id.
3521
 * If either region_id or area_id uses one of the special values
3522
 * DM_STATS_REGION_CURRENT or DM_STATS_AREA_CURRENT then the region
3523
 * or area is selected according to the current state of the dm_stats
3524
 * handle's embedded cursor.
3525
 *
3526
 * Two methods are provided to access counter values: a named function
3527
 * for each available counter field and a single function that accepts
3528
 * an enum value specifying the required field. New code is encouraged
3529
 * to use the enum based interface as calls to the named functions are
3530
 * implemented using the enum method internally.
3531
 *
3532
 * See the kernel documentation for complete descriptions of each
3533
 * counter field:
3534
 *
3535
 * Documentation/device-mapper/statistics.txt
3536
 * Documentation/iostats.txt
3537
 *
3538
 * reads: the number of reads completed
3539
 * reads_merged: the number of reads merged
3540
 * read_sectors: the number of sectors read
3541
 * read_nsecs: the number of nanoseconds spent reading
3542
 * writes: the number of writes completed
3543
 * writes_merged: the number of writes merged
3544
 * write_sectors: the number of sectors written
3545
 * write_nsecs: the number of nanoseconds spent writing
3546
 * io_in_progress: the number of I/Os currently in progress
3547
 * io_nsecs: the number of nanoseconds spent doing I/Os
3548
 * weighted_io_nsecs: the weighted number of nanoseconds spent doing I/Os
3549
 * total_read_nsecs: the total time spent reading in nanoseconds
3550
 * total_write_nsecs: the total time spent writing in nanoseconds
3551
 */
3552
3553
#define DM_STATS_REGION_CURRENT UINT64_MAX
3554
#define DM_STATS_AREA_CURRENT UINT64_MAX
3555
3556
typedef enum dm_stats_counter_e {
3557
  DM_STATS_READS_COUNT,
3558
  DM_STATS_READS_MERGED_COUNT,
3559
  DM_STATS_READ_SECTORS_COUNT,
3560
  DM_STATS_READ_NSECS,
3561
  DM_STATS_WRITES_COUNT,
3562
  DM_STATS_WRITES_MERGED_COUNT,
3563
  DM_STATS_WRITE_SECTORS_COUNT,
3564
  DM_STATS_WRITE_NSECS,
3565
  DM_STATS_IO_IN_PROGRESS_COUNT,
3566
  DM_STATS_IO_NSECS,
3567
  DM_STATS_WEIGHTED_IO_NSECS,
3568
  DM_STATS_TOTAL_READ_NSECS,
3569
  DM_STATS_TOTAL_WRITE_NSECS,
3570
  DM_STATS_NR_COUNTERS
3571
} dm_stats_counter_t;
3572
3573
uint64_t dm_stats_get_counter(const struct dm_stats *dms,
3574
            dm_stats_counter_t counter,
3575
            uint64_t region_id, uint64_t area_id);
3576
3577
uint64_t dm_stats_get_reads(const struct dm_stats *dms,
3578
          uint64_t region_id, uint64_t area_id);
3579
3580
uint64_t dm_stats_get_reads_merged(const struct dm_stats *dms,
3581
           uint64_t region_id, uint64_t area_id);
3582
3583
uint64_t dm_stats_get_read_sectors(const struct dm_stats *dms,
3584
           uint64_t region_id, uint64_t area_id);
3585
3586
uint64_t dm_stats_get_read_nsecs(const struct dm_stats *dms,
3587
         uint64_t region_id, uint64_t area_id);
3588
3589
uint64_t dm_stats_get_writes(const struct dm_stats *dms,
3590
           uint64_t region_id, uint64_t area_id);
3591
3592
uint64_t dm_stats_get_writes_merged(const struct dm_stats *dms,
3593
            uint64_t region_id, uint64_t area_id);
3594
3595
uint64_t dm_stats_get_write_sectors(const struct dm_stats *dms,
3596
            uint64_t region_id, uint64_t area_id);
3597
3598
uint64_t dm_stats_get_write_nsecs(const struct dm_stats *dms,
3599
          uint64_t region_id, uint64_t area_id);
3600
3601
uint64_t dm_stats_get_io_in_progress(const struct dm_stats *dms,
3602
             uint64_t region_id, uint64_t area_id);
3603
3604
uint64_t dm_stats_get_io_nsecs(const struct dm_stats *dms,
3605
             uint64_t region_id, uint64_t area_id);
3606
3607
uint64_t dm_stats_get_weighted_io_nsecs(const struct dm_stats *dms,
3608
          uint64_t region_id, uint64_t area_id);
3609
3610
uint64_t dm_stats_get_total_read_nsecs(const struct dm_stats *dms,
3611
               uint64_t region_id, uint64_t area_id);
3612
3613
uint64_t dm_stats_get_total_write_nsecs(const struct dm_stats *dms,
3614
          uint64_t region_id, uint64_t area_id);
3615
3616
/*
3617
 * Derived statistics access methods
3618
 *
3619
 * Each method returns the corresponding value calculated from the
3620
 * counters stored in the supplied dm_stats handle for the specified
3621
 * region_id and area_id. If either region_id or area_id uses one of the
3622
 * special values DM_STATS_REGION_CURRENT or DM_STATS_AREA_CURRENT then
3623
 * the region or area is selected according to the current state of the
3624
 * dm_stats handle's embedded cursor.
3625
 *
3626
 * The set of metrics is based on the fields provided by the Linux
3627
 * iostats program.
3628
 *
3629
 * rd_merges_per_sec: the number of reads merged per second
3630
 * wr_merges_per_sec: the number of writes merged per second
3631
 * reads_per_sec: the number of reads completed per second
3632
 * writes_per_sec: the number of writes completed per second
3633
 * read_sectors_per_sec: the number of sectors read per second
3634
 * write_sectors_per_sec: the number of sectors written per second
3635
 * average_request_size: the average size of requests submitted
3636
 * service_time: the average service time (in ns) for requests issued
3637
 * average_queue_size: the average queue length
3638
 * average_wait_time: the average time for requests to be served (in ns)
3639
 * average_rd_wait_time: the average read wait time
3640
 * average_wr_wait_time: the average write wait time
3641
 */
3642
3643
typedef enum dm_stats_metric_e {
3644
  DM_STATS_RD_MERGES_PER_SEC,
3645
  DM_STATS_WR_MERGES_PER_SEC,
3646
  DM_STATS_READS_PER_SEC,
3647
  DM_STATS_WRITES_PER_SEC,
3648
  DM_STATS_READ_SECTORS_PER_SEC,
3649
  DM_STATS_WRITE_SECTORS_PER_SEC,
3650
  DM_STATS_AVERAGE_REQUEST_SIZE,
3651
  DM_STATS_AVERAGE_QUEUE_SIZE,
3652
  DM_STATS_AVERAGE_WAIT_TIME,
3653
  DM_STATS_AVERAGE_RD_WAIT_TIME,
3654
  DM_STATS_AVERAGE_WR_WAIT_TIME,
3655
  DM_STATS_SERVICE_TIME,
3656
  DM_STATS_THROUGHPUT,
3657
  DM_STATS_UTILIZATION,
3658
  DM_STATS_NR_METRICS
3659
} dm_stats_metric_t;
3660
3661
int dm_stats_get_metric(const struct dm_stats *dms, int metric,
3662
      uint64_t region_id, uint64_t area_id, double *value);
3663
3664
int dm_stats_get_rd_merges_per_sec(const struct dm_stats *dms, double *rrqm,
3665
           uint64_t region_id, uint64_t area_id);
3666
3667
int dm_stats_get_wr_merges_per_sec(const struct dm_stats *dms, double *wrqm,
3668
           uint64_t region_id, uint64_t area_id);
3669
3670
int dm_stats_get_reads_per_sec(const struct dm_stats *dms, double *rd_s,
3671
             uint64_t region_id, uint64_t area_id);
3672
3673
int dm_stats_get_writes_per_sec(const struct dm_stats *dms, double *wr_s,
3674
        uint64_t region_id, uint64_t area_id);
3675
3676
int dm_stats_get_read_sectors_per_sec(const struct dm_stats *dms,
3677
              double *rsec_s, uint64_t region_id,
3678
              uint64_t area_id);
3679
3680
int dm_stats_get_write_sectors_per_sec(const struct dm_stats *dms,
3681
               double *wsec_s, uint64_t region_id,
3682
               uint64_t area_id);
3683
3684
int dm_stats_get_average_request_size(const struct dm_stats *dms,
3685
              double *arqsz, uint64_t region_id,
3686
              uint64_t area_id);
3687
3688
int dm_stats_get_service_time(const struct dm_stats *dms, double *svctm,
3689
            uint64_t region_id, uint64_t area_id);
3690
3691
int dm_stats_get_average_queue_size(const struct dm_stats *dms, double *qusz,
3692
            uint64_t region_id, uint64_t area_id);
3693
3694
int dm_stats_get_average_wait_time(const struct dm_stats *dms, double *await,
3695
           uint64_t region_id, uint64_t area_id);
3696
3697
int dm_stats_get_average_rd_wait_time(const struct dm_stats *dms,
3698
              double *await, uint64_t region_id,
3699
              uint64_t area_id);
3700
3701
int dm_stats_get_average_wr_wait_time(const struct dm_stats *dms,
3702
              double *await, uint64_t region_id,
3703
              uint64_t area_id);
3704
3705
int dm_stats_get_throughput(const struct dm_stats *dms, double *tput,
3706
          uint64_t region_id, uint64_t area_id);
3707
3708
int dm_stats_get_utilization(const struct dm_stats *dms, dm_percent_t *util,
3709
           uint64_t region_id, uint64_t area_id);
3710
3711
/*
3712
 * Statistics histogram access methods.
3713
 *
3714
 * Methods to access latency histograms for regions that have them
3715
 * enabled. Each histogram contains a configurable number of bins
3716
 * spanning a user defined latency interval.
3717
 *
3718
 * The bin count, upper and lower bin bounds, and bin values are
3719
 * made available via the following area methods.
3720
 *
3721
 * Methods to obtain a simple string representation of the histogram
3722
 * and its bounds are also provided.
3723
 */
3724
3725
/*
3726
 * Retrieve a pointer to the histogram associated with the specified
3727
 * area. If the area does not have a histogram configured this function
3728
 * returns NULL.
3729
 *
3730
 * The pointer does not need to be freed explicitly by the caller: it
3731
 * will become invalid following a subsequent dm_stats_list(),
3732
 * dm_stats_populate() or dm_stats_destroy() of the corresponding
3733
 * dm_stats handle.
3734
 *
3735
 * If region_id or area_id is one of the special values
3736
 * DM_STATS_REGION_CURRENT or DM_STATS_AREA_CURRENT the current cursor
3737
 * value is used to select the region or area.
3738
 */
3739
struct dm_histogram *dm_stats_get_histogram(const struct dm_stats *dms,
3740
              uint64_t region_id,
3741
              uint64_t area_id);
3742
3743
/*
3744
 * Return the number of bins in the specified histogram handle.
3745
 */
3746
int dm_histogram_get_nr_bins(const struct dm_histogram *dmh);
3747
3748
/*
3749
 * Get the lower bound of the specified bin of the histogram for the
3750
 * area specified by region_id and area_id. The value is returned in
3751
 * nanoseconds.
3752
 */
3753
uint64_t dm_histogram_get_bin_lower(const struct dm_histogram *dmh, int bin);
3754
3755
/*
3756
 * Get the upper bound of the specified bin of the histogram for the
3757
 * area specified by region_id and area_id. The value is returned in
3758
 * nanoseconds.
3759
 */
3760
uint64_t dm_histogram_get_bin_upper(const struct dm_histogram *dmh, int bin);
3761
3762
/*
3763
 * Get the width of the specified bin of the histogram for the area
3764
 * specified by region_id and area_id. The width is equal to the bin
3765
 * upper bound minus the lower bound and yields the range of latency
3766
 * values covered by this bin. The value is returned in nanoseconds.
3767
 */
3768
uint64_t dm_histogram_get_bin_width(const struct dm_histogram *dmh, int bin);
3769
3770
/*
3771
 * Get the value of the specified bin of the histogram for the area
3772
 * specified by region_id and area_id.
3773
 */
3774
uint64_t dm_histogram_get_bin_count(const struct dm_histogram *dmh, int bin);
3775
3776
/*
3777
 * Get the percentage (relative frequency) of the specified bin of the
3778
 * histogram for the area specified by region_id and area_id.
3779
 */
3780
dm_percent_t dm_histogram_get_bin_percent(const struct dm_histogram *dmh,
3781
            int bin);
3782
3783
/*
3784
 * Return the total observations (sum of bin counts) for the histogram
3785
 * of the area specified by region_id and area_id.
3786
 */
3787
uint64_t dm_histogram_get_sum(const struct dm_histogram *dmh);
3788
3789
/*
3790
 * Histogram formatting flags.
3791
 */
3792
#define DM_HISTOGRAM_SUFFIX  0x1
3793
#define DM_HISTOGRAM_VALUES  0x2
3794
#define DM_HISTOGRAM_PERCENT 0X4
3795
#define DM_HISTOGRAM_BOUNDS_LOWER 0x10
3796
#define DM_HISTOGRAM_BOUNDS_UPPER 0x20
3797
#define DM_HISTOGRAM_BOUNDS_RANGE 0x30
3798
3799
/*
3800
 * Return a string representation of the supplied histogram's values and
3801
 * bin boundaries.
3802
 *
3803
 * The bin argument selects the bin to format. If this argument is less
3804
 * than zero all bins will be included in the resulting string.
3805
 *
3806
 * width specifies a minimum width for the field in characters; if it is
3807
 * zero the width will be determined automatically based on the options
3808
 * selected for formatting. A value less than zero disables field width
3809
 * control: bin boundaries and values will be output with a minimum
3810
 * amount of whitespace.
3811
 *
3812
 * flags is a collection of flag arguments that control the string format:
3813
 *
3814
 * DM_HISTOGRAM_VALUES  - Include bin values in the string.
3815
 * DM_HISTOGRAM_SUFFIX  - Include time unit suffixes when printing bounds.
3816
 * DM_HISTOGRAM_PERCENT - Format bin values as a percentage.
3817
 *
3818
 * DM_HISTOGRAM_BOUNDS_LOWER - Include the lower bound of each bin.
3819
 * DM_HISTOGRAM_BOUNDS_UPPER - Include the upper bound of each bin.
3820
 * DM_HISTOGRAM_BOUNDS_RANGE - Show the span of each bin as "lo-up".
3821
 *
3822
 * The returned pointer does not need to be freed explicitly by the
3823
 * caller: it will become invalid following a subsequent
3824
 * dm_stats_list(), dm_stats_populate() or dm_stats_destroy() of the
3825
 * corresponding dm_stats handle.
3826
 */
3827
const char *dm_histogram_to_string(const struct dm_histogram *dmh, int bin,
3828
           int width, int flags);
3829
3830
/*************************
3831
 * config file parse/print
3832
 *************************/
3833
typedef enum dm_config_value_type_e {
3834
  DM_CFG_INT,
3835
  DM_CFG_FLOAT,
3836
  DM_CFG_STRING,
3837
  DM_CFG_EMPTY_ARRAY
3838
} dm_config_value_type_t;
3839
3840
struct dm_config_value {
3841
  dm_config_value_type_t type;
3842
3843
  union dm_config_value_u {
3844
    int64_t i;
3845
    float f;
3846
    double d;         /* Unused. */
3847
    const char *str;
3848
  } v;
3849
3850
  struct dm_config_value *next; /* For arrays */
3851
  uint32_t format_flags;
3852
};
3853
3854
struct dm_config_node {
3855
  const char *key;
3856
  struct dm_config_node *parent, *sib, *child;
3857
  struct dm_config_value *v;
3858
  int id;
3859
};
3860
3861
struct dm_config_tree {
3862
  struct dm_config_node *root;
3863
  struct dm_config_tree *cascade;
3864
  struct dm_pool *mem;
3865
  void *custom;
3866
};
3867
3868
struct dm_config_tree *dm_config_create(void);
3869
struct dm_config_tree *dm_config_from_string(const char *config_settings);
3870
int dm_config_parse(struct dm_config_tree *cft, const char *start, const char *end);
3871
int dm_config_parse_without_dup_node_check(struct dm_config_tree *cft, const char *start, const char *end);
3872
int dm_config_parse_only_section(struct dm_config_tree *cft, const char *start, const char *end, const char *section);
3873
3874
void *dm_config_get_custom(struct dm_config_tree *cft);
3875
void dm_config_set_custom(struct dm_config_tree *cft, void *custom);
3876
3877
/*
3878
 * When searching, first_cft is checked before second_cft.
3879
 */
3880
struct dm_config_tree *dm_config_insert_cascaded_tree(struct dm_config_tree *first_cft, struct dm_config_tree *second_cft);
3881
3882
/*
3883
 * If there's a cascaded dm_config_tree, remove the top layer
3884
 * and return the layer below.  Otherwise return NULL.
3885
 */
3886
struct dm_config_tree *dm_config_remove_cascaded_tree(struct dm_config_tree *cft);
3887
3888
/*
3889
 * Create a new, uncascaded config tree equivalent to the input cascade.
3890
 */
3891
struct dm_config_tree *dm_config_flatten(struct dm_config_tree *cft);
3892
3893
void dm_config_destroy(struct dm_config_tree *cft);
3894
3895
/* Simple output line by line. */
3896
typedef int (*dm_putline_fn)(const char *line, void *baton);
3897
/* More advanced output with config node reference. */
3898
typedef int (*dm_config_node_out_fn)(const struct dm_config_node *cn, const char *line, void *baton);
3899
3900
/*
3901
 * Specification for advanced config node output.
3902
 */
3903
struct dm_config_node_out_spec {
3904
  dm_config_node_out_fn prefix_fn; /* called before processing config node lines */
3905
  dm_config_node_out_fn line_fn; /* called for each config node line */
3906
  dm_config_node_out_fn suffix_fn; /* called after processing config node lines */
3907
};
3908
3909
/* Write the node and any subsequent siblings it has. */
3910
int dm_config_write_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton);
3911
int dm_config_write_node_out(const struct dm_config_node *cn, const struct dm_config_node_out_spec *out_spec, void *baton);
3912
3913
/* Write given node only without subsequent siblings. */
3914
int dm_config_write_one_node(const struct dm_config_node *cn, dm_putline_fn putline, void *baton);
3915
int dm_config_write_one_node_out(const struct dm_config_node *cn, const struct dm_config_node_out_spec *out_spec, void *baton);
3916
3917
struct dm_config_node *dm_config_find_node(const struct dm_config_node *cn, const char *path);
3918
int dm_config_has_node(const struct dm_config_node *cn, const char *path);
3919
int dm_config_remove_node(struct dm_config_node *parent, struct dm_config_node *rem_node);
3920
const char *dm_config_find_str(const struct dm_config_node *cn, const char *path, const char *fail);
3921
const char *dm_config_find_str_allow_empty(const struct dm_config_node *cn, const char *path, const char *fail);
3922
int dm_config_find_int(const struct dm_config_node *cn, const char *path, int fail);
3923
int64_t dm_config_find_int64(const struct dm_config_node *cn, const char *path, int64_t fail);
3924
float dm_config_find_float(const struct dm_config_node *cn, const char *path, float fail);
3925
3926
const struct dm_config_node *dm_config_tree_find_node(const struct dm_config_tree *cft, const char *path);
3927
const char *dm_config_tree_find_str(const struct dm_config_tree *cft, const char *path, const char *fail);
3928
const char *dm_config_tree_find_str_allow_empty(const struct dm_config_tree *cft, const char *path, const char *fail);
3929
int dm_config_tree_find_int(const struct dm_config_tree *cft, const char *path, int fail);
3930
int64_t dm_config_tree_find_int64(const struct dm_config_tree *cft, const char *path, int64_t fail);
3931
float dm_config_tree_find_float(const struct dm_config_tree *cft, const char *path, float fail);
3932
int dm_config_tree_find_bool(const struct dm_config_tree *cft, const char *path, int fail);
3933
3934
/*
3935
 * Understands (0, ~0), (y, n), (yes, no), (on,
3936
 * off), (true, false).
3937
 */
3938
int dm_config_find_bool(const struct dm_config_node *cn, const char *path, int fail);
3939
int dm_config_value_is_bool(const struct dm_config_value *v);
3940
3941
int dm_config_get_uint32(const struct dm_config_node *cn, const char *path, uint32_t *result);
3942
int dm_config_get_uint64(const struct dm_config_node *cn, const char *path, uint64_t *result);
3943
int dm_config_get_str(const struct dm_config_node *cn, const char *path, const char **result);
3944
int dm_config_get_list(const struct dm_config_node *cn, const char *path, const struct dm_config_value **result);
3945
int dm_config_get_section(const struct dm_config_node *cn, const char *path, const struct dm_config_node **result);
3946
3947
unsigned dm_config_maybe_section(const char *str, unsigned len);
3948
3949
const char *dm_config_parent_name(const struct dm_config_node *n);
3950
3951
struct dm_config_node *dm_config_clone_node_with_mem(struct dm_pool *mem, const struct dm_config_node *cn, int siblings);
3952
struct dm_config_node *dm_config_create_node(struct dm_config_tree *cft, const char *key);
3953
struct dm_config_value *dm_config_create_value(struct dm_config_tree *cft);
3954
struct dm_config_node *dm_config_clone_node(struct dm_config_tree *cft, const struct dm_config_node *cn, int siblings);
3955
3956
/*
3957
 * Common formatting flags applicable to all config node types (lower 16 bits).
3958
 */
3959
#define DM_CONFIG_VALUE_FMT_COMMON_ARRAY             0x00000001 /* value is array */
3960
#define DM_CONFIG_VALUE_FMT_COMMON_EXTRA_SPACES      0x00000002 /* add spaces in "key = value" pairs in contrast to "key=value" for better readability */
3961
3962
/*
3963
 * Type-related config node formatting flags (higher 16 bits).
3964
 */
3965
/* int-related formatting flags */
3966
#define DM_CONFIG_VALUE_FMT_INT_OCTAL                0x00010000 /* print number in octal form */
3967
3968
/* string-related formatting flags */
3969
#define DM_CONFIG_VALUE_FMT_STRING_NO_QUOTES         0x00010000 /* do not print quotes around string value */
3970
3971
void dm_config_value_set_format_flags(struct dm_config_value *cv, uint32_t format_flags);
3972
uint32_t dm_config_value_get_format_flags(struct dm_config_value *cv);
3973
3974
struct dm_pool *dm_config_memory(struct dm_config_tree *cft);
3975
3976
/* Udev device directory. */
3977
#define DM_UDEV_DEV_DIR "/dev/"
3978
3979
/* Cookie prefixes.
3980
 *
3981
 * The cookie value consists of a prefix (16 bits) and a base (16 bits).
3982
 * We can use the prefix to store the flags. These flags are sent to
3983
 * kernel within given dm task. When returned back to userspace in
3984
 * DM_COOKIE udev environment variable, we can control several aspects
3985
 * of udev rules we use by decoding the cookie prefix. When doing the
3986
 * notification, we replace the cookie prefix with DM_COOKIE_MAGIC,
3987
 * so we notify the right semaphore.
3988
 *
3989
 * It is still possible to use cookies for passing the flags to udev
3990
 * rules even when udev_sync is disabled. The base part of the cookie
3991
 * will be zero (there's no notification semaphore) and prefix will be
3992
 * set then. However, having udev_sync enabled is highly recommended.
3993
 */
3994
#define DM_COOKIE_MAGIC 0x0D4D
3995
#define DM_UDEV_FLAGS_MASK 0xFFFF0000
3996
#define DM_UDEV_FLAGS_SHIFT 16
3997
3998
/*
3999
 * DM_UDEV_DISABLE_DM_RULES_FLAG is set in case we need to disable
4000
 * basic device-mapper udev rules that create symlinks in /dev/<DM_DIR>
4001
 * directory. However, we can't reliably prevent creating default
4002
 * nodes by udev (commonly /dev/dm-X, where X is a number).
4003
 */
4004
#define DM_UDEV_DISABLE_DM_RULES_FLAG 0x0001
4005
/*
4006
 * DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG is set in case we need to disable
4007
 * subsystem udev rules, but still we need the general DM udev rules to
4008
 * be applied (to create the nodes and symlinks under /dev and /dev/disk).
4009
 */
4010
0
#define DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG 0x0002
4011
/*
4012
 * DM_UDEV_DISABLE_DISK_RULES_FLAG is set in case we need to disable
4013
 * general DM rules that set symlinks in /dev/disk directory.
4014
 */
4015
0
#define DM_UDEV_DISABLE_DISK_RULES_FLAG 0x0004
4016
/*
4017
 * DM_UDEV_DISABLE_OTHER_RULES_FLAG is set in case we need to disable
4018
 * all the other rules that are not general device-mapper nor subsystem
4019
 * related (the rules belong to other software or packages). All foreign
4020
 * rules should check this flag directly and they should ignore further
4021
 * rule processing for such event.
4022
 */
4023
0
#define DM_UDEV_DISABLE_OTHER_RULES_FLAG 0x0008
4024
/*
4025
 * DM_UDEV_LOW_PRIORITY_FLAG is set in case we need to instruct the
4026
 * udev rules to give low priority to the device that is currently
4027
 * processed. For example, this provides a way to select which symlinks
4028
 * could be overwritten by high priority ones if their names are equal.
4029
 * Common situation is a name based on FS UUID while using origin and
4030
 * snapshot devices.
4031
 */
4032
#define DM_UDEV_LOW_PRIORITY_FLAG 0x0010
4033
/*
4034
 * DM_UDEV_DISABLE_LIBRARY_FALLBACK is set in case we need to disable
4035
 * libdevmapper's node management. We will rely on udev completely
4036
 * and there will be no fallback action provided by libdevmapper if
4037
 * udev does something improperly. Using the library fallback code has
4038
 * a consequence that you need to take into account: any device node
4039
 * or symlink created without udev is not recorded in udev database
4040
 * which other applications may read to get complete list of devices.
4041
 * For this reason, use of DM_UDEV_DISABLE_LIBRARY_FALLBACK is
4042
 * recommended on systems where udev is used. Keep library fallback
4043
 * enabled just for exceptional cases where you need to debug udev-related
4044
 * problems. If you hit such problems, please contact us through upstream
4045
 * LVM2 development mailing list (see also README file). This flag is
4046
 * currently not set by default in libdevmapper so you need to set it
4047
 * explicitly if you're sure that udev is behaving correctly on your
4048
 * setups.
4049
 */
4050
0
#define DM_UDEV_DISABLE_LIBRARY_FALLBACK 0x0020
4051
/*
4052
 * DM_UDEV_PRIMARY_SOURCE_FLAG is automatically appended by
4053
 * libdevmapper for all ioctls generating udev uevents. Once used in
4054
 * udev rules, we know if this is a real "primary sourced" event or not.
4055
 * We need to distinguish real events originated in libdevmapper from
4056
 * any spurious events to gather all missing information (e.g. events
4057
 * generated as a result of "udevadm trigger" command or as a result
4058
 * of the "watch" udev rule).
4059
 */
4060
#define DM_UDEV_PRIMARY_SOURCE_FLAG 0x0040
4061
4062
/*
4063
 * Udev flags reserved for use by any device-mapper subsystem.
4064
 */
4065
#define DM_SUBSYSTEM_UDEV_FLAG0 0x0100
4066
#define DM_SUBSYSTEM_UDEV_FLAG1 0x0200
4067
#define DM_SUBSYSTEM_UDEV_FLAG2 0x0400
4068
#define DM_SUBSYSTEM_UDEV_FLAG3 0x0800
4069
#define DM_SUBSYSTEM_UDEV_FLAG4 0x1000
4070
#define DM_SUBSYSTEM_UDEV_FLAG5 0x2000
4071
#define DM_SUBSYSTEM_UDEV_FLAG6 0x4000
4072
#define DM_SUBSYSTEM_UDEV_FLAG7 0x8000
4073
4074
int dm_cookie_supported(void);
4075
4076
/*
4077
 * Udev synchronization functions.
4078
 */
4079
void dm_udev_set_sync_support(int sync_with_udev);
4080
int dm_udev_get_sync_support(void);
4081
void dm_udev_set_checking(int checking);
4082
int dm_udev_get_checking(void);
4083
4084
/*
4085
 * Default value to get new auto generated cookie created
4086
 */
4087
#define DM_COOKIE_AUTO_CREATE 0
4088
int dm_udev_create_cookie(uint32_t *cookie);
4089
int dm_udev_complete(uint32_t cookie);
4090
int dm_udev_wait(uint32_t cookie);
4091
4092
/*
4093
 * dm_dev_wait_immediate
4094
 * If *ready is 1 on return, the wait is complete.
4095
 * If *ready is 0 on return, the wait is incomplete and either
4096
 * this function or dm_udev_wait() must be called again.
4097
 * Returns 0 on error, when neither function should be called again.
4098
 */
4099
int dm_udev_wait_immediate(uint32_t cookie, int *ready);
4100
4101
#define DM_DEV_DIR_UMASK 0022
4102
#define DM_CONTROL_NODE_UMASK 0177
4103
4104
#ifdef __cplusplus
4105
}
4106
#endif
4107
#endif        /* LIB_DEVICE_MAPPER_H */