Coverage Report

Created: 2026-05-30 06:06

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