Coverage Report

Created: 2026-07-16 06:59

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.94k
{
424
5.94k
  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.94k
}
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
/*
1798
 * Skip "rebuild N" or "write_mostly N" token pair.
1799
 * Old dm-raid may drop these from table output.
1800
 */
1801
static int _skip_raid_volatile_token(const char **pp)
1802
0
{
1803
0
  const char *p = *pp;
1804
1805
0
  if (strncmp(p, "rebuild ", 8) &&
1806
0
      strncmp(p, "write_mostly ", 13))
1807
0
    return 0;
1808
1809
0
  while (*p && *p != ' ')
1810
0
    p++;
1811
0
  if (*p == ' ')
1812
0
    p++;
1813
0
  while (*p && *p != ' ')
1814
0
    p++;
1815
0
  if (*p == ' ')
1816
0
    p++;
1817
1818
0
  *pp = p;
1819
0
  return 1;
1820
0
}
1821
1822
/*
1823
 * Check if raid params are functionally equivalent.
1824
 * p1 = new table, p2 = active table (kernel STATUSTYPE_TABLE).
1825
 *
1826
 * Old dm-raid (< 1.15.1) double-counts rebuild/writemostly in
1827
 * the param count, and early versions drop "rebuild N" from
1828
 * table output once In_sync is set.  Both cause spurious string
1829
 * mismatches that defeat reload suppression.  We skip the param
1830
 * count and ignore volatile "rebuild N" / "write_mostly N" pairs.
1831
 *
1832
 * The 'suspended' flag gates which side gets the volatile skip.
1833
 * Do not simplify this to always-symmetric or always-asymmetric.
1834
 * When NOT suspended, skip only in p2 -- a rebuild in p1 is a
1835
 * deliberate request that must trigger a reload.  When suspended,
1836
 * skip in both p1 and p2 -- LVM is in the second preload pass
1837
 * (the first already loaded the rebuild table via force_reload),
1838
 * the active table is stale, and a redundant reload here would
1839
 * hit "Internal error: table load while devices are suspended".
1840
 */
1841
static int _raid_params_equiv(const char *p1, const char *p2,
1842
            int suspended)
1843
0
{
1844
0
  const char *s1 = strchr(p1, ' ');
1845
0
  const char *s2 = strchr(p2, ' ');
1846
1847
0
  if (!s1 || !s2)
1848
0
    return 0;
1849
1850
  /* Raid personality must match */
1851
0
  if ((s1 - p1) != (s2 - p2) ||
1852
0
      memcmp(p1, p2, s1 - p1))
1853
0
    return 0;
1854
1855
  /* Skip past the param count */
1856
0
  s1 = strchr(s1 + 1, ' ');
1857
0
  s2 = strchr(s2 + 1, ' ');
1858
1859
0
  if (!s1 || !s2)
1860
0
    return 0;
1861
1862
  /* Fast path */
1863
0
  if (!strcmp(s1, s2))
1864
0
    return 1;
1865
1866
0
  s1++;
1867
0
  s2++;
1868
1869
0
  for (;;) {
1870
0
    if (suspended)
1871
0
      while (*s1 && _skip_raid_volatile_token(&s1))
1872
0
        ;
1873
0
    while (*s2 && _skip_raid_volatile_token(&s2))
1874
0
      ;
1875
1876
0
    if (!*s1 && !*s2)
1877
0
      return 1;
1878
1879
0
    if (!*s1 || !*s2)
1880
0
      return 0;
1881
1882
0
    while (*s1 && *s2 && *s1 != ' ' && *s2 != ' ') {
1883
0
      if (*s1 != *s2)
1884
0
        return 0;
1885
0
      s1++;
1886
0
      s2++;
1887
0
    }
1888
1889
0
    if ((*s1 && *s1 != ' ') || (*s2 && *s2 != ' '))
1890
0
      return 0;
1891
1892
0
    if (*s1 == ' ')
1893
0
      s1++;
1894
0
    if (*s2 == ' ')
1895
0
      s2++;
1896
0
  }
1897
0
}
1898
1899
/*
1900
 * Check if integrity params are functionally equivalent.
1901
 * p1 = new table params, p2 = active table params (kernel STATUSTYPE_TABLE).
1902
 *
1903
 * Kernel's integrity table output differs from what LVM emits:
1904
 *
1905
 * - block_size:512 -- LVM always emits, kernel omits when
1906
 *   sectors_per_block==1 (the default).
1907
 *
1908
 * - fix_padding -- LVM always emits, kernel only reports when
1909
 *   SB_FLAG_FIXED_PADDING is set in the on-disk superblock
1910
 *   (written once by initialize_superblock(), NOT on reload).
1911
 *
1912
 * - recalculate -- kernel reports from SB_FLAG_RECALCULATING in the
1913
 *   on-disk superblock, not from the constructor table line.
1914
 *   The kernel NEVER clears this flag; recalc_sector simply advances
1915
 *   past provided_data_sectors.  LVM clears integrity_recalculate in
1916
 *   VG metadata after the first activation (integrity_manip.c), so
1917
 *   subsequent activations omit it from the table line, but the kernel
1918
 *   still reports it from the superblock.  A reload cannot remove the
1919
 *   flag, so treat it as cosmetic in both directions.
1920
 *
1921
 * - kernel may add tokens LVM did not set (buffer_sectors,
1922
 *   journal_watermark, commit_time, etc.)
1923
 *
1924
 * Skipped tokens are listed in _integrity_skip_p1_token().
1925
 * We verify every non-skipped token we emit exists in the kernel output.
1926
 * Extra kernel tokens are acceptable.
1927
 */
