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