xref: /dflybsd-src/sbin/hammer/cmd_version.c (revision 052fd72bde74da31753d210bbd4499705366b377)
1de1c0b31SMatthew Dillon /*
2de1c0b31SMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3de1c0b31SMatthew Dillon  *
4de1c0b31SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5de1c0b31SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6de1c0b31SMatthew Dillon  *
7de1c0b31SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8de1c0b31SMatthew Dillon  * modification, are permitted provided that the following conditions
9de1c0b31SMatthew Dillon  * are met:
10de1c0b31SMatthew Dillon  *
11de1c0b31SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12de1c0b31SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13de1c0b31SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14de1c0b31SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15de1c0b31SMatthew Dillon  *    the documentation and/or other materials provided with the
16de1c0b31SMatthew Dillon  *    distribution.
17de1c0b31SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18de1c0b31SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19de1c0b31SMatthew Dillon  *    from this software without specific, prior written permission.
20de1c0b31SMatthew Dillon  *
21de1c0b31SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22de1c0b31SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23de1c0b31SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24de1c0b31SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25de1c0b31SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26de1c0b31SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27de1c0b31SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28de1c0b31SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29de1c0b31SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30de1c0b31SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31de1c0b31SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32de1c0b31SMatthew Dillon  * SUCH DAMAGE.
33de1c0b31SMatthew Dillon  *
3474812411SMatthew Dillon  * $DragonFly: src/sbin/hammer/cmd_version.c,v 1.2 2008/11/13 02:23:53 dillon Exp $
35de1c0b31SMatthew Dillon  */
36de1c0b31SMatthew Dillon /*
37de1c0b31SMatthew Dillon  * Get and set the HAMMER filesystem version.
38de1c0b31SMatthew Dillon  */
39de1c0b31SMatthew Dillon 
40de1c0b31SMatthew Dillon #include "hammer.h"
41de1c0b31SMatthew Dillon 
42de1c0b31SMatthew Dillon /*
43de1c0b31SMatthew Dillon  * version <filesystem>
44de1c0b31SMatthew Dillon  */
45de1c0b31SMatthew Dillon void
hammer_cmd_get_version(char ** av,int ac)46de1c0b31SMatthew Dillon hammer_cmd_get_version(char **av, int ac)
47de1c0b31SMatthew Dillon {
48de1c0b31SMatthew Dillon 	struct hammer_ioc_version version;
495d5031ccSAntonio Huete Jimenez 	char wip[16];
50de1c0b31SMatthew Dillon 	int fd;
51de1c0b31SMatthew Dillon 
52*052fd72bSTomohiro Kusumi 	if (ac != 1) {
53de1c0b31SMatthew Dillon 		errx(1, "hammer version - expected a single <fs> path arg");
54*052fd72bSTomohiro Kusumi 		/* not reached */
55*052fd72bSTomohiro Kusumi 	}
56de1c0b31SMatthew Dillon         fd = open(av[0], O_RDONLY);
57*052fd72bSTomohiro Kusumi 	if (fd < 0) {
5802318f07STomohiro Kusumi 		err(1, "hammer version: unable to access %s", av[0]);
59*052fd72bSTomohiro Kusumi 		/* not reached */
60*052fd72bSTomohiro Kusumi 	}
61de1c0b31SMatthew Dillon 
62de1c0b31SMatthew Dillon 	/*
63de1c0b31SMatthew Dillon 	 * version.cur_version must be set to 0 to retrieve current version
64de1c0b31SMatthew Dillon 	 * info.
65de1c0b31SMatthew Dillon 	 */
66de1c0b31SMatthew Dillon 	bzero(&version, sizeof(version));
67*052fd72bSTomohiro Kusumi 	if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) {
6802318f07STomohiro Kusumi 		err(1, "hammer version ioctl");
69*052fd72bSTomohiro Kusumi 		/* not reached */
70*052fd72bSTomohiro Kusumi 	}
714c09d9c4SMatthew Dillon 	HammerVersion = version.cur_version;
7202318f07STomohiro Kusumi 
735d5031ccSAntonio Huete Jimenez 	snprintf(wip, 16, "%d", version.wip_version);
745d5031ccSAntonio Huete Jimenez 	printf("min=%d wip=%s max=%d current=%d description=\"%s\"\n",
75de1c0b31SMatthew Dillon 		version.min_version,
765d5031ccSAntonio Huete Jimenez 	       (version.wip_version > version.max_version) ? "none" : wip,
77de1c0b31SMatthew Dillon 		version.max_version,
78de1c0b31SMatthew Dillon 		version.cur_version,
79de1c0b31SMatthew Dillon 		version.description
80de1c0b31SMatthew Dillon 	);
81de1c0b31SMatthew Dillon 
82de1c0b31SMatthew Dillon 	/*
83de1c0b31SMatthew Dillon 	 * Loop over available versions to display description.
84de1c0b31SMatthew Dillon 	 */
85de1c0b31SMatthew Dillon 	if (QuietOpt == 0) {
86de1c0b31SMatthew Dillon 		version.cur_version = 1;
87de1c0b31SMatthew Dillon 		printf("available versions:\n");
88de1c0b31SMatthew Dillon 		while (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0) {
89de1c0b31SMatthew Dillon 			printf("    %d\t%s\t%s\n",
90de1c0b31SMatthew Dillon 				version.cur_version,
91de1c0b31SMatthew Dillon 				(version.cur_version < version.wip_version ?
92de1c0b31SMatthew Dillon 					"NORM" : "WIP "),
93de1c0b31SMatthew Dillon 				version.description);
94de1c0b31SMatthew Dillon 			++version.cur_version;
95de1c0b31SMatthew Dillon 		}
96de1c0b31SMatthew Dillon 	}
97de1c0b31SMatthew Dillon 	close(fd);
98de1c0b31SMatthew Dillon }
99de1c0b31SMatthew Dillon 
100de1c0b31SMatthew Dillon /*
101f6532f03SThomas Nikolajsen  * version-upgrade <filesystem> <version> [force]
102de1c0b31SMatthew Dillon  */
103de1c0b31SMatthew Dillon void
hammer_cmd_set_version(char ** av,int ac)104de1c0b31SMatthew Dillon hammer_cmd_set_version(char **av, int ac)
105de1c0b31SMatthew Dillon {
106de1c0b31SMatthew Dillon 	struct hammer_ioc_version version;
107de1c0b31SMatthew Dillon 	int fd;
10883f2a3aaSMatthew Dillon 	int overs;
10983f2a3aaSMatthew Dillon 	int nvers;
110de1c0b31SMatthew Dillon 
111*052fd72bSTomohiro Kusumi 	if (ac < 2 || ac > 3 || (ac == 3 && strcmp(av[2], "force") != 0)) {
11202318f07STomohiro Kusumi 		errx(1, "hammer version-upgrade: expected <fs> vers# [force]");
113*052fd72bSTomohiro Kusumi 		/* not reached */
114*052fd72bSTomohiro Kusumi 	}
115de1c0b31SMatthew Dillon 
116de1c0b31SMatthew Dillon         fd = open(av[0], O_RDONLY);
117*052fd72bSTomohiro Kusumi 	if (fd < 0) {
11802318f07STomohiro Kusumi 		err(1, "hammer version-upgrade: unable to access %s", av[0]);
119*052fd72bSTomohiro Kusumi 		/* not reached */
120*052fd72bSTomohiro Kusumi 	}
121de1c0b31SMatthew Dillon 
122de1c0b31SMatthew Dillon 	bzero(&version, sizeof(version));
123*052fd72bSTomohiro Kusumi 	if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) {
12402318f07STomohiro Kusumi 		err(1, "hammer ioctl");
125*052fd72bSTomohiro Kusumi 		/* not reached */
126*052fd72bSTomohiro Kusumi 	}
1274c09d9c4SMatthew Dillon 	HammerVersion = version.cur_version;
12883f2a3aaSMatthew Dillon 	overs = version.cur_version;
12983f2a3aaSMatthew Dillon 
130de1c0b31SMatthew Dillon 	version.cur_version = strtol(av[1], NULL, 0);
13183f2a3aaSMatthew Dillon 	nvers = version.cur_version;
132de1c0b31SMatthew Dillon 
133*052fd72bSTomohiro Kusumi 	if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0) {
13402318f07STomohiro Kusumi 		err(1, "hammer ioctl");
135*052fd72bSTomohiro Kusumi 		/* not reached */
136*052fd72bSTomohiro Kusumi 	}
1374c09d9c4SMatthew Dillon 	HammerVersion = version.cur_version;
138*052fd72bSTomohiro Kusumi 	if (version.cur_version >= version.wip_version && ac != 3) {
13902318f07STomohiro Kusumi 		errx(1, "The requested version is a work-in-progress"
14002318f07STomohiro Kusumi 			" and requires the 'force' directive");
141*052fd72bSTomohiro Kusumi 		/* not reached */
142*052fd72bSTomohiro Kusumi 	}
14302318f07STomohiro Kusumi 
144*052fd72bSTomohiro Kusumi 	if (ioctl(fd, HAMMERIOC_SET_VERSION, &version) < 0) {
14502318f07STomohiro Kusumi 		err(1, "hammer version-upgrade ioctl");
146*052fd72bSTomohiro Kusumi 		/* not reached */
147*052fd72bSTomohiro Kusumi 	}
148*052fd72bSTomohiro Kusumi 	if (version.head.error) {
14902318f07STomohiro Kusumi 		errx(1, "hammer version-upgrade ioctl");
150*052fd72bSTomohiro Kusumi 		/* not reached */
151*052fd72bSTomohiro Kusumi 	}
15202318f07STomohiro Kusumi 
15383f2a3aaSMatthew Dillon 	printf("hammer version-upgrade: succeeded\n");
154f254e677STomohiro Kusumi 	if (overs < 3 && nvers >= 3) {
155f6532f03SThomas Nikolajsen 		printf("NOTE!  Please run 'hammer cleanup' to convert the\n"
156f6532f03SThomas Nikolajsen 		       "<pfs>/snapshots directories to the new meta-data\n"
15783f2a3aaSMatthew Dillon 		       "format.  Once converted configuration data will\n"
158f6532f03SThomas Nikolajsen 		       "no longer resides in <pfs>/snapshots and you can\n"
15983f2a3aaSMatthew Dillon 		       "even rm -rf it entirely if you want.\n");
160f254e677STomohiro Kusumi 	}
16183f2a3aaSMatthew Dillon 
162de1c0b31SMatthew Dillon 	close(fd);
163de1c0b31SMatthew Dillon }
164de1c0b31SMatthew Dillon 
165