xref: /dragonfly/sbin/hammer2/cmd_volume.c (revision 5ca0a96d)
1 /*
2  * Copyright (c) 2020 Tomohiro Kusumi <tkusumi@netbsd.org>
3  * Copyright (c) 2020 The DragonFly Project
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@dragonflybsd.org>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include "hammer2.h"
38 
39 int
40 cmd_volume_list(int ac, char **av)
41 {
42 	hammer2_ioc_volume_list_t vollist;
43 	hammer2_ioc_volume_t *entry;
44 	int fd, i, j, n, w, all = 0, ecode = 0;
45 
46 	if (ac == 1 && av[0] == NULL) {
47 		av = get_hammer2_mounts(&ac);
48 		all = 1;
49 	}
50 	vollist.volumes = calloc(HAMMER2_MAX_VOLUMES, sizeof(*vollist.volumes));
51 
52 	for (i = 0; i < ac; ++i) {
53 		if (i)
54 			printf("\n");
55 		if (ac > 1 || all)
56 			printf("%s\n", av[i]);
57 		if ((fd = hammer2_ioctl_handle(av[i])) < 0) {
58 			ecode = 1;
59 			goto failed;
60 		}
61 
62 		vollist.nvolumes = HAMMER2_MAX_VOLUMES;
63 		if (ioctl(fd, HAMMER2IOC_VOLUME_LIST, &vollist) < 0) {
64 			perror("ioctl");
65 			close(fd);
66 			ecode = 1;
67 			goto failed;
68 		}
69 
70 		w = 0;
71 		for (j = 0; j < vollist.nvolumes; ++j) {
72 			entry = &vollist.volumes[j];
73 			n = (int)strlen(entry->path);
74 			if (n > w)
75 				w = n;
76 		}
77 
78 		if (QuietOpt > 0) {
79 			for (j = 0; j < vollist.nvolumes; ++j) {
80 				entry = &vollist.volumes[j];
81 				printf("%s\n", entry->path);
82 			}
83 		} else {
84 			printf("version %d\n", vollist.version);
85 			printf("@%s\n", vollist.pfs_name);
86 			for (j = 0; j < vollist.nvolumes; ++j) {
87 				entry = &vollist.volumes[j];
88 				printf("volume%-2d %-*.*s %s",
89 				       entry->id, w, w, entry->path,
90 				       sizetostr(entry->size));
91 				if (VerboseOpt > 0)
92 					printf(" 0x%016jx 0x%016jx",
93 					       (intmax_t)entry->offset,
94 					       (intmax_t)entry->size);
95 				printf("\n");
96 			}
97 		}
98 		close(fd);
99 	}
100 failed:
101 	free(vollist.volumes);
102 	if (all)
103 		put_hammer2_mounts(ac, av);
104 
105 	return (ecode);
106 }
107