Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lvm2/libdm/ioctl/libdm-iface.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
 * Copyright (C) 2004-2013 Red Hat, Inc. All rights reserved.
4
 *
5
 * This file is part of the device-mapper userspace tools.
6
 *
7
 * This copyrighted material is made available to anyone wishing to use,
8
 * modify, copy, or redistribute it subject to the terms and conditions
9
 * of the GNU Lesser General Public License v.2.1.
10
 *
11
 * You should have received a copy of the GNU Lesser General Public License
12
 * along with this program; if not, write to the Free Software Foundation,
13
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14
 */
15
16
#include "libdm/misc/dmlib.h"
17
#include "libdm-targets.h"
18
#include "libdm/libdm-common.h"
19
20
#include <stddef.h>
21
#include <fcntl.h>
22
#include <dirent.h>
23
#include <sys/ioctl.h>
24
#include <sys/utsname.h>
25
#include <limits.h>
26
27
#ifdef __linux__
28
#  include "libdm/misc/kdev_t.h"
29
#  include <linux/limits.h>
30
#else
31
#  define MAJOR(x) major((x))
32
#  define MINOR(x) minor((x))
33
#  define MKDEV(x,y) makedev(((dev_t)x),((dev_t)y))
34
#endif
35
36
#include "libdm/misc/dm-ioctl.h"
37
38
/*
39
 * Ensure build compatibility.  
40
 * The hard-coded versions here are the highest present 
41
 * in the _cmd_data arrays.
42
 */
43
44
#if !((DM_VERSION_MAJOR == 4 && DM_VERSION_MINOR >= 6))
45
#error The version of dm-ioctl.h included is incompatible.
46
#endif
47
48
/* FIXME This should be exported in device-mapper.h */
49
0
#define DM_NAME "device-mapper"
50
51
0
#define PROC_MISC "/proc/misc"
52
0
#define PROC_DEVICES "/proc/devices"
53
0
#define MISC_NAME "misc"
54
55
0
#define NUMBER_OF_MAJORS 4096
56
57
/*
58
 * Static minor number assigned since kernel version 2.6.36.
59
 * The original definition is in kernel's include/linux/miscdevice.h.
60
 * This number is also visible in modules.devname exported by depmod
61
 * utility (support included in module-init-tools version >= 3.12).
62
 */
63
0
#define MAPPER_CTRL_MINOR 236
64
0
#define MISC_MAJOR 10
65
66
/* dm major version no for running kernel */
67
static unsigned _dm_version = DM_VERSION_MAJOR;
68
static unsigned _dm_version_minor = 0;
69
static unsigned _dm_version_patchlevel = 0;
70
static int _log_suppress = 0;
71
static struct dm_timestamp *_dm_ioctl_timestamp = NULL;
72
static int _dm_warn_inactive_suppress = 0;
73
74
/*
75
 * If the kernel dm driver only supports one major number
76
 * we store it in _dm_device_major.  Otherwise we indicate
77
 * which major numbers have been claimed by device-mapper
78
 * in _dm_bitset.
79
 */
80
static unsigned _dm_multiple_major_support = 1;
81
static dm_bitset_t _dm_bitset = NULL;
82
static uint32_t _dm_device_major = 0;
83
84
static int _control_fd = -1;
85
static int _hold_control_fd_open = 0;
86
static int _version_checked = 0;
87
static int _version_ok = 1;
88
static unsigned _ioctl_buffer_double_factor = 0;
89
90
/* *INDENT-OFF* */
91
static const struct cmd_data _cmd_data_v4[] = {
92
  {"create",  DM_DEV_CREATE,    {4, 0, 0}},
93
  {"reload",  DM_TABLE_LOAD,    {4, 0, 0}},
94
  {"remove",  DM_DEV_REMOVE,    {4, 0, 0}},
95
  {"remove_all",  DM_REMOVE_ALL,    {4, 0, 0}},
96
  {"suspend", DM_DEV_SUSPEND,   {4, 0, 0}},
97
  {"resume",  DM_DEV_SUSPEND,   {4, 0, 0}},
98
  {"info",  DM_DEV_STATUS,    {4, 0, 0}},
99
  {"deps",  DM_TABLE_DEPS,    {4, 0, 0}},
100
  {"rename",  DM_DEV_RENAME,    {4, 0, 0}},
101
  {"version", DM_VERSION,   {4, 0, 0}},
102
  {"status",  DM_TABLE_STATUS,  {4, 0, 0}},
103
  {"table", DM_TABLE_STATUS,  {4, 0, 0}},
104
  {"waitevent", DM_DEV_WAIT,    {4, 0, 0}},
105
  {"names", DM_LIST_DEVICES,  {4, 0, 0}},
106
  {"clear", DM_TABLE_CLEAR,   {4, 0, 0}},
107
  {"mknodes", DM_DEV_STATUS,    {4, 0, 0}},
108
#ifdef DM_LIST_VERSIONS
109
  {"versions",  DM_LIST_VERSIONS, {4, 1, 0}},
110
#endif
111
#ifdef DM_TARGET_MSG
112
  {"message", DM_TARGET_MSG,    {4, 2, 0}},
113
#endif
114
#ifdef DM_DEV_SET_GEOMETRY
115
  {"setgeometry", DM_DEV_SET_GEOMETRY,  {4, 6, 0}},
116
#endif
117
#ifdef DM_DEV_ARM_POLL
118
  {"armpoll", DM_DEV_ARM_POLL,  {4, 36, 0}},
119
#endif
120
#ifdef DM_GET_TARGET_VERSION
121
  {"target-version", DM_GET_TARGET_VERSION, {4, 41, 0}},
122
#endif
123
};
124
/* *INDENT-ON* */
125
126
0
#define ALIGNMENT 8
127
128
/* FIXME Rejig library to record & use errno instead */
129
#ifndef DM_EXISTS_FLAG
130
0
#  define DM_EXISTS_FLAG 0x00000004
131
#endif
132
133
static char *_align(char *ptr, unsigned int a)
134
0
{
135
0
  register unsigned long agn = --a;
136
137
0
  return (char *) (((unsigned long) ptr + agn) & ~agn);
138
0
}
139
140
static unsigned _kernel_major = 0;
141
static unsigned _kernel_minor = 0;
142
static unsigned _kernel_release = 0;
143
144
static int _uname(void)
145
0
{
146
0
  static int _uts_set = 0;
147
0
  struct utsname _uts;
148
0
  int parts;
149
150
0
  if (_uts_set)
151
0
    return 1;
152
153
0
  if (uname(&_uts)) {
154
0
    log_error("uname failed: %s", strerror(errno));
155
0
    return 0;
156
0
  }
157
158
0
  parts = sscanf(_uts.release, "%u.%u.%u",
159
0
           &_kernel_major, &_kernel_minor, &_kernel_release);
160
161
  /* Kernels with a major number of 2 always had 3 parts. */
162
0
  if (parts < 1 || (_kernel_major < 3 && parts < 3)) {
163
0
    log_error("Could not determine kernel version used.");
164
0
    return 0;
165
0
  }
166
167
0
  _uts_set = 1;
168
0
  return 1;
169
0
}
170
171
int get_uname_version(unsigned *major, unsigned *minor, unsigned *release)
172
0
{
173
0
  if (!_uname())
174
0
    return_0;
175
176
0
  *major = _kernel_major;
177
0
  *minor = _kernel_minor;
178
0
  *release = _kernel_release;
179
180
0
  return 1;
181
0
}
182
183
#ifdef DM_IOCTLS
184
185
/*
186
 * Set number to NULL to populate _dm_bitset - otherwise first
187
 * match is returned.
188
 * Returns:
189
 *  0 - error
190
 *  1 - success - number found
191
 *  2 - success - number not found (only if require_module_loaded=0)
192
 */
193
static int _get_proc_number(const char *file, const char *name,
194
          uint32_t *number, int require_module_loaded)
195
0
{
196
0
  FILE *fl;
197
0
  char nm[256];
198
0
  char *line = NULL;
199
0
  size_t len;
200
0
  uint32_t num;
201
0
  unsigned blocksection = (strcmp(file, PROC_DEVICES) == 0) ? 0 : 1;
202
203
0
  if (!(fl = fopen(file, "r"))) {
204
0
    log_sys_error("fopen", file);
205
0
    return 0;
206
0
  }
207
208
0
  while (getline(&line, &len, fl) != -1) {
209
0
    if (!blocksection && (line[0] == 'B'))
210
0
      blocksection = 1;
211
0
    else if (sscanf(line, "%u %255s\n", &num, &nm[0]) == 2) {
212
0
      if (!strcmp(name, nm)) {
213
0
        if (number) {
214
0
          *number = num;
215
0
          if (fclose(fl))
216
0
            log_sys_error("fclose", file);
217
0
          free(line);
218
0
          return 1;
219
0
        }
220
0
        dm_bit_set(_dm_bitset, num);
221
0
      }
222
0
    }
223
0
  }
224
0
  if (fclose(fl))
225
0
    log_sys_error("fclose", file);
226
0
  free(line);
227
228
0
  if (number) {
229
0
    if (require_module_loaded) {
230
0
      log_error("%s: No entry for %s found", file, name);
231
0
      return 0;
232
0
    }
233
234
0
    return 2;
235
0
  }
236
237
0
  return 1;
238
0
}
239
240
static int _control_device_number(uint32_t *major, uint32_t *minor)
241
0
{
242
0
  if (!_get_proc_number(PROC_DEVICES, MISC_NAME, major, 1) ||
243
0
      !_get_proc_number(PROC_MISC, DM_NAME, minor, 1)) {
244
0
    *major = 0;
245
0
    return 0;
246
0
  }
247
248
0
  return 1;
249
0
}
250
251
static int _control_unlink(const char *control)
252
0
{
253
0
  if (unlink(control) && (errno != ENOENT)) {
254
0
    log_sys_error("unlink", control);
255
0
    return -1;
256
0
  }
257
258
0
  return 0;
259
0
}
260
261
/*
262
 * Returns 1 if it exists on returning; 0 if it doesn't; -1 if it's wrong.
263
 */
264
static int _control_exists(const char *control, uint32_t major, uint32_t minor)
265
0
{
266
0
  struct stat buf;
267
268
0
  if (stat(control, &buf) < 0) {
269
0
    if (errno != ENOENT)
270
0
      log_sys_error("stat", control);
271
0
    return 0;
272
0
  }
273
274
0
  if (!S_ISCHR(buf.st_mode)) {
275
0
    log_verbose("%s: Wrong inode type", control);
276
0
    return _control_unlink(control);
277
0
  }
278
279
0
  if (major && buf.st_rdev != MKDEV(major, minor)) {
280
0
    log_verbose("%s: Wrong device number: (%u, %u) instead of "
281
0
          "(%u, %u)", control,
282
0
          MAJOR(buf.st_mode), MINOR(buf.st_mode),
283
0
          major, minor);
284
0
    return _control_unlink(control);
285
0
  }
286
287
0
  return 1;
288
0
}
289
290
static int _create_control(const char *control, uint32_t major, uint32_t minor)
291
0
{
292
0
  int ret;
293
0
  mode_t old_umask;
294
295
  /*
296
   * Return if the control already exists with intended major/minor
297
   * or there's an error unlinking an apparently incorrect one.
298
   */
299
0
  ret = _control_exists(control, major, minor);
300
0
  if (ret == -1)
301
0
    return_0; /* Failed to unlink existing incorrect node */
302
0
  if (ret)
303
0
    return 1; /* Already exists and correct */
304
305
0
  (void) dm_prepare_selinux_context(dm_dir(), S_IFDIR);
306
0
  old_umask = umask(DM_DEV_DIR_UMASK);
307
0
  ret = dm_create_dir(dm_dir());
308
0
  umask(old_umask);
309
0
  (void) dm_prepare_selinux_context(NULL, 0);
310
311
0
  if (!ret)
312
0
    return_0;
313
314
0
  log_verbose("Creating device %s (%u, %u)", control, major, minor);
315
316
0
  (void) dm_prepare_selinux_context(control, S_IFCHR);
317
0
  old_umask = umask(DM_CONTROL_NODE_UMASK);
318
0
  if (mknod(control, S_IFCHR | S_IRUSR | S_IWUSR,
319
0
      MKDEV(major, minor)) < 0)  {
320
0
    if (errno != EEXIST) {
321
0
      log_sys_error("mknod", control);
322
0
      ret = 0;
323
0
    } else if (_control_exists(control, major, minor) != 1) {
324
0
      stack; /* Invalid control node created by parallel command ? */
325
0
      ret = 0;
326
0
    }
327
0
  }
328
0
  umask(old_umask);
329
0
  (void) dm_prepare_selinux_context(NULL, 0);
330
331
0
  return ret;
332
0
}
333
#endif
334
335
/*
336
 * FIXME Update bitset in long-running process if dm claims new major numbers.
337
 */
