]> sourceware.org Git - lvm2.git/blob - tools/dmsetup.c
spacing
[lvm2.git] / tools / dmsetup.c
1 /*
2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
4 * Copyright (C) 2005-2007 NEC Corporation
5 *
6 * This file is part of the device-mapper userspace tools.
7 *
8 * It includes tree drawing code based on pstree: http://psmisc.sourceforge.net/
9 *
10 * This copyrighted material is made available to anyone wishing to use,
11 * modify, copy, or redistribute it subject to the terms and conditions
12 * of the GNU General Public License v.2.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #define _GNU_SOURCE
20 #define _FILE_OFFSET_BITS 64
21
22 #include "configure.h"
23
24 #include "dm-logging.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36 #include <sys/param.h>
37 #include <locale.h>
38 #include <langinfo.h>
39 #include <time.h>
40
41 #include <fcntl.h>
42 #include <sys/stat.h>
43
44 #ifdef UDEV_SYNC_SUPPORT
45 # include <sys/types.h>
46 # include <sys/ipc.h>
47 # include <sys/sem.h>
48 # include <libudev.h>
49 #endif
50
51 /* FIXME Unused so far */
52 #undef HAVE_SYS_STATVFS_H
53
54 #ifdef HAVE_SYS_STATVFS_H
55 # include <sys/statvfs.h>
56 #endif
57
58 #ifdef HAVE_SYS_IOCTL_H
59 # include <sys/ioctl.h>
60 #endif
61
62 #if HAVE_TERMIOS_H
63 # include <termios.h>
64 #endif
65
66 #ifdef HAVE_GETOPTLONG
67 # include <getopt.h>
68 # define GETOPTLONG_FN(a, b, c, d, e) getopt_long((a), (b), (c), (d), (e))
69 # define OPTIND_INIT 0
70 #else
71 struct option {
72 };
73 extern int optind;
74 extern char *optarg;
75 # define GETOPTLONG_FN(a, b, c, d, e) getopt((a), (b), (c))
76 # define OPTIND_INIT 1
77 #endif
78
79 #ifndef TEMP_FAILURE_RETRY
80 # define TEMP_FAILURE_RETRY(expression) \
81 (__extension__ \
82 ({ long int __result; \
83 do __result = (long int) (expression); \
84 while (__result == -1L && errno == EINTR); \
85 __result; }))
86 #endif
87
88 #ifdef linux
89 # include "kdev_t.h"
90 #else
91 # define MAJOR(x) major((x))
92 # define MINOR(x) minor((x))
93 # define MKDEV(x,y) makedev((x),(y))
94 #endif
95
96 #define LINE_SIZE 4096
97 #define ARGS_MAX 256
98 #define LOOP_TABLE_SIZE (PATH_MAX + 255)
99
100 #define DEFAULT_DM_DEV_DIR "/dev/"
101
102 #define DM_DEV_DIR_ENV_VAR_NAME "DM_DEV_DIR"
103 #define DM_UDEV_COOKIE_ENV_VAR_NAME "DM_UDEV_COOKIE"
104
105 /* FIXME Should be imported */
106 #ifndef DM_MAX_TYPE_NAME
107 # define DM_MAX_TYPE_NAME 16
108 #endif
109
110 /* FIXME Should be elsewhere */
111 #define SECTOR_SHIFT 9L
112
113 #define err(msg, x...) fprintf(stderr, msg "\n", ##x)
114
115 /*
116 * We have only very simple switches ATM.
117 */
118 enum {
119 READ_ONLY = 0,
120 ADD_NODE_ON_CREATE_ARG,
121 ADD_NODE_ON_RESUME_ARG,
122 CHECKS_ARG,
123 COLS_ARG,
124 EXEC_ARG,
125 FORCE_ARG,
126 GID_ARG,
127 HELP_ARG,
128 INACTIVE_ARG,
129 MANGLENAME_ARG,
130 MAJOR_ARG,
131 MINOR_ARG,
132 MODE_ARG,
133 NAMEPREFIXES_ARG,
134 NOFLUSH_ARG,
135 NOHEADINGS_ARG,
136 NOLOCKFS_ARG,
137 NOOPENCOUNT_ARG,
138 NOTABLE_ARG,
139 UDEVCOOKIE_ARG,
140 NOUDEVRULES_ARG,
141 NOUDEVSYNC_ARG,
142 OPTIONS_ARG,
143 READAHEAD_ARG,
144 RETRY_ARG,
145 ROWS_ARG,
146 SEPARATOR_ARG,
147 SETUUID_ARG,
148 SHOWKEYS_ARG,
149 SORT_ARG,
150 TABLE_ARG,
151 TARGET_ARG,
152 TREE_ARG,
153 UID_ARG,
154 UNBUFFERED_ARG,
155 UNQUOTED_ARG,
156 UUID_ARG,
157 VERBOSE_ARG,
158 VERIFYUDEV_ARG,
159 VERSION_ARG,
160 YES_ARG,
161 NUM_SWITCHES
162 };
163
164 typedef enum {
165 DR_TASK = 1,
166 DR_INFO = 2,
167 DR_DEPS = 4,
168 DR_TREE = 8, /* Complete dependency tree required */
169 DR_NAME = 16
170 } report_type_t;
171
172 typedef enum {
173 DN_DEVNO, /* Major and minor number pair */
174 DN_BLK, /* Block device name (e.g. dm-0) */
175 DN_MAP /* Map name (for dm devices only, equal to DN_BLK otherwise) */
176 } dev_name_t;
177
178 static int _switches[NUM_SWITCHES];
179 static int _int_args[NUM_SWITCHES];
180 static char *_string_args[NUM_SWITCHES];
181 static int _num_devices;
182 static char *_uuid;
183 static char *_table;
184 static char *_target;
185 static char *_command;
186 static uint32_t _read_ahead_flags;
187 static uint32_t _udev_cookie;
188 static int _udev_only;
189 static struct dm_tree *_dtree;
190 static struct dm_report *_report;
191 static report_type_t _report_type;
192 static dev_name_t _dev_name_type;
193
194 /*
195 * Commands
196 */
197
198 struct command;
199 #define CMD_ARGS const struct command *cmd, int argc, char **argv, struct dm_names *names, int multiple_devices
200 typedef int (*command_fn) (CMD_ARGS);
201
202 struct command {
203 const char *name;
204 const char *help;
205 int min_args;
206 int max_args;
207 int repeatable_cmd; /* Repeat to process device list? */
208 command_fn fn;
209 };
210
211 static int _parse_line(struct dm_task *dmt, char *buffer, const char *file,
212 int line)
213 {
214 char ttype[LINE_SIZE], *ptr, *comment;
215 unsigned long long start, size;
216 int n;
217
218 /* trim trailing space */
219 for (ptr = buffer + strlen(buffer) - 1; ptr >= buffer; ptr--)
220 if (!isspace((int) *ptr))
221 break;
222 ptr++;
223 *ptr = '\0';
224
225 /* trim leading space */
226 for (ptr = buffer; *ptr && isspace((int) *ptr); ptr++)
227 ;
228
229 if (!*ptr || *ptr == '#')
230 return 1;
231
232 if (sscanf(ptr, "%llu %llu %s %n",
233 &start, &size, ttype, &n) < 3) {
234 err("Invalid format on line %d of table %s", line, file);
235 return 0;
236 }
237
238 ptr += n;
239 if ((comment = strchr(ptr, (int) '#')))
240 *comment = '\0';
241
242 if (!dm_task_add_target(dmt, start, size, ttype, ptr))
243 return 0;
244
245 return 1;
246 }
247
248 static int _parse_file(struct dm_task *dmt, const char *file)
249 {
250 char *buffer = NULL;
251 size_t buffer_size = 0;
252 FILE *fp;
253 int r = 0, line = 0;
254
255 /* one-line table on cmdline */
256 if (_table)
257 return _parse_line(dmt, _table, "", ++line);
258
259 /* OK for empty stdin */
260 if (file) {
261 if (!(fp = fopen(file, "r"))) {
262 err("Couldn't open '%s' for reading", file);
263 return 0;
264 }
265 } else
266 fp = stdin;
267
268 #ifndef HAVE_GETLINE
269 buffer_size = LINE_SIZE;
270 if (!(buffer = dm_malloc(buffer_size))) {
271 err("Failed to malloc line buffer.");
272 return 0;
273 }
274
275 while (fgets(buffer, (int) buffer_size, fp))
276 #else
277 while (getline(&buffer, &buffer_size, fp) > 0)
278 #endif
279 if (!_parse_line(dmt, buffer, file ? : "on stdin", ++line))
280 goto out;
281
282 r = 1;
283
284 out:
285 memset(buffer, 0, buffer_size);
286 #ifndef HAVE_GETLINE
287 dm_free(buffer);
288 #else
289 free(buffer);
290 #endif
291 if (file && fclose(fp))
292 fprintf(stderr, "%s: fclose failed: %s", file, strerror(errno));
293
294 return r;
295 }
296
297 struct dm_split_name {
298 char *subsystem;
299 char *vg_name;
300 char *lv_name;
301 char *lv_layer;
302 };
303
304 struct dmsetup_report_obj {
305 struct dm_task *task;
306 struct dm_info *info;
307 struct dm_task *deps_task;
308 struct dm_tree_node *tree_node;
309 struct dm_split_name *split_name;
310 };
311
312 static struct dm_task *_get_deps_task(int major, int minor)
313 {
314 struct dm_task *dmt;
315 struct dm_info info;
316
317 if (!(dmt = dm_task_create(DM_DEVICE_DEPS)))
318 return NULL;
319
320 if (!dm_task_set_major(dmt, major) ||
321 !dm_task_set_minor(dmt, minor))
322 goto err;
323
324 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
325 goto err;
326
327 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
328 goto err;
329
330 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
331 goto err;
332
333 if (!dm_task_run(dmt))
334 goto err;
335
336 if (!dm_task_get_info(dmt, &info))
337 goto err;
338
339 if (!info.exists)
340 goto err;
341
342 return dmt;
343
344 err:
345 dm_task_destroy(dmt);
346 return NULL;
347 }
348
349 static char *_extract_uuid_prefix(const char *uuid, const int separator)
350 {
351 char *ptr = NULL;
352 char *uuid_prefix = NULL;
353 size_t len;
354
355 if (uuid)
356 ptr = strchr(uuid, separator);
357
358 len = ptr ? ptr - uuid : 0;
359 if (!(uuid_prefix = dm_malloc(len + 1))) {
360 log_error("Failed to allocate memory to extract uuid prefix.");
361 return NULL;
362 }
363
364 if (uuid)
365 memcpy(uuid_prefix, uuid, len);
366
367 uuid_prefix[len] = '\0';
368
369 return uuid_prefix;
370 }
371
372 static struct dm_split_name *_get_split_name(const char *uuid, const char *name,
373 int separator)
374 {
375 struct dm_split_name *split_name;
376
377 if (!(split_name = dm_malloc(sizeof(*split_name)))) {
378 log_error("Failed to allocate memory to split device name "
379 "into components.");
380 return NULL;
381 }
382
383 if (!(split_name->subsystem = _extract_uuid_prefix(uuid, separator)))
384 return_NULL;
385
386 split_name->vg_name = split_name->lv_name =
387 split_name->lv_layer = (char *) "";
388
389 if (!strcmp(split_name->subsystem, "LVM") &&
390 (!(split_name->vg_name = dm_strdup(name)) ||
391 !dm_split_lvm_name(NULL, NULL, &split_name->vg_name,
392 &split_name->lv_name, &split_name->lv_layer)))
393 log_error("Failed to allocate memory to split LVM name "
394 "into components.");
395
396 return split_name;
397 }
398
399 static void _destroy_split_name(struct dm_split_name *split_name)
400 {
401 /*
402 * lv_name and lv_layer are allocated within the same block
403 * of memory as vg_name so don't need to be freed separately.
404 */
405 if (!strcmp(split_name->subsystem, "LVM"))
406 dm_free(split_name->vg_name);
407
408 dm_free(split_name->subsystem);
409 dm_free(split_name);
410 }
411
412 static int _display_info_cols(struct dm_task *dmt, struct dm_info *info)
413 {
414 struct dmsetup_report_obj obj;
415 int r = 0;
416
417 if (!info->exists) {
418 fprintf(stderr, "Device does not exist.\n");
419 return 0;
420 }
421
422 obj.task = dmt;
423 obj.info = info;
424 obj.deps_task = NULL;
425 obj.split_name = NULL;
426
427 if (_report_type & DR_TREE)
428 if (!(obj.tree_node = dm_tree_find_node(_dtree, info->major, info->minor))) {
429 log_error("Cannot find node %d:%d.", info->major, info->minor);
430 goto out;
431 }
432
433 if (_report_type & DR_DEPS)
434 if (!(obj.deps_task = _get_deps_task(info->major, info->minor))) {
435 log_error("Cannot get deps for %d:%d.", info->major, info->minor);
436 goto out;
437 }
438
439 if (_report_type & DR_NAME)
440 if (!(obj.split_name = _get_split_name(dm_task_get_uuid(dmt),
441 dm_task_get_name(dmt), '-')))
442 goto_out;
443
444 if (!dm_report_object(_report, &obj))
445 goto_out;
446
447 r = 1;
448
449 out:
450 if (obj.deps_task)
451 dm_task_destroy(obj.deps_task);
452 if (obj.split_name)
453 _destroy_split_name(obj.split_name);
454 return r;
455 }
456
457 static void _display_info_long(struct dm_task *dmt, struct dm_info *info)
458 {
459 const char *uuid;
460 uint32_t read_ahead;
461
462 if (!info->exists) {
463 printf("Device does not exist.\n");
464 return;
465 }
466
467 printf("Name: %s\n", dm_task_get_name(dmt));
468
469 printf("State: %s%s\n",
470 info->suspended ? "SUSPENDED" : "ACTIVE",
471 info->read_only ? " (READ-ONLY)" : "");
472
473 /* FIXME Old value is being printed when it's being changed. */
474 if (dm_task_get_read_ahead(dmt, &read_ahead))
475 printf("Read Ahead: %" PRIu32 "\n", read_ahead);
476
477 if (!info->live_table && !info->inactive_table)
478 printf("Tables present: None\n");
479 else
480 printf("Tables present: %s%s%s\n",
481 info->live_table ? "LIVE" : "",
482 info->live_table && info->inactive_table ? " & " : "",
483 info->inactive_table ? "INACTIVE" : "");
484
485 if (info->open_count != -1)
486 printf("Open count: %d\n", info->open_count);
487
488 printf("Event number: %" PRIu32 "\n", info->event_nr);
489 printf("Major, minor: %d, %d\n", info->major, info->minor);
490
491 if (info->target_count != -1)
492 printf("Number of targets: %d\n", info->target_count);
493
494 if ((uuid = dm_task_get_uuid(dmt)) && *uuid)
495 printf("UUID: %s\n", uuid);
496
497 printf("\n");
498 }
499
500 static int _display_info(struct dm_task *dmt)
501 {
502 struct dm_info info;
503
504 if (!dm_task_get_info(dmt, &info))
505 return 0;
506
507 if (!_switches[COLS_ARG])
508 _display_info_long(dmt, &info);
509 else
510 /* FIXME return code */
511 _display_info_cols(dmt, &info);
512
513 return info.exists ? 1 : 0;
514 }
515
516 static int _set_task_device(struct dm_task *dmt, const char *name, int optional)
517 {
518 if (name) {
519 if (!dm_task_set_name(dmt, name))
520 return 0;
521 } else if (_switches[UUID_ARG]) {
522 if (!dm_task_set_uuid(dmt, _uuid))
523 return 0;
524 } else if (_switches[MAJOR_ARG] && _switches[MINOR_ARG]) {
525 if (!dm_task_set_major(dmt, _int_args[MAJOR_ARG]) ||
526 !dm_task_set_minor(dmt, _int_args[MINOR_ARG]))
527 return 0;
528 } else if (!optional) {
529 fprintf(stderr, "No device specified.\n");
530 return 0;
531 }
532
533 return 1;
534 }
535
536 static int _set_task_add_node(struct dm_task *dmt)
537 {
538 if (!dm_task_set_add_node(dmt, DEFAULT_DM_ADD_NODE))
539 return 0;
540
541 if (_switches[ADD_NODE_ON_RESUME_ARG] &&
542 !dm_task_set_add_node(dmt, DM_ADD_NODE_ON_RESUME))
543 return 0;
544
545 if (_switches[ADD_NODE_ON_CREATE_ARG] &&
546 !dm_task_set_add_node(dmt, DM_ADD_NODE_ON_CREATE))
547 return 0;
548
549 return 1;
550 }
551
552 static int _load(CMD_ARGS)
553 {
554 int r = 0;
555 struct dm_task *dmt;
556 const char *file = NULL;
557 const char *name = NULL;
558
559 if (_switches[NOTABLE_ARG]) {
560 err("--notable only available when creating new device\n");
561 return 0;
562 }
563
564 if (!_switches[UUID_ARG] && !_switches[MAJOR_ARG]) {
565 if (argc == 1) {
566 err("Please specify device.\n");
567 return 0;
568 }
569 name = argv[1];
570 argc--;
571 argv++;
572 } else if (argc > 2) {
573 err("Too many command line arguments.\n");
574 return 0;
575 }
576
577 if (argc == 2)
578 file = argv[1];
579
580 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
581 return 0;
582
583 if (!_set_task_device(dmt, name, 0))
584 goto out;
585
586 if (!_switches[NOTABLE_ARG] && !_parse_file(dmt, file))
587 goto out;
588
589 if (_switches[READ_ONLY] && !dm_task_set_ro(dmt))
590 goto out;
591
592 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
593 goto out;
594
595 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
596 goto out;
597
598 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
599 goto out;
600
601 if (!dm_task_run(dmt))
602 goto out;
603
604 r = 1;
605
606 if (_switches[VERBOSE_ARG])
607 r = _display_info(dmt);
608
609 out:
610 dm_task_destroy(dmt);
611
612 return r;
613 }
614
615 static int _create(CMD_ARGS)
616 {
617 int r = 0;
618 struct dm_task *dmt;
619 const char *file = NULL;
620 uint32_t cookie = 0;
621 uint16_t udev_flags = 0;
622
623 if (argc == 3)
624 file = argv[2];
625
626 if (!(dmt = dm_task_create(DM_DEVICE_CREATE)))
627 return 0;
628
629 if (!dm_task_set_name(dmt, argv[1]))
630 goto out;
631
632 if (_switches[UUID_ARG] && !dm_task_set_uuid(dmt, _uuid))
633 goto out;
634
635 if (!_switches[NOTABLE_ARG] && !_parse_file(dmt, file))
636 goto out;
637
638 if (_switches[READ_ONLY] && !dm_task_set_ro(dmt))
639 goto out;
640
641 if (_switches[MAJOR_ARG] && !dm_task_set_major(dmt, _int_args[MAJOR_ARG]))
642 goto out;
643
644 if (_switches[MINOR_ARG] && !dm_task_set_minor(dmt, _int_args[MINOR_ARG]))
645 goto out;
646
647 if (_switches[UID_ARG] && !dm_task_set_uid(dmt, _int_args[UID_ARG]))
648 goto out;
649
650 if (_switches[GID_ARG] && !dm_task_set_gid(dmt, _int_args[GID_ARG]))
651 goto out;
652
653 if (_switches[MODE_ARG] && !dm_task_set_mode(dmt, _int_args[MODE_ARG]))
654 goto out;
655
656 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
657 goto out;
658
659 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
660 goto out;
661
662 if (_switches[READAHEAD_ARG] &&
663 !dm_task_set_read_ahead(dmt, _int_args[READAHEAD_ARG],
664 _read_ahead_flags))
665 goto out;
666
667 if (_switches[NOTABLE_ARG])
668 dm_udev_set_sync_support(0);
669
670 if (_switches[NOUDEVRULES_ARG])
671 udev_flags |= DM_UDEV_DISABLE_DM_RULES_FLAG |
672 DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG;
673
674 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
675 goto out;
676
677 if (!_set_task_add_node(dmt))
678 goto out;
679
680 if (_udev_cookie)
681 cookie = _udev_cookie;
682
683 if (_udev_only)
684 udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;
685
686 if (!dm_task_set_cookie(dmt, &cookie, udev_flags) ||
687 !dm_task_run(dmt))
688 goto out;
689
690 r = 1;
691
692 out:
693 if (!_udev_cookie)
694 (void) dm_udev_wait(cookie);
695
696 if (r && _switches[VERBOSE_ARG])
697 r = _display_info(dmt);
698
699 dm_task_destroy(dmt);
700
701 return r;
702 }
703
704 static int _do_rename(const char *name, const char *new_name, const char *new_uuid) {
705 int r = 0;
706 struct dm_task *dmt;
707 uint32_t cookie = 0;
708 uint16_t udev_flags = 0;
709
710 if (!(dmt = dm_task_create(DM_DEVICE_RENAME)))
711 return 0;
712
713 /* FIXME Kernel doesn't support uuid or device number here yet */
714 if (!_set_task_device(dmt, name, 0))
715 goto out;
716
717 if (new_uuid) {
718 if (!dm_task_set_newuuid(dmt, new_uuid))
719 goto out;
720 } else if (!dm_task_set_newname(dmt, new_name))
721 goto out;
722
723 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
724 goto out;
725
726 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
727 goto out;
728
729 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
730 goto out;
731
732 if (_switches[NOUDEVRULES_ARG])
733 udev_flags |= DM_UDEV_DISABLE_DM_RULES_FLAG |
734 DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG;
735
736 if (_udev_cookie)
737 cookie = _udev_cookie;
738
739 if (_udev_only)
740 udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;
741
742 if (!dm_task_set_cookie(dmt, &cookie, udev_flags) ||
743 !dm_task_run(dmt))
744 goto out;
745
746 r = 1;
747
748 out:
749 if (!_udev_cookie)
750 (void) dm_udev_wait(cookie);
751
752 dm_task_destroy(dmt);
753
754 return r;
755 }
756
757 static int _rename(CMD_ARGS)
758 {
759 const char *name = (argc == 3) ? argv[1] : NULL;
760
761 return _switches[SETUUID_ARG] ? _do_rename(name, NULL, argv[argc - 1]) :
762 _do_rename(name, argv[argc - 1], NULL);
763
764 }
765
766 static int _message(CMD_ARGS)
767 {
768 int r = 0, i;
769 size_t sz = 1;
770 struct dm_task *dmt;
771 char *str;
772
773 if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
774 return 0;
775
776 if (_switches[UUID_ARG] || _switches[MAJOR_ARG]) {
777 if (!_set_task_device(dmt, NULL, 0))
778 goto out;
779 } else {
780 if (!_set_task_device(dmt, argv[1], 0))
781 goto out;
782 argc--;
783 argv++;
784 }
785
786 if (!dm_task_set_sector(dmt, (uint64_t) atoll(argv[1])))
787 goto out;
788
789 argc -= 2;
790 argv += 2;
791
792 if (argc <= 0)
793 err("No message supplied.\n");
794
795 for (i = 0; i < argc; i++)
796 sz += strlen(argv[i]) + 1;
797
798 if (!(str = dm_zalloc(sz))) {
799 err("message string allocation failed");
800 goto out;
801 }
802
803 for (i = 0; i < argc; i++) {
804 if (i)
805 strcat(str, " ");
806 strcat(str, argv[i]);
807 }
808
809 i = dm_task_set_message(dmt, str);
810
811 dm_free(str);
812
813 if (!i)
814 goto out;
815
816 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
817 goto out;
818
819 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
820 goto out;
821
822 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
823 goto out;
824
825 if (!dm_task_run(dmt))
826 goto out;
827
828 r = 1;
829
830 out:
831 dm_task_destroy(dmt);
832
833 return r;
834 }
835
836 static int _setgeometry(CMD_ARGS)
837 {
838 int r = 0;
839 struct dm_task *dmt;
840
841 if (!(dmt = dm_task_create(DM_DEVICE_SET_GEOMETRY)))
842 return 0;
843
844 if (_switches[UUID_ARG] || _switches[MAJOR_ARG]) {
845 if (!_set_task_device(dmt, NULL, 0))
846 goto out;
847 } else {
848 if (!_set_task_device(dmt, argv[1], 0))
849 goto out;
850 argc--;
851 argv++;
852 }
853
854 if (!dm_task_set_geometry(dmt, argv[1], argv[2], argv[3], argv[4]))
855 goto out;
856
857 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
858 goto out;
859
860 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
861 goto out;
862
863 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
864 goto out;
865
866 /* run the task */
867 if (!dm_task_run(dmt))
868 goto out;
869
870 r = 1;
871
872 out:
873 dm_task_destroy(dmt);
874
875 return r;
876 }
877
878 static int _splitname(CMD_ARGS)
879 {
880 struct dmsetup_report_obj obj;
881 int r = 1;
882
883 obj.task = NULL;
884 obj.info = NULL;
885 obj.deps_task = NULL;
886 obj.tree_node = NULL;
887 if (!(obj.split_name = _get_split_name((argc == 3) ? argv[2] : "LVM",
888 argv[1], '\0')))
889 return_0;
890
891 r = dm_report_object(_report, &obj);
892 _destroy_split_name(obj.split_name);
893
894 return r;
895 }
896
897 static uint32_t _get_cookie_value(const char *str_value)
898 {
899 unsigned long int value;
900 char *p;
901
902 if (!(value = strtoul(str_value, &p, 0)) ||
903 *p ||
904 (value == ULONG_MAX && errno == ERANGE) ||
905 value > 0xFFFFFFFF) {
906 err("Incorrect cookie value");
907 return 0;
908 }
909 else
910 return (uint32_t) value;
911 }
912
913 static int _udevflags(CMD_ARGS)
914 {
915 uint32_t cookie;
916 uint16_t flags;
917 int i;
918 static const char *dm_flag_names[] = {"DISABLE_DM_RULES",
919 "DISABLE_SUBSYSTEM_RULES",
920 "DISABLE_DISK_RULES",
921 "DISABLE_OTHER_RULES",
922 "LOW_PRIORITY",
923 "DISABLE_LIBRARY_FALLBACK",
924 "PRIMARY_SOURCE",
925 0};
926
927 if (!(cookie = _get_cookie_value(argv[1])))
928 return 0;
929
930 flags = cookie >> DM_UDEV_FLAGS_SHIFT;
931
932 for (i = 0; i < DM_UDEV_FLAGS_SHIFT; i++)
933 if (1 << i & flags) {
934 if (i < DM_UDEV_FLAGS_SHIFT / 2 && dm_flag_names[i])
935 printf("DM_UDEV_%s_FLAG='1'\n", dm_flag_names[i]);
936 else if (i < DM_UDEV_FLAGS_SHIFT / 2)
937 /*
938 * This is just a fallback. Each new DM flag
939 * should have its symbolic name assigned.
940 */
941 printf("DM_UDEV_FLAG%d='1'\n", i);
942 else
943 /*
944 * We can't assign symbolic names to subsystem
945 * flags. Their semantics vary based on the
946 * subsystem that is currently used.
947 */
948 printf("DM_SUBSYSTEM_UDEV_FLAG%d='1'\n",
949 i - DM_UDEV_FLAGS_SHIFT / 2);
950 }
951
952 return 1;
953 }
954
955 static int _udevcomplete(CMD_ARGS)
956 {
957 uint32_t cookie;
958
959 if (!(cookie = _get_cookie_value(argv[1])))
960 return 0;
961
962 /*
963 * Strip flags from the cookie and use cookie magic instead.
964 * If the cookie has non-zero prefix and the base is zero then
965 * this one carries flags to control udev rules only and it is
966 * not meant to be for notification. Return with success in this
967 * situation.
968 */
969 if (!(cookie &= ~DM_UDEV_FLAGS_MASK))
970 return 1;
971
972 cookie |= DM_COOKIE_MAGIC << DM_UDEV_FLAGS_SHIFT;
973
974 return dm_udev_complete(cookie);
975 }
976
977 #ifndef UDEV_SYNC_SUPPORT
978 static const char _cmd_not_supported[] = "Command not supported. Recompile with \"--enable-udev-sync\" to enable.";
979
980 static int _udevcreatecookie(CMD_ARGS)
981 {
982 log_error(_cmd_not_supported);
983
984 return 0;
985 }
986
987 static int _udevreleasecookie(CMD_ARGS)
988 {
989 log_error(_cmd_not_supported);
990
991 return 0;
992 }
993
994 static int _udevcomplete_all(CMD_ARGS)
995 {
996 log_error(_cmd_not_supported);
997
998 return 0;
999 }
1000
1001 static int _udevcookies(CMD_ARGS)
1002 {
1003 log_error(_cmd_not_supported);
1004
1005 return 0;
1006 }
1007
1008 #else /* UDEV_SYNC_SUPPORT */
1009 static int _set_up_udev_support(const char *dev_dir)
1010 {
1011 struct udev *udev;
1012 const char *udev_dev_dir;
1013 size_t udev_dev_dir_len;
1014 int dirs_diff;
1015 const char *env;
1016
1017 if (_switches[NOUDEVSYNC_ARG])
1018 dm_udev_set_sync_support(0);
1019
1020 if (!_udev_cookie) {
1021 env = getenv(DM_UDEV_COOKIE_ENV_VAR_NAME);
1022 if (env && *env && (_udev_cookie = _get_cookie_value(env)))
1023 log_debug("Using udev transaction 0x%08" PRIX32
1024 " defined by %s environment variable.",
1025 _udev_cookie,
1026 DM_UDEV_COOKIE_ENV_VAR_NAME);
1027 }
1028 else if (_switches[UDEVCOOKIE_ARG])
1029 log_debug("Using udev transaction 0x%08" PRIX32
1030 " defined by --udevcookie option.",
1031 _udev_cookie);
1032
1033 if (!(udev = udev_new()) ||
1034 !(udev_dev_dir = udev_get_dev_path(udev)) ||
1035 !*udev_dev_dir) {
1036 log_error("Could not get udev dev path.");
1037 return 0;
1038 }
1039 udev_dev_dir_len = strlen(udev_dev_dir);
1040
1041 /*
1042 * Normally, there's always a fallback action by libdevmapper if udev
1043 * has not done its job correctly, e.g. the nodes were not created.
1044 * If using udev transactions by specifying existing cookie value,
1045 * we need to disable node creation by libdevmapper completely,
1046 * disabling any fallback actions, since any synchronisation happens
1047 * at the end of the transaction only. We need to do this to prevent
1048 * races between udev and libdevmapper but only in case udev "dev path"
1049 * is the same as "dev path" used by libdevmapper.
1050 */
1051
1052 /* There's always a slash at the end of dev_dir. But check udev_dev_dir! */
1053 if (udev_dev_dir[udev_dev_dir_len - 1] != '/')
1054 dirs_diff = strncmp(dev_dir, udev_dev_dir, udev_dev_dir_len);
1055 else
1056 dirs_diff = strcmp(dev_dir, udev_dev_dir);
1057
1058 _udev_only = !dirs_diff && (_udev_cookie || !_switches[VERIFYUDEV_ARG]);
1059
1060 if (dirs_diff) {
1061 log_debug("The path %s used for creating device nodes that is "
1062 "set via DM_DEV_DIR environment variable differs from "
1063 "the path %s that is used by udev. All warnings "
1064 "about udev not working correctly while processing "
1065 "particular nodes will be suppressed. These nodes "
1066 "and symlinks will be managed in each directory "
1067 "separately.", dev_dir, udev_dev_dir);
1068 dm_udev_set_checking(0);
1069 }
1070
1071 udev_unref(udev);
1072 return 1;
1073 }
1074
1075 static int _udevcreatecookie(CMD_ARGS)
1076 {
1077 uint32_t cookie;
1078
1079 if (!dm_udev_create_cookie(&cookie))
1080 return 0;
1081
1082 if (cookie)
1083 printf("0x%08" PRIX32 "\n", cookie);
1084
1085 return 1;
1086 }
1087
1088 static int _udevreleasecookie(CMD_ARGS)
1089 {
1090 if (argv[1] && !(_udev_cookie = _get_cookie_value(argv[1])))
1091 return 0;
1092
1093 if (!_udev_cookie) {
1094 log_error("No udev transaction cookie given.");
1095 return 0;
1096 }
1097
1098 return dm_udev_wait(_udev_cookie);
1099 }
1100
1101 __attribute__((format(printf, 1, 2)))
1102 static char _yes_no_prompt(const char *prompt, ...)
1103 {
1104 int c = 0, ret = 0;
1105 va_list ap;
1106
1107 do {
1108 if (c == '\n' || !c) {
1109 va_start(ap, prompt);
1110 vprintf(prompt, ap);
1111 va_end(ap);
1112 }
1113
1114 if ((c = getchar()) == EOF) {
1115 ret = 'n';
1116 break;
1117 }
1118
1119 c = tolower(c);
1120 if ((c == 'y') || (c == 'n'))
1121 ret = c;
1122 } while (!ret || c != '\n');
1123
1124 if (c != '\n')
1125 printf("\n");
1126
1127 return ret;
1128 }
1129
1130 static int _udevcomplete_all(CMD_ARGS)
1131 {
1132 int max_id, id, sid;
1133 struct seminfo sinfo;
1134 struct semid_ds sdata;
1135 int counter = 0;
1136 int skipped = 0;
1137 unsigned age = 0;
1138 time_t t;
1139
1140 if (argc == 2 && (sscanf(argv[1], "%i", &age) != 1)) {
1141 log_error("Failed to read age_in_minutes parameter.");
1142 return 0;
1143 }
1144
1145 if (!_switches[YES_ARG]) {
1146 log_warn("This operation will destroy all semaphores %s%.0d%swith keys "
1147 "that have a prefix %" PRIu16 " (0x%" PRIx16 ").",
1148 age ? "older than " : "", age, age ? " minutes " : "",
1149 DM_COOKIE_MAGIC, DM_COOKIE_MAGIC);
1150
1151 if (_yes_no_prompt("Do you really want to continue? [y/n]: ") == 'n') {
1152 log_print("Semaphores with keys prefixed by %" PRIu16
1153 " (0x%" PRIx16 ") NOT destroyed.",
1154 DM_COOKIE_MAGIC, DM_COOKIE_MAGIC);
1155 return 1;
1156 }
1157 }
1158
1159 if ((max_id = semctl(0, 0, SEM_INFO, &sinfo)) < 0) {
1160 log_sys_error("semctl", "SEM_INFO");
1161 return 0;
1162 }
1163
1164 for (id = 0; id <= max_id; id++) {
1165 if ((sid = semctl(id, 0, SEM_STAT, &sdata)) < 0)
1166 continue;
1167
1168 if (sdata.sem_perm.__key >> 16 == DM_COOKIE_MAGIC) {
1169 t = time(NULL);
1170
1171 if (sdata.sem_ctime + age * 60 > t ||
1172 sdata.sem_otime + age * 60 > t) {
1173 skipped++;
1174 continue;
1175 }
1176 if (semctl(sid, 0, IPC_RMID, 0) < 0) {
1177 log_error("Could not cleanup notification semaphore "
1178 "with semid %d and cookie value "
1179 "%" PRIu32 " (0x%" PRIx32 ")", sid,
1180 sdata.sem_perm.__key, sdata.sem_perm.__key);
1181 continue;
1182 }
1183
1184 counter++;
1185 }
1186 }
1187
1188 log_print("%d semaphores with keys prefixed by "
1189 "%" PRIu16 " (0x%" PRIx16 ") destroyed. %d skipped.",
1190 counter, DM_COOKIE_MAGIC, DM_COOKIE_MAGIC, skipped);
1191
1192 return 1;
1193 }
1194
1195 static int _udevcookies(CMD_ARGS)
1196 {
1197 int max_id, id, sid;
1198 struct seminfo sinfo;
1199 struct semid_ds sdata;
1200 int val;
1201 char otime_str[26], ctime_str[26];
1202 char *otimes, *ctimes;
1203
1204 if ((max_id = semctl(0, 0, SEM_INFO, &sinfo)) < 0) {
1205 log_sys_error("sem_ctl", "SEM_INFO");
1206 return 0;
1207 }
1208
1209 printf("Cookie Semid Value Last semop time Last change time\n");
1210
1211 for (id = 0; id <= max_id; id++) {
1212 if ((sid = semctl(id, 0, SEM_STAT, &sdata)) < 0)
1213 continue;
1214
1215 if (sdata.sem_perm.__key >> 16 == DM_COOKIE_MAGIC) {
1216 if ((val = semctl(sid, 0, GETVAL)) < 0) {
1217 log_error("semid %d: sem_ctl failed for "
1218 "cookie 0x%" PRIx32 ": %s",
1219 sid, sdata.sem_perm.__key,
1220 strerror(errno));
1221 continue;
1222 }
1223
1224 if ((otimes = ctime_r((const time_t *) &sdata.sem_otime, (char *)&otime_str)))
1225 otime_str[strlen(otimes)-1] = '\0';
1226 if ((ctimes = ctime_r((const time_t *) &sdata.sem_ctime, (char *)&ctime_str)))
1227 ctime_str[strlen(ctimes)-1] = '\0';
1228
1229 printf("0x%-10x %-10d %-10d %s %s\n", sdata.sem_perm.__key,
1230 sid, val, otimes ? : "unknown",
1231 ctimes? : "unknown");
1232 }
1233 }
1234
1235 return 1;
1236 }
1237 #endif /* UDEV_SYNC_SUPPORT */
1238
1239 static int _version(CMD_ARGS)
1240 {
1241 char version[80];
1242
1243 if (dm_get_library_version(version, sizeof(version)))
1244 printf("Library version: %s\n", version);
1245
1246 if (!dm_driver_version(version, sizeof(version)))
1247 return 0;
1248
1249 printf("Driver version: %s\n", version);
1250
1251 return 1;
1252 }
1253
1254 static int _simple(int task, const char *name, uint32_t event_nr, int display)
1255 {
1256 uint32_t cookie = 0;
1257 uint16_t udev_flags = 0;
1258 int udev_wait_flag = task == DM_DEVICE_RESUME ||
1259 task == DM_DEVICE_REMOVE;
1260 int r = 0;
1261
1262 struct dm_task *dmt;
1263
1264 if (!(dmt = dm_task_create(task)))
1265 return 0;
1266
1267 if (!_set_task_device(dmt, name, 0))
1268 goto out;
1269
1270 if (event_nr && !dm_task_set_event_nr(dmt, event_nr))
1271 goto out;
1272
1273 if (_switches[NOFLUSH_ARG] && !dm_task_no_flush(dmt))
1274 goto out;
1275
1276 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
1277 goto out;
1278
1279 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
1280 goto out;
1281
1282 if (_switches[NOLOCKFS_ARG] && !dm_task_skip_lockfs(dmt))
1283 goto out;
1284
1285 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1286 goto out;
1287
1288 /* FIXME: needs to coperate with udev */
1289 if (!_set_task_add_node(dmt))
1290 goto out;
1291
1292 if (_switches[READAHEAD_ARG] &&
1293 !dm_task_set_read_ahead(dmt, _int_args[READAHEAD_ARG],
1294 _read_ahead_flags))
1295 goto out;
1296
1297 if (_switches[NOUDEVRULES_ARG])
1298 udev_flags |= DM_UDEV_DISABLE_DM_RULES_FLAG |
1299 DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG;
1300
1301 if (_udev_cookie)
1302 cookie = _udev_cookie;
1303
1304 if (_udev_only)
1305 udev_flags |= DM_UDEV_DISABLE_LIBRARY_FALLBACK;
1306
1307 if (udev_wait_flag && !dm_task_set_cookie(dmt, &cookie, udev_flags))
1308 goto out;
1309
1310 if (_switches[RETRY_ARG] && task == DM_DEVICE_REMOVE)
1311 dm_task_retry_remove(dmt);
1312
1313 r = dm_task_run(dmt);
1314
1315 out:
1316 if (!_udev_cookie && udev_wait_flag)
1317 (void) dm_udev_wait(cookie);
1318
1319 if (r && display && _switches[VERBOSE_ARG])
1320 r = _display_info(dmt);
1321
1322 dm_task_destroy(dmt);
1323
1324 return r;
1325 }
1326
1327 static int _suspend(CMD_ARGS)
1328 {
1329 return _simple(DM_DEVICE_SUSPEND, argc > 1 ? argv[1] : NULL, 0, 1);
1330 }
1331
1332 static int _resume(CMD_ARGS)
1333 {
1334 return _simple(DM_DEVICE_RESUME, argc > 1 ? argv[1] : NULL, 0, 1);
1335 }
1336
1337 static int _clear(CMD_ARGS)
1338 {
1339 return _simple(DM_DEVICE_CLEAR, argc > 1 ? argv[1] : NULL, 0, 1);
1340 }
1341
1342 static int _wait(CMD_ARGS)
1343 {
1344 const char *name = NULL;
1345
1346 if (!_switches[UUID_ARG] && !_switches[MAJOR_ARG]) {
1347 if (argc == 1) {
1348 err("No device specified.");
1349 return 0;
1350 }
1351 name = argv[1];
1352 argc--, argv++;
1353 }
1354
1355 return _simple(DM_DEVICE_WAITEVENT, name,
1356 (argc > 1) ? (uint32_t) atoi(argv[argc - 1]) : 0, 1);
1357 }
1358
1359 static int _process_all(const struct command *cmd, int argc, char **argv, int silent,
1360 int (*fn) (CMD_ARGS))
1361 {
1362 int r = 1;
1363 struct dm_names *names;
1364 unsigned next = 0;
1365
1366 struct dm_task *dmt;
1367
1368 if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
1369 return 0;
1370
1371 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1372 goto out;
1373
1374 if (!dm_task_run(dmt)) {
1375 r = 0;
1376 goto out;
1377 }
1378
1379 if (!(names = dm_task_get_names(dmt))) {
1380 r = 0;
1381 goto out;
1382 }
1383
1384 if (!names->dev) {
1385 if (!silent)
1386 printf("No devices found\n");
1387 goto out;
1388 }
1389
1390 do {
1391 names = (struct dm_names *)((char *) names + next);
1392 if (!fn(cmd, argc, argv, names, 1))
1393 r = 0;
1394 next = names->next;
1395 } while (next);
1396
1397 out:
1398 dm_task_destroy(dmt);
1399 return r;
1400 }
1401
1402 static uint64_t _get_device_size(const char *name)
1403 {
1404 uint64_t start, length, size = UINT64_C(0);
1405 struct dm_info info;
1406 char *target_type, *params;
1407 struct dm_task *dmt;
1408 void *next = NULL;
1409
1410 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
1411 return 0;
1412
1413 if (!_set_task_device(dmt, name, 0))
1414 goto out;
1415
1416 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
1417 goto out;
1418
1419 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
1420 goto out;
1421
1422 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1423 goto out;
1424
1425 if (!dm_task_run(dmt))
1426 goto out;
1427
1428 if (!dm_task_get_info(dmt, &info) || !info.exists)
1429 goto out;
1430
1431 do {
1432 next = dm_get_next_target(dmt, next, &start, &length,
1433 &target_type, &params);
1434 size += length;
1435 } while (next);
1436
1437 out:
1438 dm_task_destroy(dmt);
1439 return size;
1440 }
1441
1442 static int _error_device(CMD_ARGS)
1443 {
1444 struct dm_task *dmt;
1445 const char *name;
1446 uint64_t size;
1447 int r = 0;
1448
1449 name = names ? names->name : argv[1];
1450
1451 size = _get_device_size(name);
1452
1453 if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
1454 return 0;
1455
1456 if (!_set_task_device(dmt, name, 0))
1457 goto error;
1458
1459 if (!dm_task_add_target(dmt, UINT64_C(0), size, "error", ""))
1460 goto error;
1461
1462 if (_switches[READ_ONLY] && !dm_task_set_ro(dmt))
1463 goto error;
1464
1465 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
1466 goto error;
1467
1468 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
1469 goto error;
1470
1471 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1472 goto error;
1473
1474 if (!dm_task_run(dmt))
1475 goto error;
1476
1477 if (!_simple(DM_DEVICE_RESUME, name, 0, 0)) {
1478 _simple(DM_DEVICE_CLEAR, name, 0, 0);
1479 goto error;
1480 }
1481
1482 r = 1;
1483
1484 error:
1485 dm_task_destroy(dmt);
1486 return r;
1487 }
1488
1489 static int _remove(CMD_ARGS)
1490 {
1491 if (_switches[FORCE_ARG] && argc > 1)
1492 (void) _error_device(cmd, argc, argv, NULL, 0);
1493
1494 return _simple(DM_DEVICE_REMOVE, argc > 1 ? argv[1] : NULL, 0, 0);
1495 }
1496
1497 static int _count_devices(CMD_ARGS)
1498 {
1499 _num_devices++;
1500
1501 return 1;
1502 }
1503
1504 static int _remove_all(CMD_ARGS)
1505 {
1506 int r;
1507
1508 /* Remove all closed devices */
1509 r = _simple(DM_DEVICE_REMOVE_ALL, "", 0, 0) | dm_mknodes(NULL);
1510
1511 if (!_switches[FORCE_ARG])
1512 return r;
1513
1514 _num_devices = 0;
1515 r |= _process_all(cmd, argc, argv, 1, _count_devices);
1516
1517 /* No devices left? */
1518 if (!_num_devices)
1519 return r;
1520
1521 r |= _process_all(cmd, argc, argv, 1, _error_device);
1522 r |= _simple(DM_DEVICE_REMOVE_ALL, "", 0, 0) | dm_mknodes(NULL);
1523
1524 _num_devices = 0;
1525 r |= _process_all(cmd, argc, argv, 1, _count_devices);
1526 if (!_num_devices)
1527 return r;
1528
1529 fprintf(stderr, "Unable to remove %d device(s).\n", _num_devices);
1530
1531 return r;
1532 }
1533
1534 static void _display_dev(struct dm_task *dmt, const char *name)
1535 {
1536 struct dm_info info;
1537
1538 if (dm_task_get_info(dmt, &info))
1539 printf("%s\t(%u, %u)\n", name, info.major, info.minor);
1540 }
1541
1542 static int _mknodes(CMD_ARGS)
1543 {
1544 return dm_mknodes(argc > 1 ? argv[1] : NULL);
1545 }
1546
1547 static int _exec_command(const char *name)
1548 {
1549 int n;
1550 static char path[PATH_MAX];
1551 static char *args[ARGS_MAX + 1];
1552 static int argc = 0;
1553 char *c;
1554 pid_t pid;
1555
1556 if (argc < 0)
1557 return 0;
1558
1559 if (!dm_mknodes(name))
1560 return 0;
1561
1562 n = snprintf(path, sizeof(path), "%s/%s", dm_dir(), name);
1563 if (n < 0 || n > (int) sizeof(path) - 1)
1564 return 0;
1565
1566 if (!argc) {
1567 c = _command;
1568 while (argc < ARGS_MAX) {
1569 while (*c && isspace(*c))
1570 c++;
1571 if (!*c)
1572 break;
1573 args[argc++] = c;
1574 while (*c && !isspace(*c))
1575 c++;
1576 if (*c)
1577 *c++ = '\0';
1578 }
1579
1580 if (!argc) {
1581 argc = -1;
1582 return 0;
1583 }
1584
1585 if (argc == ARGS_MAX) {
1586 err("Too many args to --exec\n");
1587 argc = -1;
1588 return 0;
1589 }
1590
1591 args[argc++] = path;
1592 args[argc] = NULL;
1593 }
1594
1595 if (!(pid = fork())) {
1596 execvp(args[0], args);
1597 _exit(127);
1598 } else if (pid < (pid_t) 0)
1599 return 0;
1600
1601 TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
1602
1603 return 1;
1604 }
1605
1606 static int _status(CMD_ARGS)
1607 {
1608 int r = 0;
1609 struct dm_task *dmt;
1610 void *next = NULL;
1611 uint64_t start, length;
1612 char *target_type = NULL;
1613 char *params, *c;
1614 int cmdno;
1615 const char *name = NULL;
1616 int matched = 0;
1617 int ls_only = 0;
1618 struct dm_info info;
1619
1620 if (names)
1621 name = names->name;
1622 else {
1623 if (argc == 1 && !_switches[UUID_ARG] && !_switches[MAJOR_ARG])
1624 return _process_all(cmd, argc, argv, 0, _status);
1625 name = argv[1];
1626 }
1627
1628 if (!strcmp(cmd->name, "table"))
1629 cmdno = DM_DEVICE_TABLE;
1630 else
1631 cmdno = DM_DEVICE_STATUS;
1632
1633 if (!strcmp(cmd->name, "ls"))
1634 ls_only = 1;
1635
1636 if (!(dmt = dm_task_create(cmdno)))
1637 return 0;
1638
1639 if (!_set_task_device(dmt, name, 0))
1640 goto out;
1641
1642 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
1643 goto out;
1644
1645 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
1646 goto out;
1647
1648 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1649 goto out;
1650
1651 if (!dm_task_run(dmt))
1652 goto out;
1653
1654 if (!dm_task_get_info(dmt, &info) || !info.exists)
1655 goto out;
1656
1657 if (!name)
1658 name = dm_task_get_name(dmt);
1659
1660 /* Fetch targets and print 'em */
1661 do {
1662 next = dm_get_next_target(dmt, next, &start, &length,
1663 &target_type, &params);
1664 /* Skip if target type doesn't match */
1665 if (_switches[TARGET_ARG] &&
1666 (!target_type || strcmp(target_type, _target)))
1667 continue;
1668 if (ls_only) {
1669 if (!_switches[EXEC_ARG] || !_command ||
1670 _switches[VERBOSE_ARG])
1671 _display_dev(dmt, name);
1672 next = NULL;
1673 } else if (!_switches[EXEC_ARG] || !_command ||
1674 _switches[VERBOSE_ARG]) {
1675 if (!matched && _switches[VERBOSE_ARG])
1676 _display_info(dmt);
1677 if (multiple_devices && !_switches[VERBOSE_ARG])
1678 printf("%s: ", name);
1679 if (target_type) {
1680 /* Suppress encryption key */
1681 if (!_switches[SHOWKEYS_ARG] &&
1682 cmdno == DM_DEVICE_TABLE &&
1683 !strcmp(target_type, "crypt")) {
1684 c = params;
1685 while (*c && *c != ' ')
1686 c++;
1687 if (*c)
1688 c++;
1689 while (*c && *c != ' ')
1690 *c++ = '0';
1691 }
1692 printf("%" PRIu64 " %" PRIu64 " %s %s",
1693 start, length, target_type, params);
1694 }
1695 printf("\n");
1696 }
1697 matched = 1;
1698 } while (next);
1699
1700 if (multiple_devices && _switches[VERBOSE_ARG] && matched && !ls_only)
1701 printf("\n");
1702
1703 if (matched && _switches[EXEC_ARG] && _command && !_exec_command(name))
1704 goto out;
1705
1706 r = 1;
1707
1708 out:
1709 dm_task_destroy(dmt);
1710 return r;
1711 }
1712
1713 /* Show target names and their version numbers */
1714 static int _targets(CMD_ARGS)
1715 {
1716 int r = 0;
1717 struct dm_task *dmt;
1718 struct dm_versions *target;
1719 struct dm_versions *last_target;
1720
1721 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
1722 return 0;
1723
1724 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1725 goto out;
1726
1727 if (!dm_task_run(dmt))
1728 goto out;
1729
1730 target = dm_task_get_versions(dmt);
1731
1732 /* Fetch targets and print 'em */
1733 do {
1734 last_target = target;
1735
1736 printf("%-16s v%d.%d.%d\n", target->name, target->version[0],
1737 target->version[1], target->version[2]);
1738
1739 target = (struct dm_versions *)((char *) target + target->next);
1740 } while (last_target != target);
1741
1742 r = 1;
1743
1744 out:
1745 dm_task_destroy(dmt);
1746 return r;
1747 }
1748
1749 static int _info(CMD_ARGS)
1750 {
1751 int r = 0;
1752
1753 struct dm_task *dmt;
1754 char *name = NULL;
1755
1756 if (names)
1757 name = names->name;
1758 else {
1759 if (argc == 1 && !_switches[UUID_ARG] && !_switches[MAJOR_ARG])
1760 return _process_all(cmd, argc, argv, 0, _info);
1761 name = argv[1];
1762 }
1763
1764 if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
1765 return 0;
1766
1767 if (!_set_task_device(dmt, name, 0))
1768 goto out;
1769
1770 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
1771 goto out;
1772
1773 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
1774 goto out;
1775
1776 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1777 goto out;
1778
1779 if (!dm_task_run(dmt))
1780 goto out;
1781
1782 r = _display_info(dmt);
1783
1784 out:
1785 dm_task_destroy(dmt);
1786 return r;
1787 }
1788
1789 static int _deps(CMD_ARGS)
1790 {
1791 int r = 0;
1792 uint32_t i;
1793 struct dm_deps *deps;
1794 struct dm_task *dmt;
1795 struct dm_info info;
1796 char *name = NULL;
1797 char dev_name[PATH_MAX];
1798 int major, minor;
1799
1800 if (names)
1801 name = names->name;
1802 else {
1803 if (argc == 1 && !_switches[UUID_ARG] && !_switches[MAJOR_ARG])
1804 return _process_all(cmd, argc, argv, 0, _deps);
1805 name = argv[1];
1806 }
1807
1808 if (!(dmt = dm_task_create(DM_DEVICE_DEPS)))
1809 return 0;
1810
1811 if (!_set_task_device(dmt, name, 0))
1812 goto out;
1813
1814 if (_switches[NOOPENCOUNT_ARG] && !dm_task_no_open_count(dmt))
1815 goto out;
1816
1817 if (_switches[INACTIVE_ARG] && !dm_task_query_inactive_table(dmt))
1818 goto out;
1819
1820 if (_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
1821 goto out;
1822
1823 if (!dm_task_run(dmt))
1824 goto out;
1825
1826 if (!dm_task_get_info(dmt, &info))
1827 goto out;
1828
1829 if (!(deps = dm_task_get_deps(dmt)))
1830 goto out;
1831
1832 if (!info.exists) {
1833 printf("Device does not exist.\n");
1834 r = 1;
1835 goto out;
1836 }
1837
1838 if (_switches[VERBOSE_ARG])
1839 _display_info(dmt);
1840
1841 if (multiple_devices && !_switches[VERBOSE_ARG])
1842 printf("%s: ", name);
1843 printf("%d dependencies\t:", deps->count);
1844
1845 for (i = 0; i < deps->count; i++) {
1846 major = (int) MAJOR(deps->device[i]);
1847 minor = (int) MINOR(deps->device[i]);
1848
1849 if ((_dev_name_type == DN_BLK || _dev_name_type == DN_MAP) &&
1850 dm_device_get_name(major, minor, _dev_name_type == DN_BLK,
1851 dev_name, PATH_MAX))
1852 printf(" (%s)", dev_name);
1853 else
1854 printf(" (%d, %d)", major, minor);
1855 }
1856 printf("\n");
1857
1858 if (multiple_devices && _switches[VERBOSE_ARG])
1859 printf("\n");
1860
1861 r = 1;
1862
1863 out:
1864 dm_task_destroy(dmt);
1865 return r;
1866 }
1867
1868 static int _display_name(CMD_ARGS)
1869 {
1870 char dev_name[PATH_MAX];
1871
1872 if (!names)
1873 return 1;
1874
1875 if ((_dev_name_type == DN_BLK || _dev_name_type == DN_MAP) &&
1876 dm_device_get_name((int) MAJOR(names->dev), (int) MINOR(names->dev),
1877 _dev_name_type == DN_BLK, dev_name, PATH_MAX))
1878 printf("%s\t(%s)\n", names->name, dev_name);
1879 else
1880 printf("%s\t(%d:%d)\n", names->name,
1881 (int) MAJOR(names->dev),
1882 (int) MINOR(names->dev));
1883
1884 return 1;
1885 }
1886
1887 /*
1888 * Tree drawing code
1889 */
1890
1891 enum {
1892 TR_DEVICE=0, /* display device major:minor number */
1893 TR_BLKDEVNAME, /* display device kernel name */
1894 TR_TABLE,
1895 TR_STATUS,
1896 TR_ACTIVE,
1897 TR_RW,
1898 TR_OPENCOUNT,
1899 TR_UUID,
1900 TR_COMPACT,
1901 TR_TRUNCATE,
1902 TR_BOTTOMUP,
1903 NUM_TREEMODE,
1904 };
1905
1906 static int _tree_switches[NUM_TREEMODE];
1907
1908 #define TR_PRINT_ATTRIBUTE ( _tree_switches[TR_ACTIVE] || \
1909 _tree_switches[TR_RW] || \
1910 _tree_switches[TR_OPENCOUNT] || \
1911 _tree_switches[TR_UUID] )
1912
1913 #define TR_PRINT_TARGETS ( _tree_switches[TR_TABLE] || \
1914 _tree_switches[TR_STATUS] )
1915
1916 /* Compact - fewer newlines */
1917 #define TR_PRINT_COMPACT (_tree_switches[TR_COMPACT] && \
1918 !TR_PRINT_ATTRIBUTE && \
1919 !TR_PRINT_TARGETS)
1920
1921 /* FIXME Get rid of this */
1922 #define MAX_DEPTH 100
1923
1924 /* Drawing character definition from pstree */
1925 /* [pstree comment] UTF-8 defines by Johan Myreen, updated by Ben Winslow */
1926 #define UTF_V "\342\224\202" /* U+2502, Vertical line drawing char */
1927 #define UTF_VR "\342\224\234" /* U+251C, Vertical and right */
1928 #define UTF_H "\342\224\200" /* U+2500, Horizontal */
1929 #define UTF_UR "\342\224\224" /* U+2514, Up and right */
1930 #define UTF_HD "\342\224\254" /* U+252C, Horizontal and down */
1931
1932 #define VT_BEG "\033(0\017" /* use graphic chars */
1933 #define VT_END "\033(B" /* back to normal char set */
1934 #define VT_V "x" /* see UTF definitions above */
1935 #define VT_VR "t"
1936 #define VT_H "q"
1937 #define VT_UR "m"
1938 #define VT_HD "w"
1939
1940 static struct {
1941 const char *empty_2; /* */
1942 const char *branch_2; /* |- */
1943 const char *vert_2; /* | */
1944 const char *last_2; /* `- */
1945 const char *single_3; /* --- */
1946 const char *first_3; /* -+- */
1947 }
1948 _tsym_ascii = {
1949 " ",
1950 "|-",
1951 "| ",
1952 "`-",
1953 "---",
1954 "-+-"
1955 },
1956 _tsym_utf = {
1957 " ",
1958 UTF_VR UTF_H,
1959 UTF_V " ",
1960 UTF_UR UTF_H,
1961 UTF_H UTF_H UTF_H,
1962 UTF_H UTF_HD UTF_H
1963 },
1964 _tsym_vt100 = {
1965 " ",
1966 VT_BEG VT_VR VT_H VT_END,
1967 VT_BEG VT_V VT_END " ",
1968 VT_BEG VT_UR VT_H VT_END,
1969 VT_BEG VT_H VT_H VT_H VT_END,
1970 VT_BEG VT_H VT_HD VT_H VT_END
1971 },
1972 *_tsym = &_tsym_ascii;
1973
1974 /*
1975 * Tree drawing functions.
1976 */
1977 /* FIXME Get rid of these statics - use dynamic struct */
1978 /* FIXME Explain what these vars are for */
1979 static int _tree_width[MAX_DEPTH], _tree_more[MAX_DEPTH];
1980 static int _termwidth = 80; /* Maximum output width */
1981 static int _cur_x = 1; /* Current horizontal output position */
1982 static char _last_char = 0;
1983
1984 static void _out_char(const unsigned c)
1985 {
1986 /* Only first UTF-8 char counts */
1987 _cur_x += ((c & 0xc0) != 0x80);
1988
1989 if (!_tree_switches[TR_TRUNCATE]) {
1990 putchar((int) c);
1991 return;
1992 }
1993
1994 /* Truncation? */
1995 if (_cur_x <= _termwidth)
1996 putchar((int) c);
1997
1998 if (_cur_x == _termwidth + 1 && ((c & 0xc0) != 0x80)) {
1999 if (_last_char || (c & 0x80)) {
2000 putchar('.');
2001 putchar('.');
2002 putchar('.');
2003 } else {
2004 _last_char = c;
2005 _cur_x--;
2006 }
2007 }
2008 }
2009
2010 static void _out_string(const char *str)
2011 {
2012 while (*str)
2013 _out_char((unsigned char) *str++);
2014 }
2015
2016 /* non-negative integers only */
2017 static unsigned _out_int(unsigned num)
2018 {
2019 unsigned digits = 0;
2020 unsigned divi;
2021
2022 if (!num) {
2023 _out_char('0');
2024 return 1;
2025 }
2026
2027 /* non zero case */
2028 for (divi = 1; num / divi; divi *= 10)
2029 digits++;
2030
2031 for (divi /= 10; divi; divi /= 10)
2032 _out_char('0' + (num / divi) % 10);
2033
2034 return digits;
2035 }
2036
2037 static void _out_newline(void)
2038 {
2039 if (_last_char && _cur_x == _termwidth)
2040 putchar(_last_char);
2041 _last_char = 0;
2042 putchar('\n');
2043 _cur_x = 1;
2044 }
2045
2046 static void _out_prefix(unsigned depth)
2047 {
2048 unsigned x, d;
2049
2050 for (d = 0; d < depth; d++) {
2051 for (x = _tree_width[d] + 1; x > 0; x--)
2052 _out_char(' ');
2053
2054 _out_string(d == depth - 1 ?
2055 !_tree_more[depth] ? _tsym->last_2 : _tsym->branch_2
2056 : _tree_more[d + 1] ?
2057 _tsym->vert_2 : _tsym->empty_2);
2058 }
2059 }
2060
2061 /*
2062 * Display tree
2063 */
2064 static void _display_tree_attributes(struct dm_tree_node *node)
2065 {
2066 int attr = 0;
2067 const char *uuid;
2068 const struct dm_info *info;
2069
2070 uuid = dm_tree_node_get_uuid(node);
2071 info = dm_tree_node_get_info(node);
2072
2073 if (!info->exists)
2074 return;
2075
2076 if (_tree_switches[TR_ACTIVE]) {
2077 _out_string(attr++ ? ", " : " [");
2078 _out_string(info->suspended ? "SUSPENDED" : "ACTIVE");
2079 }
2080
2081 if (_tree_switches[TR_RW]) {
2082 _out_string(attr++ ? ", " : " [");
2083 _out_string(info->read_only ? "RO" : "RW");
2084 }
2085
2086 if (_tree_switches[TR_OPENCOUNT]) {
2087 _out_string(attr++ ? ", " : " [");
2088 (void) _out_int((unsigned) info->open_count);
2089 }
2090
2091 if (_tree_switches[TR_UUID]) {
2092 _out_string(attr++ ? ", " : " [");
2093 _out_string(uuid && *uuid ? uuid : "");
2094 }
2095
2096 if (attr)
2097 _out_char(']');
2098 }
2099
2100 /* FIXME Display table or status line. (Disallow both?) */
2101 static void _display_tree_targets(struct dm_tree_node *node, unsigned depth)
2102 {
2103 }
2104
2105 static void _display_tree_node(struct dm_tree_node *node, unsigned depth,
2106 unsigned first_child __attribute__((unused)),
2107 unsigned last_child, unsigned has_children)
2108 {
2109 int offset;
2110 const char *name;
2111 const struct dm_info *info;
2112 int first_on_line = 0;
2113 char dev_name[PATH_MAX];
2114
2115 /* Sub-tree for targets has 2 more depth */
2116 if (depth + 2 > MAX_DEPTH)
2117 return;
2118
2119 name = dm_tree_node_get_name(node);
2120
2121 if ((!name || !*name) &&
2122 (!_tree_switches[TR_DEVICE] && !_tree_switches[TR_BLKDEVNAME]))
2123 return;
2124
2125 /* Indicate whether there are more nodes at this depth */
2126 _tree_more[depth] = !last_child;
2127 _tree_width[depth] = 0;
2128
2129 if (_cur_x == 1)
2130 first_on_line = 1;
2131
2132 if (!TR_PRINT_COMPACT || first_on_line)
2133 _out_prefix(depth);
2134
2135 /* Remember the starting point for compact */
2136 offset = _cur_x;
2137
2138 if (TR_PRINT_COMPACT && !first_on_line)
2139 _out_string(_tree_more[depth] ? _tsym->first_3 : _tsym->single_3);
2140
2141 /* display node */
2142 if (name)
2143 _out_string(name);
2144
2145 info = dm_tree_node_get_info(node);
2146
2147 if (_tree_switches[TR_BLKDEVNAME] &&
2148 dm_device_get_name(info->major, info->minor, 1, dev_name, PATH_MAX)) {
2149 _out_string(name ? " <" : "<");
2150 _out_string(dev_name);
2151 _out_char('>');
2152 }
2153
2154 if (_tree_switches[TR_DEVICE]) {
2155 _out_string(name ? " (" : "(");
2156 (void) _out_int(info->major);
2157 _out_char(':');
2158 (void) _out_int(info->minor);
2159 _out_char(')');
2160 }
2161
2162 /* display additional info */
2163 if (TR_PRINT_ATTRIBUTE)
2164 _display_tree_attributes(node);
2165
2166 if (TR_PRINT_COMPACT)
2167 _tree_width[depth] = _cur_x - offset;
2168
2169 if (!TR_PRINT_COMPACT || !has_children)
2170 _out_newline();
2171
2172 if (TR_PRINT_TARGETS) {
2173 _tree_more[depth + 1] = has_children;
2174 _display_tree_targets(node, depth + 2);
2175 }
2176 }
2177
2178 /*
2179 * Walk the dependency tree
2180 */
2181 static void _display_tree_walk_children(struct dm_tree_node *node,
2182 unsigned depth)
2183 {
2184 struct dm_tree_node *child, *next_child;
2185 void *handle = NULL;
2186 uint32_t inverted = _tree_switches[TR_BOTTOMUP];
2187 unsigned first_child = 1;
2188 unsigned has_children;
2189
2190 next_child = dm_tree_next_child(&handle, node, inverted);
2191
2192 while ((child = next_child)) {
2193 next_child = dm_tree_next_child(&handle, node, inverted);
2194 has_children =
2195 dm_tree_node_num_children(child, inverted) ? 1 : 0;
2196
2197 _display_tree_node(child, depth, first_child,
2198 next_child ? 0U : 1U, has_children);
2199
2200 if (has_children)
2201 _display_tree_walk_children(child, depth + 1);
2202
2203 first_child = 0;
2204 }
2205 }
2206
2207 static int _add_dep(CMD_ARGS)
2208 {
2209 if (names &&
2210 !dm_tree_add_dev(_dtree, (unsigned) MAJOR(names->dev), (unsigned) MINOR(names->dev)))
2211 return 0;
2212
2213 return 1;
2214 }
2215
2216 /*
2217 * Create and walk dependency tree
2218 */
2219 static int _build_whole_deptree(const struct command *cmd)
2220 {
2221 if (_dtree)
2222 return 1;
2223
2224 if (!(_dtree = dm_tree_create()))
2225 return 0;
2226
2227 if (!_process_all(cmd, 0, NULL, 0, _add_dep))
2228 return 0;
2229
2230 return 1;
2231 }
2232
2233 static int _display_tree(CMD_ARGS)
2234 {
2235 if (!_build_whole_deptree(cmd))
2236 return 0;
2237
2238 _display_tree_walk_children(dm_tree_find_node(_dtree, 0, 0), 0);
2239
2240 return 1;
2241 }
2242
2243 /*
2244 * Report device information
2245 */
2246
2247 /* dm specific display functions */
2248
2249 static int _int32_disp(struct dm_report *rh,
2250 struct dm_pool *mem __attribute__((unused)),
2251 struct dm_report_field *field, const void *data,
2252 void *private __attribute__((unused)))
2253 {
2254 const int32_t value = *(const int32_t *)data;
2255
2256 return dm_report_field_int32(rh, field, &value);
2257 }
2258
2259 static int _uint32_disp(struct dm_report *rh,
2260 struct dm_pool *mem __attribute__((unused)),
2261 struct dm_report_field *field, const void *data,
2262 void *private __attribute__((unused)))
2263 {
2264 const uint32_t value = *(const int32_t *)data;
2265
2266 return dm_report_field_uint32(rh, field, &value);
2267 }
2268
2269 static int _dm_name_disp(struct dm_report *rh,
2270 struct dm_pool *mem __attribute__((unused)),
2271 struct dm_report_field *field, const void *data,
2272 void *private __attribute__((unused)))
2273 {
2274 const char *name = dm_task_get_name((const struct dm_task *) data);
2275
2276 return dm_report_field_string(rh, field, &name);
2277 }
2278
2279 static int _dm_mangled_name_disp(struct dm_report *rh,
2280 struct dm_pool *mem __attribute__((unused)),
2281 struct dm_report_field *field, const void *data,
2282 void *private __attribute__((unused)))
2283 {
2284 char *name;
2285 int r = 0;
2286
2287 if ((name = dm_task_get_name_mangled((const struct dm_task *) data))) {
2288 r = dm_report_field_string(rh, field, (const char **) &name);
2289 dm_free(name);
2290 }
2291
2292 return r;
2293 }
2294
2295 static int _dm_unmangled_name_disp(struct dm_report *rh,
2296 struct dm_pool *mem __attribute__((unused)),
2297 struct dm_report_field *field, const void *data,
2298 void *private __attribute__((unused)))
2299 {
2300 char *name;
2301 int r = 0;
2302
2303 if ((name = dm_task_get_name_unmangled((const struct dm_task *) data))) {
2304 r = dm_report_field_string(rh, field, (const char **) &name);
2305 dm_free(name);
2306 }
2307
2308 return r;
2309 }
2310
2311 static int _dm_uuid_disp(struct dm_report *rh,
2312 struct dm_pool *mem __attribute__((unused)),
2313 struct dm_report_field *field,
2314 const void *data, void *private __attribute__((unused)))
2315 {
2316 const char *uuid = dm_task_get_uuid((const struct dm_task *) data);
2317
2318 if (!uuid || !*uuid)
2319 uuid = "";
2320
2321 return dm_report_field_string(rh, field, &uuid);
2322 }
2323
2324 static int _dm_read_ahead_disp(struct dm_report *rh,
2325 struct dm_pool *mem __attribute__((unused)),
2326 struct dm_report_field *field, const void *data,
2327 void *private __attribute__((unused)))
2328 {
2329 uint32_t value;
2330
2331 if (!dm_task_get_read_ahead((const struct dm_task *) data, &value))
2332 value = 0;
2333
2334 return dm_report_field_uint32(rh, field, &value);
2335 }
2336
2337 static int _dm_blk_name_disp(struct dm_report *rh,
2338 struct dm_pool *mem __attribute__((unused)),
2339 struct dm_report_field *field, const void *data,
2340 void *private __attribute__((unused)))
2341 {
2342 char dev_name[PATH_MAX];
2343 const char *s = dev_name;
2344 const struct dm_info *info = data;
2345
2346 if (!dm_device_get_name(info->major, info->minor, 1, dev_name, PATH_MAX)) {
2347 log_error("Could not resolve block device name for %d:%d.",
2348 info->major, info->minor);
2349 return 0;
2350 }
2351
2352 return dm_report_field_string(rh, field, &s);
2353 }
2354
2355 static int _dm_info_status_disp(struct dm_report *rh,
2356 struct dm_pool *mem __attribute__((unused)),
2357 struct dm_report_field *field, const void *data,
2358 void *private __attribute__((unused)))
2359 {
2360 char buf[5];
2361 const char *s = buf;
2362 const struct dm_info *info = data;
2363
2364 buf[0] = info->live_table ? 'L' : '-';
2365 buf[1] = info->inactive_table ? 'I' : '-';
2366 buf[2] = info->suspended ? 's' : '-';
2367 buf[3] = info->read_only ? 'r' : 'w';
2368 buf[4] = '\0';
2369
2370 return dm_report_field_string(rh, field, &s);
2371 }
2372
2373 static int _dm_info_table_loaded_disp(struct dm_report *rh,
2374 struct dm_pool *mem __attribute__((unused)),
2375 struct dm_report_field *field,
2376 const void *data,
2377 void *private __attribute__((unused)))
2378 {
2379 const struct dm_info *info = data;
2380
2381 if (info->live_table) {
2382 if (info->inactive_table)
2383 dm_report_field_set_value(field, "Both", NULL);
2384 else
2385 dm_report_field_set_value(field, "Live", NULL);
2386 return 1;
2387 }
2388
2389 if (info->inactive_table)
2390 dm_report_field_set_value(field, "Inactive", NULL);
2391 else
2392 dm_report_field_set_value(field, "None", NULL);
2393
2394 return 1;
2395 }
2396
2397 static int _dm_info_suspended_disp(struct dm_report *rh,
2398 struct dm_pool *mem __attribute__((unused)),
2399 struct dm_report_field *field,
2400 const void *data,
2401 void *private __attribute__((unused)))
2402 {
2403 const struct dm_info *info = data;
2404
2405 if (info->suspended)
2406 dm_report_field_set_value(field, "Suspended", NULL);
2407 else
2408 dm_report_field_set_value(field, "Active", NULL);
2409
2410 return 1;
2411 }
2412
2413 static int _dm_info_read_only_disp(struct dm_report *rh,
2414 struct dm_pool *mem __attribute__((unused)),
2415 struct dm_report_field *field,
2416 const void *data,
2417 void *private __attribute__((unused)))
2418 {
2419 const struct dm_info *info = data;
2420
2421 if (info->read_only)
2422 dm_report_field_set_value(field, "Read-only", NULL);
2423 else
2424 dm_report_field_set_value(field, "Writeable", NULL);
2425
2426 return 1;
2427 }
2428
2429
2430 static int _dm_info_devno_disp(struct dm_report *rh, struct dm_pool *mem,
2431 struct dm_report_field *field, const void *data,
2432 void *private)
2433 {
2434 char buf[PATH_MAX], *repstr;
2435 const struct dm_info *info = data;
2436
2437 if (!dm_pool_begin_object(mem, 8)) {
2438 log_error("dm_pool_begin_object failed");
2439 return 0;
2440 }
2441
2442 if (private) {
2443 if (!dm_device_get_name(info->major, info->minor,
2444 1, buf, PATH_MAX))
2445 goto out_abandon;
2446 }
2447 else {
2448 if (dm_snprintf(buf, sizeof(buf), "%d:%d",
2449 info->major, info->minor) < 0) {
2450 log_error("dm_pool_alloc failed");
2451 goto out_abandon;
2452 }
2453 }
2454
2455 if (!dm_pool_grow_object(mem, buf, strlen(buf) + 1)) {
2456 log_error("dm_pool_grow_object failed");
2457 goto out_abandon;
2458 }
2459
2460 repstr = dm_pool_end_object(mem);
2461 dm_report_field_set_value(field, repstr, repstr);
2462 return 1;
2463
2464 out_abandon:
2465 dm_pool_abandon_object(mem);
2466 return 0;
2467 }
2468
2469 static int _dm_tree_names(struct dm_report *rh, struct dm_pool *mem,
2470 struct dm_report_field *field, const void *data,
2471 void *private, unsigned inverted)
2472 {
2473 const struct dm_tree_node *node = data;
2474 struct dm_tree_node *parent;
2475 void *t = NULL;
2476 const char *name;
2477 int first_node = 1;
2478 char *repstr;
2479
2480 if (!dm_pool_begin_object(mem, 16)) {
2481 log_error("dm_pool_begin_object failed");
2482 return 0;
2483 }
2484
2485 while ((parent = dm_tree_next_child(&t, node, inverted))) {
2486 name = dm_tree_node_get_name(parent);
2487 if (!name || !*name)
2488 continue;
2489 if (!first_node && !dm_pool_grow_object(mem, ",", 1)) {
2490 log_error("dm_pool_grow_object failed");
2491 goto out_abandon;
2492 }
2493 if (!dm_pool_grow_object(mem, name, 0)) {
2494 log_error("dm_pool_grow_object failed");
2495 goto out_abandon;
2496 }
2497 if (first_node)
2498 first_node = 0;
2499 }
2500
2501 if (!dm_pool_grow_object(mem, "\0", 1)) {
2502 log_error("dm_pool_grow_object failed");
2503 goto out_abandon;
2504 }
2505
2506 repstr = dm_pool_end_object(mem);
2507 dm_report_field_set_value(field, repstr, repstr);
2508 return 1;
2509
2510 out_abandon:
2511 dm_pool_abandon_object(mem);
2512 return 0;
2513 }
2514
2515 static int _dm_deps_names_disp(struct dm_report *rh,
2516 struct dm_pool *mem,
2517 struct dm_report_field *field,
2518 const void *data, void *private)
2519 {
2520 return _dm_tree_names(rh, mem, field, data, private, 0);
2521 }
2522
2523 static int _dm_tree_parents_names_disp(struct dm_report *rh,
2524 struct dm_pool *mem,
2525 struct dm_report_field *field,
2526 const void *data, void *private)
2527 {
2528 return _dm_tree_names(rh, mem, field, data, private, 1);
2529 }
2530
2531 static int _dm_tree_parents_devs_disp(struct dm_report *rh, struct dm_pool *mem,
2532 struct dm_report_field *field,
2533 const void *data, void *private)
2534 {
2535 const struct dm_tree_node *node = data;
2536 struct dm_tree_node *parent;
2537 void *t = NULL;
2538 const struct dm_info *info;
2539 int first_node = 1;
2540 char buf[DM_MAX_TYPE_NAME], *repstr;
2541
2542 if (!dm_pool_begin_object(mem, 16)) {
2543 log_error("dm_pool_begin_object failed");
2544 return 0;
2545 }
2546
2547 while ((parent = dm_tree_next_child(&t, node, 1))) {
2548 info = dm_tree_node_get_info(parent);
2549 if (!info->major && !info->minor)
2550 continue;
2551 if (!first_node && !dm_pool_grow_object(mem, ",", 1)) {
2552 log_error("dm_pool_grow_object failed");
2553 goto out_abandon;
2554 }
2555 if (dm_snprintf(buf, sizeof(buf), "%d:%d",
2556 info->major, info->minor) < 0) {
2557 log_error("dm_snprintf failed");
2558 goto out_abandon;
2559 }
2560 if (!dm_pool_grow_object(mem, buf, 0)) {
2561 log_error("dm_pool_grow_object failed");
2562 goto out_abandon;
2563 }
2564 if (first_node)
2565 first_node = 0;
2566 }
2567
2568 if (!dm_pool_grow_object(mem, "\0", 1)) {
2569 log_error("dm_pool_grow_object failed");
2570 goto out_abandon;
2571 }
2572
2573 repstr = dm_pool_end_object(mem);
2574 dm_report_field_set_value(field, repstr, repstr);
2575 return 1;
2576
2577 out_abandon:
2578 dm_pool_abandon_object(mem);
2579 return 0;
2580 }
2581
2582 static int _dm_tree_parents_count_disp(struct dm_report *rh,
2583 struct dm_pool *mem,
2584 struct dm_report_field *field,
2585 const void *data, void *private)
2586 {
2587 const struct dm_tree_node *node = data;
2588 int num_parent = dm_tree_node_num_children(node, 1);
2589
2590 return dm_report_field_int(rh, field, &num_parent);
2591 }
2592
2593 static int _dm_deps_disp_common(struct dm_report *rh, struct dm_pool*mem,
2594 struct dm_report_field *field, const void *data,
2595 void *private, int disp_blk_dev_names)
2596 {
2597 const struct dm_deps *deps = data;
2598 char buf[PATH_MAX], *repstr;
2599 int major, minor;
2600 unsigned i;
2601
2602 if (!dm_pool_begin_object(mem, 16)) {
2603 log_error("dm_pool_begin_object failed");
2604 return 0;
2605 }
2606
2607 for (i = 0; i < deps->count; i++) {
2608 major = (int) MAJOR(deps->device[i]);
2609 minor = (int) MINOR(deps->device[i]);
2610
2611 if (disp_blk_dev_names) {
2612 if (!dm_device_get_name(major, minor, 1, buf, PATH_MAX)) {
2613 log_error("Could not resolve block device "
2614 "name for %d:%d.", major, minor);
2615 goto out_abandon;
2616 }
2617 }
2618 else if (dm_snprintf(buf, sizeof(buf), "%d:%d",
2619 major, minor) < 0) {
2620 log_error("dm_snprintf failed");
2621 goto out_abandon;
2622 }
2623
2624 if (!dm_pool_grow_object(mem, buf, 0)) {
2625 log_error("dm_pool_grow_object failed");
2626 goto out_abandon;
2627 }
2628
2629 if (i + 1 < deps->count && !dm_pool_grow_object(mem, ",", 1)) {
2630 log_error("dm_pool_grow_object failed");
2631 goto out_abandon;
2632 }
2633 }
2634
2635 if (!dm_pool_grow_object(mem, "\0", 1)) {
2636 log_error("dm_pool_grow_object failed");
2637 goto out_abandon;
2638 }
2639
2640 repstr = dm_pool_end_object(mem);
2641 dm_report_field_set_value(field, repstr, repstr);
2642 return 1;
2643
2644 out_abandon:
2645 dm_pool_abandon_object(mem);
2646 return 0;
2647 }
2648
2649 static int _dm_deps_disp(struct dm_report *rh, struct dm_pool *mem,
2650 struct dm_report_field *field, const void *data,
2651 void *private)
2652 {
2653 return _dm_deps_disp_common(rh, mem, field, data, private, 0);
2654 }
2655
2656 static int _dm_deps_blk_names_disp(struct dm_report *rh, struct dm_pool *mem,
2657 struct dm_report_field *field,
2658 const void *data, void *private)
2659 {
2660 return _dm_deps_disp_common(rh, mem, field, data, private, 1);
2661 }
2662
2663 static int _dm_subsystem_disp(struct dm_report *rh,
2664 struct dm_pool *mem __attribute__((unused)),
2665 struct dm_report_field *field, const void *data,
2666 void *private __attribute__((unused)))
2667 {
2668 return dm_report_field_string(rh, field, (const char *const *) data);
2669 }
2670
2671 static int _dm_vg_name_disp(struct dm_report *rh,
2672 struct dm_pool *mem __attribute__((unused)),
2673 struct dm_report_field *field, const void *data,
2674 void *private __attribute__((unused)))
2675 {
2676
2677 return dm_report_field_string(rh, field, (const char *const *) data);
2678 }
2679
2680 static int _dm_lv_name_disp(struct dm_report *rh,
2681 struct dm_pool *mem __attribute__((unused)),
2682 struct dm_report_field *field, const void *data,
2683 void *private __attribute__((unused)))
2684
2685 {
2686 return dm_report_field_string(rh, field, (const char *const *) data);
2687 }
2688
2689 static int _dm_lv_layer_name_disp(struct dm_report *rh,
2690 struct dm_pool *mem __attribute__((unused)),
2691 struct dm_report_field *field, const void *data,
2692 void *private __attribute__((unused)))
2693
2694 {
2695 return dm_report_field_string(rh, field, (const char *const *) data);
2696 }
2697
2698 static void *_task_get_obj(void *obj)
2699 {
2700 return ((struct dmsetup_report_obj *)obj)->task;
2701 }
2702
2703 static void *_info_get_obj(void *obj)
2704 {
2705 return ((struct dmsetup_report_obj *)obj)->info;
2706 }
2707
2708 static void *_deps_get_obj(void *obj)
2709 {
2710 return dm_task_get_deps(((struct dmsetup_report_obj *)obj)->deps_task);
2711 }
2712
2713 static void *_tree_get_obj(void *obj)
2714 {
2715 return ((struct dmsetup_report_obj *)obj)->tree_node;
2716 }
2717
2718 static void *_split_name_get_obj(void *obj)
2719 {
2720 return ((struct dmsetup_report_obj *)obj)->split_name;
2721 }
2722
2723 static const struct dm_report_object_type _report_types[] = {
2724 { DR_TASK, "Mapped Device Name", "", _task_get_obj },
2725 { DR_INFO, "Mapped Device Information", "", _info_get_obj },
2726 { DR_DEPS, "Mapped Device Relationship Information", "", _deps_get_obj },
2727 { DR_TREE, "Mapped Device Relationship Information", "", _tree_get_obj },
2728 { DR_NAME, "Mapped Device Name Components", "", _split_name_get_obj },
2729 { 0, "", "", NULL },
2730 };
2731
2732 /* Column definitions */
2733 #define OFFSET_OF(strct, field) (((char*)&((struct strct*)0)->field) - (char*)0)
2734 #define STR (DM_REPORT_FIELD_TYPE_STRING)
2735 #define NUM (DM_REPORT_FIELD_TYPE_NUMBER)
2736 #define FIELD_O(type, strct, sorttype, head, field, width, func, id, desc) {DR_ ## type, sorttype, OFFSET_OF(strct, field), width, id, head, &_ ## func ## _disp, desc},
2737 #define FIELD_F(type, sorttype, head, width, func, id, desc) {DR_ ## type, sorttype, 0, width, id, head, &_ ## func ## _disp, desc},
2738
2739 static const struct dm_report_field_type _report_fields[] = {
2740 /* *INDENT-OFF* */
2741 FIELD_F(TASK, STR, "Name", 16, dm_name, "name", "Name of mapped device.")
2742 FIELD_F(TASK, STR, "MangledName", 16, dm_mangled_name, "mangled_name", "Mangled name of mapped device.")
2743 FIELD_F(TASK, STR, "UnmangledName", 16, dm_unmangled_name, "unmangled_name", "Unmangled name of mapped device.")
2744 FIELD_F(TASK, STR, "UUID", 32, dm_uuid, "uuid", "Unique (optional) identifier for mapped device.")
2745
2746 /* FIXME Next one should be INFO */
2747 FIELD_F(TASK, NUM, "RAhead", 6, dm_read_ahead, "read_ahead", "Read ahead in sectors.")
2748
2749 FIELD_F(INFO, STR, "BlkDevName", 16, dm_blk_name, "blkdevname", "Name of block device.")
2750 FIELD_F(INFO, STR, "Stat", 4, dm_info_status, "attr", "(L)ive, (I)nactive, (s)uspended, (r)ead-only, read-(w)rite.")
2751 FIELD_F(INFO, STR, "Tables", 6, dm_info_table_loaded, "tables_loaded", "Which of the live and inactive table slots are filled.")
2752 FIELD_F(INFO, STR, "Suspended", 9, dm_info_suspended, "suspended", "Whether the device is suspended.")
2753 FIELD_F(INFO, STR, "Read-only", 9, dm_info_read_only, "readonly", "Whether the device is read-only or writeable.")
2754 FIELD_F(INFO, STR, "DevNo", 5, dm_info_devno, "devno", "Device major and minor numbers")
2755 FIELD_O(INFO, dm_info, NUM, "Maj", major, 3, int32, "major", "Block device major number.")
2756 FIELD_O(INFO, dm_info, NUM, "Min", minor, 3, int32, "minor", "Block device minor number.")
2757 FIELD_O(INFO, dm_info, NUM, "Open", open_count, 4, int32, "open", "Number of references to open device, if requested.")
2758 FIELD_O(INFO, dm_info, NUM, "Targ", target_count, 4, int32, "segments", "Number of segments in live table, if present.")
2759 FIELD_O(INFO, dm_info, NUM, "Event", event_nr, 6, uint32, "events", "Number of most recent event.")
2760
2761 FIELD_O(DEPS, dm_deps, NUM, "#Devs", count, 5, int32, "device_count", "Number of devices used by this one.")
2762 FIELD_F(TREE, STR, "DevNames", 8, dm_deps_names, "devs_used", "List of names of mapped devices used by this one.")
2763 FIELD_F(DEPS, STR, "DevNos", 6, dm_deps, "devnos_used", "List of device numbers of devices used by this one.")
2764 FIELD_F(DEPS, STR, "BlkDevNames", 16, dm_deps_blk_names, "blkdevs_used", "List of names of block devices used by this one.")
2765
2766 FIELD_F(TREE, NUM, "#Refs", 5, dm_tree_parents_count, "device_ref_count", "Number of mapped devices referencing this one.")
2767 FIELD_F(TREE, STR, "RefNames", 8, dm_tree_parents_names, "names_using_dev", "List of names of mapped devices using this one.")
2768 FIELD_F(TREE, STR, "RefDevNos", 9, dm_tree_parents_devs, "devnos_using_dev", "List of device numbers of mapped devices using this one.")
2769
2770 FIELD_O(NAME, dm_split_name, STR, "Subsys", subsystem, 6, dm_subsystem, "subsystem", "Userspace subsystem responsible for this device.")
2771 FIELD_O(NAME, dm_split_name, STR, "VG", vg_name, 4, dm_vg_name, "vg_name", "LVM Volume Group name.")
2772 FIELD_O(NAME, dm_split_name, STR, "LV", lv_name, 4, dm_lv_name, "lv_name", "LVM Logical Volume name.")
2773 FIELD_O(NAME, dm_split_name, STR, "LVLayer", lv_layer, 7, dm_lv_layer_name, "lv_layer", "LVM device layer.")
2774
2775 {0, 0, 0, 0, "", "", NULL, NULL},
2776 /* *INDENT-ON* */
2777 };
2778
2779 #undef STR
2780 #undef NUM
2781 #undef FIELD_O
2782 #undef FIELD_F
2783
2784 static const char *default_report_options = "name,major,minor,attr,open,segments,events,uuid";
2785 static const char *splitname_report_options = "vg_name,lv_name,lv_layer";
2786
2787 static int _report_init(const struct command *cmd)
2788 {
2789 char *options = (char *) default_report_options;
2790 const char *keys = "";
2791 const char *separator = " ";
2792 int aligned = 1, headings = 1, buffered = 1, field_prefixes = 0;
2793 int quoted = 1, columns_as_rows = 0;
2794 uint32_t flags = 0;
2795 size_t len = 0;
2796 int r = 0;
2797
2798 if (cmd && !strcmp(cmd->name, "splitname"))
2799 options = (char *) splitname_report_options;
2800
2801 /* emulate old dmsetup behaviour */
2802 if (_switches[NOHEADINGS_ARG]) {
2803 separator = ":";
2804 aligned = 0;
2805 headings = 0;
2806 }
2807
2808 if (_switches[UNBUFFERED_ARG])
2809 buffered = 0;
2810
2811 if (_switches[ROWS_ARG])
2812 columns_as_rows = 1;
2813
2814 if (_switches[UNQUOTED_ARG])
2815 quoted = 0;
2816
2817 if (_switches[NAMEPREFIXES_ARG]) {
2818 aligned = 0;
2819 field_prefixes = 1;
2820 }
2821
2822 if (_switches[OPTIONS_ARG] && _string_args[OPTIONS_ARG]) {
2823 if (*_string_args[OPTIONS_ARG] != '+')
2824 options = _string_args[OPTIONS_ARG];
2825 else {
2826 len = strlen(default_report_options) +
2827 strlen(_string_args[OPTIONS_ARG]) + 1;
2828 if (!(options = dm_malloc(len))) {
2829 err("Failed to allocate option string.");
2830 return 0;
2831 }
2832 if (dm_snprintf(options, len, "%s,%s",
2833 default_report_options,
2834 &_string_args[OPTIONS_ARG][1]) < 0) {
2835 err("snprintf failed");
2836 goto out;
2837 }
2838 }
2839 }
2840
2841 if (_switches[SORT_ARG] && _string_args[SORT_ARG]) {
2842 keys = _string_args[SORT_ARG];
2843 buffered = 1;
2844 if (cmd && (!strcmp(cmd->name, "status") || !strcmp(cmd->name, "table"))) {
2845 err("--sort is not yet supported with status and table");
2846 goto out;
2847 }
2848 }
2849
2850 if (_switches[SEPARATOR_ARG] && _string_args[SEPARATOR_ARG]) {
2851 separator = _string_args[SEPARATOR_ARG];
2852 aligned = 0;
2853 }
2854
2855 if (aligned)
2856 flags |= DM_REPORT_OUTPUT_ALIGNED;
2857
2858 if (buffered)
2859 flags |= DM_REPORT_OUTPUT_BUFFERED;
2860
2861 if (headings)
2862 flags |= DM_REPORT_OUTPUT_HEADINGS;
2863
2864 if (field_prefixes)
2865 flags |= DM_REPORT_OUTPUT_FIELD_NAME_PREFIX;
2866
2867 if (!quoted)
2868 flags |= DM_REPORT_OUTPUT_FIELD_UNQUOTED;
2869
2870 if (columns_as_rows)
2871 flags |= DM_REPORT_OUTPUT_COLUMNS_AS_ROWS;
2872
2873 if (!(_report = dm_report_init(&_report_type,
2874 _report_types, _report_fields,
2875 options, separator, flags, keys, NULL)))
2876 goto out;
2877
2878 if ((_report_type & DR_TREE) && !_build_whole_deptree(cmd)) {
2879 err("Internal device dependency tree creation failed.");
2880 goto out;
2881 }
2882
2883 if (field_prefixes)
2884 dm_report_set_output_field_name_prefix(_report, "dm_");
2885
2886 r = 1;
2887
2888 out:
2889 if (!strcasecmp(options, "help") || !strcmp(options, "?"))
2890 r = 1;
2891
2892 if (len)
2893 dm_free(options);
2894
2895 return r;
2896 }
2897
2898 /*
2899 * List devices
2900 */
2901 static int _ls(CMD_ARGS)
2902 {
2903 if ((_switches[TARGET_ARG] && _target) ||
2904 (_switches[EXEC_ARG] && _command))
2905 return _status(cmd, argc, argv, NULL, 0);
2906 else if ((_switches[TREE_ARG]))
2907 return _display_tree(cmd, 0, NULL, NULL, 0);
2908 else
2909 return _process_all(cmd, argc, argv, 0, _display_name);
2910 }
2911
2912 static int _mangle(CMD_ARGS)
2913 {
2914 char *name;
2915 char *new_name = NULL;
2916 struct dm_task *dmt;
2917 struct dm_info info;
2918 int r = 0;
2919 int target_format;
2920
2921 if (names)
2922 name = names->name;
2923 else {
2924 if (argc == 1 && !_switches[UUID_ARG] && !_switches[MAJOR_ARG])
2925 return _process_all(cmd, argc, argv, 0, _mangle);
2926 name = argv[1];
2927 }
2928
2929 if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
2930 return 0;
2931
2932 if (!(_set_task_device(dmt, name, 0)))
2933 goto out;
2934
2935 if (!_switches[CHECKS_ARG] && !dm_task_enable_checks(dmt))
2936 goto out;
2937
2938 if (!dm_task_run(dmt))
2939 goto out;
2940
2941 if (!dm_task_get_info(dmt, &info) || !info.exists)
2942 goto out;
2943
2944 target_format = _switches[MANGLENAME_ARG] ? _int_args[MANGLENAME_ARG]
2945 : DEFAULT_DM_NAME_MANGLING;
2946
2947 if (target_format == DM_STRING_MANGLING_AUTO && strstr(name, "\\x5cx")) {
2948 log_error("The name \"%s\" seems to be mangled more than once. "
2949 "Manual intervention required to rename the device.", name);
2950 goto out;
2951 }
2952
2953 if (target_format == DM_STRING_MANGLING_NONE) {
2954 if (!(new_name = dm_task_get_name_unmangled(dmt)))
2955 goto out;
2956 }
2957 else if (!(new_name = dm_task_get_name_mangled(dmt)))
2958 goto out;
2959
2960 /* Nothing to do if the name is in correct form already. */
2961 if (!strcmp(name, new_name)) {
2962 log_print("%s: name already in correct form", name);
2963 r = 1;
2964 goto out;
2965 }
2966 else
2967 log_print("%s: renaming to %s", name, new_name);
2968
2969 /* Rename to correct form of the name. */
2970 r = _do_rename(name, new_name, NULL);
2971
2972 out:
2973 dm_free(new_name);
2974 dm_task_destroy(dmt);
2975 return r;
2976 }
2977
2978 static int _help(CMD_ARGS);
2979
2980 /*
2981 * Dispatch table
2982 */
2983 static struct command _commands[] = {
2984 {"help", "[-c|-C|--columns]", 0, 0, 0, _help},
2985 {"create", "<dev_name> [-j|--major <major> -m|--minor <minor>]\n"
2986 "\t [-U|--uid <uid>] [-G|--gid <gid>] [-M|--mode <octal_mode>]\n"
2987 "\t [-u|uuid <uuid>] [{--addnodeonresume|--addnodeoncreate}]\n"
2988 "\t [--notable | --table <table> | <table_file>]",
2989 1, 2,0, _create},
2990 {"remove", "[-f|--force] <device>", 0, -1, 1, _remove},
2991 {"remove_all", "[-f|--force]", 0, 0, 0, _remove_all},
2992 {"suspend", "[--noflush] <device>", 0, -1, 1, _suspend},
2993 {"resume", "<device> [{--addnodeonresume|--addnodeoncreate}]", 0, -1, 1, _resume},
2994 {"load", "<device> [<table_file>]", 0, 2, 0, _load},
2995 {"clear", "<device>", 0, -1, 1, _clear},
2996 {"reload", "<device> [<table_file>]", 0, 2, 0, _load},
2997 {"wipe_table", "<device>", 0, -1, 1, _error_device},
2998 {"rename", "<device> [--setuuid] <new_name_or_uuid>", 1, 2, 0, _rename},
2999 {"message", "<device> <sector> <message>", 2, -1, 0, _message},
3000 {"ls", "[--target <target_type>] [--exec <command>] [-o options] [--tree]", 0, 0, 0, _ls},
3001 {"info", "[<device>]", 0, -1, 1, _info},
3002 {"deps", "[-o options] [<device>]", 0, -1, 1, _deps},
3003 {"status", "[<device>] [--target <target_type>]", 0, -1, 1, _status},
3004 {"table", "[<device>] [--target <target_type>] [--showkeys]", 0, -1, 1, _status},
3005 {"wait", "<device> [<event_nr>]", 0, 2, 0, _wait},
3006 {"mknodes", "[<device>]", 0, -1, 1, _mknodes},
3007 {"mangle", "[<device>]", 0, -1, 1, _mangle},
3008 {"udevcreatecookie", "", 0, 0, 0, _udevcreatecookie},
3009 {"udevreleasecookie", "[<cookie>]", 0, 1, 0, _udevreleasecookie},
3010 {"udevflags", "<cookie>", 1, 1, 0, _udevflags},
3011 {"udevcomplete", "<cookie>", 1, 1, 0, _udevcomplete},
3012 {"udevcomplete_all", "<age_in_minutes>", 0, 1, 0, _udevcomplete_all},
3013 {"udevcookies", "", 0, 0, 0, _udevcookies},
3014 {"targets", "", 0, 0, 0, _targets},
3015 {"version", "", 0, 0, 0, _version},
3016 {"setgeometry", "<device> <cyl> <head> <sect> <start>", 5, 5, 0, _setgeometry},
3017 {"splitname", "<device> [<subsystem>]", 1, 2, 0, _splitname},
3018 {NULL, NULL, 0, 0, 0, NULL}
3019 };
3020
3021 static void _usage(FILE *out)
3022 {
3023 int i;
3024
3025 fprintf(out, "Usage:\n\n");
3026 fprintf(out, "dmsetup [--version] [-h|--help [-c|-C|--columns]]\n"
3027 " [--checks] [--manglename <mangling_mode>] [-v|--verbose [-v|--verbose ...]]\n"
3028 " [-r|--readonly] [--noopencount] [--nolockfs] [--inactive]\n"
3029 " [--udevcookie [cookie]] [--noudevrules] [--noudevsync] [--verifyudev]\n"
3030 " [-y|--yes] [--readahead [+]<sectors>|auto|none] [--retry]\n"
3031 " [-c|-C|--columns] [-o <fields>] [-O|--sort <sort_fields>]\n"
3032 " [--nameprefixes] [--noheadings] [--separator <separator>]\n\n");
3033 for (i = 0; _commands[i].name; i++)
3034 fprintf(out, "\t%s %s\n", _commands[i].name, _commands[i].help);
3035 fprintf(out, "\n<device> may be device name or -u <uuid> or "
3036 "-j <major> -m <minor>\n");
3037 fprintf(out, "<mangling_mode> is one of 'none', 'auto' and 'hex'.\n");
3038 fprintf(out, "<fields> are comma-separated. Use 'help -c' for list.\n");
3039 fprintf(out, "Table_file contents may be supplied on stdin.\n");
3040 fprintf(out, "Options are: devno, devname, blkdevname.\n");
3041 fprintf(out, "Tree specific options are: ascii, utf, vt100; compact, inverted, notrunc;\n"
3042 " blkdevname, [no]device, active, open, rw and uuid.\n");
3043 fprintf(out, "\n");
3044 }
3045
3046 static void _losetup_usage(FILE *out)
3047 {
3048 fprintf(out, "Usage:\n\n");
3049 fprintf(out, "losetup [-d|-a] [-e encryption] "
3050 "[-o offset] [-f|loop_device] [file]\n\n");
3051 }
3052
3053 static int _help(CMD_ARGS)
3054 {
3055 _usage(stderr);
3056
3057 if (_switches[COLS_ARG]) {
3058 _switches[OPTIONS_ARG] = 1;
3059 _string_args[OPTIONS_ARG] = (char *) "help";
3060 _switches[SORT_ARG] = 0;
3061
3062 if (_report) {
3063 dm_report_free(_report);
3064 _report = NULL;
3065 }
3066 (void) _report_init(cmd);
3067 }
3068
3069 return 1;
3070 }
3071
3072 static struct command *_find_command(const char *name)
3073 {
3074 int i;
3075
3076 for (i = 0; _commands[i].name; i++)
3077 if (!strcmp(_commands[i].name, name))
3078 return _commands + i;
3079
3080 return NULL;
3081 }
3082
3083 static int _process_tree_options(const char *options)
3084 {
3085 const char *s, *end;
3086 struct winsize winsz;
3087 size_t len;
3088
3089 /* Symbol set default */
3090 if (!strcmp(nl_langinfo(CODESET), "UTF-8"))
3091 _tsym = &_tsym_utf;
3092 else
3093 _tsym = &_tsym_ascii;
3094
3095 /* Default */
3096 _tree_switches[TR_DEVICE] = 1;
3097 _tree_switches[TR_TRUNCATE] = 1;
3098
3099 /* parse */
3100 for (s = options; s && *s; s++) {
3101 len = 0;
3102 for (end = s; *end && *end != ','; end++, len++)
3103 ;
3104 if (!strncmp(s, "device", len))
3105 _tree_switches[TR_DEVICE] = 1;
3106 else if (!strncmp(s, "blkdevname", len))
3107 _tree_switches[TR_BLKDEVNAME] = 1;
3108 else if (!strncmp(s, "nodevice", len))
3109 _tree_switches[TR_DEVICE] = 0;
3110 else if (!strncmp(s, "status", len))
3111 _tree_switches[TR_STATUS] = 1;
3112 else if (!strncmp(s, "table", len))
3113 _tree_switches[TR_TABLE] = 1;
3114 else if (!strncmp(s, "active", len))
3115 _tree_switches[TR_ACTIVE] = 1;
3116 else if (!strncmp(s, "open", len))
3117 _tree_switches[TR_OPENCOUNT] = 1;
3118 else if (!strncmp(s, "uuid", len))
3119 _tree_switches[TR_UUID] = 1;
3120 else if (!strncmp(s, "rw", len))
3121 _tree_switches[TR_RW] = 1;
3122 else if (!strncmp(s, "utf", len))
3123 _tsym = &_tsym_utf;
3124 else if (!strncmp(s, "vt100", len))
3125 _tsym = &_tsym_vt100;
3126 else if (!strncmp(s, "ascii", len))
3127 _tsym = &_tsym_ascii;
3128 else if (!strncmp(s, "inverted", len))
3129 _tree_switches[TR_BOTTOMUP] = 1;
3130 else if (!strncmp(s, "compact", len))
3131 _tree_switches[TR_COMPACT] = 1;
3132 else if (!strncmp(s, "notrunc", len))
3133 _tree_switches[TR_TRUNCATE] = 0;
3134 else {
3135 fprintf(stderr, "Tree options not recognised: %s\n", s);
3136 return 0;
3137 }
3138 if (!*end)
3139 break;
3140 s = end;
3141 }
3142
3143 /* Truncation doesn't work well with vt100 drawing char */
3144 if (_tsym != &_tsym_vt100)
3145 if (ioctl(1, (unsigned long) TIOCGWINSZ, &winsz) >= 0 && winsz.ws_col > 3)
3146 _termwidth = winsz.ws_col - 3;
3147
3148 return 1;
3149 }
3150
3151 /*
3152 * Returns the full absolute path, or NULL if the path could
3153 * not be resolved.
3154 */
3155 static char *_get_abspath(const char *path)
3156 {
3157 char *_path;
3158
3159 #ifdef HAVE_CANONICALIZE_FILE_NAME
3160 _path = canonicalize_file_name(path);
3161 #else
3162 /* FIXME Provide alternative */
3163 log_error(INTERNAL_ERROR "Unimplemented _get_abspath.");
3164 _path = NULL;
3165 #endif
3166 return _path;
3167 }
3168
3169 static char *parse_loop_device_name(const char *dev, const char *dev_dir)
3170 {
3171 char *buf;
3172 char *device = NULL;
3173
3174 if (!(buf = dm_malloc(PATH_MAX)))
3175 return NULL;
3176
3177 if (dev[0] == '/') {
3178 if (!(device = _get_abspath(dev)))
3179 goto error;
3180
3181 if (strncmp(device, dev_dir, strlen(dev_dir)))
3182 goto error;
3183
3184 /* If dev_dir does not end in a slash, ensure that the
3185 following byte in the device string is "/". */
3186 if (dev_dir[strlen(dev_dir) - 1] != '/' &&
3187 device[strlen(dev_dir)] != '/')
3188 goto error;
3189
3190 strncpy(buf, strrchr(device, '/') + 1, PATH_MAX - 1);
3191 buf[PATH_MAX - 1] = '\0';
3192 dm_free(device);
3193
3194 } else {
3195 /* check for device number */
3196 if (!strncmp(dev, "loop", strlen("loop")))
3197 strncpy(buf, dev, (size_t) PATH_MAX);
3198 else
3199 goto error;
3200 }
3201
3202 return buf;
3203
3204 error:
3205 dm_free(device);
3206 dm_free(buf);
3207
3208 return NULL;
3209 }
3210
3211 /*
3212 * create a table for a mapped device using the loop target.
3213 */
3214 static int _loop_table(char *table, size_t tlen, char *file,
3215 char *dev __attribute__((unused)), off_t off)
3216 {
3217 struct stat fbuf;
3218 off_t size, sectors;
3219 int fd = -1;
3220 #ifdef HAVE_SYS_STATVFS_H
3221 struct statvfs fsbuf;
3222 off_t blksize;
3223 #endif
3224
3225 if (!_switches[READ_ONLY])
3226 fd = open(file, O_RDWR);
3227
3228 if (fd < 0) {
3229 _switches[READ_ONLY]++;
3230 fd = open(file, O_RDONLY);
3231 }
3232
3233 if (fd < 0)
3234 goto error;
3235
3236 if (fstat(fd, &fbuf))
3237 goto error;
3238
3239 size = (fbuf.st_size - off);
3240 sectors = size >> SECTOR_SHIFT;
3241
3242 if (_switches[VERBOSE_ARG])
3243 fprintf(stderr, "losetup: set loop size to %llukB "
3244 "(%llu sectors)\n", (long long unsigned) sectors >> 1,
3245 (long long unsigned) sectors);
3246
3247 #ifdef HAVE_SYS_STATVFS_H
3248 if (fstatvfs(fd, &fsbuf))
3249 goto error;
3250
3251 /* FIXME Fragment size currently unused */
3252 blksize = fsbuf.f_frsize;
3253 #endif
3254
3255 if (close(fd))
3256 log_sys_error("close", file);
3257
3258 if (dm_snprintf(table, tlen, "%llu %llu loop %s %llu\n", 0ULL,
3259 (long long unsigned)sectors, file, (long long unsigned)off) < 0)
3260 return 0;
3261
3262 if (_switches[VERBOSE_ARG] > 1)
3263 fprintf(stderr, "Table: %s\n", table);
3264
3265 return 1;
3266
3267 error:
3268 if (fd > -1 && close(fd))
3269 log_sys_error("close", file);
3270
3271 return 0;
3272 }
3273
3274 static int _process_losetup_switches(const char *base, int *argc, char ***argv,
3275 const char *dev_dir)
3276 {
3277 int c;
3278 int encrypt_loop = 0, delete = 0, find = 0, show_all = 0;
3279 char *device_name = NULL;
3280 char *loop_file = NULL;
3281 off_t offset = 0;
3282
3283 #ifdef HAVE_GETOPTLONG
3284 static struct option long_options[] = {
3285 {0, 0, 0, 0}
3286 };
3287 #endif
3288
3289 optarg = 0;
3290 optind = OPTIND_INIT;
3291 while ((c = GETOPTLONG_FN(*argc, *argv, "ade:fo:v",
3292 long_options, NULL)) != -1 ) {
3293 if (c == ':' || c == '?')
3294 return 0;
3295 if (c == 'a')
3296 show_all++;
3297 if (c == 'd')
3298 delete++;
3299 if (c == 'e')
3300 encrypt_loop++;
3301 if (c == 'f')
3302 find++;
3303 if (c == 'o')
3304 offset = atoi(optarg);
3305 if (c == 'v')
3306 _switches[VERBOSE_ARG]++;
3307 }
3308
3309 *argv += optind ;
3310 *argc -= optind ;
3311
3312 if (encrypt_loop){
3313 fprintf(stderr, "%s: Sorry, cryptoloop is not yet implemented "
3314 "in this version.\n", base);
3315 return 0;
3316 }
3317
3318 if (show_all) {
3319 fprintf(stderr, "%s: Sorry, show all is not yet implemented "
3320 "in this version.\n", base);
3321 return 0;
3322 }
3323
3324 if (find) {
3325 fprintf(stderr, "%s: Sorry, find is not yet implemented "
3326 "in this version.\n", base);
3327 if (!*argc)
3328 return 0;
3329 }
3330
3331 if (!*argc) {
3332 fprintf(stderr, "%s: Please specify loop_device.\n", base);
3333 _losetup_usage(stderr);
3334 return 0;
3335 }
3336
3337 if (!(device_name = parse_loop_device_name((*argv)[0], dev_dir))) {
3338 fprintf(stderr, "%s: Could not parse loop_device %s\n",
3339 base, (*argv)[0]);
3340 _losetup_usage(stderr);
3341 return 0;
3342 }
3343
3344 if (delete) {
3345 *argc = 2;
3346
3347 (*argv)[1] = device_name;
3348 (*argv)[0] = (char *) "remove";
3349
3350 return 1;
3351 }
3352
3353 if (*argc != 2) {
3354 fprintf(stderr, "%s: Too few arguments\n", base);
3355 _losetup_usage(stderr);
3356 dm_free(device_name);
3357 return 0;
3358 }
3359
3360 /* FIXME move these to make them available to native dmsetup */
3361 if (!(loop_file = _get_abspath((*argv)[(find) ? 0 : 1]))) {
3362 fprintf(stderr, "%s: Could not parse loop file name %s\n",
3363 base, (*argv)[1]);
3364 _losetup_usage(stderr);
3365 dm_free(device_name);
3366 return 0;
3367 }
3368
3369 _table = dm_malloc(LOOP_TABLE_SIZE);
3370 if (!_table ||
3371 !_loop_table(_table, (size_t) LOOP_TABLE_SIZE, loop_file, device_name, offset)) {
3372 fprintf(stderr, "Could not build device-mapper table for %s\n", (*argv)[0]);
3373 dm_free(device_name);
3374 return 0;
3375 }
3376 _switches[TABLE_ARG]++;
3377
3378 (*argv)[0] = (char *) "create";
3379 (*argv)[1] = device_name ;
3380
3381 return 1;
3382 }
3383
3384 static int _process_options(const char *options)
3385 {
3386 const char *s, *end;
3387 size_t len;
3388
3389 /* Tree options are processed separately. */
3390 if (_switches[TREE_ARG])
3391 return _process_tree_options(_string_args[OPTIONS_ARG]);
3392
3393 /* Column options are processed separately by _report_init (called later). */
3394 if (_switches[COLS_ARG])
3395 return 1;
3396
3397 /* No options specified. */
3398 if (!_switches[OPTIONS_ARG])
3399 return 1;
3400
3401 /* Set defaults. */
3402 _dev_name_type = DN_DEVNO;
3403
3404 /* Parse. */
3405 for (s = options; s && *s; s++) {
3406 len = 0;
3407 for (end = s; *end && *end != ','; end++, len++)
3408 ;
3409 if (!strncmp(s, "devno", len))
3410 _dev_name_type = DN_DEVNO;
3411 else if (!strncmp(s, "blkdevname", len))
3412 _dev_name_type = DN_BLK;
3413 else if (!strncmp(s, "devname", len))
3414 _dev_name_type = DN_MAP;
3415 else {
3416 fprintf(stderr, "Option not recognised: %s\n", s);
3417 return 0;
3418 }
3419
3420 if (!*end)
3421 break;
3422 s = end;
3423 }
3424
3425 return 1;
3426 }
3427
3428 static int _process_switches(int *argc, char ***argv, const char *dev_dir)
3429 {
3430 const char *base;
3431 char *namebase, *s;
3432 static int ind;
3433 int c, r;
3434
3435 #ifdef HAVE_GETOPTLONG
3436 static struct option long_options[] = {
3437 {"readonly", 0, &ind, READ_ONLY},
3438 {"checks", 0, &ind, CHECKS_ARG},
3439 {"columns", 0, &ind, COLS_ARG},
3440 {"exec", 1, &ind, EXEC_ARG},
3441 {"force", 0, &ind, FORCE_ARG},
3442 {"gid", 1, &ind, GID_ARG},
3443 {"help", 0, &ind, HELP_ARG},
3444 {"inactive", 0, &ind, INACTIVE_ARG},
3445 {"manglename", 1, &ind, MANGLENAME_ARG},
3446 {"major", 1, &ind, MAJOR_ARG},
3447 {"minor", 1, &ind, MINOR_ARG},
3448 {"mode", 1, &ind, MODE_ARG},
3449 {"nameprefixes", 0, &ind, NAMEPREFIXES_ARG},
3450 {"noflush", 0, &ind, NOFLUSH_ARG},
3451 {"noheadings", 0, &ind, NOHEADINGS_ARG},
3452 {"nolockfs", 0, &ind, NOLOCKFS_ARG},
3453 {"noopencount", 0, &ind, NOOPENCOUNT_ARG},
3454 {"notable", 0, &ind, NOTABLE_ARG},
3455 {"udevcookie", 1, &ind, UDEVCOOKIE_ARG},
3456 {"noudevrules", 0, &ind, NOUDEVRULES_ARG},
3457 {"noudevsync", 0, &ind, NOUDEVSYNC_ARG},
3458 {"options", 1, &ind, OPTIONS_ARG},
3459 {"readahead", 1, &ind, READAHEAD_ARG},
3460 {"retry", 0, &ind, RETRY_ARG},
3461 {"rows", 0, &ind, ROWS_ARG},
3462 {"separator", 1, &ind, SEPARATOR_ARG},
3463 {"setuuid", 0, &ind, SETUUID_ARG},
3464 {"showkeys", 0, &ind, SHOWKEYS_ARG},
3465 {"sort", 1, &ind, SORT_ARG},
3466 {"table", 1, &ind, TABLE_ARG},
3467 {"target", 1, &ind, TARGET_ARG},
3468 {"tree", 0, &ind, TREE_ARG},
3469 {"uid", 1, &ind, UID_ARG},
3470 {"uuid", 1, &ind, UUID_ARG},
3471 {"unbuffered", 0, &ind, UNBUFFERED_ARG},
3472 {"unquoted", 0, &ind, UNQUOTED_ARG},
3473 {"verbose", 1, &ind, VERBOSE_ARG},
3474 {"verifyudev", 0, &ind, VERIFYUDEV_ARG},
3475 {"version", 0, &ind, VERSION_ARG},
3476 {"yes", 0, &ind, YES_ARG},
3477 {"addnodeonresume", 0, &ind, ADD_NODE_ON_RESUME_ARG},
3478 {"addnodeoncreate", 0, &ind, ADD_NODE_ON_CREATE_ARG},
3479 {0, 0, 0, 0}
3480 };
3481 #else
3482 struct option long_options;
3483 #endif
3484
3485 /*
3486 * Zero all the index counts.
3487 */
3488 memset(&_switches, 0, sizeof(_switches));
3489 memset(&_int_args, 0, sizeof(_int_args));
3490 _read_ahead_flags = 0;
3491
3492 if (!(namebase = strdup((*argv)[0]))) {
3493 fprintf(stderr, "Failed to duplicate name.\n");
3494 return 0;
3495 }
3496 base = dm_basename(namebase);
3497
3498 if (!strcmp(base, "devmap_name")) {
3499 free(namebase);
3500 _switches[COLS_ARG]++;
3501 _switches[NOHEADINGS_ARG]++;
3502 _switches[OPTIONS_ARG]++;
3503 _switches[MAJOR_ARG]++;
3504 _switches[MINOR_ARG]++;
3505 _string_args[OPTIONS_ARG] = (char *) "name";
3506
3507 if (*argc == 3) {
3508 _int_args[MAJOR_ARG] = atoi((*argv)[1]);
3509 _int_args[MINOR_ARG] = atoi((*argv)[2]);
3510 *argc -= 2;
3511 *argv += 2;
3512 } else if ((*argc == 2) &&
3513 (2 == sscanf((*argv)[1], "%i:%i",
3514 &_int_args[MAJOR_ARG],
3515 &_int_args[MINOR_ARG]))) {
3516 *argc -= 1;
3517 *argv += 1;
3518 } else {
3519 fprintf(stderr, "Usage: devmap_name <major> <minor>\n");
3520 return 0;
3521 }
3522
3523 (*argv)[0] = (char *) "info";
3524 return 1;
3525 }
3526
3527 if (!strcmp(base, "losetup") || !strcmp(base, "dmlosetup")){
3528 r = _process_losetup_switches(base, argc, argv, dev_dir);
3529 free(namebase);
3530 return r;
3531 }
3532
3533 free(namebase);
3534
3535 optarg = 0;
3536 optind = OPTIND_INIT;
3537 while ((ind = -1, c = GETOPTLONG_FN(*argc, *argv, "cCfG:hj:m:M:no:O:ru:U:vy",
3538 long_options, NULL)) != -1) {
3539 if (c == ':' || c == '?')
3540 return 0;
3541 if (c == 'h' || ind == HELP_ARG)
3542 _switches[HELP_ARG]++;
3543 if (c == 'c' || c == 'C' || ind == COLS_ARG)
3544 _switches[COLS_ARG]++;
3545 if (c == 'f' || ind == FORCE_ARG)
3546 _switches[FORCE_ARG]++;
3547 if (c == 'r' || ind == READ_ONLY)
3548 _switches[READ_ONLY]++;
3549 if (c == 'j' || ind == MAJOR_ARG) {
3550 _switches[MAJOR_ARG]++;
3551 _int_args[MAJOR_ARG] = atoi(optarg);
3552 }
3553 if (c == 'm' || ind == MINOR_ARG) {
3554 _switches[MINOR_ARG]++;
3555 _int_args[MINOR_ARG] = atoi(optarg);
3556 }
3557 if (c == 'n' || ind == NOTABLE_ARG)
3558 _switches[NOTABLE_ARG]++;
3559 if (c == 'o' || ind == OPTIONS_ARG) {
3560 _switches[OPTIONS_ARG]++;
3561 _string_args[OPTIONS_ARG] = optarg;
3562 }
3563 if (ind == SEPARATOR_ARG) {
3564 _switches[SEPARATOR_ARG]++;
3565 _string_args[SEPARATOR_ARG] = optarg;
3566 }
3567 if (c == 'O' || ind == SORT_ARG) {
3568 _switches[SORT_ARG]++;
3569 _string_args[SORT_ARG] = optarg;
3570 }
3571 if (c == 'v' || ind == VERBOSE_ARG)
3572 _switches[VERBOSE_ARG]++;
3573 if (c == 'u' || ind == UUID_ARG) {
3574 _switches[UUID_ARG]++;
3575 _uuid = optarg;
3576 }
3577 if (c == 'y' || ind == YES_ARG)
3578 _switches[YES_ARG]++;
3579 if (ind == ADD_NODE_ON_RESUME_ARG)
3580 _switches[ADD_NODE_ON_RESUME_ARG]++;
3581 if (ind == ADD_NODE_ON_CREATE_ARG)
3582 _switches[ADD_NODE_ON_CREATE_ARG]++;
3583 if (ind == CHECKS_ARG)
3584 _switches[CHECKS_ARG]++;
3585 if (ind == UDEVCOOKIE_ARG) {
3586 _switches[UDEVCOOKIE_ARG]++;
3587 _udev_cookie = _get_cookie_value(optarg);
3588 }
3589 if (ind == NOUDEVRULES_ARG)
3590 _switches[NOUDEVRULES_ARG]++;
3591 if (ind == NOUDEVSYNC_ARG)
3592 _switches[NOUDEVSYNC_ARG]++;
3593 if (ind == VERIFYUDEV_ARG)
3594 _switches[VERIFYUDEV_ARG]++;
3595 if (c == 'G' || ind == GID_ARG) {
3596 _switches[GID_ARG]++;
3597 _int_args[GID_ARG] = atoi(optarg);
3598 }
3599 if (c == 'U' || ind == UID_ARG) {
3600 _switches[UID_ARG]++;
3601 _int_args[UID_ARG] = atoi(optarg);
3602 }
3603 if (c == 'M' || ind == MODE_ARG) {
3604 _switches[MODE_ARG]++;
3605 /* FIXME Accept modes as per chmod */
3606 _int_args[MODE_ARG] = (int) strtol(optarg, NULL, 8);
3607 }
3608 if (ind == EXEC_ARG) {
3609 _switches[EXEC_ARG]++;
3610 _command = optarg;
3611 }
3612 if (ind == TARGET_ARG) {
3613 _switches[TARGET_ARG]++;
3614 _target = optarg;
3615 }
3616 if (ind == INACTIVE_ARG)
3617 _switches[INACTIVE_ARG]++;
3618 if ((ind == MANGLENAME_ARG)) {
3619 _switches[MANGLENAME_ARG]++;
3620 if (!strcasecmp(optarg, "none"))
3621 _int_args[MANGLENAME_ARG] = DM_STRING_MANGLING_NONE;
3622 else if (!strcasecmp(optarg, "auto"))
3623 _int_args[MANGLENAME_ARG] = DM_STRING_MANGLING_AUTO;
3624 else if (!strcasecmp(optarg, "hex"))
3625 _int_args[MANGLENAME_ARG] = DM_STRING_MANGLING_HEX;
3626 else {
3627 log_error("Unknown name mangling mode");
3628 return 0;
3629 }
3630 dm_set_name_mangling_mode((dm_string_mangling_t) _int_args[MANGLENAME_ARG]);
3631 }
3632 if (ind == NAMEPREFIXES_ARG)
3633 _switches[NAMEPREFIXES_ARG]++;
3634 if (ind == NOFLUSH_ARG)
3635 _switches[NOFLUSH_ARG]++;
3636 if (ind == NOHEADINGS_ARG)
3637 _switches[NOHEADINGS_ARG]++;
3638 if (ind == NOLOCKFS_ARG)
3639 _switches[NOLOCKFS_ARG]++;
3640 if (ind == NOOPENCOUNT_ARG)
3641 _switches[NOOPENCOUNT_ARG]++;
3642 if (ind == READAHEAD_ARG) {
3643 _switches[READAHEAD_ARG]++;
3644 if (!strcasecmp(optarg, "auto"))
3645 _int_args[READAHEAD_ARG] = DM_READ_AHEAD_AUTO;
3646 else if (!strcasecmp(optarg, "none"))
3647 _int_args[READAHEAD_ARG] = DM_READ_AHEAD_NONE;
3648 else {
3649 for (s = optarg; isspace(*s); s++)
3650 ;
3651 if (*s == '+')
3652 _read_ahead_flags = DM_READ_AHEAD_MINIMUM_FLAG;
3653 _int_args[READAHEAD_ARG] = atoi(optarg);
3654 if (_int_args[READAHEAD_ARG] < -1) {
3655 log_error("Negative read ahead value "
3656 "(%d) is not understood.",
3657 _int_args[READAHEAD_ARG]);
3658 return 0;
3659 }
3660 }
3661 }
3662 if (ind == RETRY_ARG)
3663 _switches[RETRY_ARG]++;
3664 if (ind == ROWS_ARG)
3665 _switches[ROWS_ARG]++;
3666 if (ind == SETUUID_ARG)
3667 _switches[SETUUID_ARG]++;
3668 if (ind == SHOWKEYS_ARG)
3669 _switches[SHOWKEYS_ARG]++;
3670 if (ind == TABLE_ARG) {
3671 _switches[TABLE_ARG]++;
3672 if (!(_table = dm_strdup(optarg))) {
3673 log_error("Could not allocate memory for table string.");
3674 return 0;
3675 }
3676 }
3677 if (ind == TREE_ARG)
3678 _switches[TREE_ARG]++;
3679 if (ind == UNQUOTED_ARG)
3680 _switches[UNQUOTED_ARG]++;
3681 if (ind == VERSION_ARG)
3682 _switches[VERSION_ARG]++;
3683 }
3684
3685 if (_switches[VERBOSE_ARG] > 1)
3686 dm_log_init_verbose(_switches[VERBOSE_ARG] - 1);
3687
3688 if ((_switches[MAJOR_ARG] && !_switches[MINOR_ARG]) ||
3689 (!_switches[MAJOR_ARG] && _switches[MINOR_ARG])) {
3690 fprintf(stderr, "Please specify both major number and "
3691 "minor number.\n");
3692 return 0;
3693 }
3694
3695 if (!_process_options(_string_args[OPTIONS_ARG]))
3696 return 0;
3697
3698 if (_switches[TABLE_ARG] && _switches[NOTABLE_ARG]) {
3699 fprintf(stderr, "--table and --notable are incompatible.\n");
3700 return 0;
3701 }
3702
3703 if (_switches[ADD_NODE_ON_RESUME_ARG] && _switches[ADD_NODE_ON_CREATE_ARG]) {
3704 fprintf(stderr, "--addnodeonresume and --addnodeoncreate are incompatible.\n");
3705 return 0;
3706 }
3707
3708 *argv += optind;
3709 *argc -= optind;
3710 return 1;
3711 }
3712
3713 int main(int argc, char **argv)
3714 {
3715 int r = 1;
3716 const char *dev_dir;
3717 const struct command *cmd;
3718 int multiple_devices;
3719
3720 (void) setlocale(LC_ALL, "");
3721
3722 dev_dir = getenv (DM_DEV_DIR_ENV_VAR_NAME);
3723 if (dev_dir && *dev_dir) {
3724 if (!dm_set_dev_dir(dev_dir)) {
3725 fprintf(stderr, "Invalid DM_DEV_DIR environment variable value.\n");
3726 goto out;
3727 }
3728 } else
3729 dev_dir = DEFAULT_DM_DEV_DIR;
3730
3731 if (!_process_switches(&argc, &argv, dev_dir)) {
3732 fprintf(stderr, "Couldn't process command line.\n");
3733 goto out;
3734 }
3735
3736 if (_switches[HELP_ARG]) {
3737 cmd = _find_command("help");
3738 goto doit;
3739 }
3740
3741 if (_switches[VERSION_ARG]) {
3742 cmd = _find_command("version");
3743 goto doit;
3744 }
3745
3746 if (argc == 0) {
3747 _usage(stderr);
3748 goto out;
3749 }
3750
3751 if (!(cmd = _find_command(argv[0]))) {
3752 fprintf(stderr, "Unknown command\n");
3753 _usage(stderr);
3754 goto out;
3755 }
3756
3757 if (argc < cmd->min_args + 1 ||
3758 (cmd->max_args >= 0 && argc > cmd->max_args + 1)) {
3759 fprintf(stderr, "Incorrect number of arguments\n");
3760 _usage(stderr);
3761 goto out;
3762 }
3763
3764 if (!_switches[COLS_ARG] && !strcmp(cmd->name, "splitname"))
3765 _switches[COLS_ARG]++;
3766
3767 if (!strcmp(cmd->name, "mangle"))
3768 dm_set_name_mangling_mode(DM_STRING_MANGLING_NONE);
3769
3770 if (_switches[COLS_ARG]) {
3771 if (!_report_init(cmd))
3772 goto out;
3773 if (!_report) {
3774 if (!strcmp(cmd->name, "info"))
3775 r = 0; /* info -c -o help */
3776 goto out;
3777 }
3778 }
3779
3780 #ifdef UDEV_SYNC_SUPPORT
3781 if (!_set_up_udev_support(dev_dir))
3782 goto out;
3783 #endif
3784
3785 doit:
3786 multiple_devices = (cmd->repeatable_cmd && argc != 2 &&
3787 (argc != 1 || (!_switches[UUID_ARG] && !_switches[MAJOR_ARG])));
3788 do {
3789 if (!cmd->fn(cmd, argc--, argv++, NULL, multiple_devices)) {
3790 fprintf(stderr, "Command failed\n");
3791 goto out;
3792 }
3793 } while (cmd->repeatable_cmd && argc > 1);
3794
3795 r = 0;
3796
3797 out:
3798 if (_report) {
3799 dm_report_output(_report);
3800 dm_report_free(_report);
3801 }
3802
3803 if (_dtree)
3804 dm_tree_free(_dtree);
3805
3806 dm_free(_table);
3807
3808 return r;
3809 }
This page took 0.209234 seconds and 5 git commands to generate.