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