1928
1929
/* Tokens LVM emits that the kernel may legitimately omit */
1930
static int _integrity_skip_p1_token(const char *token, unsigned len)
1931
0
{
1932
0
  if (len == 14 && !memcmp(token, "block_size:512", 14))
1933
0
    return 1;
1934
0
  if (len == 11 && !memcmp(token, "fix_padding", 11))
1935
0
    return 1;
1936
0
  if (len == 11 && !memcmp(token, "recalculate", 11))
1937
0
    return 1;
1938
0
  return 0;
1939
0
}
1940
1941
/* Find exact token in a space-separated string */
1942
static int _integrity_has_token(const char *str, const char *token, unsigned len)
1943
0
{
1944
0
  const char *s = str;
1945
1946
0
  while (*s) {
1947
0
    const char *e = s;
1948
0
    while (*e && *e != ' ')
1949
0
      e++;
1950
0
    if ((unsigned)(e - s) == len && !memcmp(s, token, len))
1951
0
      return 1;
1952
0
    s = (*e == ' ') ? e + 1 : e;
1953
0
  }
1954
1955
0
  return 0;
1956
0
}
1957
1958
/* Advance past one space-separated token */
1959
static const char *_skip_one_token(const char *s)
1960
0
{
1961
0
  while (*s && *s != ' ')
1962
0
    s++;
1963
0
  while (*s == ' ')
1964
0
    s++;
1965
0
  return s;
1966
0
}
1967
1968
static int _integrity_params_equiv(const char *p1, const char *p2,
1969
           int suspended __attribute__((unused)))
1970
0
{
1971
0
  const char *s1 = p1, *s2 = p2;
1972
0
  const char *e1, *e2;
1973
0
  const char *args1, *args2;
1974
0
  const char *tok;
1975
0
  unsigned len;
1976
0
  int i;
1977
1978
  /* Compare 4 prefix tokens: dev, start, tag_size, mode */
1979
0
  for (i = 0; i < 4; i++) {
1980
0
    e1 = s1;
1981
0
    while (*e1 && *e1 != ' ')
1982
0
      e1++;
1983
0
    e2 = s2;
1984
0
    while (*e2 && *e2 != ' ')
1985
0
      e2++;
1986
0
    if ((e1 - s1) != (e2 - s2) ||
1987
0
        memcmp(s1, s2, e1 - s1))
1988
0
      return 0;
1989
0
    s1 = (*e1 == ' ') ? e1 + 1 : e1;
1990
0
    s2 = (*e2 == ' ') ? e2 + 1 : e2;
1991
0
  }
1992
1993
  /* Skip param count (5th token) -- differs due to kernel omissions */
1994
0
  args1 = _skip_one_token(s1);
1995
0
  args2 = _skip_one_token(s2);
1996
1997
  /* Fast path */
1998
0
  if (!strcmp(args1, args2))
1999
0
    return 1;
2000
2001
  /* Check every non-skipped token we emit exists in kernel output */
2002
0
  tok = args1;
2003
0
  while (*tok) {
2004
0
    e1 = tok;
2005
0
    while (*e1 && *e1 != ' ')
2006
0
      e1++;
2007
0
    len = (unsigned)(e1 - tok);
2008
0
    if (!_integrity_skip_p1_token(tok, len) &&
2009
0
        !_integrity_has_token(args2, tok, len))
2010
0
      return 0;
2011
0
    tok = (*e1 == ' ') ? e1 + 1 : e1;
2012
0
  }
2013
2014
0
  return 1;
2015
0
}
2016
2017
static int _target_params_equiv(const char *type,
2018
        const char *p1, const char *p2,
2019
        int suspended)
2020
0
{
2021
0
  if (!strcmp(type, "raid"))
2022
0
    return _raid_params_equiv(p1, p2, suspended);
2023
2024
0
  if (!strcmp(type, "integrity"))
2025
0
    return _integrity_params_equiv(p1, p2, suspended);
2026
2027
0
  return 0;
2028
0
}
2029
2030
static int _reload_with_suppression_v4(struct dm_task *dmt)
2031
0
{
2032
0
  struct dm_task *task;
2033
0
  struct target *t1, *t2;
2034
0
  size_t len;
2035
0
  int r;
2036
2037
  /* New task to get existing table information */
2038
0
  if (!(task = dm_task_create(DM_DEVICE_TABLE))) {
2039
0
    log_error("Failed to create device-mapper task struct");
2040
0
    return 0;
2041
0
  }
2042
2043
  /* Copy across relevant fields */
2044
0
  if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name)) {
2045
0
    dm_task_destroy(task);
2046
0
    return 0;
2047
0
  }
2048
2049
0
  if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid)) {
2050
0
    dm_task_destroy(task);
2051
0
    return 0;
2052
0
  }
2053
2054
0
  task->major = dmt->major;
2055
0
  task->minor = dmt->minor;
2056
2057
0
  r = dm_task_run(task);
2058
2059
0
  if (!r) {
2060
0
    dm_task_destroy(task);
2061
0
    return r;
2062
0
  }
2063
2064
  /* Store existing table size */
2065
0
  t2 = task->head;
2066
0
  while (t2 && t2->next)
2067
0
    t2 = t2->next;
2068
0
  dmt->existing_table_size = t2 ? t2->start + t2->length : 0;
2069
2070
0
  if (((task->dmi.v4->flags & DM_READONLY_FLAG) ? 1 : 0) != dmt->read_only)
2071
0
    goto no_match;
2072
2073
0
  t1 = dmt->head;
2074
0
  t2 = task->head;