338
/*
339
 * If require_module_loaded=0, caller is responsible to check
340
 * whether _dm_device_major or _dm_bitset is really set. If
341
 * it's not, it means the module is not loaded.
342
 */
343
static int _create_dm_bitset(int require_module_loaded)
344
0
{
345
0
  int r;
346
347
0
#ifdef DM_IOCTLS
348
0
  if (_dm_bitset || _dm_device_major)
349
0
    return 1;
350
351
0
  if (!_uname())
352
0
    return 0;
353
354
  /*
355
   * 2.6 kernels are limited to one major number.
356
   * Assume 2.4 kernels are patched not to.
357
   * FIXME Check _dm_version and _dm_version_minor if 2.6 changes this.
358
   */
359
0
  if (KERNEL_VERSION(_kernel_major, _kernel_minor, _kernel_release) >=
360
0
      KERNEL_VERSION(2, 6, 0))
361
0
    _dm_multiple_major_support = 0;
362
363
0
  if (!_dm_multiple_major_support) {
364
0
    if (!_get_proc_number(PROC_DEVICES, DM_NAME, &_dm_device_major,
365
0
              require_module_loaded))
366
0
      return 0;
367
0
    return 1;
368
0
  }
369
370
  /* Multiple major numbers supported */
371
0
  if (!(_dm_bitset = dm_bitset_create(NULL, NUMBER_OF_MAJORS)))
372
0
    return 0;
373
374
0
  r = _get_proc_number(PROC_DEVICES, DM_NAME, NULL, require_module_loaded);
375
0
  if (!r || r == 2) {
376
0
    dm_bitset_destroy(_dm_bitset);
377
0
    _dm_bitset = NULL;
378
    /*
379
     * It's not an error if we didn't find anything and we
380
     * didn't require module to be loaded at the same time.
381
     */
382
0
    return r == 2;
383
0
  }
384
385
0
  return 1;
386
#else
387
  return 0;
388
#endif
389
0
}
390
391
int dm_is_dm_major(uint32_t major)
392
0
{
393
0
  if (!_create_dm_bitset(0))
394
0
    return 0;
395
396
0
  if (_dm_multiple_major_support) {
397
0
    if (!_dm_bitset)
398
0
      return 0;
399
0
    return dm_bit(_dm_bitset, major) ? 1 : 0;
400
0
  }
401
402
0
  if (!_dm_device_major)
403
0
    return 0;
404
405
0
  return (major == _dm_device_major) ? 1 : 0;
406
0
}
407
408
static void _close_control_fd(void)
409
4.82k
{
410
4.82k
  if (_control_fd != -1) {
411
0
    if (close(_control_fd) < 0)
412
0
      log_sys_debug("close", "_control_fd");
413
0
    _control_fd = -1;
414
0
  }
415
4.82k
}
416
417
#ifdef DM_IOCTLS
418
static int _open_and_assign_control_fd(const char *control)
419
0
{
420
0
  if ((_control_fd = open(control, O_RDWR)) < 0) {
421
0
    log_sys_error("open", control);
422
0
    return 0;
423
0
  }
424
425
0
  return 1;
426
0
}
427
#endif
428
429
static int _open_control(void)
430
0
{
431
0
#ifdef DM_IOCTLS
432
0
  char control[PATH_MAX];
433
0
  uint32_t major = MISC_MAJOR;
434
0
  uint32_t minor = MAPPER_CTRL_MINOR;
435
436
0
  if (_control_fd != -1)
437
0
    return 1;
438
439
0
  if (!_uname())
440
0
    return 0;
441
442
0
  if (dm_snprintf(control, sizeof(control), "%s/%s", dm_dir(), DM_CONTROL_NODE) < 0)
443
0
    goto_bad;
444
445
  /*
446
   * Prior to 2.6.36 the minor number should be looked up in /proc.
447
   */
448
0
  if ((KERNEL_VERSION(_kernel_major, _kernel_minor, _kernel_release) <
449
0
       KERNEL_VERSION(2, 6, 36)) &&
450
0
      !_control_device_number(&major, &minor))
451
0
    goto_bad;
452
453
  /*
454
   * Create the node with correct major and minor if not already done.
455
   * Udev may already have created /dev/mapper/control
456
   * from the modules.devname file generated by depmod.
457
   */
458
0
  if (!_create_control(control, major, minor))
459
0
    goto_bad;
460
461
  /*
462
   * As of 2.6.36 kernels, the open can trigger autoloading dm-mod.
463
   */
464
0
  if (!_open_and_assign_control_fd(control))
465
0
    goto_bad;
466
  
467
0
  if (!_create_dm_bitset(1)) {
468
0
    log_error("Failed to set up list of device-mapper major numbers");
469
0
    return 0;
470
0
  }
471
472
0
  return 1;
473
474
0
bad:
475
0
  log_error("Failure to communicate with kernel device-mapper driver.");
476
0
  if (!geteuid())
477
0
    log_error("Check that device-mapper is available in the kernel.");
478
0
  return 0;
479
#else
480
  return 1;
481
#endif
482
0
}
483
484
static void _dm_zfree_string(char *string)
485
0
{
486
0
  if (string) {
487
0
    memset(string, 0, strlen(string));
488
0
    __asm__ volatile ("" ::: "memory"); /* Compiler barrier. */
489
0
    dm_free(string);
490
0
  }
491
0
}
492
493
static void _dm_zfree_dmi(struct dm_ioctl *dmi)
494
0
{
495
0
  if (dmi) {
496
0
    memset(dmi, 0, dmi->data_size);
497
0
    __asm__ volatile ("" ::: "memory"); /* Compiler barrier. */
498
0
    dm_free(dmi);
499
0
  }
500
0
}
501
502
static void _dm_task_free_targets(struct dm_task *dmt)
503
0
{
504
0
  struct target *t, *n;
505
506
0
  for (t = dmt->head; t; t = n) {
507
0
    n = t->next;
508
0
    if (dmt->secure_data)
509
0
      _dm_zfree_string(t->params);
510
0
    else
511
0
      dm_free(t->params);
512
0
    dm_free(t->type);
513
0
    dm_free(t);
514
0
  }
515
516
0
  dmt->head = dmt->tail = NULL;
517
0
}
518
519
void dm_task_destroy(struct dm_task *dmt)
520
0
{
521
0
  _dm_task_free_targets(dmt);
522
0
  if (dmt->secure_data)
523
0
    _dm_zfree_dmi(dmt->dmi.v4);
524
0
  else
525
0
    dm_free(dmt->dmi.v4);
526
0
  dm_free(dmt->dev_name);
527
0
  dm_free(dmt->mangled_dev_name);
528
0
  dm_free(dmt->newname);
529
0
  dm_free(dmt->message);
530
0
  dm_free(dmt->geometry);
531
0
  dm_free(dmt->uuid);
532
0
  dm_free(dmt->mangled_uuid);
533
0
  dm_free(dmt);
534
0
}
535
536
/*
537
 * Protocol Version 4 functions.
538
 */
539
540
int dm_task_get_driver_version(struct dm_task *dmt, char *version, size_t size)
541
0
{
542
0
  unsigned *v;
543
544
0
  if (!dmt->dmi.v4) {
545
0
    if (version)
546
0
      version[0] = '\0';
547
0
    return 0;
548
0
  }
549
550
0
  v = dmt->dmi.v4->version;
551
0
  _dm_version_minor = v[1];
552
0
  _dm_version_patchlevel = v[2];
553
0
  if (version &&
554
0
      (snprintf(version, size, "%u.%u.%u", v[0], v[1], v[2]) < 0)) {
555
0
    log_error("Buffer for version is to short.");
556
0
    if (size > 0)
557
0
      version[0] = '\0';
558
0
    return 0;
559
0
  }
560
561
0
  return 1;
562
0
}
563
564
static int _check_version(char *version, size_t size, int log_suppress)
565
0
{
566
0
  struct dm_task *task;
567
0
  int r;
568
569
0
  if (!(task = dm_task_create(DM_DEVICE_VERSION))) {
570
0
    log_error("Failed to get device-mapper version");
571
0
    version[0] = '\0';
572
0
    return 0;
573
0
  }
574
575
0
  if (log_suppress)
576
0
    _log_suppress = 1;
577
578
0
  r = dm_task_run(task);
579
0
  if (!dm_task_get_driver_version(task, version, size))
580
0
    stack;
581
0
  dm_task_destroy(task);
582
0
  _log_suppress = 0;
583
584
0
  return r;
585
0
}
586
587
/*
588
 * Find out device-mapper's major version number the first time 
589
 * this is called and whether or not we support it.
590
 */
591
int dm_check_version(void)
592
0
{
593
0
  char libversion[64] = "", dmversion[64] = "";
594
0
  const char *compat = "";
595
596
0
  if (_version_checked)
597
0
    return _version_ok;
598
599
0
  _version_checked = 1;
600
601
0
  if (_check_version(dmversion, sizeof(dmversion), 0))
602
0
    return 1;
603
604
0
  dm_get_library_version(libversion, sizeof(libversion));
605
606
0
  log_error("Incompatible libdevmapper %s%s and kernel driver %s.",
607
0
      *libversion ? libversion : "(unknown version)", compat,
608
0
      *dmversion ? dmversion : "(unknown version)");
609
610
0
  _version_ok = 0;
611
0
  return 0;
612
0
}
613
614
int dm_cookie_supported(void)
615
0
{
616
0
  return (dm_check_version() &&
617
0
    ((_dm_version == 4) ? _dm_version_minor >= 15 : _dm_version > 4));
618
0
}
619
620
static int _dm_inactive_supported(void)
621
0
{
622
0
  int inactive_supported = 0;
623
624
0
  if (dm_check_version() && _dm_version >= 4) {
625
0
    if (_dm_version_minor >= 16)
626
0
      inactive_supported = 1; /* upstream */
627
0
    else if (_dm_version_minor == 11 &&
628
0
       (_dm_version_patchlevel >= 6 &&
629
0
        _dm_version_patchlevel <= 40)) {
630
0
      inactive_supported = 1; /* RHEL 5.7 */
631
0
    }
632
0
  }
633
634
0
  return inactive_supported;
635
0
}
636
637
int dm_message_supports_precise_timestamps(void)
638
0
{
639
  /*
640
   * 4.32.0 supports "precise_timestamps" and "histogram:" options
641
   * to @stats_create messages but lacks the ability to report
642
   * these properties via a subsequent @stats_list: require at
643
   * least 4.33.0 in order to use these features.
644
   */
645
0
  if (dm_check_version() && _dm_version >= 4)
646
0
    if (_dm_version_minor >= 33)
647
0
      return 1;
648
0
  return 0;
649
0
}
650
651
void *dm_get_next_target(struct dm_task *dmt, void *next,
652
       uint64_t *start, uint64_t *length,
653
       char **target_type, char **params)
654
0
{
655
0
  struct target *t = (struct target *) next;
656
657
0
  if (!t)
658
0
    t = dmt->head;
659
660
0
  if (!t) {
661
0
    *start = 0;
662
0
    *length = 0;
663
0
    *target_type = 0;
664
0
    *params = 0;
665
0
    return NULL;
666
0
  }
667
668
0
  *start = t->start;
669
0
  *length = t->length;
670
0
  *target_type = t->type;
671
0
  *params = t->params;
672
673
0
  return t->next;
674
0
}
675
676
/* Unmarshal the target info returned from a status call */
677
static int _unmarshal_status(struct dm_task *dmt, struct dm_ioctl *dmi)
678
0
{
679
0
  char *outbuf = (char *) dmi + dmi->data_start;
680
0
  char *outptr = outbuf;
681
0
  uint32_t i;
682
0
  struct dm_target_spec *spec;
683
684
0
  _dm_task_free_targets(dmt);
685
686
0
  for (i = 0; i < dmi->target_count; i++) {
687
0
    spec = (struct dm_target_spec *) outptr;
688
0
    if (!dm_task_add_target(dmt, spec->sector_start,
689
0
          spec->length,
690
0
          spec->target_type,
691
0
          outptr + sizeof(*spec))) {
692
0
      return 0;
693
0
    }
694
695
0
    outptr = outbuf + spec->next;
696
0
  }
697
698
0
  return 1;
699
0
}
700
701
int dm_format_dev(char *buf, int bufsize, uint32_t dev_major,
702
      uint32_t dev_minor)
