xref: /dragonfly/sbin/hammer/cmd_volume.c (revision 0db87cb7)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com> and
6  * Michael Neumann <mneumann@ntecs.de>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 /*
37  * Volume operations:
38  *
39  *   - volume-add: Add new volume to HAMMER filesystem
40  *   - volume-del: Remove volume from HAMMER filesystem
41  *   - volume-list: List volumes making up a HAMMER filesystem
42  *   - volume-blkdevs: List volumes making up a HAMMER filesystem
43  *     in blkdevs format
44  */
45 
46 #include "hammer.h"
47 
48 /*
49  * volume-add <device> <filesystem>
50  */
51 void
52 hammer_cmd_volume_add(char **av, int ac)
53 {
54 	struct hammer_ioc_volume ioc;
55 	struct volume_info *vol;
56 	int fd;
57 
58 	if (ac != 2) {
59 		fprintf(stderr, "hammer volume-add <device> <filesystem>\n");
60 		exit(1);
61 	}
62 
63 	char *device = av[0];
64 	char *filesystem = av[1];
65 
66         fd = open(filesystem, O_RDONLY);
67 	if (fd < 0) {
68 		fprintf(stderr, "hammer volume-add: unable to access %s: %s\n",
69 			filesystem, strerror(errno));
70 		exit(1);
71 	}
72 
73 	/*
74 	 * Initialize and check the device
75 	 */
76 	vol = setup_volume(-1, device, 1, O_RDONLY);
77 	assert(vol->vol_no == -1);
78 	check_volume(vol);
79 	if (strcmp(vol->type, "DEVICE")) {
80 		fprintf(stderr, "Not a block device: %s\n", device);
81 		exit(1);
82 	}
83 	close(vol->fd);
84 
85 	/*
86 	 * volume-add ioctl
87 	 */
88 	bzero(&ioc, sizeof(ioc));
89 	strncpy(ioc.device_name, device, MAXPATHLEN);
90 	ioc.vol_size = vol->size;
91 	ioc.boot_area_size = init_boot_area_size(0, ioc.vol_size);
92 	ioc.mem_area_size = init_mem_area_size(0, ioc.vol_size);
93 
94 	if (ioctl(fd, HAMMERIOC_ADD_VOLUME, &ioc) < 0) {
95 		fprintf(stderr, "hammer volume-add ioctl: %s\n",
96 			strerror(errno));
97 		exit(1);
98 	}
99 
100 	close(fd);
101 }
102 
103 /*
104  * volume-del <device> <filesystem>
105  */
106 void
107 hammer_cmd_volume_del(char **av, int ac)
108 {
109 	struct hammer_ioc_volume ioc;
110 	int fd;
111 
112 	if (ac != 2) {
113 		fprintf(stderr, "hammer volume-del <device> <filesystem>\n");
114 		exit(1);
115 	}
116 
117 
118 	char *device = av[0];
119 	char *filesystem = av[1];
120 
121         fd = open(filesystem, O_RDONLY);
122 	if (fd < 0) {
123 		fprintf(stderr, "hammer volume-del: unable to access %s: %s\n",
124 			filesystem, strerror(errno));
125 		exit(1);
126 	}
127 
128 	/*
129 	 * volume-del ioctl
130 	 */
131 	bzero(&ioc, sizeof(ioc));
132 	strncpy(ioc.device_name, device, MAXPATHLEN);
133 
134 	if (ioctl(fd, HAMMERIOC_DEL_VOLUME, &ioc) < 0) {
135 		fprintf(stderr, "hammer volume-del ioctl: %s\n",
136 			strerror(errno));
137 		exit(1);
138 	}
139 
140 	close(fd);
141 }
142 
143 static void
144 hammer_print_volumes(char **av, int ac, const char *cmd, const char sep)
145 {
146 	struct hammer_ioc_volume_list ioc;
147 	int fd;
148 	int i;
149 
150 	if (ac != 1) {
151 		fprintf(stderr, "hammer %s <filesystem>\n", cmd);
152 		exit(1);
153 	}
154 
155 	char *filesystem = av[0];
156 
157 	fd = open(filesystem, O_RDONLY);
158 	if (fd < 0) {
159 		fprintf(stderr,
160 		    "hammer %s: unable to access %s: %s\n",
161 		    cmd, filesystem, strerror(errno));
162 		exit(1);
163 	}
164 
165 	bzero(&ioc, sizeof(ioc));
166 	ioc.vols = malloc(HAMMER_MAX_VOLUMES *
167 			  sizeof(struct hammer_ioc_volume));
168 	if (ioc.vols == NULL) {
169 		fprintf(stderr,
170 		    "hammer %s: unable to allocate memory: %s\n",
171 		    cmd, strerror(errno));
172 		exit(1);
173 	}
174 	ioc.nvols = HAMMER_MAX_VOLUMES;
175 
176 	if (ioctl(fd, HAMMERIOC_LIST_VOLUMES, &ioc) < 0) {
177 		fprintf(stderr, "hammer %s ioctl: %s\n",
178 			cmd, strerror(errno));
179 		free(ioc.vols);
180 		exit(1);
181 	}
182 
183 	for (i = 0; i < ioc.nvols; i++) {
184 		printf("%s", ioc.vols[i].device_name);
185 		if (i != ioc.nvols - 1)
186 			printf("%c", sep);
187 	}
188 	printf("\n");
189 
190 	free(ioc.vols);
191 	close(fd);
192 }
193 
194 /*
195  * volume-list <filesystem>
196  */
197 void
198 hammer_cmd_volume_list(char **av, int ac, const char *cmd)
199 {
200 	hammer_print_volumes(av, ac, cmd, '\n');
201 }
202 
203 /*
204  * volume-blkdevs <filesystem>
205  */
206 void
207 hammer_cmd_volume_blkdevs(char **av, int ac, const char *cmd)
208 {
209 	hammer_print_volumes(av, ac, cmd, ':');
210 }
211