HAMMER - Implement experimental volume removal
[dragonfly.git] / sbin / hammer / cmd_volume.c
CommitLineData
e27700cf
MN
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
90ecab35
MN
5 * by Matthew Dillon <dillon@backplane.com> and
6 * Michael Neumann <mneumann@ntecs.de>
e27700cf
MN
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/*
d121f61c
MN
37 * Volume operations:
38 *
39 * - volume-add: Add new volume to HAMMER filesystem
865c9609 40 * - volume-del: Remove volume from HAMMER filesystem
e27700cf
MN
41 */
42
43#include "hammer.h"
44#include <string.h>
865c9609 45#include <stdlib.h>
e27700cf 46
90ecab35 47static uint64_t check_volume(const char *vol_name);
e27700cf
MN
48
49/*
d121f61c 50 * volume-add <device> <filesystem>
e27700cf
MN
51 */
52void
d121f61c 53hammer_cmd_volume_add(char **av, int ac)
e27700cf 54{
865c9609 55 struct hammer_ioc_volume ioc;
e27700cf
MN
56 int fd;
57
865c9609
MN
58 if (ac != 2) {
59 fprintf(stderr, "hammer volume-add <device> <filesystem>\n");
60 exit(1);
61 }
62
d121f61c
MN
63 char *device = av[0];
64 char *filesystem = av[1];
65
d121f61c 66 fd = open(filesystem, O_RDONLY);
e27700cf 67 if (fd < 0) {
d121f61c
MN
68 fprintf(stderr, "hammer volume-add: unable to access %s: %s\n",
69 filesystem, strerror(errno));
e27700cf
MN
70 exit(1);
71 }
72
e3102897 73 /*
d121f61c 74 * volume-add ioctl
e3102897 75 */
d121f61c
MN
76 bzero(&ioc, sizeof(ioc));
77 strncpy(ioc.device_name, device, MAXPATHLEN);
78 ioc.vol_size = check_volume(device);
79 ioc.boot_area_size = HAMMER_BOOT_NOMBYTES;
80 ioc.mem_area_size = HAMMER_MEM_NOMBYTES;
e27700cf 81
d121f61c
MN
82 if (ioctl(fd, HAMMERIOC_ADD_VOLUME, &ioc) < 0) {
83 fprintf(stderr, "hammer volume-add ioctl: %s\n",
84 strerror(errno));
e27700cf
MN
85 exit(1);
86 }
87
88 close(fd);
89}
90
865c9609
MN
91/*
92 * volume-del <device> <filesystem>
93 */
e27700cf 94void
865c9609 95hammer_cmd_volume_del(char **av, int ac)
e27700cf 96{
865c9609
MN
97 struct hammer_ioc_volume ioc;
98 int fd;
99
100 if (ac != 2) {
101 fprintf(stderr, "hammer volume-del <device> <filesystem>\n");
102 exit(1);
103 }
104
105
106 char *device = av[0];
107 char *filesystem = av[1];
108
109 fd = open(filesystem, O_RDONLY);
110 if (fd < 0) {
111 fprintf(stderr, "hammer volume-add: unable to access %s: %s\n",
112 filesystem, strerror(errno));
113 exit(1);
114 }
115
116 /*
117 * volume-del ioctl
118 */
119 bzero(&ioc, sizeof(ioc));
120 strncpy(ioc.device_name, device, MAXPATHLEN);
121
122 if (ioctl(fd, HAMMERIOC_DEL_VOLUME, &ioc) < 0) {
123 fprintf(stderr, "hammer volume-del ioctl: %s\n",
124 strerror(errno));
125 exit(1);
126 }
127
128 close(fd);
e27700cf 129}
90ecab35
MN
130
131/*
132 * Check basic volume characteristics. HAMMER filesystems use a minimum
133 * of a 16KB filesystem buffer size.
134 *
135 * Returns the size of the device.
136 *
137 * From newfs_hammer.c
138 */
139static
140uint64_t
141check_volume(const char *vol_name)
142{
143 struct partinfo pinfo;
144 int fd;
145
146 /*
147 * Get basic information about the volume
148 */
149 fd = open(vol_name, O_RDWR);
150 if (fd < 0)
151 errx(1, "Unable to open %s R+W", vol_name);
152
153 if (ioctl(fd, DIOCGPART, &pinfo) < 0) {
154 errx(1, "No block device: %s", vol_name);
155 }
156 /*
157 * When formatting a block device as a HAMMER volume the
158 * sector size must be compatible. HAMMER uses 16384 byte
159 * filesystem buffers.
160 */
161 if (pinfo.reserved_blocks) {
162 errx(1, "HAMMER cannot be placed in a partition "
163 "which overlaps the disklabel or MBR");
164 }
165 if (pinfo.media_blksize > 16384 ||
166 16384 % pinfo.media_blksize) {
167 errx(1, "A media sector size of %d is not supported",
168 pinfo.media_blksize);
169 }
170
171 close(fd);
172 return pinfo.media_size;
173}