xref: /dragonfly/sbin/hammer2/cmd_pfs.c (revision 31524921)
1 /*
2  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
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 #include "hammer2.h"
37 
38 int
39 cmd_pfs_list(const char *sel_path)
40 {
41 	hammer2_ioc_pfs_t pfs;
42 	int ecode = 0;
43 	int count = 0;
44 	int fd;
45 	uint32_t status;
46 	char *pfs_id_str = NULL;
47 
48 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
49 		return(1);
50 	bzero(&pfs, sizeof(pfs));
51 
52 	while ((pfs.name_key = pfs.name_next) != (hammer2_key_t)-1) {
53 		if (ioctl(fd, HAMMER2IOC_PFS_GET, &pfs) < 0) {
54 			perror("ioctl");
55 			ecode = 1;
56 			break;
57 		}
58 		if (count == 0) {
59 			printf("Type        "
60 			       "ClusterId (pfs_clid)                 "
61 			       "Label\n");
62 		}
63 		switch(pfs.pfs_type) {
64 		case HAMMER2_PFSTYPE_NONE:
65 			printf("NONE        ");
66 			break;
67 		case HAMMER2_PFSTYPE_CACHE:
68 			printf("CACHE       ");
69 			break;
70 		case HAMMER2_PFSTYPE_SLAVE:
71 			printf("SLAVE       ");
72 			break;
73 		case HAMMER2_PFSTYPE_SOFT_SLAVE:
74 			printf("SOFT_SLAVE  ");
75 			break;
76 		case HAMMER2_PFSTYPE_SOFT_MASTER:
77 			printf("SOFT_MASTER ");
78 			break;
79 		case HAMMER2_PFSTYPE_MASTER:
80 			switch (pfs.pfs_subtype) {
81 			case HAMMER2_PFSSUBTYPE_NONE:
82 				printf("MASTER      ");
83 				break;
84 			case HAMMER2_PFSSUBTYPE_SNAPSHOT:
85 				printf("SNAPSHOT    ");
86 				break;
87 			case HAMMER2_PFSSUBTYPE_AUTOSNAP:
88 				printf("AUTOSNAP    ");
89 				break;
90 			default:
91 				printf("MASTER(sub?)");
92 				break;
93 			}
94 			break;
95 		default:
96 			printf("%02x          ", pfs.pfs_type);
97 			break;
98 		}
99 		uuid_to_string(&pfs.pfs_clid, &pfs_id_str, &status);
100 		printf("%s ", pfs_id_str);
101 		free(pfs_id_str);
102 		pfs_id_str = NULL;
103 		printf("%s\n", pfs.name);
104 		++count;
105 	}
106 	close(fd);
107 
108 	return (ecode);
109 }
110 
111 int
112 cmd_pfs_getid(const char *sel_path, const char *name, int privateid)
113 {
114 	hammer2_ioc_pfs_t pfs;
115 	int ecode = 0;
116 	int fd;
117 	uint32_t status;
118 	char *pfs_id_str = NULL;
119 
120 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
121 		return(1);
122 	bzero(&pfs, sizeof(pfs));
123 
124 	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
125 	if (ioctl(fd, HAMMER2IOC_PFS_LOOKUP, &pfs) < 0) {
126 		perror("ioctl");
127 		ecode = 1;
128 	} else {
129 		if (privateid)
130 			uuid_to_string(&pfs.pfs_fsid, &pfs_id_str, &status);
131 		else
132 			uuid_to_string(&pfs.pfs_clid, &pfs_id_str, &status);
133 		printf("%s\n", pfs_id_str);
134 		free(pfs_id_str);
135 		pfs_id_str = NULL;
136 	}
137 	close(fd);
138 	return (ecode);
139 }
140 
141 
142 int
143 cmd_pfs_create(const char *sel_path, const char *name,
144 	       uint8_t pfs_type, const char *uuid_str)
145 {
146 	hammer2_ioc_pfs_t pfs;
147 	int ecode = 0;
148 	int fd;
149 	uint32_t status;
150 
151 	/*
152 	 * Default to MASTER if no uuid was specified.
153 	 * Default to SLAVE if a uuid was specified.
154 	 *
155 	 * When adding masters to a cluster, the new PFS must be added as
156 	 * a slave and then upgraded to ensure proper synchronization.
157 	 */
158 	if (pfs_type == HAMMER2_PFSTYPE_NONE) {
159 		if (uuid_str)
160 			pfs_type = HAMMER2_PFSTYPE_SLAVE;
161 		else
162 			pfs_type = HAMMER2_PFSTYPE_MASTER;
163 	}
164 
165 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
166 		return(1);
167 	bzero(&pfs, sizeof(pfs));
168 	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
169 	pfs.pfs_type = pfs_type;
170 	if (uuid_str) {
171 		uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
172 	} else {
173 		uuid_create(&pfs.pfs_clid, &status);
174 	}
175 	if (status == uuid_s_ok)
176 		uuid_create(&pfs.pfs_fsid, &status);
177 	if (status == uuid_s_ok) {
178 		if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
179 			if (errno == EEXIST) {
180 				fprintf(stderr,
181 					"NOTE: Typically the same name is "
182 					"used for cluster elements on "
183 					"different mounts,\n"
184 					"      but cluster elements on the "
185 					"same mount require unique names.\n"
186 					"pfs-create %s: already present\n",
187 					name);
188 			} else {
189 				perror("ioctl");
190 			}
191 			ecode = 1;
192 		}
193 	} else {
194 		fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
195 		ecode = 1;
196 	}
197 	close(fd);
198 	return (ecode);
199 }
200 
201 int
202 cmd_pfs_delete(const char *sel_path, const char *name)
203 {
204 	hammer2_ioc_pfs_t pfs;
205 	int ecode = 0;
206 	int fd;
207 
208 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
209 		return(1);
210 	bzero(&pfs, sizeof(pfs));
211 	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
212 
213 	if (ioctl(fd, HAMMER2IOC_PFS_DELETE, &pfs) < 0) {
214 		fprintf(stderr, "hammer2: pfs_delete(%s): %s\n",
215 			name, strerror(errno));
216 		ecode = 1;
217 	}
218 	close(fd);
219 
220 	return (ecode);
221 }
222