703
0
{
704
0
  int r;
705
706
0
  if (bufsize < 8)
707
0
    return 0;
708
709
0
  r = snprintf(buf, (size_t) bufsize, "%u:%u", dev_major, dev_minor);
710
0
  if (r < 0 || r > bufsize - 1)
711
0
    return 0;
712
713
0
  return 1;
714
0
}
715
716
DM_EXPORT_NEW_SYMBOL(int, dm_task_get_info, 1_02_97)
717
  (struct dm_task *dmt, struct dm_info *info)
718
0
{
719
0
  if (!dmt->dmi.v4)
720
0
    return 0;
721
722
0
  memset(info, 0, sizeof(*info));
723
724
0
  info->exists = dmt->dmi.v4->flags & DM_EXISTS_FLAG ? 1 : 0;
725
0
  if (!info->exists)
726
0
    return 1;
727
728
0
  info->suspended = dmt->dmi.v4->flags & DM_SUSPEND_FLAG ? 1 : 0;
729
0
  info->read_only = dmt->dmi.v4->flags & DM_READONLY_FLAG ? 1 : 0;
730
0
  info->live_table = dmt->dmi.v4->flags & DM_ACTIVE_PRESENT_FLAG ? 1 : 0;
731
0
  info->inactive_table = dmt->dmi.v4->flags & DM_INACTIVE_PRESENT_FLAG ?
732
0
      1 : 0;
733
0
  info->deferred_remove = dmt->dmi.v4->flags & DM_DEFERRED_REMOVE;
734
0
  info->internal_suspend = (dmt->dmi.v4->flags & DM_INTERNAL_SUSPEND_FLAG) ? 1 : 0;
735
0
  info->target_count = dmt->dmi.v4->target_count;
736
0
  info->open_count = dmt->dmi.v4->open_count;
737
0
  info->event_nr = dmt->dmi.v4->event_nr;
738
0
  info->major = MAJOR(dmt->dmi.v4->dev);
739
0
  info->minor = MINOR(dmt->dmi.v4->dev);
740
741
0
  return 1;
742
0
}
743
744
uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead)
745
0
{
746
0
  const char *dev_name;
747
748
0
  *read_ahead = 0;
749
750
0
  if (!dmt->dmi.v4 || !(dmt->dmi.v4->flags & DM_EXISTS_FLAG))
751
0
    return 0;
752
753
0
  if (*dmt->dmi.v4->name)
754
0
    dev_name = dmt->dmi.v4->name;
755
0
  else if (!(dev_name = DEV_NAME(dmt))) {
756
0
    log_error("Get read ahead request failed: device name unrecorded.");
757
0
    return 0;
758
0
  }
759
760
0
  return get_dev_node_read_ahead(dev_name, MAJOR(dmt->dmi.v4->dev),
761
0
               MINOR(dmt->dmi.v4->dev), read_ahead);
762
0
}
763
764
struct dm_deps *dm_task_get_deps(struct dm_task *dmt)
765
0
{
766
0
  if (!dmt) {
767
0
    log_error(INTERNAL_ERROR "Missing dm_task.");
768
0
    return NULL;
769
0
  }
770
771
0
  return (struct dm_deps *) (((char *) dmt->dmi.v4) +
772
0
           dmt->dmi.v4->data_start);
773
0
}
774
775
/*
776
 * Round up the ptr to an 8-byte boundary.
777
 * Follow kernel pattern.
778
 */
779
0
#define ALIGN_MASK 7
780
static size_t _align_val(size_t val)
781
0
{
782
0
  return (val + ALIGN_MASK) & ~ALIGN_MASK;
783
0
}
784
static void *_align_ptr(void *ptr)
785
0
{
786
0
  return (void *)(uintptr_t)_align_val((size_t)ptr);
787
0
}
788
789
0
static int _check_has_event_nr(void) {
790
0
  static int _has_event_nr = -1;
791
792
0
  if (_has_event_nr < 0)
793
0
    _has_event_nr = dm_check_version() &&
794
0
      ((_dm_version == 4) ?  _dm_version_minor >= 38 : _dm_version > 4);
795
796
0
  return _has_event_nr;
797
0
}
798
799
struct dm_names *dm_task_get_names(struct dm_task *dmt)
800
0
{
801
0
  return (struct dm_names *) (((char *) dmt->dmi.v4) +
802
0
            dmt->dmi.v4->data_start);
803
0
}
804
805
struct dm_versions *dm_task_get_versions(struct dm_task *dmt)
806
0
{
807
0
  return (struct dm_versions *) (((char *) dmt->dmi.v4) +
808
0
               dmt->dmi.v4->data_start);
809
0
}
810
811
int dm_task_get_device_list(struct dm_task *dmt, struct dm_list **devs_list,
812
          unsigned *devs_features)
813
0
{
814
0
  struct dm_names *names, *names1;
815
0
  struct dm_active_device *dm_dev, *dm_new_dev;
816
0
  struct dm_list *devs;
817
0
  unsigned next = 0;
818
0
  uint32_t *event_nr;
819
0
  char *uuid_ptr;
820
0
  size_t len;
821
0
  int cnt = 0;
822
823
0
  *devs_list = 0;
824
0
  *devs_features = 0;
825
826
0
  if ((names = dm_task_get_names(dmt)) && names->dev) {
827
0
    names1 = names;
828
0
    if (!names->name[0])
829
0
      cnt = -1; /* -> cnt == 0 when no device is really present */
830
0
    do {
831
0
      names1 = (struct dm_names *)((char *) names1 + next);
832
0
      next = names1->next;
833
0
      ++cnt;
834
0
    } while (next);
835
0
  }
836
837
  /* buffer for devs +  sorted ptrs + dm_devs + aligned strings */
838
0
  if (!(devs = malloc(sizeof(*devs) + cnt * (2 * sizeof(void*) + sizeof(*dm_dev)) +
839
0
          (cnt ? (char*)names1 - (char*)names + 256 : 0))))
840
0
    return_0;
841
842
0
  dm_list_init(devs);
843
844
0
  if (!cnt) {
845
    /* nothing in the list -> mark all features present */
846
0
    *devs_features |= (DM_DEVICE_LIST_HAS_EVENT_NR | DM_DEVICE_LIST_HAS_UUID);
847
0
    goto out; /* nothing else to do */
848
0
  }
849
850
  /* Shift position where to store individual dm_devs */
851
0
  dm_dev = (struct dm_active_device *) ((long*) (devs + 1) + cnt);
852
853
0
  do {
854
0
    names = (struct dm_names *)((char *) names + next);
855
856
0
    dm_dev->devno = (dev_t) names->dev;
857
0
    dm_dev->name = (const char *)(dm_dev + 1);
858
0
    dm_dev->event_nr = 0;
859
0
    dm_dev->uuid = "";
860
861
0
    len = strlen(names->name) + 1;
862
0
    memcpy((char*)dm_dev->name, names->name, len);
863
864
0
    dm_new_dev = _align_ptr((char*)(dm_dev + 1) + len);
865
0
    if (_check_has_event_nr()) {
866
867
0
      *devs_features |= DM_DEVICE_LIST_HAS_EVENT_NR;
868
0
      event_nr = _align_ptr(names->name + len);
869
0
      dm_dev->event_nr = event_nr[0];
870
871
0
      if ((event_nr[1] & DM_NAME_LIST_FLAG_HAS_UUID)) {
872
0
        *devs_features |= DM_DEVICE_LIST_HAS_UUID;
873
0
        uuid_ptr = _align_ptr(event_nr + 2);
874
0
        len = strlen(uuid_ptr) + 1;
875
0
        memcpy(dm_new_dev, uuid_ptr, len);
876
0
        dm_dev->uuid = (const char *) dm_new_dev;
877
0
        dm_new_dev = _align_ptr((char*)dm_new_dev + len);
878
0
      }
879
0
    }
880
881
0
    dm_list_add(devs, &dm_dev->list);
882
0
    dm_dev = dm_new_dev;
883
0
    next = names->next;
884
0
  } while (next);
885
886
0
    out:
887
0
  *devs_list = devs;
888
889
0
  return 1;
890
0
}
891
892
void dm_device_list_destroy(struct dm_list **devs_list)
893
0
{
894
0
  struct dm_device_list *devs = (struct dm_device_list *) *devs_list;
895
896
0
  if (devs) {
897
0
    free(devs);
898
0
    *devs_list = NULL;
899
0
  }
900
0
}
901
902
int dm_device_list_equal(const struct dm_list *list1, const struct dm_list *list2)
903
0
{
904
0
  const struct dm_active_device *dev1, *dev2;
905
0
  const struct dm_list *l2;
906
907
0
  if (!list1 || !list2)
908
0
    return (list1 == list2);
909
910
0
  if (!(l2 = dm_list_first(list2)))
911
0
    return dm_list_empty(list1);
912
913
0
  dm_list_iterate_items(dev1, list1) {
914
0
    dev2 = dm_list_item(l2, struct dm_active_device);
915
0
    if ((dev1->devno != dev2->devno) || strcmp(dev1->uuid, dev2->uuid))
916
0
      return 0;
917
0
    if (!(l2 = dm_list_next(list2, l2)))
918
0
      return !dm_list_next(list1, &dev1->list);
919
0
  }
920
921
0
  return 1;
922
0
}
923
924
const char *dm_task_get_message_response(struct dm_task *dmt)
925
0
{
926
0
  const char *start, *end;
927
928
0
  if (!(dmt->dmi.v4->flags & DM_DATA_OUT_FLAG))
929
0
    return NULL;
930
931
0
  start = (const char *) dmt->dmi.v4 + dmt->dmi.v4->data_start;
932
0
  end = (const char *) dmt->dmi.v4 + dmt->dmi.v4->data_size;
933
934
0
  if (end < start) {
935
0
    log_error(INTERNAL_ERROR "Corrupted message structure returned: start %d > end %d", (int)dmt->dmi.v4->data_start, (int)dmt->dmi.v4->data_size);
936
0
    return NULL;
937
0
  }
938
939
0
  if (!memchr(start, 0, end - start)) {
940
0
    log_error(INTERNAL_ERROR "Message response doesn't contain terminating NUL character");
941
0
    return NULL;
942
0
  }
943
944
0
  return start;
945
0
}
946
947
int dm_task_set_ro(struct dm_task *dmt)
948
0
{
949
0
  dmt->read_only = 1;
950
0
  return 1;
951
0
}
952
953
int dm_task_set_read_ahead(struct dm_task *dmt, uint32_t read_ahead,
954
         uint32_t read_ahead_flags)