2075
2076
0
  while (t1 && t2) {
2077
0
    len = strlen(t2->params);
2078
0
    while (len-- > 0 && t2->params[len] == ' ')
2079
0
      t2->params[len] = '\0';
2080
2081
0
    if (t1->start != t2->start) {
2082
0
      log_debug("reload %d:%d diff start %llu %llu type %s %s.", task->major, task->minor,
2083
0
           (unsigned long long)t1->start, (unsigned long long)t2->start, t1->type, t2->type);
2084
0
      goto no_match;
2085
0
    }
2086
0
    if (t1->length != t2->length) {
2087
0
      log_debug("reload %d:%d diff length %llu %llu type %s %s.", task->major, task->minor,
2088
0
          (unsigned long long)t1->length, (unsigned long long)t2->length, t1->type, t2->type);
2089
0
      goto no_match;
2090
0
    }
2091
0
    if (strcmp(t1->type, t2->type)) {
2092
0
      log_debug("reload %d:%d diff type %s %s.", task->major, task->minor, t1->type, t2->type);
2093
0
      goto no_match;
2094
0
    }
2095
0
    if (strcmp(t1->params, t2->params)) {
2096
0
      if (dmt->skip_reload_params_compare) {
2097
0
        log_debug("reload %d:%d diff params ignore for type %s.",
2098
0
            task->major, task->minor, t1->type);
2099
0
        log_debug("reload params1 %s", t1->params);
2100
0
        log_debug("reload params2 %s", t2->params);
2101
0
      } else if (_target_params_equiv(t1->type, t1->params, t2->params,
2102
0
                 task->dmi.v4->flags & DM_SUSPEND_FLAG)) {
2103
0
        log_debug("reload %d:%d diff params equiv for type %s.",
2104
0
            task->major, task->minor, t1->type);
2105
0
        log_debug("reload params1 %s", t1->params);
2106
0
        log_debug("reload params2 %s", t2->params);
2107
0
      } else {
2108
0
        log_debug("reload %d:%d diff params for type %s.",
2109
0
            task->major, task->minor, t1->type);
2110
0
        log_debug("reload params1 %s", t1->params);
2111
0
        log_debug("reload params2 %s", t2->params);
2112
0
        goto no_match;
2113
0
      }
2114
0
    }
2115
2116
0
    t1 = t1->next;
2117
0
    t2 = t2->next;
2118
0
  }
2119
2120
0
  if (!t1 && !t2) {
2121
0
    dmt->dmi.v4 = task->dmi.v4;
2122
0
    task->dmi.v4 = NULL;
2123
0
    dm_task_destroy(task);
2124
0
    return 1;
2125
0
  }
2126
2127
0
no_match:
2128
0
  dm_task_destroy(task);
2129
2130
  /* Now do the original reload */
2131
0
  dmt->suppress_identical_reload = 0;
2132
0
  r = dm_task_run(dmt);
2133
2134
0
  return r;
2135
0
}
2136
2137
static int _check_children_not_suspended_v4(struct dm_task *dmt, uint64_t device)
2138
0
{
2139
0
  struct dm_task *task;
2140
0
  struct dm_info info;
2141
0
  struct dm_deps *deps;
2142
0
  int r = 0;
2143
0
  uint32_t i;
2144
2145
  /* Find dependencies */
2146
0
  if (!(task = dm_task_create(DM_DEVICE_DEPS)))
2147
0
    return 0;
2148
2149
  /* Copy across or set relevant fields */
2150
0
  if (device) {
2151
0
    task->major = MAJOR(device);
2152
0
    task->minor = MINOR(device);
2153
0
  } else {
2154
0
    if (dmt->dev_name && !dm_task_set_name(task, dmt->dev_name))
2155
0
      goto out;
2156
2157
0
    if (dmt->uuid && !dm_task_set_uuid(task, dmt->uuid))
2158
0
      goto out;
2159
2160
0
    task->major = dmt->major;
2161
0
    task->minor = dmt->minor;
2162
0
  }
2163
2164
0
  task->uid = dmt->uid;
2165
0
  task->gid = dmt->gid;
2166
0
  task->mode = dmt->mode;
2167
  /* FIXME: Just for udev_check in dm_task_run. Can we avoid this? */
2168
0
  task->event_nr = dmt->event_nr & DM_UDEV_FLAGS_MASK;
2169
0
  task->cookie_set = dmt->cookie_set;
2170
0
  task->add_node = dmt->add_node;
2171
2172
0
  if (!(r = dm_task_run(task)))
2173
0
    goto out;
2174
2175
0
  if (!dm_task_get_info(task, &info) || !info.exists)
2176
0
    goto out;
2177
2178
  /*
2179
   * Warn if any of the devices this device depends upon are already
2180
   * suspended: I/O could become trapped between the two devices.
2181
   */
2182
0
  if (info.suspended) {
2183
0
    if (!device)
2184
0
      log_debug_activation("Attempting to suspend a device that is already suspended "
2185
0
               "(%u:%u)", info.major, info.minor);
2186
0
    else
2187
0
      log_error(INTERNAL_ERROR "Attempt to suspend device %s%s%s%.0d%s%.0d%s%s"
2188
0
          "that uses already-suspended device (%u:%u)",
2189
0
          DEV_NAME(dmt) ? : "", DEV_UUID(dmt) ? : "",
2190
0
          dmt->major > 0 ? "(" : "",
2191
0
          dmt->major > 0 ? dmt->major : 0,
2192
0
          dmt->major > 0 ? ":" : "",
2193
0
          dmt->minor > 0 ? dmt->minor : 0,
2194
0
          dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2195
0
          dmt->major > 0 ? ") " : "",
2196
0
          info.major, info.minor);
2197
2198
    /* No need for further recursion */
2199
0
    r = 1;
2200
0
    goto out;
2201
0
  }
2202
2203
0
  if (!(deps = dm_task_get_deps(task)))
2204
0
    goto out;
2205
2206
0
  for (i = 0; i < deps->count; i++) {
2207
    /* Only recurse with dm devices */
2208
0
    if (MAJOR(deps->device[i]) != _dm_device_major)
2209
0
      continue;
2210
2211
0
    if (!_check_children_not_suspended_v4(task, deps->device[i]))
2212
0
      goto out;
2213
0
  }
2214
2215
0
  r = 1;
2216
2217
0
out:
2218
0
  dm_task_destroy(task);
2219
2220
0
  return r;
2221
0
}
2222
2223
static int _suspend_with_validation_v4(struct dm_task *dmt)
2224
0
{
2225
  /* Avoid recursion */
2226
0
  dmt->enable_checks = 0;
2227
2228
  /*
2229
   * Ensure we can't leave any I/O trapped between suspended devices.
2230
   */
2231
0
  if (!_check_children_not_suspended_v4(dmt, 0))
2232
0
    return 0;
2233
2234
  /* Finally, perform the original suspend. */
2235
0
  return dm_task_run(dmt);
2236
0
}
2237
2238
static const char *_sanitise_message(char *message)
2239
0
{
2240
0
  const char *sanitised_message = message ?: "";
2241
2242
  /* FIXME: Check for whitespace variations. */
2243
  /* This traps what cryptsetup sends us. */
2244
0
  if (message && !strncasecmp(message, "key set", 7))
2245
0
    sanitised_message = "key set";
2246
2247
0
  return sanitised_message;
2248
0
}
2249
2250
#ifdef DM_IOCTLS
2251
static int _do_dm_ioctl_unmangle_string(char *str, const char *str_name,
2252
          char *buf, size_t buf_size,
2253
          dm_string_mangling_t mode)
