xref: /dragonfly/sbin/hammer/cmd_pfs.c (revision 02318f07)
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sbin/hammer/cmd_pseudofs.c,v 1.12 2008/10/08 21:01:54 thomas Exp $
35  */
36 
37 #include <libgen.h>
38 
39 #include "hammer.h"
40 
41 static int scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path);
42 static void parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd);
43 static void init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave);
44 static void pseudofs_usage(int code);
45 static int timetosecs(char *str);
46 
47 void
48 clrpfs(struct hammer_ioc_pseudofs_rw *pfs, hammer_pseudofs_data_t pfsd,
49 	int pfs_id)
50 {
51 	bzero(pfs, sizeof(*pfs));
52 
53 	if (pfsd)
54 		pfs->ondisk = pfsd;
55 	else
56 		pfs->ondisk = malloc(sizeof(*pfs->ondisk));
57 	bzero(pfs->ondisk, sizeof(*pfs->ondisk));
58 
59 	pfs->pfs_id = pfs_id;
60 	pfs->bytes = sizeof(*pfs->ondisk);
61 	pfs->version = HAMMER_IOC_PSEUDOFS_VERSION;
62 }
63 
64 /*
65  * If path is a symlink, return strdup'd path.
66  * If it's a directory via symlink, strip trailing /
67  * from strdup'd path and return the symlink.
68  */
69 static char*
70 getlink(const char *path)
71 {
72 	int i;
73 	char *linkpath;
74 	struct stat st;
75 
76 	if (lstat(path, &st))
77 		return(NULL);
78 	linkpath = strdup(path);
79 
80 	if (S_ISDIR(st.st_mode)) {
81 		i = strlen(linkpath) - 1;
82 		while (i > 0 && linkpath[i] == '/')
83 			linkpath[i--] = 0;
84 		lstat(linkpath, &st);
85 	}
86 	if (S_ISLNK(st.st_mode)) {
87 		return(linkpath);
88 	}
89 
90 	free(linkpath);
91 	return(NULL);
92 }
93 
94 /*
95  * Calculate the PFS id given a path to a file/directory or
96  * a @@%llx:%d softlink.
97  */
98 int
99 getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
100 {
101 	int fd;
102 
103 	clrpfs(pfs, NULL, -1);
104 
105 	/*
106 	 * Extract the PFS id.
107 	 * dirname(path) is supposed to be a directory in PFS#0.
108 	 */
109 	if (scanpfsid(pfs, path) == 0) {
110 		path = dirname(path); /* strips trailing / first if any */
111 	}
112 
113 	/*
114 	 * Open the path regardless of scanpfsid() result, since some
115 	 * commands can take a regular file/directory (e.g. pfs-status).
116 	 */
117 	fd = open(path, O_RDONLY);
118 	if (fd < 0)
119 		err(1, "Failed to open %s", path);
120 
121 	/*
122 	 * If pfs.pfs_id has been set to non -1, the file descriptor fd
123 	 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
124 	 * doesn't depend on inode attributes if it's set to a valid id.
125 	 */
126 	if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0)
127 		err(1, "Cannot access %s", path);
128 
129 	return(fd);
130 }
131 
132 /*
133  * Extract the PFS id from path.
134  */
135 static int
136 scanpfsid(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
137 {
138 	char *linkpath;
139 	char buf[64];
140 	uintmax_t dummy_tid;
141 	struct stat st;
142 
143 	if (stat(path, &st)) {
144 		/* possibly slave PFS */
145 	} else if (S_ISDIR(st.st_mode)) {
146 		/* possibly master or slave PFS */
147 	} else {
148 		return(-1);  /* neither */
149 	}
150 
151 	linkpath = getlink(path);
152 	if (linkpath) {
153 		/*
154 		 * Read the symlink assuming it's a link to PFS.
155 		 */
156 		bzero(buf, sizeof(buf));
157 		if (readlink(linkpath, buf, sizeof(buf) - 1) < 0) {
158 			free(linkpath);
159 			return(-1);
160 		}
161 		free(linkpath);
162 		path = buf;
163 	}
164 
165 	/*
166 	 * The symlink created by pfs-master|slave is just a symlink.
167 	 * One could happen to remove a symlink and relink PFS as
168 	 * # ln -s ./@@-1:00001 ./link
169 	 * which results PFS having something extra before @@.
170 	 * One could also directly use the PFS and results the same.
171 	 * Get rid of it before we extract the PFS id.
172 	 */
173 	if (strchr(path, '/')) {
174 		path = basename(path); /* strips trailing / first if any */
175 		if (path == NULL)
176 			err(1, "basename");
177 	}
178 
179 	/*
180 	 * Test and extract the PFS id from the link.
181 	 * "@@%jx:%d" covers both "@@-1:%05d" format for master PFS
182 	 * and "@@0x%016jx:%05d" format for slave PFS.
183 	 */
184 	if (sscanf(path, "@@%jx:%d", &dummy_tid, &pfs->pfs_id) == 2) {
185 		assert(pfs->pfs_id > 0);
186 		return(0);
187 	}
188 
189 	return(-1);
190 }
191 
192 void
193 relpfs(int fd, struct hammer_ioc_pseudofs_rw *pfs)
194 {
195 	if (fd >= 0)
196 		close(fd);
197 	if (pfs->ondisk) {
198 		free(pfs->ondisk);
199 		pfs->ondisk = NULL;
200 	}
201 }
202 
203 static void
204 print_pfs_status(char *path)
205 {
206 	struct hammer_ioc_pseudofs_rw pfs;
207 	int fd;
208 
209 	fd = getpfs(&pfs, path);
210 	printf("%s\t", path);
211 	if (fd < 0 || ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
212 		printf("Invalid PFS path %s\n", path);
213 	} else {
214 		printf("PFS#%d {\n", pfs.pfs_id);
215 		dump_pfsd(pfs.ondisk, fd);
216 		printf("}\n");
217 	}
218 	if (fd >= 0)
219 		close(fd);
220 	if (pfs.ondisk)
221 		free(pfs.ondisk);
222 	relpfs(fd, &pfs);
223 }
224 
225 void
226 hammer_cmd_pseudofs_status(char **av, int ac)
227 {
228 	int i;
229 
230 	if (ac == 0) {
231 		char buf[2] = "."; /* can't be readonly string */
232 		print_pfs_status(buf);
233 		return;
234 	}
235 
236 	for (i = 0; i < ac; ++i)
237 		print_pfs_status(av[i]);
238 }
239 
240 void
241 hammer_cmd_pseudofs_create(char **av, int ac, int is_slave)
242 {
243 	struct hammer_ioc_pseudofs_rw pfs;
244 	struct hammer_pseudofs_data pfsd;
245 	struct stat st;
246 	const char *path;
247 	char *dirpath;
248 	char *linkpath;
249 	int pfs_id;
250 	int fd;
251 
252 	if (ac == 0)
253 		pseudofs_usage(1);
254 	path = av[0];
255 	if (lstat(path, &st) == 0) {
256 		errx(1, "Cannot create %s, file exists!", path);
257 	} else if (path[strlen(path) - 1] == '/') {
258 		errx(1, "Invalid PFS path %s with trailing /", path);
259 	}
260 
261 	/*
262 	 * Figure out the directory prefix, taking care of degenerate
263 	 * cases.
264 	 */
265 	dirpath = dirname(path);
266 	fd = open(dirpath, O_RDONLY);
267 	if (fd < 0)
268 		err(1, "Cannot open directory %s", dirpath);
269 
270 	/*
271 	 * Avoid foot-shooting.  Don't let the user create a PFS
272 	 * softlink via a PFS.  PFS softlinks may only be accessed
273 	 * via the master filesystem.  Checking it here ensures
274 	 * other PFS commands access PFS under the master filesystem.
275 	 */
276 	clrpfs(&pfs, &pfsd, -1);
277 
278 	ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs);
279 	if (pfs.pfs_id != HAMMER_ROOT_PFSID) {
280 		fprintf(stderr,
281 			"You are attempting to access a PFS softlink "
282 			"from a PFS.  It may not represent the PFS\n"
283 			"on the main filesystem mount that you "
284 			"expect!  You may only access PFS softlinks\n"
285 			"via the main filesystem mount!\n");
286 		exit(1);
287 	}
288 
289 	for (pfs_id = 0; pfs_id < HAMMER_MAX_PFS; ++pfs_id) {
290 		clrpfs(&pfs, &pfsd, pfs_id);
291 		if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
292 			if (errno != ENOENT)
293 				err(1, "Cannot create %s", path);
294 			break;
295 		}
296 	}
297 	if (pfs_id == HAMMER_MAX_PFS) {
298 		errx(1, "Cannot create %s, all PFSs in use", path);
299 	} else if (pfs_id == HAMMER_ROOT_PFSID) {
300 		errx(1, "Fatal error: PFS#%d must exist", HAMMER_ROOT_PFSID);
301 	}
302 
303 	/*
304 	 * Create the new PFS
305 	 */
306 	printf("Creating PFS#%d\t", pfs_id);
307 	clrpfs(&pfs, &pfsd, pfs_id);
308 	init_pfsd(&pfsd, is_slave);
309 
310 	if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) < 0) {
311 		printf("failed: %s\n", strerror(errno));
312 	} else {
313 		/* special symlink, must be exactly 10 characters */
314 		asprintf(&linkpath, "@@PFS%05d", pfs_id);
315 		if (symlink(linkpath, path) < 0) {
316 			printf("failed: cannot create symlink: %s\n",
317 				strerror(errno));
318 		} else {
319 			printf("succeeded!\n");
320 			hammer_cmd_pseudofs_update(av, ac);
321 		}
322 	}
323 	free(dirpath);
324 	close(fd);
325 }
326 
327 void
328 hammer_cmd_pseudofs_destroy(char **av, int ac)
329 {
330 	struct hammer_ioc_pseudofs_rw pfs;
331 	char *linkpath;
332 	int fd;
333 	int i;
334 
335 	if (ac == 0)
336 		pseudofs_usage(1);
337 	fd = getpfs(&pfs, av[0]);
338 
339 	if (pfs.pfs_id == HAMMER_ROOT_PFSID)
340 		errx(1, "You cannot destroy PFS#0");
341 
342 	printf("You have requested that PFS#%d (%s) be destroyed\n",
343 		pfs.pfs_id, pfs.ondisk->label);
344 	printf("This will irrevocably destroy all data on this PFS!!!!!\n");
345 	printf("Do you really want to do this? [y/n] ");
346 	fflush(stdout);
347 	if (getyn() == 0)
348 		errx(1, "No action taken on PFS#%d", pfs.pfs_id);
349 
350 	if (hammer_is_pfs_master(pfs.ondisk)) {
351 		printf("This PFS is currently setup as a MASTER!\n");
352 		printf("Are you absolutely sure you want to destroy it? [y/n] ");
353 		fflush(stdout);
354 		if (getyn() == 0)
355 			errx(1, "No action taken on PFS#%d", pfs.pfs_id);
356 	}
357 
358 	printf("Destroying PFS#%d (%s)", pfs.pfs_id, pfs.ondisk->label);
359 	if (DebugOpt) {
360 		printf("\n");
361 	} else {
362 		printf(" in");
363 		for (i = 5; i; --i) {
364 			printf(" %d", i);
365 			fflush(stdout);
366 			sleep(1);
367 		}
368 		printf(".. starting destruction pass\n");
369 	}
370 
371 	/*
372 	 * Remove the softlink on success.
373 	 */
374 	if (ioctl(fd, HAMMERIOC_RMR_PSEUDOFS, &pfs) == 0) {
375 		printf("pfs-destroy of PFS#%d succeeded!\n", pfs.pfs_id);
376 		linkpath = getlink(av[0]);
377 		if (linkpath) {
378 			if (remove(linkpath) < 0) {
379 				fprintf(stderr,
380 					"Unable to remove softlink %s: %s\n",
381 					linkpath, strerror(errno));
382 					/* exit status 0 anyway */
383 			}
384 			free(linkpath);
385 		}
386 	} else {
387 		printf("pfs-destroy of PFS#%d failed: %s\n",
388 			pfs.pfs_id, strerror(errno));
389 	}
390 	relpfs(fd, &pfs);
391 }
392 
393 void
394 hammer_cmd_pseudofs_upgrade(char **av, int ac)
395 {
396 	struct hammer_ioc_pseudofs_rw pfs;
397 	int fd;
398 
399 	if (ac == 0)
400 		pseudofs_usage(1);
401 	fd = getpfs(&pfs, av[0]);
402 
403 	if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
404 		errx(1, "You cannot upgrade PFS#0"
405 			" (It should already be a master)");
406 	} else if (hammer_is_pfs_master(pfs.ondisk)) {
407 		errx(1, "It is already a master");
408 	}
409 
410 	if (ioctl(fd, HAMMERIOC_UPG_PSEUDOFS, &pfs) == 0) {
411 		printf("pfs-upgrade of PFS#%d (%s) succeeded\n",
412 			pfs.pfs_id, pfs.ondisk->label);
413 	} else {
414 		fprintf(stderr, "pfs-upgrade of PFS#%d (%s) failed: %s\n",
415 			pfs.pfs_id, pfs.ondisk->label, strerror(errno));
416 	}
417 	relpfs(fd, &pfs);
418 }
419 
420 void
421 hammer_cmd_pseudofs_downgrade(char **av, int ac)
422 {
423 	struct hammer_ioc_pseudofs_rw pfs;
424 	int fd;
425 
426 	if (ac == 0)
427 		pseudofs_usage(1);
428 	fd = getpfs(&pfs, av[0]);
429 
430 	if (pfs.pfs_id == HAMMER_ROOT_PFSID) {
431 		errx(1, "You cannot downgrade PFS#0");
432 	} else if (hammer_is_pfs_slave(pfs.ondisk)) {
433 		errx(1, "It is already a slave");
434 	}
435 
436 	if (ioctl(fd, HAMMERIOC_DGD_PSEUDOFS, &pfs) == 0) {
437 		printf("pfs-downgrade of PFS#%d (%s) succeeded\n",
438 			pfs.pfs_id, pfs.ondisk->label);
439 	} else {
440 		fprintf(stderr, "pfs-downgrade of PFS#%d (%s) failed: %s\n",
441 			pfs.pfs_id, pfs.ondisk->label, strerror(errno));
442 	}
443 	relpfs(fd, &pfs);
444 }
445 
446 void
447 hammer_cmd_pseudofs_update(char **av, int ac)
448 {
449 	struct hammer_ioc_pseudofs_rw pfs;
450 	int fd;
451 
452 	if (ac == 0)
453 		pseudofs_usage(1);
454 	fd = getpfs(&pfs, av[0]);
455 
456 	printf("%s\n", av[0]);
457 	fflush(stdout);
458 
459 	if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
460 		parse_pfsd_options(av + 1, ac - 1, pfs.ondisk);
461 		if (hammer_is_pfs_slave(pfs.ondisk) &&
462 		    pfs.pfs_id == HAMMER_ROOT_PFSID) {
463 			errx(1, "The real mount point cannot be made a PFS "
464 			       "slave, only PFS sub-directories can be made "
465 			       "slaves");
466 		}
467 		pfs.bytes = sizeof(*pfs.ondisk);
468 		if (ioctl(fd, HAMMERIOC_SET_PSEUDOFS, &pfs) == 0) {
469 			if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) == 0) {
470 				dump_pfsd(pfs.ondisk, fd);
471 			} else {
472 				err(1, "Unable to retrieve PFS configuration "
473 					"after successful update");
474 			}
475 		} else {
476 			err(1, "Unable to adjust PFS configuration");
477 		}
478 	}
479 	relpfs(fd, &pfs);
480 }
481 
482 static void
483 init_pfsd(hammer_pseudofs_data_t pfsd, int is_slave)
484 {
485 	uint32_t status;
486 
487 	bzero(pfsd, sizeof(*pfsd));
488 	pfsd->sync_beg_tid = 1;
489 	pfsd->sync_end_tid = 1;
490 	pfsd->sync_beg_ts = 0;
491 	pfsd->sync_end_ts = 0;
492 	uuid_create(&pfsd->shared_uuid, &status);
493 	uuid_create(&pfsd->unique_uuid, &status);
494 	if (is_slave)
495 		pfsd->mirror_flags |= HAMMER_PFSD_SLAVE;
496 }
497 
498 void
499 dump_pfsd(hammer_pseudofs_data_t pfsd, int fd)
500 {
501 	struct hammer_ioc_version	version;
502 	uint32_t status;
503 	char *str = NULL;
504 
505 	printf("    sync-beg-tid=0x%016jx\n", (uintmax_t)pfsd->sync_beg_tid);
506 	printf("    sync-end-tid=0x%016jx\n", (uintmax_t)pfsd->sync_end_tid);
507 	uuid_to_string(&pfsd->shared_uuid, &str, &status);
508 	printf("    shared-uuid=%s\n", str);
509 	free(str);
510 	uuid_to_string(&pfsd->unique_uuid, &str, &status);
511 	printf("    unique-uuid=%s\n", str);
512 	free(str);
513 	printf("    label=\"%s\"\n", pfsd->label);
514 	if (pfsd->snapshots[0])
515 		printf("    snapshots=\"%s\"\n", pfsd->snapshots);
516 	if (pfsd->prune_min < (60 * 60 * 24)) {
517 		printf("    prune-min=%02d:%02d:%02d\n",
518 			pfsd->prune_min / 60 / 60 % 24,
519 			pfsd->prune_min / 60 % 60,
520 			pfsd->prune_min % 60);
521 	} else if (pfsd->prune_min % (60 * 60 * 24)) {
522 		printf("    prune-min=%dd/%02d:%02d:%02d\n",
523 			pfsd->prune_min / 60 / 60 / 24,
524 			pfsd->prune_min / 60 / 60 % 24,
525 			pfsd->prune_min / 60 % 60,
526 			pfsd->prune_min % 60);
527 	} else {
528 		printf("    prune-min=%dd\n", pfsd->prune_min / 60 / 60 / 24);
529 	}
530 
531 	if (hammer_is_pfs_slave(pfsd)) {
532 		printf("    operating as a SLAVE\n");
533 	} else {
534 		printf("    operating as a MASTER\n");
535 	}
536 
537 	/*
538 	 * Snapshots directory cannot be shown when there is no fd since
539 	 * hammer version can't be retrieved. mirror-dump passes -1 because
540 	 * its input came from mirror-read output thus no path is available
541 	 * to open(2).
542 	 */
543 	if (fd >= 0 && pfsd->snapshots[0] == 0) {
544 		bzero(&version, sizeof(version));
545 		if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
546 			return;
547 		if (version.cur_version < 3) {
548 			if (hammer_is_pfs_slave(pfsd)) {
549 				printf("    snapshots directory not set for "
550 				       "slave\n");
551 			} else {
552 				printf("    snapshots directory for master "
553 				       "defaults to <pfs>/snapshots\n");
554 			}
555 		} else {
556 			printf("    snapshots directory defaults to "
557 			       "/var/hammer/<pfs>\n");
558 		}
559 	}
560 }
561 
562 static void
563 parse_pfsd_options(char **av, int ac, hammer_pseudofs_data_t pfsd)
564 {
565 	char *cmd;
566 	char *ptr;
567 	int len;
568 	uint32_t status;
569 
570 	while (ac) {
571 		cmd = *av;
572 		if ((ptr = strchr(cmd, '=')) != NULL)
573 			*ptr++ = 0;
574 
575 		/*
576 		 * Basic assignment value test
577 		 */
578 		if (ptr == NULL)
579 			errx(1, "option %s requires an assignment", cmd);
580 
581 		status = uuid_s_ok;
582 		if (strcmp(cmd, "sync-beg-tid") == 0) {
583 			pfsd->sync_beg_tid = strtoull(ptr, NULL, 16);
584 		} else if (strcmp(cmd, "sync-end-tid") == 0) {
585 			pfsd->sync_end_tid = strtoull(ptr, NULL, 16);
586 		} else if (strcmp(cmd, "shared-uuid") == 0) {
587 			uuid_from_string(ptr, &pfsd->shared_uuid, &status);
588 		} else if (strcmp(cmd, "unique-uuid") == 0) {
589 			uuid_from_string(ptr, &pfsd->unique_uuid, &status);
590 		} else if (strcmp(cmd, "label") == 0) {
591 			len = strlen(ptr);
592 			if (ptr[0] == '"' && ptr[len-1] == '"') {
593 				ptr[len-1] = 0;
594 				++ptr;
595 			} else if (ptr[0] == '"') {
596 				errx(1, "option %s: malformed string", cmd);
597 			}
598 			snprintf(pfsd->label, sizeof(pfsd->label), "%s", ptr);
599 		} else if (strcmp(cmd, "snapshots") == 0) {
600 			len = strlen(ptr);
601 			if (ptr[0] != '/') {
602 				fprintf(stderr,
603 					"option %s: '%s' must be an "
604 					"absolute path\n", cmd, ptr);
605 				if (ptr[0] == 0) {
606 					fprintf(stderr,
607 						"use 'snapshots-clear' "
608 						"to unset snapshots dir\n");
609 				}
610 				exit(1);
611 			}
612 			if (len >= (int)sizeof(pfsd->snapshots)) {
613 				errx(1, "option %s: path too long, %d "
614 					"character limit", cmd, len);
615 			}
616 			snprintf(pfsd->snapshots, sizeof(pfsd->snapshots),
617 				 "%s", ptr);
618 		} else if (strcmp(cmd, "snapshots-clear") == 0) {
619 			pfsd->snapshots[0] = 0;
620 		} else if (strcmp(cmd, "prune-min") == 0) {
621 			pfsd->prune_min = timetosecs(ptr);
622 			if (pfsd->prune_min < 0) {
623 				errx(1, "option %s: illegal time spec, "
624 					"use Nd or [Nd/]hh[:mm[:ss]]", ptr);
625 			}
626 		} else {
627 			errx(1, "invalid option: %s", cmd);
628 		}
629 		if (status != uuid_s_ok) {
630 			errx(1, "option %s: error parsing uuid %s", cmd, ptr);
631 		}
632 		--ac;
633 		++av;
634 	}
635 }
636 
637 static
638 void
639 pseudofs_usage(int code)
640 {
641 	fprintf(stderr,
642 		"hammer pfs-status <dirpath> ...\n"
643 		"hammer pfs-master <dirpath> [options]\n"
644 		"hammer pfs-slave <dirpath> [options]\n"
645 		"hammer pfs-update <dirpath> [options]\n"
646 		"hammer pfs-upgrade <dirpath>\n"
647 		"hammer pfs-downgrade <dirpath>\n"
648 		"hammer pfs-destroy <dirpath>\n"
649 		"\n"
650 		"options:\n"
651 		"    sync-beg-tid=0x16llx\n"
652 		"    sync-end-tid=0x16llx\n"
653 		"    shared-uuid=0x16llx\n"
654 		"    unique-uuid=0x16llx\n"
655 		"    label=\"string\"\n"
656 		"    snapshots=\"/path\"\n"
657 		"    snapshots-clear\n"
658 		"    prune-min=Nd\n"
659 		"    prune-min=[Nd/]hh[:mm[:ss]]\n"
660 	);
661 	exit(code);
662 }
663 
664 /*
665  * Convert time in the form [Nd/]hh[:mm[:ss]] to seconds.
666  *
667  * Return -1 if a parse error occurs.
668  * Return 0x7FFFFFFF if the time exceeds the maximum allowed.
669  */
670 static
671 int
672 timetosecs(char *str)
673 {
674 	int days = 0;
675 	int hrs = 0;
676 	int mins = 0;
677 	int secs = 0;
678 	int n;
679 	long long v;
680 	char *ptr;
681 
682 	n = strtol(str, &ptr, 10);
683 	if (n < 0)
684 		return(-1);
685 	if (*ptr == 'd') {
686 		days = n;
687 		++ptr;
688 		if (*ptr == '/')
689 		    n = strtol(ptr + 1, &ptr, 10);
690 		else
691 		    n = 0;
692 	}
693 	if (n < 0)
694 		return(-1);
695 	hrs = n;
696 	if (*ptr == ':') {
697 		n = strtol(ptr + 1, &ptr, 10);
698 		if (n < 0)
699 			return(-1);
700 		mins = n;
701 		if (*ptr == ':') {
702 			n = strtol(ptr + 1, &ptr, 10);
703 			if (n < 0)
704 				return(-1);
705 			secs = n;
706 		}
707 	}
708 	if (*ptr)
709 		return(-1);
710 	v = days * 24 * 60 * 60 + hrs *  60 * 60 + mins * 60 + secs;
711 	if (v > 0x7FFFFFFF)
712 		v = 0x7FFFFFFF;
713 	return((int)v);
714 }
715