955
0
{
956
0
  dmt->read_ahead = read_ahead;
957
0
  dmt->read_ahead_flags = read_ahead_flags;
958
959
0
  return 1;
960
0
}
961
962
int dm_task_suppress_identical_reload(struct dm_task *dmt)
963
0
{
964
0
  dmt->suppress_identical_reload = 1;
965
0
  return 1;
966
0
}
967
968
int dm_task_set_add_node(struct dm_task *dmt, dm_add_node_t add_node)
969
0
{
970
0
  switch (add_node) {
971
0
  case DM_ADD_NODE_ON_RESUME:
972
0
  case DM_ADD_NODE_ON_CREATE:
973
0
    dmt->add_node = add_node;
974
0
    return 1;
975
0
  default:
976
0
    log_error("Unknown add node parameter");
977
0
    return 0;
978
0
  }
979
0
}
980
981
int dm_task_set_newuuid(struct dm_task *dmt, const char *newuuid)
982
0
{
983
0
  dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode();
984
0
  char mangled_uuid[DM_UUID_LEN];
985
0
  int r = 0;
986
987
0
  if (strlen(newuuid) >= DM_UUID_LEN) {
988
0
    log_error("Uuid \"%s\" too long", newuuid);
989
0
    return 0;
990
0
  }
991
992
0
  if (!check_multiple_mangled_string_allowed(newuuid, "new UUID", mangling_mode))
993
0
    return_0;
994
995
0
  if (mangling_mode != DM_STRING_MANGLING_NONE &&
996
0
      (r = mangle_string(newuuid, "new UUID", strlen(newuuid), mangled_uuid,
997
0
             sizeof(mangled_uuid), mangling_mode)) < 0) {
998
0
    log_error("Failed to mangle new device UUID \"%s\"", newuuid);
999
0
    return 0;
1000
0
  }
1001
1002
0
  if (r) {
1003
0
    log_debug_activation("New device uuid mangled [%s]: %s --> %s",
1004
0
             mangling_mode == DM_STRING_MANGLING_AUTO ? "auto" : "hex",
1005
0
             newuuid, mangled_uuid);
1006
0
    newuuid = mangled_uuid;
1007
0
  }
1008
1009
0
  dm_free(dmt->newname);
1010
0
  if (!(dmt->newname = dm_strdup(newuuid))) {
1011
0
    log_error("dm_task_set_newuuid: strdup(%s) failed", newuuid);
1012
0
    return 0;
1013
0
  }
1014
0
  dmt->new_uuid = 1;
1015
1016
0
  return 1;
1017
0
}
1018
1019
int dm_task_set_message(struct dm_task *dmt, const char *message)
1020
0
{
1021
0
  dm_free(dmt->message);
1022
0
  if (!(dmt->message = dm_strdup(message))) {
1023
0
    log_error("dm_task_set_message: strdup failed");
1024
0
    return 0;
1025
0
  }
1026
1027
0
  return 1;
1028
0
}
1029
1030
int dm_task_set_sector(struct dm_task *dmt, uint64_t sector)
1031
0
{
1032
0
  dmt->sector = sector;
1033
1034
0
  return 1;
1035
0
}
1036
1037
int dm_task_set_geometry(struct dm_task *dmt, const char *cylinders, const char *heads,
1038
       const char *sectors, const char *start)
1039
0
{
1040
0
  dm_free(dmt->geometry);
1041
0
  if (dm_asprintf(&(dmt->geometry), "%s %s %s %s",
1042
0
      cylinders, heads, sectors, start) < 0) {
1043
0
    log_error("dm_task_set_geometry: sprintf failed");
1044
0
    return 0;
1045
0
  }
1046
1047
0
  return 1;
1048
0
}
1049
1050
int dm_task_no_flush(struct dm_task *dmt)
1051
0
{
1052
0
  dmt->no_flush = 1;
1053
1054
0
  return 1;
1055
0
}
1056
1057
int dm_task_no_open_count(struct dm_task *dmt)
1058
0
{
1059
0
  dmt->no_open_count = 1;
1060
1061
0
  return 1;
1062
0
}
1063
1064
int dm_task_skip_lockfs(struct dm_task *dmt)
1065
0
{
1066
0
  dmt->skip_lockfs = 1;
1067
1068
0
  return 1;
1069
0
}
1070
1071
int dm_task_secure_data(struct dm_task *dmt)
1072
0
{
1073
0
  dmt->secure_data = 1;
1074
1075
0
  return 1;
1076
0
}
1077
1078
int dm_task_ima_measurement(struct dm_task *dmt)
1079
0
{
1080
0
  dmt->ima_measurement = 1;
1081
1082
0
  return 1;
1083
0
}
1084
1085
int dm_task_retry_remove(struct dm_task *dmt)
1086
0
{
1087
0
  dmt->retry_remove = 1;
1088
1089
0
  return 1;
1090
0
}
1091
1092
int dm_task_deferred_remove(struct dm_task *dmt)
1093
0
{
1094
0
  dmt->deferred_remove = 1;
1095
1096
0
  return 1;
1097
0
}
1098
1099
int dm_task_query_inactive_table(struct dm_task *dmt)
1100
0
{
1101
0
  dmt->query_inactive_table = 1;
1102
1103
0
  return 1;
1104
0
}
1105
1106
int dm_task_set_event_nr(struct dm_task *dmt, uint32_t event_nr)
1107
0
{
1108
0
  dmt->event_nr = event_nr;
1109
1110
0
  return 1;
1111
0
}
1112
1113
int dm_task_set_record_timestamp(struct dm_task *dmt)
1114
0
{
1115
0
  if (!_dm_ioctl_timestamp)
1116
0
    _dm_ioctl_timestamp = dm_timestamp_alloc();
1117
1118
0
  if (!_dm_ioctl_timestamp)
1119
0
    return_0;
1120
1121
0
  dmt->record_timestamp = 1;
1122
1123
0
  return 1;
1124
0
}
1125
1126
struct dm_timestamp *dm_task_get_ioctl_timestamp(struct dm_task *dmt)
1127
0
{
1128
0
  return dmt->record_timestamp ? _dm_ioctl_timestamp : NULL;
1129
0
}
1130
1131
struct target *create_target(uint64_t start, uint64_t len, const char *type,
1132
           const char *params)