2254
0
{
2255
0
  int r;
2256
2257
0
  if (mode == DM_STRING_MANGLING_NONE)
2258
0
    return 1;
2259
2260
0
  if (!check_multiple_mangled_string_allowed(str, str_name, mode))
2261
0
    return_0;
2262
2263
0
  if ((r = unmangle_string(str, str_name, strlen(str), buf, buf_size, mode)) < 0) {
2264
0
    log_debug_activation("_do_dm_ioctl_unmangle_string: failed to "
2265
0
             "unmangle %s \"%s\"", str_name, str);
2266
0
    return 0;
2267
0
  }
2268
2269
0
  if (r)
2270
0
    memcpy(str, buf, strlen(buf) + 1);
2271
2272
0
  return 1;
2273
0
}
2274
2275
static int _dm_ioctl_unmangle_names(int type, struct dm_ioctl *dmi)
2276
0
{
2277
0
  char buf[DM_NAME_LEN];
2278
0
  char buf_uuid[DM_UUID_LEN];
2279
0
  struct dm_name_list *names;
2280
0
  unsigned next = 0;
2281
0
  char *name;
2282
0
  int r = 1;
2283
0
  uint32_t *event_nr;
2284
0
  char *uuid_ptr;
2285
0
  dm_string_mangling_t mangling_mode = dm_get_name_mangling_mode();
2286
2287
0
  if ((name = dmi->name))
2288
0
    r &= _do_dm_ioctl_unmangle_string(name, "name", buf, sizeof(buf),
2289
0
              mangling_mode);
2290
2291
0
  if (type == DM_DEVICE_LIST &&
2292
0
      ((names = ((struct dm_name_list *) ((char *)dmi + dmi->data_start)))) &&
2293
0
      names->dev) {
2294
0
    do {
2295
0
      names = (struct dm_name_list *)((char *) names + next);
2296
0
      event_nr = _align_ptr(names->name + strlen(names->name) + 1);
2297
0
      r &= _do_dm_ioctl_unmangle_string(names->name, "name",
2298
0
                buf, sizeof(buf), mangling_mode);
2299
      /* Unmangle also UUID within same loop */
2300
0
      if (_check_has_event_nr() &&
2301
0
          (event_nr[1] & DM_NAME_LIST_FLAG_HAS_UUID)) {
2302
0
        uuid_ptr = _align_ptr(event_nr + 2);
2303
0
        r &= _do_dm_ioctl_unmangle_string(uuid_ptr, "UUID", buf_uuid,
2304
0
                  sizeof(buf_uuid), mangling_mode);
2305
0
      }
2306
0
      next = names->next;
2307
0
    } while (next);
2308
0
  }
2309
2310
0
  return r;
2311
0
}
2312
2313
static int _dm_ioctl_unmangle_uuids(int type, struct dm_ioctl *dmi)
2314
0
{
2315
0
  char buf[DM_UUID_LEN];
2316
0
  char *uuid = dmi->uuid;
2317
2318
0
  if (uuid)
2319
0
    return _do_dm_ioctl_unmangle_string(uuid, "UUID", buf, sizeof(buf),
2320
0
                dm_get_name_mangling_mode());
2321
2322
0
  return 1;
2323
0
}
2324
#endif
2325
2326
/* True for ioctl types that generate a udev event and need cookie handling. */
2327
static inline int _dmt_has_uevent(const struct dm_task *dmt)
2328
0
{
2329
0
  return dmt->type == DM_DEVICE_RESUME ||
2330
0
         dmt->type == DM_DEVICE_REMOVE ||
2331
0
         dmt->type == DM_DEVICE_RENAME;
2332
0
}
2333
2334
/*
2335
 * Flatten the task into a fresh dm_ioctl buffer and set all flags and
2336
 * udev cookie bits.  Returns the allocated buffer (caller owns it) or
2337
 * NULL on failure.
2338
 */
2339
static struct dm_ioctl *_dm_task_build_dmi(struct dm_task *dmt,
2340
             unsigned buffer_repeat_count)
