hammer2 - cleanup, add manual page
[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                         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 if no uuid was specified.
146          * Default to SLAVE if a uuid was specified.
147          *
148          * When adding masters to a cluster, the new PFS must be added as
149          * a slave and then upgraded to ensure proper synchronization.
150          */
151         if (pfs_type == HAMMER2_PFSTYPE_NONE) {
152                 if (uuid_str)
153                         pfs_type = HAMMER2_PFSTYPE_SLAVE;
154                 else
155                         pfs_type = HAMMER2_PFSTYPE_MASTER;
156         }
157
158         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
159                 return(1);
160         bzero(&pfs, sizeof(pfs));
161         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
162         pfs.pfs_type = pfs_type;
163         if (uuid_str) {
164                 uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
165         } else {
166                 uuid_create(&pfs.pfs_clid, &status);
167         }
168         if (status == uuid_s_ok)
169                 uuid_create(&pfs.pfs_fsid, &status);
170         if (status == uuid_s_ok) {
171                 if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
172                         if (errno == EEXIST) {
173                                 fprintf(stderr,
174                                         "NOTE: Typically the same name is "
175                                         "used for cluster elements on "
176                                         "different mounts,\n"
177                                         "      but cluster elements on the "
178                                         "same mount require unique names.\n"
179                                         "pfs-create %s: already present\n",
180                                         name);
181                         } else {
182                                 perror("ioctl");
183                         }
184                         ecode = 1;
185                 }
186         } else {
187                 fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
188                 ecode = 1;
189         }
190         close(fd);
191         return (ecode);
192 }
193
194 int
195 cmd_pfs_delete(const char *sel_path, const char *name)
196 {
197         hammer2_ioc_pfs_t pfs;
198         int ecode = 0;
199         int fd;
200
201         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
202                 return(1);
203         bzero(&pfs, sizeof(pfs));
204         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
205
206         if (ioctl(fd, HAMMER2IOC_PFS_DELETE, &pfs) < 0) {
207                 fprintf(stderr, "hammer2: pfs_delete(%s): %s\n",
208                         name, strerror(errno));
209                 ecode = 1;
210         }
211         close(fd);
212
213         return (ecode);
214 }