1133
0
{
1134
0
  struct target *t;
1135
1136
0
  if (strlen(type) >= DM_MAX_TYPE_NAME) {
1137
0
    log_error("Target type name %s is too long.", type);
1138
0
    return NULL;
1139
0
  }
1140
1141
0
  if (!(t = dm_zalloc(sizeof(*t)))) {
1142
0
    log_error("create_target: malloc(%" PRIsize_t ") failed",
1143
0
        sizeof(*t));
1144
0
    return NULL;
1145
0
  }
1146
1147
0
  if (!(t->params = dm_strdup(params))) {
1148
0
    log_error("create_target: strdup(params) failed");
1149
0
    goto bad;
1150
0
  }
1151
1152
0
  if (!(t->type = dm_strdup(type))) {
1153
0
    log_error("create_target: strdup(type) failed");
1154
0
    goto bad;
1155
0
  }
1156
1157
0
  t->start = start;
1158
0
  t->length = len;
1159
0
  return t;
1160
1161
0
      bad:
1162
0
  _dm_zfree_string(t->params);
1163
0
  dm_free(t->type);
1164
0
  dm_free(t);
1165
0
  return NULL;
1166
0
}
1167
1168
static char *_add_target(struct target *t, char *out, char *end)
1169
0
{
1170
0
  char *out_sp = out;
1171
0
  struct dm_target_spec sp;
1172
0
  size_t sp_size = sizeof(struct dm_target_spec);
1173
0
  unsigned int backslash_count = 0;
1174
0
  int len;
1175
0
  char *pt;
1176
1177
0
  if (strlen(t->type) >= sizeof(sp.target_type)) {
1178
0
    log_error("Target type name %s is too long.", t->type);
1179
0
    return NULL;
1180
0
  }
1181
1182
0
  sp.status = 0;
1183
0
  sp.sector_start = t->start;
1184
0
  sp.length = t->length;
1185
0
  strncpy(sp.target_type, t->type, sizeof(sp.target_type) - 1);
1186
0
  sp.target_type[sizeof(sp.target_type) - 1] = '\0';
1187
1188
0
  out += sp_size;
1189
0
  pt = t->params;
1190
1191
0
  while (*pt)
1192
0
    if (*pt++ == '\\')
1193
0
      backslash_count++;
1194
1195
0
  len = strlen(t->params) + 1;
1196
1197
0
  if ((out >= end) || (out + len + backslash_count) >= end) {
1198
0
    log_error("Ran out of memory building ioctl parameter");
1199
0
    return NULL;
1200
0
  }
1201
1202
0
  if (backslash_count) {
1203
    /* replace "\" with "\\" */
1204
0
    pt = t->params;
1205
0
    do {
1206
0
      if (*pt == '\\')
1207
0
        *out++ = '\\';
1208
0
      *out++ = *pt++;
1209
0
    } while (*pt);
1210
0
    *out++ = '\0';
1211
0
  }
1212
0
  else {
1213
0
    memcpy(out, t->params, len);
1214
0
    out += len + backslash_count;
1215
0
  }
1216
1217
  /* align next block */
1218
0
  out = _align(out, ALIGNMENT);
1219
1220
0
  sp.next = out - out_sp;
1221
0
  memcpy(out_sp, &sp, sp_size);
1222
1223
0
  return out;
1224
0
}
1225
1226
static int _lookup_dev_name(uint64_t dev, char *buf, size_t len)
1227
0
{
1228
0
  struct dm_names *names;
1229
0
  unsigned next = 0;
1230
0
  struct dm_task *dmt;
1231
0
  int r = 0;
1232
 
1233
0
  if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1234
0
    return 0;
1235
 
1236
0
  if (!dm_task_run(dmt))
1237
0
    goto out;
1238
1239
0
  if (!(names = dm_task_get_names(dmt)))
1240
0
    goto out;
1241
 
1242
0
  if (!names->dev)
1243
0
    goto out;
1244
 
1245
0
  do {
1246
0
    names = (struct dm_names *)((char *) names + next);
1247
0
    if (names->dev == dev) {
1248
0
      memccpy(buf, names->name, 0, len);
1249
0
      r = 1;
1250
0
      break;
1251
0
    }
1252
0
    next = names->next;
1253
0
  } while (next);
1254
1255
0
      out:
1256
0
  dm_task_destroy(dmt);
1257
0
  return r;
1258
0
}
1259
1260
static int _add_params(int type)
1261
0
{
1262
0
  switch (type) {
1263
0
  case DM_DEVICE_REMOVE_ALL:
1264
0
  case DM_DEVICE_CREATE:
1265
0
  case DM_DEVICE_REMOVE:
1266
0
  case DM_DEVICE_SUSPEND:
1267
0
  case DM_DEVICE_STATUS:
1268
0
  case DM_DEVICE_CLEAR:
1269
0
  case DM_DEVICE_ARM_POLL:
1270
0
    return 0; /* IOCTL_FLAGS_NO_PARAMS in drivers/md/dm-ioctl.c */
1271
0
  default:
1272
0
    return 1;
1273
0
  }
1274
0
}
1275
1276
static struct dm_ioctl *_flatten(struct dm_task *dmt, unsigned repeat_count)
1277
0
{
1278
0
  size_t min_size;
1279
0
  const int (*version)[3];
1280
1281
0
  struct dm_ioctl *dmi;
1282
0
  struct target *t;
1283
0
  struct dm_target_msg *tmsg;
1284
0
  size_t len = sizeof(struct dm_ioctl);
1285
0
  size_t message_len = 0, newname_len = 0, geometry_len = 0;
1286
0
  char *b, *e;
1287
0
  int count = 0;
1288
1289
0
  if (_add_params(dmt->type))
1290
0
    for (t = dmt->head; t; t = t->next) {
1291
0
      len += sizeof(struct dm_target_spec);
1292
0
      len += strlen(t->params) + 1 + ALIGNMENT;
1293
0
      count++;
1294
0
    }
1295
0
  else if (dmt->head)
1296
0
    log_debug_activation(INTERNAL_ERROR "dm '%s' ioctl should not define parameters.",
1297
0
             _cmd_data_v4[dmt->type].name);
1298
0
  switch (dmt->type) {
1299
0
  case DM_DEVICE_CREATE:
1300
0
  case DM_DEVICE_DEPS:
1301
0
  case DM_DEVICE_LIST:
1302
0
  case DM_DEVICE_STATUS:
1303
0
  case DM_DEVICE_TABLE:
1304
0
  case DM_DEVICE_TARGET_MSG:
1305
0
    min_size = 16 * 1024;
1306
0
    break;
1307
0
  default:
1308
0
    min_size = 2 * 1024;
1309
0
  }
1310
1311
0
  if (count && (dmt->sector || dmt->message)) {
1312
0
    log_error("targets and message are incompatible");
1313
0
    return NULL;
1314
0
  }
1315
1316
0
  if (count && dmt->newname) {
1317
0
    log_error("targets and rename are incompatible");
1318
0
    return NULL;
1319
0
  }
1320
1321
0
  if (count && dmt->geometry) {
1322
0
    log_error("targets and geometry are incompatible");
1323
0
    return NULL;
1324
0
  }
1325
1326
0
  if (dmt->newname && (dmt->sector || dmt->message)) {
1327
0
    log_error("message and rename are incompatible");
1328
0
    return NULL;
1329
0
  }
1330
1331
0
  if (dmt->newname && dmt->geometry) {
1332
0
    log_error("geometry and rename are incompatible");
1333
0
    return NULL;
1334
0
  }
1335
1336
0
  if (dmt->geometry && (dmt->sector || dmt->message)) {
1337
0
    log_error("geometry and message are incompatible");
1338
0
    return NULL;
1339
0
  }
1340
1341
0
  if (dmt->sector && !dmt->message) {
1342
0
    log_error("message is required with sector");
1343
0
    return NULL;
1344
0
  }
1345
1346
0
  if (dmt->newname) {
1347
0
    newname_len = strlen(dmt->newname) + 1;
1348
0
    len += newname_len;
1349
0
  }
1350
1351
0
  if (dmt->message) {
1352
0
    message_len = strlen(dmt->message) + 1;
1353
0
    len += sizeof(struct dm_target_msg) + message_len;
1354
0
  }
1355
1356
0
  if (dmt->geometry) {
1357
0
    geometry_len = strlen(dmt->geometry) + 1;
1358
0
    len += geometry_len;
1359
0
  }
1360
1361
  /*
1362
   * Give len a minimum size so that we have space to store
1363
   * dependencies or status information.
1364
   */
1365
0
  if (len < min_size)
1366
0
    len = min_size;
1367
1368
  /* Increase buffer size if repeating because buffer was too small */
1369
0
  while (repeat_count--)
1370
0
    len *= 2;
1371
1372
0
  if (!(dmi = dm_zalloc(len)))
1373
0
    return NULL;
1374
1375
0
  version = &_cmd_data_v4[dmt->type].version;
1376
1377
0
  dmi->version[0] = (*version)[0];
1378
0
  dmi->version[1] = (*version)[1];
1379
0
  dmi->version[2] = (*version)[2];
1380
1381
0
  dmi->data_size = len;
1382
0
  dmi->data_start = sizeof(struct dm_ioctl);
1383
1384
0
  if (dmt->minor >= 0) {
1385
0
    if (!_dm_multiple_major_support && dmt->allow_default_major_fallback &&
1386
0
        dmt->major != (int) _dm_device_major) {
1387
0
      log_verbose("Overriding major number of %d "
1388
0
            "with %u for persistent device.",
1389
0
            dmt->major, _dm_device_major);
1390
0
      dmt->major = _dm_device_major;
1391
0
    }
1392
1393
0
    if (dmt->major <= 0) {
1394
0
      log_error("Missing major number for persistent device.");
1395
0
      goto bad;
1396
0
    }
1397
1398
0
    dmi->flags |= DM_PERSISTENT_DEV_FLAG;
1399
0
    dmi->dev = MKDEV(dmt->major, dmt->minor);
1400
0
  }
1401
1402
  /* Does driver support device number referencing? */
1403
0
  if (_dm_version_minor < 3 && !DEV_NAME(dmt) && !DEV_UUID(dmt) && dmi->dev) {
1404
0
    if (!_lookup_dev_name(dmi->dev, dmi->name, sizeof(dmi->name))) {
1405
0
      log_error("Unable to find name for device (%" PRIu32
1406
0
          ":%" PRIu32 ")", dmt->major, dmt->minor);
1407
0
      goto bad;
1408
0
    }
1409
0
    log_verbose("device (%" PRIu32 ":%" PRIu32 ") is %s "
1410
0
          "for compatibility with old kernel",
1411
0
          dmt->major, dmt->minor, dmi->name);
1412
0
  }
1413
1414
  /* FIXME Until resume ioctl supplies name, use dev_name for readahead */
1415
0
  if (DEV_NAME(dmt) &&
1416
0
      (((dmt->type != DM_DEVICE_RESUME) &&
1417
0
        (dmt->type != DM_DEVICE_RELOAD)) ||
1418
0
       (dmt->minor < 0) || (dmt->major < 0)))
1419
    /* When RESUME or RELOAD sets maj:min and dev_name, use just maj:min,
1420
     * passed dev_name is useful for better error/debug messages */
1421
0
    memccpy(dmi->name, DEV_NAME(dmt), 0, sizeof(dmi->name));
1422
1423
0
  if (DEV_UUID(dmt))
1424
0
    memccpy(dmi->uuid, DEV_UUID(dmt), 0, sizeof(dmi->uuid));
1425
1426
0
  if (dmt->type == DM_DEVICE_SUSPEND)
1427
0
    dmi->flags |= DM_SUSPEND_FLAG;
1428
0
  if (dmt->no_flush) {
1429
0
    if (_dm_version_minor < 12)
1430
0
      log_verbose("No flush flag unsupported by kernel. "
1431
0
            "Buffers will be flushed.");
1432
0
    else
1433
0
      dmi->flags |= DM_NOFLUSH_FLAG;
1434
0
  }
1435
0
  if (dmt->read_only)
1436
0
    dmi->flags |= DM_READONLY_FLAG;
1437
0
  if (dmt->skip_lockfs)
1438
0
    dmi->flags |= DM_SKIP_LOCKFS_FLAG;
1439
0
  if (dmt->deferred_remove && (dmt->type == DM_DEVICE_REMOVE || dmt->type == DM_DEVICE_REMOVE_ALL))
1440
0
    dmi->flags |= DM_DEFERRED_REMOVE;
1441
1442
0
  if (dmt->secure_data) {
1443
0
    if (_dm_version_minor < 20)
1444
0
      log_verbose("Secure data flag unsupported by kernel. "
1445
0
            "Buffers will not be wiped after use.");
1446
0
    dmi->flags |= DM_SECURE_DATA_FLAG;
1447
0
  }
1448
0
  if (dmt->query_inactive_table) {
1449
0
    if (!_dm_inactive_supported())
1450
0
      log_warn_suppress(_dm_warn_inactive_suppress++,
1451
0
            "WARNING: Inactive table query unsupported by kernel. "
1452
0
            "It will use live table.");
1453
0
    dmi->flags |= DM_QUERY_INACTIVE_TABLE_FLAG;
1454
0
  }
1455
0
  if (dmt->new_uuid) {
1456
0
    if (_dm_version_minor < 19) {
1457
0
      log_error("Setting UUID unsupported by kernel. "
1458
0
          "Aborting operation.");
1459
0
      goto bad;
1460
0
    }
1461
0
    dmi->flags |= DM_UUID_FLAG;
1462
0
  }
1463
0
  if (dmt->ima_measurement) {
1464
0
    if (_dm_version_minor < 45) {
1465
0
      log_error("IMA measurement unsupported by kernel. "
1466
0
          "Aborting operation.");
1467
0
      goto bad;
1468
0
    }
1469
0
    dmi->flags |= DM_IMA_MEASUREMENT_FLAG;
1470
0
  }
1471
1472
0
  dmi->target_count = count;
1473
0
  dmi->event_nr = dmt->event_nr;
1474
1475
0
  b = (char *) (dmi + 1);
1476
0
  e = (char *) dmi + len;
1477
1478
0
  if (_add_params(dmt->type))
1479
0
    for (t = dmt->head; t; t = t->next)
1480
0
      if (!(b = _add_target(t, b, e)))
1481
0
        goto_bad;
1482
1483
0
  if (dmt->newname)
1484
0
    memcpy(b, dmt->newname, newname_len);
1485
1486
0
  if (dmt->message) {
1487
0
    tmsg = (struct dm_target_msg *) b;
1488
0
    tmsg->sector = dmt->sector;
1489
0
    memcpy(tmsg->message, dmt->message, message_len);
1490
0
  }
1491
1492
0
  if (dmt->geometry)
1493
0
    memcpy(b, dmt->geometry, geometry_len);
1494
1495
0
  return dmi;
1496
1497
0
      bad:
1498
0
  _dm_zfree_dmi(dmi);
1499
0
  return NULL;
1500
0
}
1501
1502
static int _process_mapper_dir(struct dm_task *dmt)
1503
0
{
1504
0
  struct dirent *dirent;
1505
0
  DIR *d;
1506
0
  const char *dir;
1507
0
  int r = 1;
1508
1509
0
  dir = dm_dir();
1510
0
  if (!(d = opendir(dir))) {
1511
0
    log_sys_error("opendir", dir);
1512
0
    return 0;
1513
0
  }
1514
1515
0
  while ((dirent = readdir(d))) {
1516
0
    if (!strcmp(dirent->d_name, ".") ||
1517
0
        !strcmp(dirent->d_name, "..") ||
1518
0
        !strcmp(dirent->d_name, "control"))
1519
0
      continue;
1520
0
    if (!dm_task_set_name(dmt, dirent->d_name)) {
1521
0
      r = 0;
1522
0
      stack;
1523
0
      continue; /* try next name */
1524
0
    }
1525
0
    if (!dm_task_run(dmt)) {
1526
0
      r = 0;
1527
0
      stack;  /* keep going */
1528
0
    }
1529
0
  }
1530
1531
0
  if (closedir(d))
1532
0
    log_sys_debug("closedir", dir);
1533
1534
0
  return r;
1535
0
}
1536
1537
static int _process_all_v4(struct dm_task *dmt)
1538
0
{
1539
0
  struct dm_task *task;
1540
0
  struct dm_names *names;
1541
0
  unsigned next = 0;
1542
0
  int r = 1;
1543
1544
0
  if (!(task = dm_task_create(DM_DEVICE_LIST)))
1545
0
    return 0;
1546
1547
0
  if (!dm_task_run(task)) {
1548
0
    r = 0;
1549
0
    goto out;
1550
0
  }
1551
1552
0
  if (!(names = dm_task_get_names(task))) {
1553
0
    r = 0;
1554
0
    goto out;
1555
0
  }
1556
1557
0
  if (!names->dev)
1558
0
    goto out;
1559
1560
0
  do {
1561
0
    names = (struct dm_names *)((char *) names + next);
1562
0
    if (!dm_task_set_name(dmt, names->name)) {
1563
0
      r = 0;
1564
0
      goto out;
1565
0
    }
1566
0
    if (!dm_task_run(dmt))
1567
0
      r = 0;
1568
0
    next = names->next;
1569
0
  } while (next);
1570
1571
0
      out:
1572
0
  dm_task_destroy(task);
1573
0
  return r;
1574
0
}
1575
1576
static int _mknodes_v4(struct dm_task *dmt)
1577
0
{
1578
0
  (void) _process_mapper_dir(dmt);
1579
1580
0
  return _process_all_v4(dmt);
1581
0
}
1582
1583
/*
1584
 * If an operation that uses a cookie fails, decrement the
1585
 * semaphore instead of udev.
1586
 */
1587
static int _udev_complete(struct dm_task *dmt)
1588
0
{
1589
0
  uint16_t base;
1590
1591
0
  if (dmt->cookie_set &&
1592
0
      (base = dmt->event_nr & ~DM_UDEV_FLAGS_MASK))
1593
    /* strip flags from the cookie and use cookie magic instead */
1594
0
    return dm_udev_complete(base | (DM_COOKIE_MAGIC <<
1595
0
            DM_UDEV_FLAGS_SHIFT));
1596
1597
0
  return 1;
1598
0
}
1599
1600
#ifdef DM_IOCTLS
1601
static int _check_uevent_generated(struct dm_ioctl *dmi)
1602
0
{
1603
0
  if (!dm_check_version() ||
1604
0
      ((_dm_version == 4) ? _dm_version_minor < 17 : _dm_version < 4))
1605
    /* can't check, assume uevent is generated */
1606
0
    return 1;
1607
1608
0
  return dmi->flags & DM_UEVENT_GENERATED_FLAG;
1609
0
}
1610
#endif
1611
1612
static int _create_and_load_v4(struct dm_task *dmt)
1613
0
{
1614
0
  struct dm_info info;
1615
0
  struct dm_task *task;
1616
0
  int r, ioctl_errno = 0;
1617
0
  uint32_t cookie;
1618
1619
  /* Use new task struct to create the device */
1620
0
  if (!(task = dm_task_create(DM_DEVICE_CREATE))) {
1621
0
    _udev_complete(dmt);
1622
0
    return_0;
1623
0
  }
1624
1625
  /* Copy across relevant fields */
1626
0
  if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name))
1627
0
    goto_bad;
1628
1629
0
  if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid))
1630
0
    goto_bad;
1631
1632
0
  task->major = dmt->major;
1633
0
  task->minor = dmt->minor;
1634
0
  task->uid = dmt->uid;
1635
0
  task->gid = dmt->gid;
1636
0
  task->mode = dmt->mode;
1637
  /* FIXME: Just for udev_check in dm_task_run. Can we avoid this? */
1638
0
  task->event_nr = dmt->event_nr & DM_UDEV_FLAGS_MASK;
1639
0
  task->cookie_set = dmt->cookie_set;
