16b669ab4SMichael Neumann /*
26b669ab4SMichael Neumann * Copyright (c) 2008 The DragonFly Project. All rights reserved.
36b669ab4SMichael Neumann *
46b669ab4SMichael Neumann * This code is derived from software contributed to The DragonFly Project
56b669ab4SMichael Neumann * by Matthew Dillon <dillon@backplane.com>
66b669ab4SMichael Neumann *
76b669ab4SMichael Neumann * Redistribution and use in source and binary forms, with or without
86b669ab4SMichael Neumann * modification, are permitted provided that the following conditions
96b669ab4SMichael Neumann * are met:
106b669ab4SMichael Neumann *
116b669ab4SMichael Neumann * 1. Redistributions of source code must retain the above copyright
126b669ab4SMichael Neumann * notice, this list of conditions and the following disclaimer.
136b669ab4SMichael Neumann * 2. Redistributions in binary form must reproduce the above copyright
146b669ab4SMichael Neumann * notice, this list of conditions and the following disclaimer in
156b669ab4SMichael Neumann * the documentation and/or other materials provided with the
166b669ab4SMichael Neumann * distribution.
176b669ab4SMichael Neumann * 3. Neither the name of The DragonFly Project nor the names of its
186b669ab4SMichael Neumann * contributors may be used to endorse or promote products derived
196b669ab4SMichael Neumann * from this software without specific, prior written permission.
206b669ab4SMichael Neumann *
216b669ab4SMichael Neumann * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
226b669ab4SMichael Neumann * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
236b669ab4SMichael Neumann * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
246b669ab4SMichael Neumann * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
256b669ab4SMichael Neumann * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
266b669ab4SMichael Neumann * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
276b669ab4SMichael Neumann * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
286b669ab4SMichael Neumann * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
296b669ab4SMichael Neumann * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
306b669ab4SMichael Neumann * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
316b669ab4SMichael Neumann * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326b669ab4SMichael Neumann * SUCH DAMAGE.
336b669ab4SMichael Neumann *
34e0af86f0SMichael Neumann * $DragonFly: src/sbin/hammer/cmd_snapshot.c,v 1.7 2008/07/10 18:47:22 mneumann Exp $
356b669ab4SMichael Neumann */
366b669ab4SMichael Neumann
37b45803e3STomohiro Kusumi #include "hammer.h"
38b45803e3STomohiro Kusumi
3996e6b862SMichael Neumann #define DEFAULT_SNAPSHOT_NAME "snap-%Y%m%d-%H%M"
4096e6b862SMichael Neumann
416b669ab4SMichael Neumann static void snapshot_usage(int exit_code);
4283f2a3aaSMatthew Dillon static void snapshot_add(int fd, const char *fsym, const char *tsym,
4383f2a3aaSMatthew Dillon const char *label, hammer_tid_t tid);
4483f2a3aaSMatthew Dillon static void snapshot_ls(const char *path);
4583f2a3aaSMatthew Dillon static void snapshot_del(int fsfd, hammer_tid_t tid);
4683f2a3aaSMatthew Dillon static char *dirpart(const char *path);
4783f2a3aaSMatthew Dillon
4883f2a3aaSMatthew Dillon /*
4916265794SThomas Nikolajsen * hammer snap <path> [<note>]
5083f2a3aaSMatthew Dillon *
5116265794SThomas Nikolajsen * Path may be a directory, softlink, or non-existent (a softlink will be
5283f2a3aaSMatthew Dillon * created).
5383f2a3aaSMatthew Dillon */
5483f2a3aaSMatthew Dillon void
hammer_cmd_snap(char ** av,int ac,int tostdout,int fsbase)5583f2a3aaSMatthew Dillon hammer_cmd_snap(char **av, int ac, int tostdout, int fsbase)
5683f2a3aaSMatthew Dillon {
5783f2a3aaSMatthew Dillon struct hammer_ioc_synctid synctid;
5883f2a3aaSMatthew Dillon struct hammer_ioc_version version;
5983f2a3aaSMatthew Dillon char *dirpath;
60f111635eSTomohiro Kusumi char *fsym = NULL;
61f111635eSTomohiro Kusumi char *tsym = NULL;
6283f2a3aaSMatthew Dillon struct stat st;
6383f2a3aaSMatthew Dillon char note[64];
6483f2a3aaSMatthew Dillon int fsfd;
6583f2a3aaSMatthew Dillon
6683f2a3aaSMatthew Dillon if (ac == 0 || ac > 2) {
6783f2a3aaSMatthew Dillon snapshot_usage(1);
6883f2a3aaSMatthew Dillon /* not reached */
6983f2a3aaSMatthew Dillon }
7083f2a3aaSMatthew Dillon
7183f2a3aaSMatthew Dillon if (ac == 2)
7283f2a3aaSMatthew Dillon snprintf(note, sizeof(note), "%s", av[1]);
7383f2a3aaSMatthew Dillon else
7483f2a3aaSMatthew Dillon note[0] = 0;
7583f2a3aaSMatthew Dillon
7683f2a3aaSMatthew Dillon /*
7783f2a3aaSMatthew Dillon * Figure out the softlink path and directory path
7883f2a3aaSMatthew Dillon */
7983f2a3aaSMatthew Dillon if (stat(av[0], &st) < 0) {
8083f2a3aaSMatthew Dillon dirpath = dirpart(av[0]);
81f111635eSTomohiro Kusumi tsym = strdup(av[0]);
8283f2a3aaSMatthew Dillon } else if (S_ISDIR(st.st_mode)) {
8383f2a3aaSMatthew Dillon time_t t = time(NULL);
8483f2a3aaSMatthew Dillon struct tm *tp;
8583f2a3aaSMatthew Dillon char extbuf[64];
8683f2a3aaSMatthew Dillon
8783f2a3aaSMatthew Dillon tp = localtime(&t);
8883f2a3aaSMatthew Dillon strftime(extbuf, sizeof(extbuf), DEFAULT_SNAPSHOT_NAME, tp);
8983f2a3aaSMatthew Dillon
9083f2a3aaSMatthew Dillon dirpath = strdup(av[0]);
9183f2a3aaSMatthew Dillon asprintf(&tsym, "%s/%s", dirpath, extbuf);
9283f2a3aaSMatthew Dillon } else {
93c3e30e91STomohiro Kusumi err(2, "hammer snap: File %s exists and is not a directory",
9483f2a3aaSMatthew Dillon av[0]);
95f254e677STomohiro Kusumi /* not reached */
9683f2a3aaSMatthew Dillon }
9783f2a3aaSMatthew Dillon
9883f2a3aaSMatthew Dillon /*
9983f2a3aaSMatthew Dillon * Get a handle on some directory in the filesystem for the
10083f2a3aaSMatthew Dillon * ioctl (so it is stored in the correct PFS).
10183f2a3aaSMatthew Dillon */
10283f2a3aaSMatthew Dillon fsfd = open(dirpath, O_RDONLY);
103f254e677STomohiro Kusumi if (fsfd < 0) {
104c3e30e91STomohiro Kusumi err(2, "hammer snap: Cannot open directory %s", dirpath);
105f254e677STomohiro Kusumi /* not reached */
106f254e677STomohiro Kusumi }
10783f2a3aaSMatthew Dillon
10883f2a3aaSMatthew Dillon /*
10983f2a3aaSMatthew Dillon * Must be at least version 3 to use this command.
11083f2a3aaSMatthew Dillon */
11183f2a3aaSMatthew Dillon bzero(&version, sizeof(version));
11283f2a3aaSMatthew Dillon
113f254e677STomohiro Kusumi if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
11483f2a3aaSMatthew Dillon err(2, "Unable to create snapshot");
115f254e677STomohiro Kusumi /* not reached */
116f254e677STomohiro Kusumi } else if (version.cur_version < 3) {
11783f2a3aaSMatthew Dillon errx(2, "Unable to create snapshot: This directive requires "
11816265794SThomas Nikolajsen "you to upgrade\n"
11916265794SThomas Nikolajsen "the filesystem to version 3. "
12016265794SThomas Nikolajsen "Use 'hammer snapshot' for legacy operation.");
121f254e677STomohiro Kusumi /* not reached */
122f254e677STomohiro Kusumi }
1234c09d9c4SMatthew Dillon HammerVersion = version.cur_version;
12483f2a3aaSMatthew Dillon
12583f2a3aaSMatthew Dillon /*
12683f2a3aaSMatthew Dillon * Synctid to get a transaction id for the snapshot.
12783f2a3aaSMatthew Dillon */
12883f2a3aaSMatthew Dillon bzero(&synctid, sizeof(synctid));
12983f2a3aaSMatthew Dillon synctid.op = HAMMER_SYNCTID_SYNC2;
130f254e677STomohiro Kusumi if (ioctl(fsfd, HAMMERIOC_SYNCTID, &synctid) < 0) {
13183f2a3aaSMatthew Dillon err(2, "hammer snap: Synctid %s failed",
13283f2a3aaSMatthew Dillon dirpath);
133*052fd72bSTomohiro Kusumi /* not reached */
134f254e677STomohiro Kusumi }
13583f2a3aaSMatthew Dillon if (tostdout) {
136f254e677STomohiro Kusumi if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
137b78f1a00SMatthew Dillon printf("%s/@@0x%016jx\n",
138b78f1a00SMatthew Dillon dirpath, (uintmax_t)synctid.tid);
139f254e677STomohiro Kusumi } else {
140b78f1a00SMatthew Dillon printf("%s@@0x%016jx\n",
141b78f1a00SMatthew Dillon dirpath, (uintmax_t)synctid.tid);
142f254e677STomohiro Kusumi }
14383f2a3aaSMatthew Dillon fsym = NULL;
14483f2a3aaSMatthew Dillon tsym = NULL;
14583f2a3aaSMatthew Dillon }
14683f2a3aaSMatthew Dillon
14783f2a3aaSMatthew Dillon /*
14883f2a3aaSMatthew Dillon * Contents of the symlink being created.
14983f2a3aaSMatthew Dillon */
15083f2a3aaSMatthew Dillon if (fsbase) {
15183f2a3aaSMatthew Dillon struct statfs buf;
15283f2a3aaSMatthew Dillon
153f254e677STomohiro Kusumi if (statfs(dirpath, &buf) < 0) {
15416265794SThomas Nikolajsen err(2, "hammer snap: Cannot determine mount for %s",
15583f2a3aaSMatthew Dillon dirpath);
156*052fd72bSTomohiro Kusumi /* not reached */
157f254e677STomohiro Kusumi }
15883f2a3aaSMatthew Dillon asprintf(&fsym, "%s/@@0x%016jx",
15983f2a3aaSMatthew Dillon buf.f_mntonname, (uintmax_t)synctid.tid);
160b78f1a00SMatthew Dillon } else if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
16183f2a3aaSMatthew Dillon asprintf(&fsym, "%s/@@0x%016jx",
16283f2a3aaSMatthew Dillon dirpath, (uintmax_t)synctid.tid);
163b78f1a00SMatthew Dillon } else {
164b78f1a00SMatthew Dillon asprintf(&fsym, "%s@@0x%016jx",
165b78f1a00SMatthew Dillon dirpath, (uintmax_t)synctid.tid);
16683f2a3aaSMatthew Dillon }
16783f2a3aaSMatthew Dillon
16883f2a3aaSMatthew Dillon /*
16983f2a3aaSMatthew Dillon * Create the snapshot.
17083f2a3aaSMatthew Dillon */
17183f2a3aaSMatthew Dillon snapshot_add(fsfd, fsym, tsym, note, synctid.tid);
17283f2a3aaSMatthew Dillon free(dirpath);
173f111635eSTomohiro Kusumi free(fsym);
174f111635eSTomohiro Kusumi free(tsym);
17583f2a3aaSMatthew Dillon }
17683f2a3aaSMatthew Dillon
17783f2a3aaSMatthew Dillon /*
17816265794SThomas Nikolajsen * hammer snapls [<path> ...]
17983f2a3aaSMatthew Dillon *
18083f2a3aaSMatthew Dillon * If no arguments are specified snapshots for the PFS containing the
18183f2a3aaSMatthew Dillon * current directory are listed.
18283f2a3aaSMatthew Dillon */
18383f2a3aaSMatthew Dillon void
hammer_cmd_snapls(char ** av,int ac)18483f2a3aaSMatthew Dillon hammer_cmd_snapls(char **av, int ac)
18583f2a3aaSMatthew Dillon {
18683f2a3aaSMatthew Dillon int i;
18783f2a3aaSMatthew Dillon
18883f2a3aaSMatthew Dillon for (i = 0; i < ac; ++i)
18983f2a3aaSMatthew Dillon snapshot_ls(av[i]);
19083f2a3aaSMatthew Dillon if (ac == 0)
19183f2a3aaSMatthew Dillon snapshot_ls(".");
19283f2a3aaSMatthew Dillon }
19383f2a3aaSMatthew Dillon
19483f2a3aaSMatthew Dillon /*
195aaf93065SThomas Nikolajsen * hammer snaprm <path> ...
196aaf93065SThomas Nikolajsen * hammer snaprm <transid> ...
197aaf93065SThomas Nikolajsen * hammer snaprm <filesystem> <transid> ...
19883f2a3aaSMatthew Dillon */
19983f2a3aaSMatthew Dillon void
hammer_cmd_snaprm(char ** av,int ac)20083f2a3aaSMatthew Dillon hammer_cmd_snaprm(char **av, int ac)
20183f2a3aaSMatthew Dillon {
20283f2a3aaSMatthew Dillon struct stat st;
20383f2a3aaSMatthew Dillon char linkbuf[1024];
20483f2a3aaSMatthew Dillon intmax_t tid;
20583f2a3aaSMatthew Dillon int fsfd = -1;
20683f2a3aaSMatthew Dillon int i;
207aa6862a4SThomas Nikolajsen int delete;
208aa6862a4SThomas Nikolajsen enum snaprm_mode { none_m, path_m, tid_m } mode = none_m;
20983f2a3aaSMatthew Dillon char *dirpath;
210aa6862a4SThomas Nikolajsen char *ptr, *ptr2;
211aa6862a4SThomas Nikolajsen
212aa6862a4SThomas Nikolajsen if (ac == 0) {
213aa6862a4SThomas Nikolajsen snapshot_usage(1);
214aa6862a4SThomas Nikolajsen /* not reached */
215aa6862a4SThomas Nikolajsen }
21683f2a3aaSMatthew Dillon
21783f2a3aaSMatthew Dillon for (i = 0; i < ac; ++i) {
21883f2a3aaSMatthew Dillon if (lstat(av[i], &st) < 0) {
21983f2a3aaSMatthew Dillon tid = strtoull(av[i], &ptr, 16);
220f254e677STomohiro Kusumi if (*ptr) {
22183f2a3aaSMatthew Dillon err(2, "hammer snaprm: not a file or tid: %s",
22283f2a3aaSMatthew Dillon av[i]);
223f254e677STomohiro Kusumi /* not reached */
224f254e677STomohiro Kusumi }
225aa6862a4SThomas Nikolajsen if (mode == path_m) {
226aa6862a4SThomas Nikolajsen snapshot_usage(1);
227aa6862a4SThomas Nikolajsen /* not reached */
228aa6862a4SThomas Nikolajsen }
229aa6862a4SThomas Nikolajsen mode = tid_m;
23092ed14a3SMatthew Dillon if (fsfd < 0)
23192ed14a3SMatthew Dillon fsfd = open(".", O_RDONLY);
23283f2a3aaSMatthew Dillon snapshot_del(fsfd, tid);
23383f2a3aaSMatthew Dillon } else if (S_ISDIR(st.st_mode)) {
234aa6862a4SThomas Nikolajsen if (i != 0 || ac < 2) {
235aa6862a4SThomas Nikolajsen snapshot_usage(1);
236aa6862a4SThomas Nikolajsen /* not reached */
237aa6862a4SThomas Nikolajsen }
23883f2a3aaSMatthew Dillon if (fsfd >= 0)
23983f2a3aaSMatthew Dillon close(fsfd);
24083f2a3aaSMatthew Dillon fsfd = open(av[i], O_RDONLY);
241f254e677STomohiro Kusumi if (fsfd < 0) {
24283f2a3aaSMatthew Dillon err(2, "hammer snaprm: cannot open dir %s",
24383f2a3aaSMatthew Dillon av[i]);
244f254e677STomohiro Kusumi /* not reached */
245f254e677STomohiro Kusumi }
246aa6862a4SThomas Nikolajsen mode = tid_m;
24783f2a3aaSMatthew Dillon } else if (S_ISLNK(st.st_mode)) {
24883f2a3aaSMatthew Dillon dirpath = dirpart(av[i]);
249bf2c6489SMatthew Dillon bzero(linkbuf, sizeof(linkbuf));
250f254e677STomohiro Kusumi if (readlink(av[i], linkbuf, sizeof(linkbuf) - 1) < 0) {
251bf2c6489SMatthew Dillon err(2, "hammer snaprm: cannot read softlink: "
252bf2c6489SMatthew Dillon "%s", av[i]);
253f254e677STomohiro Kusumi /* not reached */
254f254e677STomohiro Kusumi }
255bf2c6489SMatthew Dillon if (linkbuf[0] == '/') {
256bf2c6489SMatthew Dillon free(dirpath);
257bf2c6489SMatthew Dillon dirpath = dirpart(linkbuf);
258bf2c6489SMatthew Dillon } else {
259bf2c6489SMatthew Dillon asprintf(&ptr, "%s/%s", dirpath, linkbuf);
260bf2c6489SMatthew Dillon free(dirpath);
261bf2c6489SMatthew Dillon dirpath = dirpart(ptr);
262f111635eSTomohiro Kusumi free(ptr);
263bf2c6489SMatthew Dillon }
264bf2c6489SMatthew Dillon
26583f2a3aaSMatthew Dillon if (fsfd >= 0)
26683f2a3aaSMatthew Dillon close(fsfd);
26783f2a3aaSMatthew Dillon fsfd = open(dirpath, O_RDONLY);
268f254e677STomohiro Kusumi if (fsfd < 0) {
26983f2a3aaSMatthew Dillon err(2, "hammer snaprm: cannot open dir %s",
27083f2a3aaSMatthew Dillon dirpath);
271f254e677STomohiro Kusumi /* not reached */
272f254e677STomohiro Kusumi }
27383f2a3aaSMatthew Dillon
274aa6862a4SThomas Nikolajsen delete = 1;
275aa6862a4SThomas Nikolajsen if (i == 0 && ac > 1) {
276aa6862a4SThomas Nikolajsen mode = path_m;
277aa6862a4SThomas Nikolajsen if (lstat(av[1], &st) < 0) {
278aa6862a4SThomas Nikolajsen tid = strtoull(av[1], &ptr, 16);
279aa6862a4SThomas Nikolajsen if (*ptr == '\0') {
280aa6862a4SThomas Nikolajsen delete = 0;
281aa6862a4SThomas Nikolajsen mode = tid_m;
28283f2a3aaSMatthew Dillon }
283aa6862a4SThomas Nikolajsen }
284aa6862a4SThomas Nikolajsen } else {
285aa6862a4SThomas Nikolajsen if (mode == tid_m) {
286aa6862a4SThomas Nikolajsen snapshot_usage(1);
287aa6862a4SThomas Nikolajsen /* not reached */
288aa6862a4SThomas Nikolajsen }
289aa6862a4SThomas Nikolajsen mode = path_m;
290aa6862a4SThomas Nikolajsen }
291aa6862a4SThomas Nikolajsen if (delete && (ptr = strrchr(linkbuf, '@')) &&
292a6d893d9SThomas Nikolajsen ptr > linkbuf && ptr[-1] == '@' && ptr[1]) {
293aa6862a4SThomas Nikolajsen tid = strtoull(ptr + 1, &ptr2, 16);
294aa6862a4SThomas Nikolajsen if (*ptr2 == '\0') {
295aa6862a4SThomas Nikolajsen snapshot_del(fsfd, tid);
29683f2a3aaSMatthew Dillon remove(av[i]);
297aa6862a4SThomas Nikolajsen }
298aa6862a4SThomas Nikolajsen }
29983f2a3aaSMatthew Dillon free(dirpath);
30083f2a3aaSMatthew Dillon } else {
30183f2a3aaSMatthew Dillon err(2, "hammer snaprm: not directory or snapshot "
30283f2a3aaSMatthew Dillon "softlink: %s", av[i]);
303f254e677STomohiro Kusumi /* not reached */
30483f2a3aaSMatthew Dillon }
30583f2a3aaSMatthew Dillon }
30683f2a3aaSMatthew Dillon if (fsfd >= 0)
30783f2a3aaSMatthew Dillon close(fsfd);
30883f2a3aaSMatthew Dillon }
3096b669ab4SMichael Neumann
3106b669ab4SMichael Neumann /*
31116265794SThomas Nikolajsen * snapshot <softlink-dir>
31216265794SThomas Nikolajsen * snapshot <filesystem> <softlink-dir> [<note>]
3136b669ab4SMichael Neumann */
3146b669ab4SMichael Neumann void
hammer_cmd_snapshot(char ** av,int ac)3156b669ab4SMichael Neumann hammer_cmd_snapshot(char **av, int ac)
3166b669ab4SMichael Neumann {
3176b669ab4SMichael Neumann const char *filesystem;
31896e6b862SMichael Neumann const char *softlink_dir;
31996e6b862SMichael Neumann char *softlink_fmt;
3206b669ab4SMichael Neumann struct statfs buf;
32196e6b862SMichael Neumann struct stat st;
3226b669ab4SMichael Neumann struct hammer_ioc_synctid synctid;
3236b669ab4SMichael Neumann char *from;
3246b669ab4SMichael Neumann char *to;
325b5ec5ad4SMatthew Dillon char *note = NULL;
3266b669ab4SMichael Neumann
327f8052532SMichael Neumann if (ac == 1) {
328f8052532SMichael Neumann filesystem = NULL;
32996e6b862SMichael Neumann softlink_dir = av[0];
3303267eb8aSSascha Wildner } else if (ac == 2) {
331f8052532SMichael Neumann filesystem = av[0];
33296e6b862SMichael Neumann softlink_dir = av[1];
333b5ec5ad4SMatthew Dillon } else if (ac == 3) {
334b5ec5ad4SMatthew Dillon filesystem = av[0];
335b5ec5ad4SMatthew Dillon softlink_dir = av[1];
336b5ec5ad4SMatthew Dillon note = av[2];
3373267eb8aSSascha Wildner } else {
3386b669ab4SMichael Neumann snapshot_usage(1);
33983f2a3aaSMatthew Dillon /* not reached */
340f8052532SMichael Neumann }
3416b669ab4SMichael Neumann
34296e6b862SMichael Neumann if (stat(softlink_dir, &st) == 0) {
343*052fd72bSTomohiro Kusumi if (!S_ISDIR(st.st_mode)) {
34496e6b862SMichael Neumann err(2, "File %s already exists", softlink_dir);
345*052fd72bSTomohiro Kusumi /* not reached */
346*052fd72bSTomohiro Kusumi }
3476b669ab4SMichael Neumann
34896e6b862SMichael Neumann if (filesystem == NULL) {
349f254e677STomohiro Kusumi if (statfs(softlink_dir, &buf) != 0) {
350f8052532SMichael Neumann err(2, "Unable to determine filesystem of %s",
351f8052532SMichael Neumann softlink_dir);
352*052fd72bSTomohiro Kusumi /* not reached */
353f254e677STomohiro Kusumi }
3546b669ab4SMichael Neumann filesystem = buf.f_mntonname;
35596e6b862SMichael Neumann }
35696e6b862SMichael Neumann
35796e6b862SMichael Neumann softlink_fmt = malloc(strlen(softlink_dir) + 1 + 1 +
35896e6b862SMichael Neumann sizeof(DEFAULT_SNAPSHOT_NAME));
359*052fd72bSTomohiro Kusumi if (softlink_fmt == NULL) {
36096e6b862SMichael Neumann err(2, "Failed to allocate string");
361*052fd72bSTomohiro Kusumi /* not reached */
362*052fd72bSTomohiro Kusumi }
36396e6b862SMichael Neumann
36496e6b862SMichael Neumann strcpy(softlink_fmt, softlink_dir);
36596e6b862SMichael Neumann if (softlink_fmt[strlen(softlink_fmt)-1] != '/')
36696e6b862SMichael Neumann strcat(softlink_fmt, "/");
36796e6b862SMichael Neumann strcat(softlink_fmt, DEFAULT_SNAPSHOT_NAME);
3683267eb8aSSascha Wildner } else {
36996e6b862SMichael Neumann softlink_fmt = strdup(softlink_dir);
37096e6b862SMichael Neumann
37196e6b862SMichael Neumann if (filesystem == NULL) {
37296e6b862SMichael Neumann /*
37396e6b862SMichael Neumann * strip-off last '/path' segment to get the softlink
37496e6b862SMichael Neumann * directory, which we need to determine the filesystem
37596e6b862SMichael Neumann * we are on.
37696e6b862SMichael Neumann */
37796e6b862SMichael Neumann char *pos = strrchr(softlink_fmt, '/');
37896e6b862SMichael Neumann if (pos != NULL)
37996e6b862SMichael Neumann *pos = '\0';
38096e6b862SMichael Neumann
38196e6b862SMichael Neumann if (stat(softlink_fmt, &st) != 0 ||
382f254e677STomohiro Kusumi !S_ISDIR(st.st_mode)) {
38396e6b862SMichael Neumann err(2, "Unable to determine softlink dir %s",
38496e6b862SMichael Neumann softlink_fmt);
385*052fd72bSTomohiro Kusumi /* not reached */
386f254e677STomohiro Kusumi }
387f254e677STomohiro Kusumi if (statfs(softlink_fmt, &buf) != 0) {
38896e6b862SMichael Neumann err(2, "Unable to determine filesystem of %s",
38996e6b862SMichael Neumann softlink_fmt);
390*052fd72bSTomohiro Kusumi /* not reached */
391f254e677STomohiro Kusumi }
39296e6b862SMichael Neumann filesystem = buf.f_mntonname;
39396e6b862SMichael Neumann
394e0af86f0SMichael Neumann /* restore '/' */
39596e6b862SMichael Neumann if (pos != NULL)
39696e6b862SMichael Neumann *pos = '/';
39796e6b862SMichael Neumann }
398a231693eSMichael Neumann }
399f8052532SMichael Neumann
400f8052532SMichael Neumann /*
401f8052532SMichael Neumann * Synctid
402f8052532SMichael Neumann */
4036b669ab4SMichael Neumann bzero(&synctid, sizeof(synctid));
4046b669ab4SMichael Neumann synctid.op = HAMMER_SYNCTID_SYNC2;
40583f2a3aaSMatthew Dillon
40696e6b862SMichael Neumann int fd = open(filesystem, O_RDONLY);
407*052fd72bSTomohiro Kusumi if (fd < 0) {
4086b669ab4SMichael Neumann err(2, "Unable to open %s", filesystem);
409*052fd72bSTomohiro Kusumi /* not reached */
410*052fd72bSTomohiro Kusumi }
411*052fd72bSTomohiro Kusumi if (ioctl(fd, HAMMERIOC_SYNCTID, &synctid) < 0) {
4126b669ab4SMichael Neumann err(2, "Synctid %s failed", filesystem);
413*052fd72bSTomohiro Kusumi /* not reached */
414*052fd72bSTomohiro Kusumi }
4156764f177SMichael Neumann
416a276dc6bSMatthew Dillon asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
417*052fd72bSTomohiro Kusumi if (from == NULL) {
4186b669ab4SMichael Neumann err(2, "Couldn't generate string");
419*052fd72bSTomohiro Kusumi /* not reached */
420*052fd72bSTomohiro Kusumi }
4216b669ab4SMichael Neumann
422f8052532SMichael Neumann int sz = strlen(softlink_fmt) + 50;
423f8052532SMichael Neumann to = malloc(sz);
424*052fd72bSTomohiro Kusumi if (to == NULL) {
425f8052532SMichael Neumann err(2, "Failed to allocate string");
426*052fd72bSTomohiro Kusumi /* not reached */
427*052fd72bSTomohiro Kusumi }
428f8052532SMichael Neumann
429f8052532SMichael Neumann time_t t = time(NULL);
430*052fd72bSTomohiro Kusumi if (strftime(to, sz, softlink_fmt, localtime(&t)) == 0) {
431f8052532SMichael Neumann err(2, "String buffer too small");
432*052fd72bSTomohiro Kusumi /* not reached */
433*052fd72bSTomohiro Kusumi }
4346b669ab4SMichael Neumann
43583f2a3aaSMatthew Dillon asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
4366b669ab4SMichael Neumann
437b5ec5ad4SMatthew Dillon snapshot_add(fd, from, to, note, synctid.tid);
43883f2a3aaSMatthew Dillon
43983f2a3aaSMatthew Dillon close(fd);
4406b669ab4SMichael Neumann printf("%s\n", to);
4416764f177SMichael Neumann
44296e6b862SMichael Neumann free(softlink_fmt);
4436764f177SMichael Neumann free(from);
4446764f177SMichael Neumann free(to);
4456b669ab4SMichael Neumann }
4466b669ab4SMichael Neumann
4476b669ab4SMichael Neumann static
4486b669ab4SMichael Neumann void
snapshot_add(int fd,const char * fsym,const char * tsym,const char * label,hammer_tid_t tid)44983f2a3aaSMatthew Dillon snapshot_add(int fd, const char *fsym, const char *tsym, const char *label,
45083f2a3aaSMatthew Dillon hammer_tid_t tid)
45183f2a3aaSMatthew Dillon {
45283f2a3aaSMatthew Dillon struct hammer_ioc_version version;
45383f2a3aaSMatthew Dillon struct hammer_ioc_snapshot snapshot;
45483f2a3aaSMatthew Dillon
45583f2a3aaSMatthew Dillon bzero(&version, sizeof(version));
45683f2a3aaSMatthew Dillon bzero(&snapshot, sizeof(snapshot));
45783f2a3aaSMatthew Dillon
458b5ec5ad4SMatthew Dillon /*
459b5ec5ad4SMatthew Dillon * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
460b5ec5ad4SMatthew Dillon */
46183f2a3aaSMatthew Dillon if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0 &&
46283f2a3aaSMatthew Dillon version.cur_version >= 3) {
4634c09d9c4SMatthew Dillon HammerVersion = version.cur_version;
46483f2a3aaSMatthew Dillon snapshot.index = 0;
46583f2a3aaSMatthew Dillon snapshot.count = 1;
46683f2a3aaSMatthew Dillon snapshot.snaps[0].tid = tid;
46783f2a3aaSMatthew Dillon snapshot.snaps[0].ts = time(NULL) * 1000000ULL;
468f254e677STomohiro Kusumi if (label) {
46983f2a3aaSMatthew Dillon snprintf(snapshot.snaps[0].label,
47083f2a3aaSMatthew Dillon sizeof(snapshot.snaps[0].label),
47183f2a3aaSMatthew Dillon "%s",
47283f2a3aaSMatthew Dillon label);
473f254e677STomohiro Kusumi }
474f254e677STomohiro Kusumi if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, &snapshot) < 0) {
47583f2a3aaSMatthew Dillon err(2, "Unable to create snapshot");
476*052fd72bSTomohiro Kusumi /* not reached */
477f254e677STomohiro Kusumi } else if (snapshot.head.error &&
478f254e677STomohiro Kusumi snapshot.head.error != EEXIST) {
479c3e30e91STomohiro Kusumi errx(2, "Unable to create snapshot: %s",
48083f2a3aaSMatthew Dillon strerror(snapshot.head.error));
481*052fd72bSTomohiro Kusumi /* not reached */
48283f2a3aaSMatthew Dillon }
483f254e677STomohiro Kusumi }
484b5ec5ad4SMatthew Dillon
485b5ec5ad4SMatthew Dillon /*
486b5ec5ad4SMatthew Dillon * Create a symlink for the snapshot. If a file exists with the same
487b5ec5ad4SMatthew Dillon * name the new symlink will replace it.
488b5ec5ad4SMatthew Dillon */
48983f2a3aaSMatthew Dillon if (fsym && tsym) {
49083f2a3aaSMatthew Dillon remove(tsym);
491f254e677STomohiro Kusumi if (symlink(fsym, tsym) < 0) {
49283f2a3aaSMatthew Dillon err(2, "Unable to create symlink %s", tsym);
493*052fd72bSTomohiro Kusumi /* not reached */
49483f2a3aaSMatthew Dillon }
49583f2a3aaSMatthew Dillon }
496f254e677STomohiro Kusumi }
49783f2a3aaSMatthew Dillon
49883f2a3aaSMatthew Dillon static
49983f2a3aaSMatthew Dillon void
snapshot_ls(const char * path)50083f2a3aaSMatthew Dillon snapshot_ls(const char *path)
50183f2a3aaSMatthew Dillon {
5022c1d3cefSStathis Kamperis struct hammer_ioc_info info;
50383f2a3aaSMatthew Dillon struct hammer_ioc_snapshot snapshot;
50483f2a3aaSMatthew Dillon struct hammer_ioc_pseudofs_rw pfs;
5052c1d3cefSStathis Kamperis struct hammer_pseudofs_data pfs_od;
50646e9f340STomohiro Kusumi hammer_snapshot_data_t snap;
50783f2a3aaSMatthew Dillon struct tm *tp;
50883f2a3aaSMatthew Dillon time_t t;
50946137e17STomohiro Kusumi uint32_t i;
510d428efb7SAntonio Huete Jimenez int fd;
51183f2a3aaSMatthew Dillon char snapts[64];
51283f2a3aaSMatthew Dillon
51383f2a3aaSMatthew Dillon fd = open(path, O_RDONLY);
514f254e677STomohiro Kusumi if (fd < 0) {
51583f2a3aaSMatthew Dillon err(2, "hammer snapls: cannot open %s", path);
516f254e677STomohiro Kusumi /* not reached */
517f254e677STomohiro Kusumi }
51883f2a3aaSMatthew Dillon
5191b23fc22STomohiro Kusumi clrpfs(&pfs, &pfs_od, -1);
520f254e677STomohiro Kusumi if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
52183f2a3aaSMatthew Dillon err(2, "hammer snapls: cannot retrieve PFS info on %s", path);
522f254e677STomohiro Kusumi /* not reached */
523f254e677STomohiro Kusumi }
52483f2a3aaSMatthew Dillon
5252c1d3cefSStathis Kamperis bzero(&info, sizeof(info));
526f254e677STomohiro Kusumi if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
5272c1d3cefSStathis Kamperis err(2, "hammer snapls: cannot retrieve HAMMER info");
528f254e677STomohiro Kusumi /* not reached */
529f254e677STomohiro Kusumi }
5302c1d3cefSStathis Kamperis
5312dd9a2a5STomohiro Kusumi printf("Snapshots on %s\tPFS#%d\n", path, pfs.pfs_id);
5322c1d3cefSStathis Kamperis printf("Transaction ID\t\tTimestamp\t\tNote\n");
53383f2a3aaSMatthew Dillon
53483f2a3aaSMatthew Dillon bzero(&snapshot, sizeof(snapshot));
53583f2a3aaSMatthew Dillon do {
536f254e677STomohiro Kusumi if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
53783f2a3aaSMatthew Dillon err(2, "hammer snapls: %s: not HAMMER fs or "
53883f2a3aaSMatthew Dillon "version < 3", path);
539f254e677STomohiro Kusumi /* not reached */
540f254e677STomohiro Kusumi }
54183f2a3aaSMatthew Dillon for (i = 0; i < snapshot.count; ++i) {
54283f2a3aaSMatthew Dillon snap = &snapshot.snaps[i];
54383f2a3aaSMatthew Dillon
54483f2a3aaSMatthew Dillon t = snap->ts / 1000000ULL;
54583f2a3aaSMatthew Dillon tp = localtime(&t);
54683f2a3aaSMatthew Dillon strftime(snapts, sizeof(snapts),
54783f2a3aaSMatthew Dillon "%Y-%m-%d %H:%M:%S %Z", tp);
54883f2a3aaSMatthew Dillon printf("0x%016jx\t%s\t%s\n",
5492c1d3cefSStathis Kamperis (uintmax_t)snap->tid, snapts,
5502c1d3cefSStathis Kamperis strlen(snap->label) ? snap->label : "-");
55183f2a3aaSMatthew Dillon }
55283f2a3aaSMatthew Dillon } while (snapshot.head.error == 0 && snapshot.count);
55383f2a3aaSMatthew Dillon }
55483f2a3aaSMatthew Dillon
55583f2a3aaSMatthew Dillon static
55683f2a3aaSMatthew Dillon void
snapshot_del(int fsfd,hammer_tid_t tid)55783f2a3aaSMatthew Dillon snapshot_del(int fsfd, hammer_tid_t tid)
55883f2a3aaSMatthew Dillon {
55983f2a3aaSMatthew Dillon struct hammer_ioc_snapshot snapshot;
56083f2a3aaSMatthew Dillon struct hammer_ioc_version version;
56183f2a3aaSMatthew Dillon
56283f2a3aaSMatthew Dillon bzero(&version, sizeof(version));
56383f2a3aaSMatthew Dillon
564f254e677STomohiro Kusumi if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
56583f2a3aaSMatthew Dillon err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
566*052fd72bSTomohiro Kusumi /* not reached */
567f254e677STomohiro Kusumi }
5684c09d9c4SMatthew Dillon HammerVersion = version.cur_version;
569f254e677STomohiro Kusumi if (version.cur_version < 3) {
57083f2a3aaSMatthew Dillon errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
57183f2a3aaSMatthew Dillon " 3 to use this directive", (uintmax_t)tid);
572*052fd72bSTomohiro Kusumi /* not reached */
573f254e677STomohiro Kusumi }
57483f2a3aaSMatthew Dillon
57583f2a3aaSMatthew Dillon bzero(&snapshot, sizeof(snapshot));
57683f2a3aaSMatthew Dillon snapshot.count = 1;
57783f2a3aaSMatthew Dillon snapshot.snaps[0].tid = tid;
57883f2a3aaSMatthew Dillon
57992ed14a3SMatthew Dillon /*
58092ed14a3SMatthew Dillon * Do not abort if we are unable to remove the meta-data.
58192ed14a3SMatthew Dillon */
582f254e677STomohiro Kusumi if (ioctl(fsfd, HAMMERIOC_DEL_SNAPSHOT, &snapshot) < 0) {
58392ed14a3SMatthew Dillon err(2, "hammer snaprm 0x%016jx",
58492ed14a3SMatthew Dillon (uintmax_t)tid);
585*052fd72bSTomohiro Kusumi /* not reached */
586f254e677STomohiro Kusumi } else if (snapshot.head.error == ENOENT) {
58792ed14a3SMatthew Dillon fprintf(stderr, "Warning: hammer snaprm 0x%016jx: "
58892ed14a3SMatthew Dillon "meta-data not found\n",
58992ed14a3SMatthew Dillon (uintmax_t)tid);
590f254e677STomohiro Kusumi } else if (snapshot.head.error) {
59192ed14a3SMatthew Dillon fprintf(stderr, "Warning: hammer snaprm 0x%016jx: %s\n",
59283f2a3aaSMatthew Dillon (uintmax_t)tid, strerror(snapshot.head.error));
59383f2a3aaSMatthew Dillon }
594f254e677STomohiro Kusumi }
59583f2a3aaSMatthew Dillon
59683f2a3aaSMatthew Dillon static
59783f2a3aaSMatthew Dillon void
snapshot_usage(int exit_code)5986b669ab4SMichael Neumann snapshot_usage(int exit_code)
5996b669ab4SMichael Neumann {
60083f2a3aaSMatthew Dillon fprintf(stderr,
60116265794SThomas Nikolajsen "hammer snap <path> [<note>]\t\tcreate snapshot & link, points to\n"
60216265794SThomas Nikolajsen "\t\t\t\t\tbase of PFS mount\n"
60316265794SThomas Nikolajsen "hammer snaplo <path> [<note>]\t\tcreate snapshot & link, points to\n"
60416265794SThomas Nikolajsen "\t\t\t\t\ttarget dir\n"
60516265794SThomas Nikolajsen "hammer snapq <dir> [<note>]\t\tcreate snapshot, output path to stdout\n"
606aaf93065SThomas Nikolajsen "hammer snaprm <path> ...\t\tdelete snapshots; filesystem is CWD\n"
607aaf93065SThomas Nikolajsen "hammer snaprm <transid> ...\t\tdelete snapshots\n"
608aaf93065SThomas Nikolajsen "hammer snaprm <filesystem> <transid> ...\tdelete snapshots\n"
60916265794SThomas Nikolajsen "hammer snapls [<path> ...]\t\tlist available snapshots\n"
61083f2a3aaSMatthew Dillon "\n"
61183f2a3aaSMatthew Dillon "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
61283f2a3aaSMatthew Dillon " in a HAMMER filesystem or PFS may be specified. If the path\n"
61383f2a3aaSMatthew Dillon " specified does not exist this function will also create a\n"
61483f2a3aaSMatthew Dillon " softlink.\n"
61583f2a3aaSMatthew Dillon "\n"
61683f2a3aaSMatthew Dillon " When deleting snapshots transaction ids may be directly specified\n"
61716265794SThomas Nikolajsen " or file paths to snapshot softlinks may be specified. If a\n"
61816265794SThomas Nikolajsen " softlink is specified the softlink will also be deleted.\n"
61983f2a3aaSMatthew Dillon "\n"
62083f2a3aaSMatthew Dillon "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
62183f2a3aaSMatthew Dillon " is still accepted but is a deprecated form. This form will\n"
62283f2a3aaSMatthew Dillon " work for older hammer versions. The new forms only work for\n"
62383f2a3aaSMatthew Dillon " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
62416265794SThomas Nikolajsen " to version 3 in-place.\n"
62583f2a3aaSMatthew Dillon );
6266b669ab4SMichael Neumann exit(exit_code);
6276b669ab4SMichael Neumann }
62883f2a3aaSMatthew Dillon
62983f2a3aaSMatthew Dillon static
63083f2a3aaSMatthew Dillon char *
dirpart(const char * path)63183f2a3aaSMatthew Dillon dirpart(const char *path)
63283f2a3aaSMatthew Dillon {
63383f2a3aaSMatthew Dillon const char *ptr;
63483f2a3aaSMatthew Dillon char *res;
63583f2a3aaSMatthew Dillon
63683f2a3aaSMatthew Dillon ptr = strrchr(path, '/');
63783f2a3aaSMatthew Dillon if (ptr) {
63883f2a3aaSMatthew Dillon while (ptr > path && ptr[-1] == '/')
63983f2a3aaSMatthew Dillon --ptr;
64083f2a3aaSMatthew Dillon if (ptr == path)
64183f2a3aaSMatthew Dillon ptr = NULL;
64283f2a3aaSMatthew Dillon }
64383f2a3aaSMatthew Dillon if (ptr == NULL) {
64483f2a3aaSMatthew Dillon path = ".";
64583f2a3aaSMatthew Dillon ptr = path + 1;
64683f2a3aaSMatthew Dillon }
64783f2a3aaSMatthew Dillon res = malloc(ptr - path + 1);
64883f2a3aaSMatthew Dillon bcopy(path, res, ptr - path);
64983f2a3aaSMatthew Dillon res[ptr - path] = 0;
65083f2a3aaSMatthew Dillon return(res);
65183f2a3aaSMatthew Dillon }
652