2341
0
{
2342
0
  struct dm_ioctl *dmi;
2343
2344
0
  dmi = _flatten(dmt, buffer_repeat_count);
2345
0
  if (!dmi) {
2346
0
    log_error("Couldn't create ioctl argument.");
2347
0
    return NULL;
2348
0
  }
2349
2350
0
  if (dmt->type == DM_DEVICE_TABLE)
2351
0
    dmi->flags |= DM_STATUS_TABLE_FLAG;
2352
2353
0
  dmi->flags |= DM_EXISTS_FLAG; /* FIXME */
2354
2355
0
  if (dmt->no_open_count)
2356
0
    dmi->flags |= DM_SKIP_BDGET_FLAG;
2357
2358
0
  if (_dmt_has_uevent(dmt) && dm_cookie_supported()) {
2359
    /*
2360
     * Always mark events coming from libdevmapper as
2361
     * "primary sourced". This is needed to distinguish
2362
     * any spurious events so we can act appropriately.
2363
     * This needs to be applied even when udev_sync is
2364
     * not used because udev flags could be used alone.
2365
     */
2366
0
    dmi->event_nr |= DM_UDEV_PRIMARY_SOURCE_FLAG <<
2367
0
         DM_UDEV_FLAGS_SHIFT;
2368
2369
    /*
2370
     * Prevent udev vs. libdevmapper race when processing nodes
2371
     * and symlinks. This can happen when the udev rules are
2372
     * installed and udev synchronization code is enabled in
2373
     * libdevmapper but the software using libdevmapper does not
2374
     * make use of it (by not calling dm_task_set_cookie before).
2375
     * We need to instruct the udev rules not to be applied at
2376
     * all in this situation so we can gracefully fallback to
2377
     * libdevmapper's node and symlink creation code.
2378
     */
2379
0
    if (!dmt->cookie_set && dm_udev_get_sync_support()) {
2380
0
      log_debug_activation("Cookie value is not set while trying to call %s "
2381
0
               "ioctl. Please, consider using libdevmapper's udev "
2382
0
               "synchronization interface or disable it explicitly "
2383
0
               "by calling dm_udev_set_sync_support(0).",
2384
0
               dmt->type == DM_DEVICE_RESUME ? "DM_DEVICE_RESUME" :
2385
0
               dmt->type == DM_DEVICE_REMOVE ? "DM_DEVICE_REMOVE" :
2386
0
                       "DM_DEVICE_RENAME");
2387
0
      log_debug_activation("Switching off device-mapper and all subsystem related "
2388
0
               "udev rules. Falling back to libdevmapper node creation.");
2389
      /*
2390
       * Disable general dm and subsystem rules but keep
2391
       * dm disk rules if not flagged out explicitly before.
2392
       * We need /dev/disk content for the software that expects it.
2393
      */
2394
0
      dmi->event_nr |= (DM_UDEV_DISABLE_DM_RULES_FLAG |
2395
0
            DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG) <<
2396
0
           DM_UDEV_FLAGS_SHIFT;
2397
0
    }
2398
0
  }
2399
2400
0
  log_debug_activation("dm %s %s%s %s%s%s %s%.0d%s%.0d%s"
2401
0
           "%s[ %s%s%s%s%s%s%s%s%s%s] %.0" PRIu64 " %s [%u]",
2402
0
           _cmd_data_v4[dmt->type].name,
2403
0
           dmt->new_uuid ? "UUID " : "",
2404
0
           dmi->name, dmi->uuid, dmt->newname ? " " : "",
2405
0
           dmt->newname ? dmt->newname : "",
2406
0
           dmt->major > 0 ? "(" : "",
2407
0
           dmt->major > 0 ? dmt->major : 0,
2408
0
           dmt->major > 0 ? ":" : "",
2409
0
           dmt->minor > 0 ? dmt->minor : 0,
2410
0
           dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2411
0
           dmt->major > 0 ? ") " : "",
2412
0
           dmt->no_open_count ? "noopencount " : "opencount ",
2413
0
           dmt->no_flush ? "noflush " : "flush ",
2414
0
           dmt->read_only ? "readonly " : "",
2415
0
           dmt->skip_lockfs ? "skiplockfs " : "",
2416
0
           dmt->retry_remove ? "retryremove " : "",
2417
0
           dmt->deferred_remove ? "deferredremove " : "",
2418
0
           dmt->secure_data ? "securedata " : "",
2419
0
           dmt->ima_measurement ? "ima_measurement " : "",
2420
0
           dmt->query_inactive_table ? "inactive " : "",
2421
0
           dmt->enable_checks ? "enablechecks " : "",
2422
0
           dmt->sector, _sanitise_message(dmt->message),
2423
0
           dmi->data_size);
2424
2425
0
  return dmi;
2426
0
}
2427
2428
/* Execute a single DM ioctl.  Sets dmt->ioctl_errno on failure.
2429
 * Caller must validate dmt->type via _validate_task_type(). */
2430
int dm_ioctl_exec(int fd, struct dm_task *dmt, struct dm_ioctl *dmi)
2431
0
{
2432
0
  int r;
2433
2434
0
  dmt->ioctl_errno = 0;
2435
2436
0
#ifdef DM_IOCTLS
2437
0
  r = ioctl(fd, _cmd_data_v4[dmt->type].cmd, dmi);
2438
0
  if (r < 0)
2439
0
    dmt->ioctl_errno = errno;
2440
#else /* Userspace alternative for testing */
2441
  r = 0;
2442
#endif
2443
0
  return r;
2444
0
}
2445
2446
/*
2447
 * Execute ioctl with EBUSY retry for remove operations.
2448
 *
2449
 * The dmi buffer is reused across retries without rebuilding.
2450
 * On EBUSY the kernel fails the ioctl early without modifying
2451
 * the dmi structure, so the input fields (name, uuid, dev)
2452
 * remain valid for the next attempt.
2453
 */
2454
static int _dm_ioctl_exec_retry(int fd, struct dm_task *dmt)
2455
0
{
2456
0
  unsigned retries = 0;
2457
0
  int r;
2458
2459
0
  do {
2460
0
    if (retries) {
2461
0
      log_debug_activation("EBUSY retry %u/%u for %s.",
2462
0
               retries, (unsigned) DM_IOCTL_RETRIES,
2463
0
               dmt->dmi.v4->name);
2464
0
      usleep(DM_RETRY_USLEEP_DELAY);
2465
0
    }
2466
2467
0
    r = dm_ioctl_exec(fd, dmt, dmt->dmi.v4);
2468
2469
0
    if (r >= 0)
2470
0
      break;
2471
2472
0
  } while (dmt->retry_remove &&
2473
0
     (dmt->ioctl_errno == EBUSY) &&
2474
0
     (dmt->type == DM_DEVICE_REMOVE) &&
2475
0
     (++retries <= DM_IOCTL_RETRIES));
2476
2477
0
  return r;
2478
0
}
2479
2480
/*
2481
 * Post-ioctl processing: error logging, uevent completion,
2482
 * and name/uuid unmangling.
2483
 * On ioctl error returns 0; caller must call _udev_complete().
2484
 * Calls _udev_complete() only when uevent was not generated by kernel.
2485
 * Returns 1 on success, 0 on failure.
2486
 */