1640
0
  task->add_node = dmt->add_node;
1641
1642
0
  if (!dm_task_run(task)) {
1643
0
    ioctl_errno = task->ioctl_errno;
1644
0
    goto_bad;
1645
0
  }
1646
1647
0
  if (!dm_task_get_info(task, &info) || !info.exists)
1648
0
    goto_bad;
1649
1650
0
  dm_task_destroy(task);
1651
1652
  /* Next load the table */
1653
0
  if (!(task = dm_task_create(DM_DEVICE_RELOAD))) {
1654
0
    stack;
1655
0
    _udev_complete(dmt);
1656
0
    goto revert;
1657
0
  }
1658
1659
  /* Copy across relevant fields */
1660
0
  if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
1661
0
    stack;
1662
0
    dm_task_destroy(task);
1663
0
    _udev_complete(dmt);
1664
0
    goto revert;
1665
0
  }
1666
1667
0
  task->major = info.major;
1668
0
  task->minor = info.minor;
1669
0
  task->read_only = dmt->read_only;
1670
0
  task->head = dmt->head;
1671
0
  task->tail = dmt->tail;
1672
0
  task->secure_data = dmt->secure_data;
1673
0
  task->ima_measurement = dmt->ima_measurement;
1674
1675
0
  r = dm_task_run(task);
1676
0
  if (!r)
1677
0
    ioctl_errno = task->ioctl_errno;
1678
1679
0
  task->head = NULL;
1680
0
  task->tail = NULL;
1681
0
  dm_task_destroy(task);
1682
1683
0
  if (!r) {
1684
0
    stack;
1685
0
    _udev_complete(dmt);
1686
0
    goto revert;
1687
0
  }
1688
1689
  /* Use the original structure last so the info will be correct */
1690
0
  dmt->type = DM_DEVICE_RESUME;
1691
0
  dm_free(dmt->uuid);
1692
0
  dmt->uuid = NULL;
1693
0
  dm_free(dmt->mangled_uuid);
1694
0
  dmt->mangled_uuid = NULL;
1695
  /* coverity[double_free] recursive function call */
1696
0
  _dm_task_free_targets(dmt);
1697
1698
0
  if (dm_task_run(dmt))
1699
0
    return 1;
1700
1701
0
      revert:
1702
0
  dmt->type = DM_DEVICE_REMOVE;
1703
0
  dm_free(dmt->uuid);
1704
0
  dmt->uuid = NULL;
1705
0
  dm_free(dmt->mangled_uuid);
1706
0
  dmt->mangled_uuid = NULL;
1707
  /* coverity[double_free] recursive function call */
1708
0
  _dm_task_free_targets(dmt);
1709
1710
  /*
1711
   * Also udev-synchronize "remove" dm task that is a part of this revert!
1712
   * But only if the original dm task was supposed to be synchronized.
1713
   */
1714
0
  if (dmt->cookie_set) {
1715
0
    cookie = (dmt->event_nr & ~DM_UDEV_FLAGS_MASK) |
1716
0
       (DM_COOKIE_MAGIC << DM_UDEV_FLAGS_SHIFT);
1717
0
    if (!dm_task_set_cookie(dmt, &cookie,
1718
0
          (dmt->event_nr & DM_UDEV_FLAGS_MASK) >>
1719
0
          DM_UDEV_FLAGS_SHIFT))
1720
0
      stack; /* keep going */
1721
0
  }
1722
1723
0
  if (!dm_task_run(dmt))
1724
0
    log_error("Failed to revert device creation.");
1725
1726
0
  if (ioctl_errno != 0)
1727
0
    dmt->ioctl_errno =  ioctl_errno;
1728
1729
0
  return 0;
1730
1731
0
      bad:
1732
0
  dm_task_destroy(task);
1733
0
  _udev_complete(dmt);
1734
1735
0
  if (ioctl_errno != 0)
1736
0
    dmt->ioctl_errno =  ioctl_errno;
1737
1738
0
  return 0;
1739
0
}
1740
1741
uint64_t dm_task_get_existing_table_size(struct dm_task *dmt)
1742
0
{
1743
0
  return dmt->existing_table_size;
1744
0
}
1745
1746
static int _reload_with_suppression_v4(struct dm_task *dmt)
1747
0
{
1748
0
  struct dm_task *task;
1749
0
  struct target *t1, *t2;
1750
0
  size_t len;
1751
0
  int r;
1752
1753
  /* New task to get existing table information */
1754
0
  if (!(task = dm_task_create(DM_DEVICE_TABLE))) {
1755
0
    log_error("Failed to create device-mapper task struct");
1756
0
    return 0;
1757
0
  }
1758
1759
  /* Copy across relevant fields */
1760
0
  if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
1761
0
    dm_task_destroy(task);
1762
0
    return 0;
1763
0
  }
1764
1765
0
  if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
1766
0
    dm_task_destroy(task);
1767
0
    return 0;
1768
0
  }
1769
1770
0
  task->major = dmt->major;
1771
0
  task->minor = dmt->minor;
1772
1773
0
  r = dm_task_run(task);
1774
1775
0
  if (!r) {
1776
0
    dm_task_destroy(task);
1777
0
    return r;
1778
0
  }
1779
1780
  /* Store existing table size */
1781
0
  t2 = task->head;
1782
0
  while (t2 && t2->next)
1783
0
    t2 = t2->next;
1784
0
  dmt->existing_table_size = t2 ? t2->start + t2->length : 0;
1785
1786
0
  if (((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0) != dmt->read_only)
1787
0
    goto no_match;
1788
1789
0
  t1 = dmt->head;
1790
0
  t2 = task->head;
1791
1792
0
  while (t1 && t2) {
1793
0
    len = strlen(t2->params);
1794
0
    while (len-- > 0 && t2->params[len] == ' ')
1795
0
      t2->params[len] = '\0';
1796
1797
0
    if (t1->start != t2->start) {
1798
0
      log_debug("reload %u:%u diff start %llu %llu type %s %s", task->major, task->minor,
1799
0
           (unsigned long long)t1->start, (unsigned long long)t2->start, t1->type, t2->type);
1800
0
      goto no_match;
1801
0
    }
1802
0
    if (t1->length != t2->length) {
1803
0
      log_debug("reload %u:%u diff length %llu %llu type %s %s", task->major, task->minor,
1804
0
          (unsigned long long)t1->length, (unsigned long long)t2->length, t1->type, t2->type);
1805
0
      goto no_match;
1806
0
    }
1807
0
    if (strcmp(t1->type, t2->type)) {
1808
0
      log_debug("reload %u:%u diff type %s %s", task->major, task->minor, t1->type, t2->type);
1809
0
      goto no_match;
1810
0
    }
1811
0
    if (strcmp(t1->params, t2->params)) {
1812
0
      if (dmt->skip_reload_params_compare) {
1813
0
        log_debug("reload %u:%u diff params ignore for type %s",
1814
0
            task->major, task->minor, t1->type);
1815
0
        log_debug("reload params1 %s", t1->params);
1816
0
        log_debug("reload params2 %s", t2->params);
1817
0
      } else {
1818
0
        log_debug("reload %u:%u diff params for type %s",
1819
0
            task->major, task->minor, t1->type);
1820
0
        log_debug("reload params1 %s", t1->params);
1821
0
        log_debug("reload params2 %s", t2->params);
1822
0
        goto no_match;
1823
0
      }
1824
0
    }
1825
1826
0
    t1 = t1->next;
1827
0
    t2 = t2->next;
1828
0
  }
1829
  
1830
0
  if (!t1 && !t2) {
1831
0
    dmt->dmi.v4 = task->dmi.v4;
1832
0
    task->dmi.v4 = NULL;
1833
0
    dm_task_destroy(task);
1834
0
    return 1;
1835
0
  }
1836
1837
0
no_match:
1838
0
  dm_task_destroy(task);
1839
1840
  /* Now do the original reload */
1841
0
  dmt->suppress_identical_reload = 0;
1842
0
  r = dm_task_run(dmt);
1843
1844
0
  return r;
1845
0
}
1846
1847
static int _check_children_not_suspended_v4(struct dm_task *dmt, uint64_t device)
1848
0
{
1849
0
  struct dm_task *task;
1850
0
  struct dm_info info;
1851
0
  struct dm_deps *deps;
1852
0
  int r = 0;
1853
0
  uint32_t i;
1854
1855
  /* Find dependencies */
1856
0
  if (!(task = dm_task_create(DM_DEVICE_DEPS)))
1857
0
    return 0;
1858
1859
  /* Copy across or set relevant fields */
1860
0
  if (device) {
1861
0
    task->major = MAJOR(device);
1862
0
    task->minor = MINOR(device);
1863
0
  } else {
1864
0
    if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name))
1865
0
      goto out;
1866
1867
0
    if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid))
1868
0
      goto out;
1869
1870
0
    task->major = dmt->major;
1871
0
    task->minor = dmt->minor;
1872
0
  }
1873
1874
0
  task->uid = dmt->uid;
1875
0
  task->gid = dmt->gid;
1876
0
  task->mode = dmt->mode;
1877
  /* FIXME: Just for udev_check in dm_task_run. Can we avoid this? */
1878
0
  task->event_nr = dmt->event_nr & DM_UDEV_FLAGS_MASK;
1879
0
  task->cookie_set = dmt->cookie_set;
1880
0
  task->add_node = dmt->add_node;
1881
  
1882
0
  if (!(r = dm_task_run(task)))
1883
0
    goto out;
1884
1885
0
  if (!dm_task_get_info(task, &info) || !info.exists)
1886
0
    goto out;
1887
1888
  /*
1889
   * Warn if any of the devices this device depends upon are already
1890
   * suspended: I/O could become trapped between the two devices.
1891
   */
1892
0
  if (info.suspended) {
1893
0
    if (!device)
1894
0
      log_debug_activation("Attempting to suspend a device that is already suspended "
1895
0
               "(%u:%u)", info.major, info.minor);
1896
0
    else
1897
0
      log_error(INTERNAL_ERROR "Attempt to suspend device %s%s%s%.0d%s%.0d%s%s"
1898
0
          "that uses already-suspended device (%u:%u)", 
1899
0
          DEV_NAME(dmt) ? : "", DEV_UUID(dmt) ? : "",
1900
0
          dmt->major > 0 ? "(" : "",
1901
0
          dmt->major > 0 ? dmt->major : 0,
1902
0
          dmt->major > 0 ? ":" : "",
1903
0
          dmt->minor > 0 ? dmt->minor : 0,
1904
0
          dmt->major > 0 && dmt->minor == 0 ? "0" : "",
1905
0
          dmt->major > 0 ? ") " : "",
1906
0
          info.major, info.minor);
1907
1908
    /* No need for further recursion */
1909
0
    r = 1;
1910
0
    goto out;
1911
0
  }
1912
1913
0
  if (!(deps = dm_task_get_deps(task)))
1914
0
    goto out;
1915
1916
0
  for (i = 0; i < deps->count; i++) {
1917
    /* Only recurse with dm devices */
1918
0
    if (MAJOR(deps->device[i]) != _dm_device_major)
1919
0
      continue;
1920
1921
0
    if (!_check_children_not_suspended_v4(task, deps->device[i]))
1922
0
      goto out;
1923
0
  }
1924
1925
0
  r = 1;
1926
1927
0
out:
1928
0
  dm_task_destroy(task);
1929
1930
0
  return r;
1931
0
}
1932
1933
static int _suspend_with_validation_v4(struct dm_task *dmt)
1934
0
{
1935
  /* Avoid recursion */
1936
0
  dmt->enable_checks = 0;
1937
1938
  /*
1939
   * Ensure we can't leave any I/O trapped between suspended devices.
1940
   */
1941
0
  if (!_check_children_not_suspended_v4(dmt, 0))
1942
0
    return 0;
1943
1944
  /* Finally, perform the original suspend. */
1945
0
  return dm_task_run(dmt);
1946
0
}
1947
1948
static const char *_sanitise_message(char *message)
1949
0
{
1950
0
  const char *sanitised_message = message ?: "";
1951
1952
  /* FIXME: Check for whitespace variations. */
1953
  /* This traps what cryptsetup sends us. */
1954
0
  if (message && !strncasecmp(message, "key set", 7))
1955
0
    sanitised_message = "key set";
1956
1957
0
  return sanitised_message;
1958
0
}
1959
1960
#ifdef DM_IOCTLS
1961
static int _do_dm_ioctl_unmangle_string(char *str, const char *str_name,
1962
          char *buf, size_t buf_size,
1963
          dm_string_mangling_t mode)
