1ae183399SMatthew Dillon /*
2ae183399SMatthew Dillon * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3ae183399SMatthew Dillon *
4ae183399SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
5ae183399SMatthew Dillon * by Matthew Dillon <dillon@dragonflybsd.org>
6ae183399SMatthew Dillon * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7ae183399SMatthew Dillon *
8ae183399SMatthew Dillon * Redistribution and use in source and binary forms, with or without
9ae183399SMatthew Dillon * modification, are permitted provided that the following conditions
10ae183399SMatthew Dillon * are met:
11ae183399SMatthew Dillon *
12ae183399SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
13ae183399SMatthew Dillon * notice, this list of conditions and the following disclaimer.
14ae183399SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
15ae183399SMatthew Dillon * notice, this list of conditions and the following disclaimer in
16ae183399SMatthew Dillon * the documentation and/or other materials provided with the
17ae183399SMatthew Dillon * distribution.
18ae183399SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
19ae183399SMatthew Dillon * contributors may be used to endorse or promote products derived
20ae183399SMatthew Dillon * from this software without specific, prior written permission.
21ae183399SMatthew Dillon *
22ae183399SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23ae183399SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24ae183399SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25ae183399SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26ae183399SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27ae183399SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28ae183399SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29ae183399SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30ae183399SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31ae183399SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32ae183399SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ae183399SMatthew Dillon * SUCH DAMAGE.
34ae183399SMatthew Dillon */
35ae183399SMatthew Dillon
36ae183399SMatthew Dillon #include "hammer2.h"
37ae183399SMatthew Dillon
38913bc49bSTomohiro Kusumi struct pfs_entry {
39913bc49bSTomohiro Kusumi TAILQ_ENTRY(pfs_entry) entry;
40913bc49bSTomohiro Kusumi char name[NAME_MAX+1];
41913bc49bSTomohiro Kusumi char s[NAME_MAX+1];
42913bc49bSTomohiro Kusumi };
43913bc49bSTomohiro Kusumi
44ae183399SMatthew Dillon int
cmd_pfs_list(int ac,char ** av)4583d90983SMatthew Dillon cmd_pfs_list(int ac, char **av)
46ae183399SMatthew Dillon {
47ae183399SMatthew Dillon hammer2_ioc_pfs_t pfs;
48ae183399SMatthew Dillon int ecode = 0;
49ae183399SMatthew Dillon int fd;
50b92bfd39SMatthew Dillon int i;
5183d90983SMatthew Dillon int all = 0;
52ae183399SMatthew Dillon char *pfs_id_str = NULL;
53cac06d4eSTomohiro Kusumi const char *type_str;
54913bc49bSTomohiro Kusumi TAILQ_HEAD(, pfs_entry) head;
55913bc49bSTomohiro Kusumi struct pfs_entry *p, *e;
56ae183399SMatthew Dillon
5783d90983SMatthew Dillon if (ac == 1 && av[0] == NULL) {
5883d90983SMatthew Dillon av = get_hammer2_mounts(&ac);
5983d90983SMatthew Dillon all = 1;
6083d90983SMatthew Dillon }
6183d90983SMatthew Dillon
62b92bfd39SMatthew Dillon for (i = 0; i < ac; ++i) {
63b92bfd39SMatthew Dillon if ((fd = hammer2_ioctl_handle(av[i])) < 0)
64ae183399SMatthew Dillon return(1);
65ae183399SMatthew Dillon bzero(&pfs, sizeof(pfs));
66913bc49bSTomohiro Kusumi TAILQ_INIT(&head);
6783d90983SMatthew Dillon if (i)
6883d90983SMatthew Dillon printf("\n");
69ae183399SMatthew Dillon
70ae183399SMatthew Dillon while ((pfs.name_key = pfs.name_next) != (hammer2_key_t)-1) {
71ae183399SMatthew Dillon if (ioctl(fd, HAMMER2IOC_PFS_GET, &pfs) < 0) {
72ae183399SMatthew Dillon perror("ioctl");
73ae183399SMatthew Dillon ecode = 1;
74ae183399SMatthew Dillon break;
75ae183399SMatthew Dillon }
76d4a14304STomohiro Kusumi hammer2_uuid_to_str(&pfs.pfs_clid, &pfs_id_str);
77*856d0d8fSTomohiro Kusumi if (pfs.pfs_type == HAMMER2_PFSTYPE_MASTER) {
78*856d0d8fSTomohiro Kusumi if (pfs.pfs_subtype == HAMMER2_PFSSUBTYPE_NONE)
79*856d0d8fSTomohiro Kusumi type_str = "MASTER";
80cac06d4eSTomohiro Kusumi else
81*856d0d8fSTomohiro Kusumi type_str = hammer2_pfssubtype_to_str(
82*856d0d8fSTomohiro Kusumi pfs.pfs_subtype);
83*856d0d8fSTomohiro Kusumi } else {
84cac06d4eSTomohiro Kusumi type_str = hammer2_pfstype_to_str(pfs.pfs_type);
85*856d0d8fSTomohiro Kusumi }
86913bc49bSTomohiro Kusumi e = calloc(1, sizeof(*e));
87913bc49bSTomohiro Kusumi snprintf(e->name, sizeof(e->name), "%s", pfs.name);
88cac06d4eSTomohiro Kusumi snprintf(e->s, sizeof(e->s), "%-11s %s",
89cac06d4eSTomohiro Kusumi type_str, pfs_id_str);
90ae183399SMatthew Dillon free(pfs_id_str);
91ae183399SMatthew Dillon pfs_id_str = NULL;
92913bc49bSTomohiro Kusumi
93913bc49bSTomohiro Kusumi p = TAILQ_FIRST(&head);
94913bc49bSTomohiro Kusumi while (p) {
95913bc49bSTomohiro Kusumi if (strcmp(e->name, p->name) <= 0) {
96913bc49bSTomohiro Kusumi TAILQ_INSERT_BEFORE(p, e, entry);
97913bc49bSTomohiro Kusumi break;
98913bc49bSTomohiro Kusumi }
99913bc49bSTomohiro Kusumi p = TAILQ_NEXT(p, entry);
100913bc49bSTomohiro Kusumi }
101913bc49bSTomohiro Kusumi if (!p)
102913bc49bSTomohiro Kusumi TAILQ_INSERT_TAIL(&head, e, entry);
103ae183399SMatthew Dillon }
104ae183399SMatthew Dillon close(fd);
105913bc49bSTomohiro Kusumi
106913bc49bSTomohiro Kusumi printf("Type "
107913bc49bSTomohiro Kusumi "ClusterId (pfs_clid) "
108929eef7aSTomohiro Kusumi "Label on %s\n", av[i]);
109913bc49bSTomohiro Kusumi while ((p = TAILQ_FIRST(&head)) != NULL) {
110913bc49bSTomohiro Kusumi printf("%s %s\n", p->s, p->name);
111913bc49bSTomohiro Kusumi TAILQ_REMOVE(&head, p, entry);
112913bc49bSTomohiro Kusumi free(p);
113913bc49bSTomohiro Kusumi }
114b92bfd39SMatthew Dillon }
115ae183399SMatthew Dillon
11683d90983SMatthew Dillon if (all)
11783d90983SMatthew Dillon put_hammer2_mounts(ac, av);
11883d90983SMatthew Dillon
119ae183399SMatthew Dillon return (ecode);
120ae183399SMatthew Dillon }
121ae183399SMatthew Dillon
122ae183399SMatthew Dillon int
cmd_pfs_getid(const char * sel_path,const char * name,int privateid)123964e3b01SMatthew Dillon cmd_pfs_getid(const char *sel_path, const char *name, int privateid)
124964e3b01SMatthew Dillon {
125964e3b01SMatthew Dillon hammer2_ioc_pfs_t pfs;
126964e3b01SMatthew Dillon int ecode = 0;
127964e3b01SMatthew Dillon int fd;
128964e3b01SMatthew Dillon char *pfs_id_str = NULL;
129964e3b01SMatthew Dillon
130964e3b01SMatthew Dillon if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
131964e3b01SMatthew Dillon return(1);
132964e3b01SMatthew Dillon bzero(&pfs, sizeof(pfs));
133964e3b01SMatthew Dillon
134964e3b01SMatthew Dillon snprintf(pfs.name, sizeof(pfs.name), "%s", name);
135964e3b01SMatthew Dillon if (ioctl(fd, HAMMER2IOC_PFS_LOOKUP, &pfs) < 0) {
136964e3b01SMatthew Dillon perror("ioctl");
137964e3b01SMatthew Dillon ecode = 1;
138964e3b01SMatthew Dillon } else {
139964e3b01SMatthew Dillon if (privateid)
140d4a14304STomohiro Kusumi hammer2_uuid_to_str(&pfs.pfs_fsid, &pfs_id_str);
141964e3b01SMatthew Dillon else
142d4a14304STomohiro Kusumi hammer2_uuid_to_str(&pfs.pfs_clid, &pfs_id_str);
143964e3b01SMatthew Dillon printf("%s\n", pfs_id_str);
144964e3b01SMatthew Dillon free(pfs_id_str);
145964e3b01SMatthew Dillon pfs_id_str = NULL;
146964e3b01SMatthew Dillon }
147964e3b01SMatthew Dillon close(fd);
148964e3b01SMatthew Dillon return (ecode);
149964e3b01SMatthew Dillon }
150964e3b01SMatthew Dillon
151964e3b01SMatthew Dillon
152964e3b01SMatthew Dillon int
cmd_pfs_create(const char * sel_path,const char * name,uint8_t pfs_type,const char * uuid_str)153ae183399SMatthew Dillon cmd_pfs_create(const char *sel_path, const char *name,
154ae183399SMatthew Dillon uint8_t pfs_type, const char *uuid_str)
155ae183399SMatthew Dillon {
156ae183399SMatthew Dillon hammer2_ioc_pfs_t pfs;
157ae183399SMatthew Dillon int ecode = 0;
158ae183399SMatthew Dillon int fd;
159ae183399SMatthew Dillon uint32_t status;
160ae183399SMatthew Dillon
161004f88b4SMatthew Dillon /*
1623c198419SMatthew Dillon * Default to MASTER if no uuid was specified.
1633c198419SMatthew Dillon * Default to SLAVE if a uuid was specified.
1643c198419SMatthew Dillon *
1653c198419SMatthew Dillon * When adding masters to a cluster, the new PFS must be added as
1663c198419SMatthew Dillon * a slave and then upgraded to ensure proper synchronization.
167004f88b4SMatthew Dillon */
1687750fd72SMatthew Dillon if (pfs_type == HAMMER2_PFSTYPE_NONE) {
1693c198419SMatthew Dillon if (uuid_str)
1703c198419SMatthew Dillon pfs_type = HAMMER2_PFSTYPE_SLAVE;
1713c198419SMatthew Dillon else
172004f88b4SMatthew Dillon pfs_type = HAMMER2_PFSTYPE_MASTER;
173ae183399SMatthew Dillon }
174ae183399SMatthew Dillon
175ae183399SMatthew Dillon if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
176ae183399SMatthew Dillon return(1);
177ae183399SMatthew Dillon bzero(&pfs, sizeof(pfs));
178ae183399SMatthew Dillon snprintf(pfs.name, sizeof(pfs.name), "%s", name);
179ae183399SMatthew Dillon pfs.pfs_type = pfs_type;
180ae183399SMatthew Dillon if (uuid_str) {
1818c280d5dSMatthew Dillon uuid_from_string(uuid_str, &pfs.pfs_clid, &status);
182ae183399SMatthew Dillon } else {
1838c280d5dSMatthew Dillon uuid_create(&pfs.pfs_clid, &status);
184ae183399SMatthew Dillon }
185ae183399SMatthew Dillon if (status == uuid_s_ok)
186ae183399SMatthew Dillon uuid_create(&pfs.pfs_fsid, &status);
187ae183399SMatthew Dillon if (status == uuid_s_ok) {
188ae183399SMatthew Dillon if (ioctl(fd, HAMMER2IOC_PFS_CREATE, &pfs) < 0) {
1893c198419SMatthew Dillon if (errno == EEXIST) {
1903c198419SMatthew Dillon fprintf(stderr,
1913c198419SMatthew Dillon "NOTE: Typically the same name is "
1923c198419SMatthew Dillon "used for cluster elements on "
1933c198419SMatthew Dillon "different mounts,\n"
1943c198419SMatthew Dillon " but cluster elements on the "
1953c198419SMatthew Dillon "same mount require unique names.\n"
196a12b2972STomohiro Kusumi "hammer2: pfs_create(%s): already present\n",
1973c198419SMatthew Dillon name);
1983c198419SMatthew Dillon } else {
199a12b2972STomohiro Kusumi fprintf(stderr, "hammer2: pfs_create(%s): %s\n",
200a12b2972STomohiro Kusumi name, strerror(errno));
2013c198419SMatthew Dillon }
202ae183399SMatthew Dillon ecode = 1;
203a12b2972STomohiro Kusumi } else {
204a12b2972STomohiro Kusumi printf("hammer2: pfs_create(%s): SUCCESS\n", name);
205ae183399SMatthew Dillon }
206ae183399SMatthew Dillon } else {
207a12b2972STomohiro Kusumi fprintf(stderr, "hammer2: pfs_create(%s): badly formed uuid\n",
208a12b2972STomohiro Kusumi name);
209ae183399SMatthew Dillon ecode = 1;
210ae183399SMatthew Dillon }
211ae183399SMatthew Dillon close(fd);
212ae183399SMatthew Dillon return (ecode);
213ae183399SMatthew Dillon }
214ae183399SMatthew Dillon
215ae183399SMatthew Dillon int
cmd_pfs_delete(const char * sel_path,char ** av,int ac)21683d90983SMatthew Dillon cmd_pfs_delete(const char *sel_path, char **av, int ac)
217ae183399SMatthew Dillon {
218ae183399SMatthew Dillon hammer2_ioc_pfs_t pfs;
219ae183399SMatthew Dillon int ecode = 0;
220ae183399SMatthew Dillon int fd;
22183d90983SMatthew Dillon int i;
22283d90983SMatthew Dillon int n;
22383d90983SMatthew Dillon int use_fd;
22483d90983SMatthew Dillon int nmnts = 0;
22583d90983SMatthew Dillon char **mnts = NULL;
226ae183399SMatthew Dillon
22783d90983SMatthew Dillon if (sel_path == NULL)
22883d90983SMatthew Dillon mnts = get_hammer2_mounts(&nmnts);
22983d90983SMatthew Dillon
23083d90983SMatthew Dillon for (i = 1; i < ac; ++i) {
23186993811STomohiro Kusumi int enoents = 0;
232ae183399SMatthew Dillon bzero(&pfs, sizeof(pfs));
23383d90983SMatthew Dillon snprintf(pfs.name, sizeof(pfs.name), "%s", av[i]);
234ae183399SMatthew Dillon
23583d90983SMatthew Dillon if (sel_path) {
23683d90983SMatthew Dillon use_fd = hammer2_ioctl_handle(sel_path);
23783d90983SMatthew Dillon } else {
23883d90983SMatthew Dillon use_fd = -1;
23983d90983SMatthew Dillon for (n = 0; n < nmnts; ++n) {
24086993811STomohiro Kusumi if ((fd = hammer2_ioctl_handle(mnts[n])) < 0) {
24186993811STomohiro Kusumi enoents++;
24283d90983SMatthew Dillon continue;
24386993811STomohiro Kusumi }
24486993811STomohiro Kusumi if (ioctl(fd, HAMMER2IOC_PFS_LOOKUP, &pfs) < 0) {
24586993811STomohiro Kusumi enoents++;
24683d90983SMatthew Dillon continue;
24786993811STomohiro Kusumi }
24883d90983SMatthew Dillon if (use_fd >= 0) {
24983d90983SMatthew Dillon fprintf(stderr,
25083d90983SMatthew Dillon "hammer2: pfs_delete(%s): "
25183d90983SMatthew Dillon "Duplicate PFS name, "
25283d90983SMatthew Dillon "must specify mount\n",
25383d90983SMatthew Dillon av[i]);
25483d90983SMatthew Dillon close(use_fd);
25583d90983SMatthew Dillon use_fd = -1;
25683d90983SMatthew Dillon break;
25783d90983SMatthew Dillon }
25883d90983SMatthew Dillon use_fd = fd;
25983d90983SMatthew Dillon }
26083d90983SMatthew Dillon }
26183d90983SMatthew Dillon if (use_fd >= 0) {
26283d90983SMatthew Dillon if (ioctl(use_fd, HAMMER2IOC_PFS_DELETE, &pfs) < 0) {
26383d90983SMatthew Dillon printf("hammer2: pfs_delete(%s): %s\n",
26483d90983SMatthew Dillon av[i], strerror(errno));
26583d90983SMatthew Dillon ecode = 1;
26683d90983SMatthew Dillon } else {
26783d90983SMatthew Dillon printf("hammer2: pfs_delete(%s): SUCCESS\n",
26883d90983SMatthew Dillon av[i]);
26983d90983SMatthew Dillon }
27083d90983SMatthew Dillon close(use_fd);
27183d90983SMatthew Dillon } else {
27286993811STomohiro Kusumi if (enoents == nmnts)
27386993811STomohiro Kusumi printf("hammer2: pfs_delete(%s): %s not found\n",
27486993811STomohiro Kusumi av[i], av[i]);
27586993811STomohiro Kusumi else
27683d90983SMatthew Dillon printf("hammer2: pfs_delete(%s): FAILED\n",
27783d90983SMatthew Dillon av[i]);
278ae183399SMatthew Dillon ecode = 1;
279ae183399SMatthew Dillon }
28083d90983SMatthew Dillon }
28183d90983SMatthew Dillon if (mnts)
28283d90983SMatthew Dillon put_hammer2_mounts(nmnts, mnts);
283ae183399SMatthew Dillon return (ecode);
284ae183399SMatthew Dillon }
285