2487
static int _dm_ioctl_post(struct dm_task *dmt, struct dm_ioctl *dmi,
2488
        int r)
2489
0
{
2490
0
#ifdef DM_IOCTLS
2491
0
  if (r < 0 && dmt->expected_errno != dmt->ioctl_errno) {
2492
0
    if (dmt->ioctl_errno == ENXIO && ((dmt->type == DM_DEVICE_INFO) ||
2493
0
              (dmt->type == DM_DEVICE_MKNODES) ||
2494
0
              (dmt->type == DM_DEVICE_STATUS)))
2495
0
      dmi->flags &= ~DM_EXISTS_FLAG; /* FIXME */
2496
0
    else {
2497
0
      if (_log_suppress || dmt->ioctl_errno == EINTR)
2498
0
        log_verbose("device-mapper: %s ioctl on %s %s%s%.0d%s%.0d%s%s "
2499
0
              "failed: %s",
2500
0
              _cmd_data_v4[dmt->type].name,
2501
0
              dmi->name[0] ? dmi->name : DEV_NAME(dmt) ? : "",
2502
0
              dmi->uuid[0] ? dmi->uuid : DEV_UUID(dmt) ? : "",
2503
0
              dmt->major > 0 ? "(" : "",
2504
0
              dmt->major > 0 ? dmt->major : 0,
2505
0
              dmt->major > 0 ? ":" : "",
2506
0
              dmt->minor > 0 ? dmt->minor : 0,
2507
0
              dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2508
0
              dmt->major > 0 ? ")" : "",
2509
0
              strerror(dmt->ioctl_errno));
2510
0
      else
2511
0
        log_error("device-mapper: %s ioctl on %s %s%s%.0d%s%.0d%s%s "
2512
0
            "failed: %s",
2513
0
            _cmd_data_v4[dmt->type].name,
2514
0
            dmi->name[0] ? dmi->name : DEV_NAME(dmt) ? : "",
2515
0
            dmi->uuid[0] ? dmi->uuid : DEV_UUID(dmt) ? : "",
2516
0
            dmt->major > 0 ? "(" : "",
2517
0
            dmt->major > 0 ? dmt->major : 0,
2518
0
            dmt->major > 0 ? ":" : "",
2519
0
            dmt->minor > 0 ? dmt->minor : 0,
2520
0
            dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2521
0
            dmt->major > 0 ? ")" : "",
2522
0
            strerror(dmt->ioctl_errno));
2523
2524
0
      return 0;
2525
0
    }
2526
0
  }
2527
2528
0
  if (_dmt_has_uevent(dmt) && dm_udev_get_sync_support() &&
2529
0
      !_check_uevent_generated(dmi)) {
2530
0
    if (dmt->deferred_remove)
2531
0
      log_debug_activation("Deferred remove: device busy, "
2532
0
               "no uevent generated.");
2533
0
    else {
2534
0
      log_debug_activation("Uevent not generated! Calling "
2535
0
               "udev_complete internally to "
2536
0
               "avoid process lock-up.");
2537
0
    }
2538
0
    _udev_complete(dmt);
2539
0
  }
2540
2541
0
  if (!_dm_ioctl_unmangle_names(dmt->type, dmi))
2542
0
    return 0;
2543
2544
0
  if (dmt->type != DM_DEVICE_REMOVE &&
2545
0
      !_dm_ioctl_unmangle_uuids(dmt->type, dmi))
2546
0
    return 0;
2547
2548
#else /* Userspace alternative for testing */
2549
  return 0;
2550
#endif
2551
0
  return 1;
2552
0
}
2553
2554
/* Build ioctl buffer, execute it, and post-process the result. */
2555
static struct dm_ioctl *_do_dm_ioctl(struct dm_task *dmt,
2556
             unsigned buffer_repeat_count)
2557
0
{
2558
0
  struct dm_ioctl *dmi;
2559
0
  int r;
2560
2561
0
  if (!(dmi = _dm_task_build_dmi(dmt, buffer_repeat_count)))
2562
0
    return_NULL;
2563
2564
0
  _dm_zfree_dmi(dmt->dmi.v4);
2565
0
  dmt->dmi.v4 = dmi;
2566
2567
0
  r = _dm_ioctl_exec_retry(_control_fd, dmt);
2568
2569
0
  if (dmt->record_timestamp)
2570
0
    if (!dm_timestamp_get(_dm_ioctl_timestamp))
2571
0
      stack;
2572
2573
0
  if (!_dm_ioctl_post(dmt, dmi, r)) {
2574
0
    _dm_zfree_dmi(dmi);
2575
0
    dmt->dmi.v4 = NULL;
2576
0
    return_NULL;
2577
0
  }
2578
2579
0
  return dmi;
2580
0
}
2581
2582
void dm_task_update_nodes(void)
2583
0
{
2584
0
  update_devs();
2585
0
}
2586
2587
/*
2588
 * Perform device-node operations after a successful ioctl.
2589
 * Returns 1 on success, 0 on failure.
2590
 */