1964
0
{
1965
0
  int r;
1966
1967
0
  if (mode == DM_STRING_MANGLING_NONE)
1968
0
    return 1;
1969
1970
0
  if (!check_multiple_mangled_string_allowed(str, str_name, mode))
1971
0
    return_0;
1972
1973
0
  if ((r = unmangle_string(str, str_name, strlen(str), buf, buf_size, mode)) < 0) {
1974
0
    log_debug_activation("_do_dm_ioctl_unmangle_string: failed to "
1975
0
             "unmangle %s \"%s\"", str_name, str);
1976
0
    return 0;
1977
0
  }
1978
1979
0
  if (r)
1980
0
    memcpy(str, buf, strlen(buf) + 1);
1981
1982
0
  return 1;
1983
0
}
1984
1985
static int _dm_ioctl_unmangle_names(int type, struct dm_ioctl *dmi)
1986
0
{
1987
0
  char buf[DM_NAME_LEN];
1988
0
  char buf_uuid[DM_UUID_LEN];
1989
0
  struct dm_name_list *names;
1990
0
  unsigned next = 0;
1991
0
  char *name;
1992
0
  int r = 1;
1993
0
  uint32_t *event_nr;
1994
0
  char *uuid_ptr;
1995
0
  dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode();
1996
1997
0
  if ((name = dmi->name))
1998
0
    r &= _do_dm_ioctl_unmangle_string(name, "name", buf, sizeof(buf),
1999
0
              mangling_mode);
2000
2001
0
  if (type == DM_DEVICE_LIST &&
2002
0
      ((names = ((struct dm_name_list *) ((char *)dmi + dmi->data_start)))) &&
2003
0
      names->dev) {
2004
0
    do {
2005
0
      names = (struct dm_name_list *)((char *) names + next);
2006
0
      event_nr = _align_ptr(names->name + strlen(names->name) + 1);
2007
0
      r &= _do_dm_ioctl_unmangle_string(names->name, "name",
2008
0
                buf, sizeof(buf), mangling_mode);
2009
      /* Unmangle also UUID within same loop */
2010
0
      if (_check_has_event_nr() &&
2011
0
          (event_nr[1] & DM_NAME_LIST_FLAG_HAS_UUID)) {
2012
0
        uuid_ptr = _align_ptr(event_nr + 2);
2013
0
        r &= _do_dm_ioctl_unmangle_string(uuid_ptr, "UUID", buf_uuid,
2014
0
                  sizeof(buf_uuid), mangling_mode);
2015
0
      }
2016
0
      next = names->next;
2017
0
    } while (next);
2018
0
  }
2019
2020
0
  return r;
2021
0
}
2022
2023
static int _dm_ioctl_unmangle_uuids(int type, struct dm_ioctl *dmi)
2024
0
{
2025
0
  char buf[DM_UUID_LEN];
2026
0
  char *uuid = dmi->uuid;
2027
2028
0
  if (uuid)
2029
0
    return _do_dm_ioctl_unmangle_string(uuid, "UUID", buf, sizeof(buf),
2030
0
                dm_get_name_mangling_mode());
2031
2032
0
  return 1;
2033
0
}
2034
#endif
2035
2036
static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt, unsigned command,
2037
             unsigned buffer_repeat_count,
2038
             unsigned retry_repeat_count,
2039
             int *retryable)
2040
0
{
2041
0
  struct dm_ioctl *dmi;
2042
0
  int ioctl_with_uevent;
2043
0
  int r;
2044
2045
0
  dmt->ioctl_errno = 0;
2046
2047
0
  dmi = _flatten(dmt, buffer_repeat_count);
2048
0
  if (!dmi) {
2049
0
    log_error("Couldn't create ioctl argument.");
2050
0
    return NULL;
2051
0
  }
2052
2053
0
  if (dmt->type == DM_DEVICE_TABLE)
2054
0
    dmi->flags |= DM_STATUS_TABLE_FLAG;
2055
2056
0
  dmi->flags |= DM_EXISTS_FLAG; /* FIXME */
2057
2058
0
  if (dmt->no_open_count)
2059
0
    dmi->flags |= DM_SKIP_BDGET_FLAG;
2060
2061
0
  ioctl_with_uevent = dmt->type == DM_DEVICE_RESUME ||
2062
0
          dmt->type == DM_DEVICE_REMOVE ||
2063
0
          dmt->type == DM_DEVICE_RENAME;
2064
2065
0
  if (ioctl_with_uevent && dm_cookie_supported()) {
2066
    /*
2067
     * Always mark events coming from libdevmapper as
2068
     * "primary sourced". This is needed to distinguish
2069
     * any spurious events so we can act appropriately.
2070
     * This needs to be applied even when udev_sync is
2071
     * not used because udev flags could be used alone.
2072
     */
2073
0
    dmi->event_nr |= DM_UDEV_PRIMARY_SOURCE_FLAG <<
2074
0
         DM_UDEV_FLAGS_SHIFT;
2075
2076
    /*
2077
     * Prevent udev vs. libdevmapper race when processing nodes
2078
     * and symlinks. This can happen when the udev rules are
2079
     * installed and udev synchronization code is enabled in
2080
     * libdevmapper but the software using libdevmapper does not
2081
     * make use of it (by not calling dm_task_set_cookie before).
2082
     * We need to instruct the udev rules not to be applied at
2083
     * all in this situation so we can gracefully fallback to
2084
     * libdevmapper's node and symlink creation code.
2085
     */
2086
0
    if (!dmt->cookie_set && dm_udev_get_sync_support()) {
2087
0
      log_debug_activation("Cookie value is not set while trying to call %s "
2088
0
               "ioctl. Please, consider using libdevmapper's udev "
2089
0
               "synchronization interface or disable it explicitly "
2090
0
               "by calling dm_udev_set_sync_support(0).",
2091
0
               dmt->type == DM_DEVICE_RESUME ? "DM_DEVICE_RESUME" :
2092
0
               dmt->type == DM_DEVICE_REMOVE ? "DM_DEVICE_REMOVE" :
2093
0
                       "DM_DEVICE_RENAME");
2094
0
      log_debug_activation("Switching off device-mapper and all subsystem related "
2095
0
               "udev rules. Falling back to libdevmapper node creation.");
2096
      /*
2097
       * Disable general dm and subsystem rules but keep
2098
       * dm disk rules if not flagged out explicitly before.
2099
       * We need /dev/disk content for the software that expects it.
2100
      */
2101
0
      dmi->event_nr |= (DM_UDEV_DISABLE_DM_RULES_FLAG |
2102
0
            DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG) <<
2103
0
           DM_UDEV_FLAGS_SHIFT;
2104
0
    }
2105
0
  }
2106
2107
0
  log_debug_activation("dm %s %s%s %s%s%s %s%.0d%s%.0d%s"
2108
0
           "%s[ %s%s%s%s%s%s%s%s%s%s] %.0" PRIu64 " %s [%u] (*%u)",
2109
0
           _cmd_data_v4[dmt->type].name,
2110
0
           dmt->new_uuid ? "UUID " : "",
2111
0
           dmi->name, dmi->uuid, dmt->newname ? " " : "",
2112
0
           dmt->newname ? dmt->newname : "",
2113
0
           dmt->major > 0 ? "(" : "",
2114
0
           dmt->major > 0 ? dmt->major : 0,
2115
0
           dmt->major > 0 ? ":" : "",
2116
0
           dmt->minor > 0 ? dmt->minor : 0,
2117
0
           dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2118
0
           dmt->major > 0 ? ") " : "",
2119
0
           dmt->no_open_count ? "noopencount " : "opencount ",
2120
0
           dmt->no_flush ? "noflush " : "flush ",
2121
0
           dmt->read_only ? "readonly " : "",
2122
0
           dmt->skip_lockfs ? "skiplockfs " : "",
2123
0
           dmt->retry_remove ? "retryremove " : "",
2124
0
           dmt->deferred_remove ? "deferredremove " : "",
2125
0
           dmt->secure_data ? "securedata " : "",
2126
0
           dmt->ima_measurement ? "ima_measurement " : "",
2127
0
           dmt->query_inactive_table ? "inactive " : "",
2128
0
           dmt->enable_checks ? "enablechecks " : "",
2129
0
           dmt->sector, _sanitise_message(dmt->message),
2130
0
           dmi->data_size, retry_repeat_count);
2131
0
#ifdef DM_IOCTLS
2132
0
  r = ioctl(_control_fd, command, dmi);
2133
2134
0
  if (dmt->record_timestamp)
2135
0
    if (!dm_timestamp_get(_dm_ioctl_timestamp))
2136
0
      stack;
2137
2138
0
  if (r < 0 && dmt->expected_errno != errno) {
2139
0
    dmt->ioctl_errno = errno;
2140
0
    if (dmt->ioctl_errno == ENXIO && ((dmt->type == DM_DEVICE_INFO) ||
2141
0
              (dmt->type == DM_DEVICE_MKNODES) ||
2142
0
              (dmt->type == DM_DEVICE_STATUS)))
2143
0
      dmi->flags &= ~DM_EXISTS_FLAG; /* FIXME */
2144
0
    else {
2145
0
      if (_log_suppress || dmt->ioctl_errno == EINTR)
2146
0
        log_verbose("device-mapper: %s ioctl on %s %s%s%.0d%s%.0d%s%s "
2147
0
              "failed: %s",
2148
0
              _cmd_data_v4[dmt->type].name,
2149
0
              dmi->name[0] ? dmi->name : DEV_NAME(dmt) ? : "",
2150
0
              dmi->uuid[0] ? dmi->uuid : DEV_UUID(dmt) ? : "",
2151
0
              dmt->major > 0 ? "(" : "",
2152
0
              dmt->major > 0 ? dmt->major : 0,
2153
0
              dmt->major > 0 ? ":" : "",
2154
0
              dmt->minor > 0 ? dmt->minor : 0,
2155
0
              dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2156
0
              dmt->major > 0 ? ")" : "",
2157
0
              strerror(dmt->ioctl_errno));
2158
0
      else
2159
0
        log_error("device-mapper: %s ioctl on %s %s%s%.0d%s%.0d%s%s "
2160
0
            "failed: %s",
2161
0
            _cmd_data_v4[dmt->type].name,
2162
0
            dmi->name[0] ? dmi->name : DEV_NAME(dmt) ? : "",
2163
0
            dmi->uuid[0] ? dmi->uuid : DEV_UUID(dmt) ? : "",
2164
0
            dmt->major > 0 ? "(" : "",
2165
0
            dmt->major > 0 ? dmt->major : 0,
2166
0
            dmt->major > 0 ? ":" : "",
2167
0
            dmt->minor > 0 ? dmt->minor : 0,
2168
0
            dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2169
0
            dmt->major > 0 ? ")" : "",
2170
0
            strerror(dmt->ioctl_errno));
2171
2172
      /*
2173
       * It's sometimes worth retrying after EBUSY in case
2174
       * it's a transient failure caused by an asynchronous
2175
       * process quickly scanning the device.
2176
       */
2177
0
      *retryable = dmt->ioctl_errno == EBUSY;
2178
2179
0
      goto error;
2180
0
    }
2181
0
  }
2182
2183
0
  if (ioctl_with_uevent && dm_udev_get_sync_support() &&
2184
0
      !_check_uevent_generated(dmi)) {
2185
0
    log_debug_activation("Uevent not generated! Calling udev_complete "
2186
0
             "internally to avoid process lock-up.");
2187
0
    _udev_complete(dmt);
2188
0
  }
2189
2190
0
  if (!_dm_ioctl_unmangle_names(dmt->type, dmi))
2191
0
    goto error;
2192
2193
0
  if (dmt->type != DM_DEVICE_REMOVE &&
2194
0
      !_dm_ioctl_unmangle_uuids(dmt->type, dmi))
2195
0
    goto error;
2196
2197
#else /* Userspace alternative for testing */
2198
  goto error;
2199
#endif
2200
0
  return dmi;
2201
2202
0
error:
2203
0
  _dm_zfree_dmi(dmi);
2204
0
  return NULL;
2205
0
}
2206
2207
void dm_task_update_nodes(void)
2208
0
{
2209
0
  update_devs();
2210
0
}
2211
2212
0
#define DM_IOCTL_RETRIES 25
2213
0
#define DM_RETRY_USLEEP_DELAY 200000
2214
2215
int dm_task_get_errno(struct dm_task *dmt)
2216
0
{
2217
0
  return dmt->ioctl_errno;
2218
0
}
2219
2220
#if defined(GNU_SYMVER)
2221
/*
2222
 * Enforce new version 1_02_197 of dm_task_run() that propagates
2223
 * ioctl() errno is being linked to app.
2224
 */
