hammer2 - Cleanup error paths
[dragonfly.git] / sbin / hammer2 / cmd_pfs.c
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_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                         switch (pfs.pfs_subtype) {
84                         case HAMMER2_PFSSUBTYPE_NONE:
85                                 printf("MASTER      ");
86                                 break;
87                         case HAMMER2_PFSSUBTYPE_SNAPSHOT:
88                                 printf("SNAPSHOT    ");
89                                 break;
90                         default:
91                                 printf("MASTER(sub?)");
92                                 break;
93                         }
94                 default:
95                         printf("%02x          ", pfs.pfs_type);
96                         break;
97                 }
98                 uuid_to_string(&pfs.pfs_clid, &pfs_id_str, &status);
99                 printf("%s ", pfs_id_str);
100                 free(pfs_id_str);
101                 pfs_id_str = NULL;
102                 printf("%s\n", pfs.name);
103                 ++count;
104         }
105         close(fd);
106
107         return (ecode);
108 }
109
110 int
111 cmd_pfs_getid(const char *sel_path, const char *name, int privateid)
112 {
113         hammer2_ioc_pfs_t pfs;
114         int ecode = 0;
115         int fd;
116         uint32_t status;
117         char *pfs_id_str = NULL;
118
119         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
120                 return(1);
121         bzero(&pfs, sizeof(pfs));
122
123         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
124         if (ioctl(fd, HAMMER2IOC_PFS_LOOKUP, &pfs) < 0) {
125                 perror("ioctl");
126                 ecode = 1;
127         } else {
128                 if (privateid)
129                         uuid_to_string(&pfs.pfs_fsid, &pfs_id_str, &status);
130                 else
131                         uuid_to_string(&pfs.pfs_clid, &pfs_id_str, &status);
132                 printf("%s\n", pfs_id_str);
133                 free(pfs_id_str);
134                 pfs_id_str = NULL;
135         }
136         close(fd);
137         return (ecode);
138 }
139
140
141 int
142 cmd_pfs_create(const char *sel_path, const char *name,
143                uint8_t pfs_type, const char *uuid_str)
144 {
145         hammer2_ioc_pfs_t pfs;
146         int ecode = 0;
147         int fd;
148         uint32_t status;
149
150         /*
151          * Default to MASTER if no uuid was specified.
152          * Default to SLAVE if a uuid was specified.
153          *
154          * When adding masters to a cluster, the new PFS must be added as
155          * a slave and then upgraded to ensure proper synchronization.
156          */
157         if (pfs_type == HAMMER2_PFSTYPE_NONE) {
158                 if (uuid_str)
159                         pfs_type = HAMMER2_PFSTYPE_SLAVE;
160                 else
161                         pfs_type = HAMMER2_PFSTYPE_MASTER;
162         }
163
164         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
165                 return(1);
166         bzero(&pfs, sizeof(pfs));
167         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
168         pfs.pfs_type = pfs_type;
169         if (uuid_str) {
170                 uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
171         } else {
172                 uuid_create(&pfs.pfs_clid, &status);
173         }
174         if (status == uuid_s_ok)
175                 uuid_create(&pfs.pfs_fsid, &status);
176         if (status == uuid_s_ok) {
177                 if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
178                         if (errno == EEXIST) {
179                                 fprintf(stderr,
180                                         "NOTE: Typically the same name is "
181                                         "used for cluster elements on "
182                                         "different mounts,\n"
183                                         "      but cluster elements on the "
184                                         "same mount require unique names.\n"
185                                         "pfs-create %s: already present\n",
186                                         name);
187                         } else {
188                                 perror("ioctl");
189                         }
190                         ecode = 1;
191                 }
192         } else {
193                 fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
194                 ecode = 1;
195         }
196         close(fd);
197         return (ecode);
198 }
199
200 int
201 cmd_pfs_delete(const char *sel_path, const char *name)
202 {
203         hammer2_ioc_pfs_t pfs;
204         int ecode = 0;
205         int fd;
206
207         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
208                 return(1);
209         bzero(&pfs, sizeof(pfs));
210         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
211
212         if (ioctl(fd, HAMMER2IOC_PFS_DELETE, &pfs) < 0) {
213                 fprintf(stderr, "hammer2: pfs_delete(%s): %s\n",
214                         name, strerror(errno));
215                 ecode = 1;
216         }
217         close(fd);
218
219         return (ecode);
220 }