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_snapshot.c,v 1.7 2008/07/10 18:47:22 mneumann Exp $
37 #include <libhammer.h>
41 #define DEFAULT_SNAPSHOT_NAME "snap-%Y%m%d-%H%M"
43 static void snapshot_usage(int exit_code);
44 static void snapshot_add(int fd, const char *fsym, const char *tsym,
45 const char *label, hammer_tid_t tid);
46 static void snapshot_ls(const char *path);
47 static void snapshot_del(int fsfd, hammer_tid_t tid);
48 static char *dirpart(const char *path);
51 * hammer snap <path> [<note>]
53 * Path may be a directory, softlink, or non-existent (a softlink will be
57 hammer_cmd_snap(char **av, int ac, int tostdout, int fsbase)
59 struct hammer_ioc_synctid synctid;
60 struct hammer_ioc_version version;
68 if (ac == 0 || ac > 2) {
74 snprintf(note, sizeof(note), "%s", av[1]);
79 * Figure out the softlink path and directory path
81 if (stat(av[0], &st) < 0) {
82 dirpath = dirpart(av[0]);
84 } else if (S_ISDIR(st.st_mode)) {
85 time_t t = time(NULL);
90 strftime(extbuf, sizeof(extbuf), DEFAULT_SNAPSHOT_NAME, tp);
92 dirpath = strdup(av[0]);
93 asprintf(&tsym, "%s/%s", dirpath, extbuf);
95 err(2, "hammer snap: File %s exists and is not a directory\n",
101 * Get a handle on some directory in the filesystem for the
102 * ioctl (so it is stored in the correct PFS).
104 fsfd = open(dirpath, O_RDONLY);
106 err(2, "hammer snap: Cannot open directory %s\n", dirpath);
111 * Must be at least version 3 to use this command.
113 bzero(&version, sizeof(version));
115 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
116 err(2, "Unable to create snapshot");
118 } else if (version.cur_version < 3) {
119 errx(2, "Unable to create snapshot: This directive requires "
121 "the filesystem to version 3. "
122 "Use 'hammer snapshot' for legacy operation.");
127 * Synctid to get a transaction id for the snapshot.
129 bzero(&synctid, sizeof(synctid));
130 synctid.op = HAMMER_SYNCTID_SYNC2;
131 if (ioctl(fsfd, HAMMERIOC_SYNCTID, &synctid) < 0) {
132 err(2, "hammer snap: Synctid %s failed",
136 if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
137 printf("%s/@@0x%016jx\n",
138 dirpath, (uintmax_t)synctid.tid);
140 printf("%s@@0x%016jx\n",
141 dirpath, (uintmax_t)synctid.tid);
148 * Contents of the symlink being created.
153 if (statfs(dirpath, &buf) < 0) {
154 err(2, "hammer snap: Cannot determine mount for %s",
157 asprintf(&fsym, "%s/@@0x%016jx",
158 buf.f_mntonname, (uintmax_t)synctid.tid);
159 } else if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
160 asprintf(&fsym, "%s/@@0x%016jx",
161 dirpath, (uintmax_t)synctid.tid);
163 asprintf(&fsym, "%s@@0x%016jx",
164 dirpath, (uintmax_t)synctid.tid);
168 * Create the snapshot.
170 snapshot_add(fsfd, fsym, tsym, note, synctid.tid);
177 * hammer snapls [<path> ...]
179 * If no arguments are specified snapshots for the PFS containing the
180 * current directory are listed.
183 hammer_cmd_snapls(char **av, int ac)
187 for (i = 0; i < ac; ++i)
194 * hammer snaprm <path> ...
195 * hammer snaprm <transid> ...
196 * hammer snaprm <filesystem> <transid> ...
199 hammer_cmd_snaprm(char **av, int ac)
207 enum snaprm_mode { none_m, path_m, tid_m } mode = none_m;
216 for (i = 0; i < ac; ++i) {
217 if (lstat(av[i], &st) < 0) {
218 tid = strtoull(av[i], &ptr, 16);
220 err(2, "hammer snaprm: not a file or tid: %s",
224 if (mode == path_m) {
230 fsfd = open(".", O_RDONLY);
231 snapshot_del(fsfd, tid);
232 } else if (S_ISDIR(st.st_mode)) {
233 if (i != 0 || ac < 2) {
239 fsfd = open(av[i], O_RDONLY);
241 err(2, "hammer snaprm: cannot open dir %s",
246 } else if (S_ISLNK(st.st_mode)) {
247 dirpath = dirpart(av[i]);
248 bzero(linkbuf, sizeof(linkbuf));
249 if (readlink(av[i], linkbuf, sizeof(linkbuf) - 1) < 0) {
250 err(2, "hammer snaprm: cannot read softlink: "
254 if (linkbuf[0] == '/') {
256 dirpath = dirpart(linkbuf);
258 asprintf(&ptr, "%s/%s", dirpath, linkbuf);
260 dirpath = dirpart(ptr);
266 fsfd = open(dirpath, O_RDONLY);
268 err(2, "hammer snaprm: cannot open dir %s",
274 if (i == 0 && ac > 1) {
276 if (lstat(av[1], &st) < 0) {
277 tid = strtoull(av[1], &ptr, 16);
290 if (delete && (ptr = strrchr(linkbuf, '@')) &&
291 ptr > linkbuf && ptr[-1] == '@' && ptr[1]) {
292 tid = strtoull(ptr + 1, &ptr2, 16);
294 snapshot_del(fsfd, tid);
300 err(2, "hammer snaprm: not directory or snapshot "
301 "softlink: %s", av[i]);
310 * snapshot <softlink-dir>
311 * snapshot <filesystem> <softlink-dir> [<note>]
314 hammer_cmd_snapshot(char **av, int ac)
316 const char *filesystem;
317 const char *softlink_dir;
321 struct hammer_ioc_synctid synctid;
328 softlink_dir = av[0];
329 } else if (ac == 2) {
331 softlink_dir = av[1];
332 } else if (ac == 3) {
334 softlink_dir = av[1];
341 if (stat(softlink_dir, &st) == 0) {
342 if (!S_ISDIR(st.st_mode))
343 err(2, "File %s already exists", softlink_dir);
345 if (filesystem == NULL) {
346 if (statfs(softlink_dir, &buf) != 0) {
347 err(2, "Unable to determine filesystem of %s",
350 filesystem = buf.f_mntonname;
353 softlink_fmt = malloc(strlen(softlink_dir) + 1 + 1 +
354 sizeof(DEFAULT_SNAPSHOT_NAME));
355 if (softlink_fmt == NULL)
356 err(2, "Failed to allocate string");
358 strcpy(softlink_fmt, softlink_dir);
359 if (softlink_fmt[strlen(softlink_fmt)-1] != '/')
360 strcat(softlink_fmt, "/");
361 strcat(softlink_fmt, DEFAULT_SNAPSHOT_NAME);
363 softlink_fmt = strdup(softlink_dir);
365 if (filesystem == NULL) {
367 * strip-off last '/path' segment to get the softlink
368 * directory, which we need to determine the filesystem
371 char *pos = strrchr(softlink_fmt, '/');
375 if (stat(softlink_fmt, &st) != 0 ||
376 !S_ISDIR(st.st_mode)) {
377 err(2, "Unable to determine softlink dir %s",
380 if (statfs(softlink_fmt, &buf) != 0) {
381 err(2, "Unable to determine filesystem of %s",
384 filesystem = buf.f_mntonname;
395 bzero(&synctid, sizeof(synctid));
396 synctid.op = HAMMER_SYNCTID_SYNC2;
398 int fd = open(filesystem, O_RDONLY);
400 err(2, "Unable to open %s", filesystem);
401 if (ioctl(fd, HAMMERIOC_SYNCTID, &synctid) < 0)
402 err(2, "Synctid %s failed", filesystem);
404 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
406 err(2, "Couldn't generate string");
408 int sz = strlen(softlink_fmt) + 50;
411 err(2, "Failed to allocate string");
413 time_t t = time(NULL);
414 if (strftime(to, sz, softlink_fmt, localtime(&t)) == 0)
415 err(2, "String buffer too small");
417 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
419 snapshot_add(fd, from, to, note, synctid.tid);
431 snapshot_add(int fd, const char *fsym, const char *tsym, const char *label,
434 struct hammer_ioc_version version;
435 struct hammer_ioc_snapshot snapshot;
437 bzero(&version, sizeof(version));
438 bzero(&snapshot, sizeof(snapshot));
441 * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
443 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0 &&
444 version.cur_version >= 3) {
447 snapshot.snaps[0].tid = tid;
448 snapshot.snaps[0].ts = time(NULL) * 1000000ULL;
450 snprintf(snapshot.snaps[0].label,
451 sizeof(snapshot.snaps[0].label),
455 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, &snapshot) < 0) {
456 err(2, "Unable to create snapshot");
457 } else if (snapshot.head.error &&
458 snapshot.head.error != EEXIST) {
459 errx(2, "Unable to create snapshot: %s\n",
460 strerror(snapshot.head.error));
465 * Create a symlink for the snapshot. If a file exists with the same
466 * name the new symlink will replace it.
470 if (symlink(fsym, tsym) < 0) {
471 err(2, "Unable to create symlink %s", tsym);
478 snapshot_ls(const char *path)
480 struct hammer_ioc_info info;
481 struct hammer_ioc_snapshot snapshot;
482 struct hammer_ioc_pseudofs_rw pfs;
483 struct hammer_pseudofs_data pfs_od;
484 struct hammer_snapshot_data *snap;
492 fd = open(path, O_RDONLY);
494 err(2, "hammer snapls: cannot open %s", path);
498 bzero(&pfs, sizeof(pfs));
499 bzero(&pfs_od, sizeof(pfs_od));
501 pfs.ondisk = &pfs_od;
502 pfs.bytes = sizeof(struct hammer_pseudofs_data);
503 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
504 err(2, "hammer snapls: cannot retrieve PFS info on %s", path);
508 bzero(&info, sizeof(info));
509 if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
510 err(2, "hammer snapls: cannot retrieve HAMMER info");
514 mntpoint = libhammer_find_pfs_mount(&pfs.ondisk->unique_uuid);
516 printf("Snapshots on %s\tPFS #%d\n",
517 mntpoint ? mntpoint : path, pfs.pfs_id);
518 printf("Transaction ID\t\tTimestamp\t\tNote\n");
523 bzero(&snapshot, sizeof(snapshot));
525 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
526 err(2, "hammer snapls: %s: not HAMMER fs or "
527 "version < 3", path);
530 for (i = 0; i < snapshot.count; ++i) {
531 snap = &snapshot.snaps[i];
533 t = snap->ts / 1000000ULL;
535 strftime(snapts, sizeof(snapts),
536 "%Y-%m-%d %H:%M:%S %Z", tp);
537 printf("0x%016jx\t%s\t%s\n",
538 (uintmax_t)snap->tid, snapts,
539 strlen(snap->label) ? snap->label : "-");
541 } while (snapshot.head.error == 0 && snapshot.count);
546 snapshot_del(int fsfd, hammer_tid_t tid)
548 struct hammer_ioc_snapshot snapshot;
549 struct hammer_ioc_version version;
551 bzero(&version, sizeof(version));
553 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
554 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
556 if (version.cur_version < 3) {
557 errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
558 " 3 to use this directive", (uintmax_t)tid);
561 bzero(&snapshot, sizeof(snapshot));
563 snapshot.snaps[0].tid = tid;
566 * Do not abort if we are unable to remove the meta-data.
568 if (ioctl(fsfd, HAMMERIOC_DEL_SNAPSHOT, &snapshot) < 0) {
569 err(2, "hammer snaprm 0x%016jx",
571 } else if (snapshot.head.error == ENOENT) {
572 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: "
573 "meta-data not found\n",
575 } else if (snapshot.head.error) {
576 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: %s\n",
577 (uintmax_t)tid, strerror(snapshot.head.error));
583 snapshot_usage(int exit_code)
586 "hammer snap <path> [<note>]\t\tcreate snapshot & link, points to\n"
587 "\t\t\t\t\tbase of PFS mount\n"
588 "hammer snaplo <path> [<note>]\t\tcreate snapshot & link, points to\n"
589 "\t\t\t\t\ttarget dir\n"
590 "hammer snapq <dir> [<note>]\t\tcreate snapshot, output path to stdout\n"
591 "hammer snaprm <path> ...\t\tdelete snapshots; filesystem is CWD\n"
592 "hammer snaprm <transid> ...\t\tdelete snapshots\n"
593 "hammer snaprm <filesystem> <transid> ...\tdelete snapshots\n"
594 "hammer snapls [<path> ...]\t\tlist available snapshots\n"
596 "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
597 " in a HAMMER filesystem or PFS may be specified. If the path\n"
598 " specified does not exist this function will also create a\n"
601 " When deleting snapshots transaction ids may be directly specified\n"
602 " or file paths to snapshot softlinks may be specified. If a\n"
603 " softlink is specified the softlink will also be deleted.\n"
605 "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
606 " is still accepted but is a deprecated form. This form will\n"
607 " work for older hammer versions. The new forms only work for\n"
608 " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
609 " to version 3 in-place.\n"
616 dirpart(const char *path)
621 ptr = strrchr(path, '/');
623 while (ptr > path && ptr[-1] == '/')
632 res = malloc(ptr - path + 1);
633 bcopy(path, res, ptr - path);