2225
DM_EXPORT_SYMBOL_BASE(dm_task_run)
2226
int dm_task_run_base(struct dm_task *dmt);
2227
int dm_task_run_base(struct dm_task *dmt)
2228
0
{
2229
0
  return dm_task_run(dmt);
2230
0
}
2231
#endif
2232
2233
DM_EXPORT_NEW_SYMBOL(int, dm_task_run, 1_02_197)
2234
  (struct dm_task *dmt)
2235
0
{
2236
0
  struct dm_ioctl *dmi;
2237
0
  unsigned command;
2238
0
  int check_udev;
2239
0
  int rely_on_udev;
2240
0
  int suspended_counter;
2241
0
  unsigned ioctl_retry = 1;
2242
0
  int retryable = 0;
2243
0
  const char *dev_name = DEV_NAME(dmt);
2244
0
  const char *dev_uuid = DEV_UUID(dmt);
2245
2246
0
  if ((unsigned) dmt->type >= DM_ARRAY_SIZE(_cmd_data_v4)) {
2247
0
    log_error(INTERNAL_ERROR "unknown device-mapper task %d",
2248
0
        dmt->type);
2249
0
    return 0;
2250
0
  }
2251
2252
0
  command = _cmd_data_v4[dmt->type].cmd;
2253
2254
  /* Old-style creation had a table supplied */
2255
0
  if (dmt->type == DM_DEVICE_CREATE && dmt->head)
2256
0
    return _create_and_load_v4(dmt);
2257
2258
0
  if (dmt->type == DM_DEVICE_MKNODES && !dev_name &&
2259
0
      !dev_uuid && dmt->major <= 0)
2260
0
    return _mknodes_v4(dmt);
2261
2262
0
  if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
2263
0
    return _reload_with_suppression_v4(dmt);
2264
2265
0
  if ((dmt->type == DM_DEVICE_SUSPEND) && dmt->enable_checks)
2266
0
    return _suspend_with_validation_v4(dmt);
2267
2268
0
  if (!_open_control()) {
2269
0
    _udev_complete(dmt);
2270
0
    return_0;
2271
0
  }
2272
2273
0
  if ((suspended_counter = dm_get_suspended_counter()) &&
2274
0
      dmt->type == DM_DEVICE_RELOAD)
2275
0
    log_error(INTERNAL_ERROR "Performing unsafe table load while %d device(s) "
2276
0
        "are known to be suspended: "
2277
0
        "%s%s%s %s%.0d%s%.0d%s%s",
2278
0
        suspended_counter,
2279
0
        dev_name ? : "",
2280
0
        dev_uuid ? " UUID " : "",
2281
0
        dev_uuid ? : "",
2282
0
        dmt->major > 0 ? "(" : "",
2283
0
        dmt->major > 0 ? dmt->major : 0,
2284
0
        dmt->major > 0 ? ":" : "",
2285
0
        dmt->minor > 0 ? dmt->minor : 0,
2286
0
        dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2287
0
        dmt->major > 0 ? ") " : "");
2288
2289
  /* FIXME Detect and warn if cookie set but should not be. */
2290
0
repeat_ioctl:
2291
0
  if (!(dmi = _do_dm_ioctl(dmt, command, _ioctl_buffer_double_factor,
2292
0
         ioctl_retry, &retryable))) {
2293
    /*
2294
     * Async udev rules that scan devices commonly cause transient
2295
     * failures.  Normally you'd expect the user to have made sure
2296
     * nothing was using the device before issuing REMOVE, so it's
2297
     * worth retrying in case the failure is indeed transient.
2298
     */
2299
0
    if (retryable && dmt->type == DM_DEVICE_REMOVE &&
2300
0
        dmt->retry_remove && ++ioctl_retry <= DM_IOCTL_RETRIES) {
2301
0
      usleep(DM_RETRY_USLEEP_DELAY);
2302
0
      goto repeat_ioctl;
2303
0
    }
2304
2305
0
    _udev_complete(dmt);
2306
0
    return 0;
2307
0
  }
2308
2309
0
  if (dmi->flags & DM_BUFFER_FULL_FLAG) {
2310
0
    switch (dmt->type) {
2311
0
    case DM_DEVICE_LIST_VERSIONS:
2312
0
    case DM_DEVICE_LIST:
2313
0
    case DM_DEVICE_DEPS:
2314
0
    case DM_DEVICE_STATUS:
2315
0
    case DM_DEVICE_TABLE:
2316
0
    case DM_DEVICE_WAITEVENT:
2317
0
    case DM_DEVICE_TARGET_MSG:
2318
0
      _ioctl_buffer_double_factor++;
2319
0
      _dm_zfree_dmi(dmi);
2320
0
      goto repeat_ioctl;
2321
0
    default:
2322
0
      log_error("WARNING: libdevmapper buffer too small for data");
2323
0
    }
2324
0
  }
2325
2326
  /*
2327
   * Are we expecting a udev operation to occur that we need to check for?
2328
   */
2329
0
  check_udev = dmt->cookie_set &&
2330
0
         !(dmt->event_nr >> DM_UDEV_FLAGS_SHIFT &
2331
0
           DM_UDEV_DISABLE_DM_RULES_FLAG);
2332
2333
0
  rely_on_udev = dmt->cookie_set ? (dmt->event_nr >> DM_UDEV_FLAGS_SHIFT &
2334
0
            DM_UDEV_DISABLE_LIBRARY_FALLBACK) : 0;
2335
2336
0
  switch (dmt->type) {
2337
0
  case DM_DEVICE_CREATE:
2338
0
    if ((dmt->add_node == DM_ADD_NODE_ON_CREATE) &&
2339
0
        dev_name && *dev_name && !rely_on_udev)
2340
0
      add_dev_node(dev_name, MAJOR(dmi->dev),
2341
0
             MINOR(dmi->dev), dmt->uid, dmt->gid,
2342
0
             dmt->mode, check_udev, rely_on_udev);
2343
0
    break;
2344
0
  case DM_DEVICE_REMOVE:
2345
    /* FIXME Kernel needs to fill in dmi->name */
2346
0
    if (dev_name && !rely_on_udev)
2347
0
      rm_dev_node(dev_name, check_udev, rely_on_udev);
2348
0
    break;
2349
2350
0
  case DM_DEVICE_RENAME:
2351
    /* FIXME Kernel needs to fill in dmi->name */
2352
0
    if (!dmt->new_uuid && dev_name)
2353
0
      rename_dev_node(dev_name, dmt->newname,
2354
0
          check_udev, rely_on_udev);
2355
0
    break;
2356
2357
0
  case DM_DEVICE_RESUME:
2358
0
    if ((dmt->add_node == DM_ADD_NODE_ON_RESUME) &&
2359
0
        dev_name && *dev_name)
2360
0
      add_dev_node(dev_name, MAJOR(dmi->dev),
2361
0
             MINOR(dmi->dev), dmt->uid, dmt->gid,
2362
0
             dmt->mode, check_udev, rely_on_udev);
2363
    /* FIXME Kernel needs to fill in dmi->name */
2364
0
    set_dev_node_read_ahead(dev_name,
2365
0
          MAJOR(dmi->dev), MINOR(dmi->dev),
2366
0
          dmt->read_ahead, dmt->read_ahead_flags);
2367
0
    break;
2368
  
2369
0
  case DM_DEVICE_MKNODES:
2370
0
    if (dmi->flags & DM_EXISTS_FLAG)
2371
0
      add_dev_node(dmi->name, MAJOR(dmi->dev),
2372
0
             MINOR(dmi->dev), dmt->uid,
2373
0
             dmt->gid, dmt->mode, 0, rely_on_udev);
2374
0
    else if (dev_name)
2375
0
      rm_dev_node(dev_name, 0, rely_on_udev);
2376
0
    break;
2377
2378
0
  case DM_DEVICE_STATUS:
2379
0
  case DM_DEVICE_TABLE:
2380
0
  case DM_DEVICE_WAITEVENT:
2381
0
    if (!_unmarshal_status(dmt, dmi))
2382
0
      goto bad;
2383
0
    break;
2384
0
  }
2385
2386
  /* Was structure reused? */
2387
0
  _dm_zfree_dmi(dmt->dmi.v4);
2388
0
  dmt->dmi.v4 = dmi;
2389
0
  return 1;
2390
2391
0
      bad:
2392
0
  _dm_zfree_dmi(dmi);
2393
0
  return 0;
2394
0
}
2395
2396
void dm_hold_control_dev(int hold_open)
2397
0
{
2398
0
  _hold_control_fd_open = hold_open ? 1 : 0;
2399
2400
0
  log_debug("Hold of control device is now %sset.",
2401
0
      _hold_control_fd_open ? "" : "un");
2402
0
}
2403
2404
void dm_lib_release(void)
2405
4.82k
{
2406
4.82k
  if (!_hold_control_fd_open)
2407
4.82k
    _close_control_fd();
2408
4.82k
  dm_timestamp_destroy(_dm_ioctl_timestamp);
2409
4.82k
  _dm_ioctl_timestamp = NULL;
2410
4.82k
  update_devs();
2411
4.82k
}
2412
2413
void dm_pools_check_leaks(void);
2414
2415
void dm_lib_exit(void)
2416
0
{
2417
0
  int suspended_counter;
2418
0
  static unsigned _exited = 0;
2419
2420
0
  if (_exited++)
2421
0
    return;
2422
2423
0
  if ((suspended_counter = dm_get_suspended_counter()))
2424
0
    log_error("libdevmapper exiting with %d device(s) still suspended.", suspended_counter);
2425
2426
0
  dm_lib_release();
2427
0
  selinux_release();
2428
0
  if (_dm_bitset)
2429
0
    dm_bitset_destroy(_dm_bitset);
2430
0
  _dm_bitset = NULL;
2431
0
  dm_pools_check_leaks();
2432
0
  dm_dump_memory();
2433
0
  _version_ok = 1;
2434
0
  _version_checked = 0;
2435
0
}
2436
2437
#if defined(GNU_SYMVER)
2438
/*
2439
 * Maintain binary backward compatibility.
2440
 * Version script mechanism works with 'gcc' compatible compilers only.
2441
 */
2442
2443
/*
2444
 * This following code is here to retain ABI compatibility after adding
2445
 * the field deferred_remove to struct dm_info in version 1.02.89.
2446
 *
2447
 * Binaries linked against version 1.02.88 of libdevmapper or earlier
2448
 * will use this function that returns dm_info without the
2449
 * deferred_remove field.
2450
 *
2451
 * Binaries compiled against version 1.02.89 onwards will use
2452
 * the new function dm_task_get_info_with_deferred_remove due to the
2453
 * #define.
2454
 *
2455
 * N.B. Keep this function at the end of the file to make sure that
2456
 * no code in this file accidentally calls it.
2457
 */
2458
2459
DM_EXPORT_SYMBOL_BASE(dm_task_get_info)
2460
int dm_task_get_info_base(struct dm_task *dmt, struct dm_info *info);
2461
int dm_task_get_info_base(struct dm_task *dmt, struct dm_info *info)
2462
0
{
2463
0
  struct dm_info new_info;
2464
2465
0
  if (!dm_task_get_info(dmt, &new_info))
2466
0
    return 0;
2467
2468
0
  memcpy(info, &new_info, offsetof(struct dm_info, deferred_remove));
2469
2470
0
  return 1;
2471
0
}
2472
2473
#endif
2474
2475
int dm_task_get_info_with_deferred_remove(struct dm_task *dmt, struct dm_info *info);
2476
int dm_task_get_info_with_deferred_remove(struct dm_task *dmt, struct dm_info *info)
2477
0
{
2478
0
  struct dm_info new_info;
2479
2480
0
  if (!dm_task_get_info(dmt, &new_info))
2481
0
    return 0;
2482
2483
0
  memcpy(info, &new_info, offsetof(struct dm_info, internal_suspend));
2484
2485
0
  return 1;
2486
0
}