xref: /dragonfly/sbin/hammer/cmd_volume.c (revision 0d27ae55)
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 	const char *device, *filesystem;
58 
59 	if (ac != 2) {
60 		fprintf(stderr, "hammer volume-add <device> <filesystem>\n");
61 		exit(1);
62 	}
63 
64 	device = av[0];
65 	filesystem = av[1];
66 
67         fd = open(filesystem, O_RDONLY);
68 	if (fd < 0) {
69 		fprintf(stderr, "hammer volume-add: unable to access %s: %s\n",
70 			filesystem, strerror(errno));
71 		exit(1);
72 	}
73 
74 	/*
75 	 * Initialize and check the device
76 	 */
77 	vol = init_volume(-1, device, O_RDONLY);
78 	assert(vol->vol_no == -1);
79 	check_volume(vol);
80 	if (strcmp(vol->type, "DEVICE")) {
81 		fprintf(stderr, "Not a block device: %s\n", device);
82 		exit(1);
83 	}
84 	close(vol->fd);
85 
86 	/*
87 	 * volume-add ioctl
88 	 */
89 	bzero(&ioc, sizeof(ioc));
90 	strncpy(ioc.device_name, device, MAXPATHLEN);
91 	ioc.vol_size = vol->size;
92 	ioc.boot_area_size = init_boot_area_size(0, ioc.vol_size);
93 	ioc.mem_area_size = init_mem_area_size(0, ioc.vol_size);
94 
95 	if (ioctl(fd, HAMMERIOC_ADD_VOLUME, &ioc) < 0) {
96 		fprintf(stderr, "hammer volume-add ioctl: %s\n",
97 			strerror(errno));
98 		exit(1);
99 	}
100 
101 	close(fd);
102 	hammer_cmd_volume_list(av + 1, ac - 1);
103 }
104 
105 /*
106  * volume-del <device> <filesystem>
107  */
108 void
109 hammer_cmd_volume_del(char **av, int ac)
110 {
111 	struct hammer_ioc_volume ioc;
112 	int fd, error, retried = 0;
113 	const char *device, *filesystem;
114 
115 	if (ac != 2) {
116 		fprintf(stderr, "hammer volume-del <device> <filesystem>\n");
117 		exit(1);
118 	}
119 
120 	device = av[0];
121 	filesystem = av[1];
122 
123         fd = open(filesystem, O_RDONLY);
124 	if (fd < 0) {
125 		fprintf(stderr, "hammer volume-del: unable to access %s: %s\n",
126 			filesystem, strerror(errno));
127 		exit(1);
128 	}
129 
130 	/*
131 	 * volume-del ioctl
132 	 */
133 	bzero(&ioc, sizeof(ioc));
134 	strncpy(ioc.device_name, device, MAXPATHLEN);
135 	if (ForceOpt)
136 		ioc.flag |= HAMMER_IOC_VOLUME_REBLOCK;
137 retry:
138 	if (ioctl(fd, HAMMERIOC_DEL_VOLUME, &ioc) < 0) {
139 		error = errno;
140 		if ((error == ENOTEMPTY) && (retried++ == 0)) {
141 			printf("%s is not empty, ", device);
142 			printf("do you want to reblock %s? [y/n]\n", device);
143 			fflush(stdout);
144 			if (getyn() == 1) {
145 				ioc.flag |= HAMMER_IOC_VOLUME_REBLOCK;
146 				goto retry;
147 			}
148 		}
149 		fprintf(stderr, "hammer volume-del ioctl: %s\n",
150 			strerror(error));
151 		exit(1);
152 	}
153 
154 	close(fd);
155 	hammer_cmd_volume_list(av + 1, ac - 1);
156 }
157 
158 /*
159  * volume-list <filesystem>
160  */
161 void
162 hammer_cmd_volume_list(char **av, int ac)
163 {
164 	struct hammer_ioc_volume_list ioc;
165 	char *device_name;
166 	int vol_no, i;
167 
168 	if (ac < 1) {
169 		fprintf(stderr, "hammer volume-list <filesystem>\n");
170 		exit(1);
171 	}
172 
173 	if (hammer_fs_to_vol(av[0], &ioc) == -1) {
174 		fprintf(stderr, "hammer volume-list: failed\n");
175 		exit(1);
176 	}
177 
178 	for (i = 0; i < ioc.nvols; i++) {
179 		device_name = ioc.vols[i].device_name;
180 		vol_no = ioc.vols[i].vol_no;
181 		if (VerboseOpt) {
182 			printf("%d\t%s%s\n", vol_no, device_name,
183 				(vol_no == HAMMER_ROOT_VOLNO ?
184 				" (Root Volume)" : ""));
185 		} else {
186 			printf("%s\n", device_name);
187 		}
188 	}
189 
190 	free(ioc.vols);
191 }
192 
193 /*
194  * volume-blkdevs <filesystem>
195  */
196 void
197 hammer_cmd_volume_blkdevs(char **av, int ac)
198 {
199 	struct hammer_ioc_volume_list ioc;
200 	int i;
201 
202 	if (ac < 1) {
203 		fprintf(stderr, "hammer volume-blkdevs <filesystem>\n");
204 		exit(1);
205 	}
206 
207 	if (hammer_fs_to_vol(av[0], &ioc) == -1) {
208 		fprintf(stderr, "hammer volume-list: failed\n");
209 		exit(1);
210 	}
211 
212 	for (i = 0; i < ioc.nvols; i++) {
213 		printf("%s", ioc.vols[i].device_name);
214 		if (i != ioc.nvols - 1)
215 			printf(":");
216 	}
217 	printf("\n");
218 
219 	free(ioc.vols);
220 }
221