Coverage Report

Created: 2026-02-22 06:11

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