2591
static int _dm_task_node_ops(struct dm_task *dmt, struct dm_ioctl *dmi)
2592
0
{
2593
0
  const char *dev_name = DEV_NAME(dmt);
2594
0
  int check_udev = dmt->cookie_set &&
2595
0
       !(dmt->event_nr >> DM_UDEV_FLAGS_SHIFT &
2596
0
         DM_UDEV_DISABLE_DM_RULES_FLAG);
2597
0
  int rely_on_udev = dmt->cookie_set ? (dmt->event_nr >> DM_UDEV_FLAGS_SHIFT &
2598
0
                DM_UDEV_DISABLE_LIBRARY_FALLBACK) : 0;
2599
2600
0
  switch (dmt->type) {
2601
0
  case DM_DEVICE_CREATE:
2602
0
    if ((dmt->add_node == DM_ADD_NODE_ON_CREATE) &&
2603
0
        dev_name && *dev_name && !rely_on_udev)
2604
0
      add_dev_node(dev_name, MAJOR(dmi->dev),
2605
0
             MINOR(dmi->dev), dmt->uid, dmt->gid,
2606
0
             dmt->mode, check_udev, rely_on_udev);
2607
0
    break;
2608
0
  case DM_DEVICE_REMOVE:
2609
0
    if (dev_name && !rely_on_udev)
2610
0
      rm_dev_node(dev_name, check_udev, rely_on_udev);
2611
0
    break;
2612
2613
0
  case DM_DEVICE_RENAME:
2614
0
    if (!dmt->new_uuid && dev_name)
2615
0
      rename_dev_node(dev_name, dmt->newname,
2616
0
          check_udev, rely_on_udev);
2617
0
    break;
2618
2619
0
  case DM_DEVICE_RESUME:
2620
0
    if (dev_name && *dev_name) {
2621
0
      if (dmt->add_node == DM_ADD_NODE_ON_RESUME)
2622
0
        add_dev_node(dev_name, MAJOR(dmi->dev),
2623
0
               MINOR(dmi->dev), dmt->uid, dmt->gid,
2624
0
               dmt->mode, check_udev, rely_on_udev);
2625
0
      set_dev_node_read_ahead(dev_name,
2626
0
            MAJOR(dmi->dev), MINOR(dmi->dev),
2627
0
            dmt->read_ahead, dmt->read_ahead_flags);
2628
0
    }
2629
0
    break;
2630
2631
0
  case DM_DEVICE_MKNODES:
2632
0
    if (dmi->flags & DM_EXISTS_FLAG)
2633
0
      add_dev_node(dmi->name, MAJOR(dmi->dev),
2634
0
             MINOR(dmi->dev), dmt->uid,
2635
0
             dmt->gid, dmt->mode, 0, rely_on_udev);
2636
0
    else if (dev_name)
2637
0
      rm_dev_node(dev_name, 0, rely_on_udev);
2638
0
    break;
2639
2640
0
  case DM_DEVICE_STATUS:
2641
0
  case DM_DEVICE_TABLE:
2642
0
  case DM_DEVICE_WAITEVENT:
2643
0
    if (!_unmarshal_status(dmt, dmi))
2644
0
      return 0;
2645
0
    break;
2646
0
  }
2647
2648
0
  return 1;
2649
0
}
2650
2651
int dm_task_get_errno(struct dm_task *dmt)
2652
0
{
2653
0
  return dmt->ioctl_errno;
2654
0
}
2655
2656
/*
2657
 * Check whether a DM_BUFFER_FULL_FLAG retry is allowed for this task type.
2658
 * Increments the global doubling factor on success.
2659
 * Returns 1 if the caller should retry with a larger buffer, 0 otherwise.
2660
 */
2661
static int _can_retry_buffer_full(struct dm_task *dmt)
2662
0
{
2663
0
  switch (dmt->type) {
2664
0
  case DM_DEVICE_LIST_VERSIONS:
2665
0
  case DM_DEVICE_LIST:
2666
0
  case DM_DEVICE_DEPS:
2667
0
  case DM_DEVICE_STATUS:
2668
0
  case DM_DEVICE_TABLE:
2669
0
  case DM_DEVICE_WAITEVENT:
2670
0
  case DM_DEVICE_TARGET_MSG:
2671
0
    break;
2672
0
  default:
2673
0
    log_error("libdevmapper buffer too small for data.");
2674
0
    return 0;
2675
0
  }
2676
2677
0
  if (_ioctl_buffer_double_factor >= DM_IOCTL_BUFFER_MAX_DOUBLINGS) {
2678
0
    log_error("Ioctl buffer maximum reached (16KB << %u = %zu bytes), giving up.",
2679
0
        _ioctl_buffer_double_factor,
2680
0
        (size_t)16 * 1024 << _ioctl_buffer_double_factor);
2681
0
    return 0;
2682
0
  }
2683
2684
0
  _ioctl_buffer_double_factor++;
2685
2686
0
  return 1;
2687
0
}
2688
2689
#if defined(GNU_SYMVER)
2690
/*
2691
 * Enforce new version 1_02_197 of dm_task_run() that propagates
2692
 * ioctl() errno is being linked to app.
2693
 */
2694
DM_EXPORT_SYMBOL_BASE(dm_task_run)
2695
int dm_task_run_base(struct dm_task *dmt);
2696
int dm_task_run_base(struct dm_task *dmt)
2697
0
{
2698
0
  return dm_task_run(dmt);
2699
0
}
2700
#endif
2701
2702
DM_EXPORT_NEW_SYMBOL(int, dm_task_run, 1_02_197)
2703
  (struct dm_task *dmt)
