xref: /dragonfly/sbin/hammer2/cmd_pfs.c (revision cab8bf9b)
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 DMSG_PFSTYPE_NONE:
65 			printf("NONE        ");
66 			break;
67 		case HAMMER2_PFSTYPE_CACHE:
68 			printf("CACHE       ");
69 			break;
70 		case HAMMER2_PFSTYPE_COPY:
71 			printf("COPY        ");
72 			break;
73 		case HAMMER2_PFSTYPE_SLAVE:
74 			printf("SLAVE       ");
75 			break;
76 		case HAMMER2_PFSTYPE_SOFT_SLAVE:
77 			printf("SOFT_SLAVE  ");
78 			break;
79 		case HAMMER2_PFSTYPE_SOFT_MASTER:
80 			printf("SOFT_MASTER ");
81 			break;
82 		case HAMMER2_PFSTYPE_MASTER:
83 			printf("MASTER      ");
84 			break;
85 		case HAMMER2_PFSTYPE_SNAPSHOT:
86 			printf("SNAPSHOT    ");
87 			break;
88 		default:
89 			printf("%02x          ", pfs.pfs_type);
90 			break;
91 		}
92 		uuid_to_string(&pfs.pfs_clid, &pfs_id_str, &status);
93 		printf("%s ", pfs_id_str);
94 		free(pfs_id_str);
95 		pfs_id_str = NULL;
96 		printf("%s\n", pfs.name);
97 		++count;
98 	}
99 	close(fd);
100 
101 	return (ecode);
102 }
103 
104 int
105 cmd_pfs_getid(const char *sel_path, const char *name, int privateid)
106 {
107 	hammer2_ioc_pfs_t pfs;
108 	int ecode = 0;
109 	int fd;
110 	uint32_t status;
111 	char *pfs_id_str = NULL;
112 
113 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
114 		return(1);
115 	bzero(&pfs, sizeof(pfs));
116 
117 	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
118 	if (ioctl(fd, HAMMER2IOC_PFS_LOOKUP, &pfs) < 0) {
119 		perror("ioctl");
120 		ecode = 1;
121 	} else {
122 		if (privateid)
123 			uuid_to_string(&pfs.pfs_fsid, &pfs_id_str, &status);
124 		else
125 			uuid_to_string(&pfs.pfs_clid, &pfs_id_str, &status);
126 		printf("%s\n", pfs_id_str);
127 		free(pfs_id_str);
128 		pfs_id_str = NULL;
129 	}
130 	close(fd);
131 	return (ecode);
132 }
133 
134 
135 int
136 cmd_pfs_create(const char *sel_path, const char *name,
137 	       uint8_t pfs_type, const char *uuid_str)
138 {
139 	hammer2_ioc_pfs_t pfs;
140 	int ecode = 0;
141 	int fd;
142 	uint32_t status;
143 
144 	/*
145 	 * Default to MASTER
146 	 */
147 	if (pfs_type == DMSG_PFSTYPE_NONE) {
148 		pfs_type = HAMMER2_PFSTYPE_MASTER;
149 	}
150 
151 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
152 		return(1);
153 	bzero(&pfs, sizeof(pfs));
154 	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
155 	pfs.pfs_type = pfs_type;
156 	if (uuid_str) {
157 		uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
158 	} else {
159 		uuid_create(&pfs.pfs_clid, &status);
160 	}
161 	if (status == uuid_s_ok)
162 		uuid_create(&pfs.pfs_fsid, &status);
163 	if (status == uuid_s_ok) {
164 		if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
165 			perror("ioctl");
166 			ecode = 1;
167 		}
168 	} else {
169 		fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
170 		ecode = 1;
171 	}
172 	close(fd);
173 	return (ecode);
174 }
175 
176 int
177 cmd_pfs_delete(const char *sel_path, const char *name)
178 {
179 	hammer2_ioc_pfs_t pfs;
180 	int ecode = 0;
181 	int fd;
182 
183 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
184 		return(1);
185 	bzero(&pfs, sizeof(pfs));
186 	snprintf(pfs.name, sizeof(pfs.name), "%s", name);
187 
188 	if (ioctl(fd, HAMMER2IOC_PFS_DELETE, &pfs) < 0) {
189 		fprintf(stderr, "hammer2: pfs_delete(%s): %s\n",
190 			name, strerror(errno));
191 		ecode = 1;
192 	}
193 	close(fd);
194 
195 	return (ecode);
196 }
197