2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.5 2008/07/09 10:32:30 dillon Exp $
39 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
40 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
41 static void dump_pfsd(hammer_pseudofs_data_t pfsd);
42 static void pseudofs_usage(int code);
45 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
49 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
51 hammer_tid_t dummy_tid;
58 bzero(pfs, sizeof(*pfs));
59 pfs->ondisk = malloc(sizeof(*pfs->ondisk));
60 bzero(pfs->ondisk, sizeof(*pfs->ondisk));
61 pfs->bytes = sizeof(*pfs->ondisk);
64 * Calculate the directory containing the softlink
66 dirpath = strdup(path);
67 if (strrchr(dirpath, '/'))
68 *strrchr(dirpath, '/') = 0;
70 dirpath = strdup(".");
72 if (lstat(path, &st) == 0 && S_ISLNK(st.st_mode)) {
73 n = readlink(path, buf, sizeof(buf) - 1);
77 if (sscanf(buf, "@@PFS%d", &pfs->pfs_id) == 1) {
78 fd = open(dirpath, O_RDONLY);
81 if (sscanf(buf, "@@%llx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
82 fd = open(dirpath, O_RDONLY);
88 * Try to open the path and request the pfs_id that way.
90 fd = open(path, O_RDONLY);
93 ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
94 if (pfs->pfs_id == -1) {
105 fprintf(stderr, "Cannot access PFS %s: %s\n",
106 path, strerror(errno));
109 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
110 fprintf(stderr, "Cannot access PFS %s: %s\n",
111 path, strerror(errno));
119 hammer_cmd_pseudofs_status(char **av, int ac)
121 struct hammer_ioc_pseudofs_rw pfs;
125 for (i = 0; i < ac; ++i) {
126 printf("%s\t", av[i]);
127 fd = getpfs(&pfs, av[i]);
128 if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
129 printf("Not a HAMMER root\n");
131 printf("PFS #%d {\n", pfs.pfs_id);
132 dump_pfsd(pfs.ondisk);
143 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
145 struct hammer_ioc_pseudofs_rw pfs;
146 struct hammer_pseudofs_data pfsd;
158 if (lstat(path, &st) == 0) {
159 fprintf(stderr, "cannot create %s, file exists!\n", path);
163 dirpath = strdup(path);
164 if (strrchr(dirpath, '/') != NULL)
165 *strrchr(dirpath, '/') = 0;
167 dirpath = strdup(".");
168 fd = open(dirpath, O_RDONLY);
170 fprintf(stderr, "Cannot open directory %s\n", dirpath);
175 for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
176 bzero(&pfs, sizeof(pfs));
177 bzero(&pfsd, sizeof(pfsd));
180 pfs.bytes = sizeof(pfsd);
181 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
182 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
187 if (pfs_id == HAMMER_MAX_PFS) {
188 fprintf(stderr, "Cannot create %s, all PFSs in use\n", path);
191 if (error != ENOENT) {
192 fprintf(stderr, "Cannot create %s, got %s during scan\n",
193 path, strerror(error));
200 printf("Creating PFS #%d\t", pfs_id);
201 bzero(&pfsd, sizeof(pfsd));
202 init_pfsd(&pfsd, is_slave);
205 pfs.bytes = sizeof(pfsd);
206 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
208 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
209 printf("failed: %s\n", strerror(errno));
211 /* special symlink, must be exactly 10 characters */
212 asprintf(&linkpath, "@@PFS%05d", pfs_id);
213 if (symlink(linkpath, path) < 0) {
214 printf("failed: cannot create symlink: %s\n",
217 printf("succeeded!\n");
218 hammer_cmd_pseudofs_update(av, ac);
225 hammer_cmd_pseudofs_destroy(char **av, int ac)
227 fprintf(stderr, "pfs-destroy not implemented yet\n");
231 hammer_cmd_pseudofs_update(char **av, int ac)
233 struct hammer_ioc_pseudofs_rw pfs;
238 bzero(&pfs, sizeof(pfs));
239 fd = getpfs(&pfs, av[0]);
241 printf("%s\n", av[0]);
244 if (fd >= 0 && ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
245 parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
246 if ((pfs.ondisk->mirror_flags & HAMMER_PFSD_SLAVE) &&
248 fprintf(stderr, "The real mount point cannot be made a PFS slave, only PFS sub-directories can be made slaves\n");
251 pfs.bytes = sizeof(*pfs.ondisk);
252 if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
253 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
254 dump_pfsd(pfs.ondisk);
256 printf("Unable to retrieve pfs configuration after successful update: %s\n", strerror(errno));
260 printf("Unable to adjust pfs configuration: %s\n", strerror(errno));
264 printf("PFS Creation failed: %s\n", strerror(errno));
270 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
274 pfsd->sync_beg_tid = 1;
275 pfsd->sync_end_tid = 1;
276 pfsd->sync_beg_ts = 0;
277 pfsd->sync_end_ts = 0;
278 uuid_create(&pfsd->shared_uuid, &status);
279 uuid_create(&pfsd->unique_uuid, &status);
281 pfsd->master_id = -1;
282 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
290 dump_pfsd(hammer_pseudofs_data_t pfsd)
295 printf(" sync-beg-tid=0x%016llx\n", pfsd->sync_beg_tid);
296 printf(" sync-end-tid=0x%016llx\n", pfsd->sync_end_tid);
297 uuid_to_string(&pfsd->shared_uuid, &str, &status);
298 printf(" shared-uuid=%s\n", str);
299 uuid_to_string(&pfsd->unique_uuid, &str, &status);
300 printf(" unique-uuid=%s\n", str);
301 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
303 } else if (pfsd->master_id < 0) {
304 printf(" no-mirror\n");
306 printf(" master=%d\n", pfsd->master_id);
308 printf(" label=\"%s\"\n", pfsd->label);
312 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
321 if ((ptr = strchr(cmd, '=')) != NULL)
325 * Basic assignment value test
327 if (strcmp(cmd, "no-mirror") == 0 ||
328 strcmp(cmd, "slave") == 0) {
331 "option %s should not have "
339 "option %s requires an assignment\n",
346 if (strcmp(cmd, "sync-beg-tid") == 0) {
347 pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
348 } else if (strcmp(cmd, "sync-end-tid") == 0) {
349 pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
350 } else if (strcmp(cmd, "shared-uuid") == 0) {
351 uuid_from_string(ptr, &pfsd->shared_uuid, &status);
352 } else if (strcmp(cmd, "unique-uuid") == 0) {
353 uuid_from_string(ptr, &pfsd->unique_uuid, &status);
354 } else if (strcmp(cmd, "master") == 0) {
355 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
356 fprintf(stderr, "master mode cannot be set on a slave PFS\n");
359 pfsd->master_id = strtol(ptr, NULL, 0);
360 pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
361 } else if (strcmp(cmd, "slave") == 0) {
362 if ((pfsd->mirror_flags & HAMMER_PFSD_SLAVE) == 0) {
363 fprintf(stderr, "slave mode cannot be set on a master PFS\n");
366 pfsd->master_id = -1;
367 pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
368 } else if (strcmp(cmd, "no-mirror") == 0) {
369 if (pfsd->mirror_flags & HAMMER_PFSD_SLAVE) {
370 fprintf(stderr, "no-mirror master mode cannot be set on a slave PFS\n");
373 pfsd->master_id = -1;
374 pfsd->mirror_flags &= ~HAMMER_PFSD_SLAVE;
375 } else if (strcmp(cmd, "label") == 0) {
377 if (ptr[0] == '"' && ptr[len-1] == '"') {
380 } else if (ptr[0] == '"') {
382 "option %s: malformed string\n",
386 snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
388 fprintf(stderr, "invalid option: %s\n", cmd);
391 if (status != uuid_s_ok) {
392 fprintf(stderr, "option %s: error parsing uuid %s\n",
403 pseudofs_usage(int code)
406 "hammer pfs-status <dirpath1>...<dirpathN>\n"
407 "hammer pfs-master <dirpath> [options]\n"
408 "hammer pfs-slave <dirpath> [options]\n"
409 "hammer pfs-destroy <dirpath>\n"
410 "hammer pfs-update <dirpath> [options]\n"
412 " sync-beg-tid=0x16llx\n"
413 " sync-end-tid=0x16llx\n"
414 " shared-uuid=0x16llx\n"
415 " unique-uuid=0x16llx\n"
419 " label=\"string\"\n"