46df7cf5aa20fe48016a389bc97aaa9a26c02200
[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                                "Pfs_id                               "
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                 default:
89                         printf("%02x          ", pfs.pfs_type);
90                         break;
91                 }
92                 uuid_to_string(&pfs.pfs_id, &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_create(const char *sel_path, const char *name,
106                uint8_t pfs_type, const char *uuid_str)
107 {
108         hammer2_ioc_pfs_t pfs;
109         int ecode = 0;
110         int fd;
111         uint32_t status;
112
113         /*
114          * Default to MASTER
115          */
116         if (pfs_type == HAMMER2_PFSTYPE_NONE) {
117                 pfs_type = HAMMER2_PFSTYPE_MASTER;
118         }
119
120         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
121                 return(1);
122         bzero(&pfs, sizeof(pfs));
123         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
124         pfs.pfs_type = pfs_type;
125         if (uuid_str) {
126                 uuid_from_string(uuid_str, &pfs.pfs_id, &status);
127         } else {
128                 uuid_create(&pfs.pfs_id, &status);
129         }
130         if (status == uuid_s_ok)
131                 uuid_create(&pfs.pfs_fsid, &status);
132         if (status == uuid_s_ok) {
133                 if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
134                         perror("ioctl");
135                         ecode = 1;
136                 }
137         } else {
138                 fprintf(stderr, "hammer2: pfs_create: badly formed uuid\n");
139                 ecode = 1;
140         }
141         close(fd);
142         return (ecode);
143 }
144
145 int
146 cmd_pfs_delete(const char *sel_path, const char *name)
147 {
148         hammer2_ioc_pfs_t pfs;
149         int ecode = 0;
150         int fd;
151
152         if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
153                 return(1);
154         bzero(&pfs, sizeof(pfs));
155         snprintf(pfs.name, sizeof(pfs.name), "%s", name);
156
157         if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
158                 fprintf(stderr, "hammer2: pfs_delete(%s): %s\n",
159                         name, strerror(errno));
160                 ecode = 1;
161         }
162         close(fd);
163
164         return (ecode);
165 }