2704
0
{
2705
0
  struct dm_ioctl *dmi;
2706
0
  int suspended_counter;
2707
0
  const char *dev_name = DEV_NAME(dmt);
2708
0
  const char *dev_uuid = DEV_UUID(dmt);
2709
2710
0
  if (!_validate_task_type(dmt))
2711
0
    return_0;
2712
2713
  /* Old-style creation had a table supplied */
2714
0
  if (dmt->type == DM_DEVICE_CREATE && dmt->head)
2715
0
    return _create_and_load_v4(dmt);
2716
2717
0
  if (dmt->type == DM_DEVICE_MKNODES && !dev_name &&
2718
0
      !dev_uuid && dmt->major <= 0)
2719
0
    return _mknodes_v4(dmt);
2720
2721
0
  if ((dmt->type == DM_DEVICE_RELOAD) && dmt->suppress_identical_reload)
2722
0
    return _reload_with_suppression_v4(dmt);
2723
2724
0
  if ((dmt->type == DM_DEVICE_SUSPEND) && dmt->enable_checks)
2725
0
    return _suspend_with_validation_v4(dmt);
2726
2727
0
  if (!_open_control()) {
2728
0
    _udev_complete(dmt);
2729
0
    return_0;
2730
0
  }
2731
2732
0
  if ((suspended_counter = dm_get_suspended_counter()) &&
2733
0
      dmt->type == DM_DEVICE_RELOAD)
2734
0
    log_error(INTERNAL_ERROR "Performing unsafe table load while %d device(s) "
2735
0
        "are known to be suspended: "
2736
0
        "%s%s%s %s%.0d%s%.0d%s%s",
2737
0
        suspended_counter,
2738
0
        dev_name ? : "",
2739
0
        dev_uuid ? " UUID " : "",
2740
0
        dev_uuid ? : "",
2741
0
        dmt->major > 0 ? "(" : "",
2742
0
        dmt->major > 0 ? dmt->major : 0,
2743
0
        dmt->major > 0 ? ":" : "",
2744
0
        dmt->minor > 0 ? dmt->minor : 0,
2745
0
        dmt->major > 0 && dmt->minor == 0 ? "0" : "",
2746
0
        dmt->major > 0 ? ") " : "");
2747
2748
  /* FIXME Detect and warn if cookie set but should not be. */
2749
0
repeat_ioctl:
2750
0
  if (!(dmi = _do_dm_ioctl(dmt, _ioctl_buffer_double_factor))) {
2751
0
    _udev_complete(dmt);
2752
0
    return_0;
2753
0
  }
2754
2755
0
  if (dmi->flags & DM_BUFFER_FULL_FLAG) {
2756
0
    if (_can_retry_buffer_full(dmt))
2757
0
      goto repeat_ioctl;
2758
2759
0
    goto_bad;
2760
0
  }
2761
2762
0
  if (!_dm_task_node_ops(dmt, dmi))
2763
0
    goto_bad;
2764
2765
0
  return 1;
2766
2767
0
      bad:
2768
0
  _dm_zfree_dmi(dmt->dmi.v4);
2769
0
  dmt->dmi.v4 = NULL;
2770
0
  return 0;
2771
0
}
2772
2773
void dm_hold_control_dev(int hold_open)
2774
0
{
2775
0
  _hold_control_fd_open = hold_open ? 1 : 0;
2776
2777
0
  log_debug("Hold of control device is now %sset.",
2778
0
      _hold_control_fd_open ? "" : "un");
2779
0
}
2780
2781
void dm_lib_release(void)
2782
5.94k
{
2783
5.94k
  if (!_hold_control_fd_open)
2784
5.94k
    _close_control_fd();
2785
5.94k
  dm_timestamp_destroy(_dm_ioctl_timestamp);
2786
5.94k
  _dm_ioctl_timestamp = NULL;
2787
5.94k
  update_devs();
2788
5.94k
}
2789
2790
void dm_pools_check_leaks(void);
2791
2792
void dm_lib_exit(void)
2793
0
{
2794
0
  int suspended_counter;
2795
0
  static unsigned _exited = 0;
2796
2797
0
  if (_exited++)
2798
0
    return;
2799
2800
0
  if ((suspended_counter = dm_get_suspended_counter()))
2801
0
    log_error("libdevmapper exiting with %d device(s) still suspended.", suspended_counter);
2802
2803
0
  dm_lib_release();
2804
0
  selinux_release();
2805
0
  if (_dm_bitset)
2806
0
    dm_bitset_destroy(_dm_bitset);
2807
0
  _dm_bitset = NULL;
2808
0
  dm_pools_check_leaks();
2809
0
  dm_dump_memory();
2810
0
  _version_ok = 1;
2811
0
  _version_checked = 0;
2812
0
}
2813
2814
#if defined(GNU_SYMVER)
2815
/*
2816
 * Maintain binary backward compatibility.
2817
 * Version script mechanism works with 'gcc' compatible compilers only.
2818
 */
2819
2820
/*
2821
 * This following code is here to retain ABI compatibility after adding
2822
 * the field deferred_remove to struct dm_info in version 1.02.89.
2823
 *
2824
 * Binaries linked against version 1.02.88 of libdevmapper or earlier
2825
 * will use this function that returns dm_info without the
2826
 * deferred_remove field.
2827
 *
2828
 * Binaries compiled against version 1.02.89 onwards will use
2829
 * the new function dm_task_get_info_with_deferred_remove due to the
2830
 * #define.
2831
 *
2832
 * N.B. Keep this function at the end of the file to make sure that
2833
 * no code in this file accidentally calls it.
2834
 */
2835
2836
DM_EXPORT_SYMBOL_BASE(dm_task_get_info)
2837
int dm_task_get_info_base(struct dm_task *dmt, struct dm_info *info);
2838
int dm_task_get_info_base(struct dm_task *dmt, struct dm_info *info)
2839
0
{
2840
0
  struct dm_info new_info;
2841
2842
0
  if (!dm_task_get_info(dmt, &new_info))
2843
0
    return 0;
2844
2845
0
  memcpy(info, &new_info, offsetof(struct dm_info, deferred_remove));
2846
2847
0
  return 1;
2848
0
}
2849
2850
#endif
2851
2852
int dm_task_get_info_with_deferred_remove(struct dm_task *dmt, struct dm_info *info);
2853
int dm_task_get_info_with_deferred_remove(struct dm_task *dmt, struct dm_info *info)
2854
0
{
2855
0
  struct dm_info new_info;
2856
2857
0
  if (!dm_task_get_info(dmt, &new_info))
2858
0
    return 0;
2859
2860
0
  memcpy(info, &new_info, offsetof(struct dm_info, internal_suspend));
2861
2862
0
  return 1;
2863
0
}