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 $
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/types.h>
46 #define DEFAULT_SNAPSHOT_NAME "snap-%Y%m%d-%H%M"
48 static void snapshot_usage(int exit_code);
49 static void snapshot_add(int fd, const char *fsym, const char *tsym,
50 const char *label, hammer_tid_t tid);
51 static void snapshot_ls(const char *path);
52 static void snapshot_del(int fsfd, hammer_tid_t tid);
53 static char *dirpart(const char *path);
56 * hammer snap path ["note"]
58 * Path may be a directory, softlink, or non-existant (a softlink will be
62 hammer_cmd_snap(char **av, int ac, int tostdout, int fsbase)
64 struct hammer_ioc_synctid synctid;
65 struct hammer_ioc_version version;
73 if (ac == 0 || ac > 2) {
80 snprintf(note, sizeof(note), "%s", av[1]);
85 * Figure out the softlink path and directory path
87 if (stat(av[0], &st) < 0) {
88 dirpath = dirpart(av[0]);
90 } else if (S_ISLNK(st.st_mode)) {
91 dirpath = dirpart(av[0]);
93 } else if (S_ISDIR(st.st_mode)) {
94 time_t t = time(NULL);
99 strftime(extbuf, sizeof(extbuf), DEFAULT_SNAPSHOT_NAME, tp);
101 dirpath = strdup(av[0]);
102 asprintf(&tsym, "%s/%s", dirpath, extbuf);
104 err(2, "hammer snap: File %s exists and is not a softlink\n",
110 * Get a handle on some directory in the filesystem for the
111 * ioctl (so it is stored in the correct PFS).
113 fsfd = open(dirpath, O_RDONLY);
115 err(2, "hammder snap: Cannot open directory %s\n", dirpath);
120 * Must be at least version 3 to use this command.
122 bzero(&version, sizeof(version));
124 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
125 err(2, "Unable to create snapshot");
127 } else if (version.cur_version < 3) {
128 errx(2, "Unable to create snapshot: This directive requires "
129 "you to upgrade the filesystem\n"
130 "to version 3. Use 'hammer snapshot' for legacy "
136 * Synctid to get a transaction id for the snapshot.
138 bzero(&synctid, sizeof(synctid));
139 synctid.op = HAMMER_SYNCTID_SYNC2;
140 if (ioctl(fsfd, HAMMERIOC_SYNCTID, &synctid) < 0) {
141 err(2, "hammer snap: Synctid %s failed",
145 printf("%s/@@%016jx\n", dirpath, (uintmax_t)synctid.tid);
151 * Contents of the symlink being created.
156 if (statfs(dirpath, &buf) < 0) {
157 err(2, "hammer snapfs: Cannot determine mount for %s",
160 asprintf(&fsym, "%s/@@0x%016jx",
161 buf.f_mntonname, (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);
175 * hammer snapls [path]*
177 * If no arguments are specified snapshots for the PFS containing the
178 * current directory are listed.
181 hammer_cmd_snapls(char **av, int ac)
185 for (i = 0; i < ac; ++i)
192 * hammer snaprm [fsdir] [path/transid]*
195 hammer_cmd_snaprm(char **av, int ac)
205 for (i = 0; i < ac; ++i) {
206 if (lstat(av[i], &st) < 0) {
207 tid = strtoull(av[i], &ptr, 16);
208 if (tid == 0 || *ptr) {
209 err(2, "hammer snaprm: not a file or tid: %s",
213 snapshot_del(fsfd, tid);
214 } else if (S_ISDIR(st.st_mode)) {
217 fsfd = open(av[i], O_RDONLY);
219 err(2, "hammer snaprm: cannot open dir %s",
223 } else if (S_ISLNK(st.st_mode)) {
224 dirpath = dirpart(av[i]);
227 fsfd = open(dirpath, O_RDONLY);
229 err(2, "hammer snaprm: cannot open dir %s",
234 bzero(linkbuf, sizeof(linkbuf));
235 if (readlink(av[i], linkbuf, sizeof(linkbuf) - 1) < 0) {
236 err(2, "hammer snaprm: cannot read softlink: "
240 if ((ptr = strrchr(linkbuf, '@')) &&
241 ptr > linkbuf && ptr[-1] == '@') {
242 tid = strtoull(ptr + 1, NULL, 16);
243 snapshot_del(fsfd, tid);
248 err(2, "hammer snaprm: not directory or snapshot "
249 "softlink: %s", av[i]);
258 * snapshot <softlink-dir-in-filesystem>
259 * snapshot <filesystem> <softlink-dir>
262 hammer_cmd_snapshot(char **av, int ac)
264 const char *filesystem;
265 const char *softlink_dir;
269 struct hammer_ioc_synctid synctid;
276 softlink_dir = av[0];
277 } else if (ac == 2) {
279 softlink_dir = av[1];
280 } else if (ac == 3) {
282 softlink_dir = av[1];
291 if (stat(softlink_dir, &st) == 0) {
292 if (!S_ISDIR(st.st_mode))
293 err(2, "File %s already exists", softlink_dir);
295 if (filesystem == NULL) {
296 if (statfs(softlink_dir, &buf) != 0) {
297 err(2, "Unable to determine filesystem of %s",
300 filesystem = buf.f_mntonname;
303 softlink_fmt = malloc(strlen(softlink_dir) + 1 + 1 +
304 sizeof(DEFAULT_SNAPSHOT_NAME));
305 if (softlink_fmt == NULL)
306 err(2, "Failed to allocate string");
308 strcpy(softlink_fmt, softlink_dir);
309 if (softlink_fmt[strlen(softlink_fmt)-1] != '/')
310 strcat(softlink_fmt, "/");
311 strcat(softlink_fmt, DEFAULT_SNAPSHOT_NAME);
313 softlink_fmt = strdup(softlink_dir);
315 if (filesystem == NULL) {
317 * strip-off last '/path' segment to get the softlink
318 * directory, which we need to determine the filesystem
321 char *pos = strrchr(softlink_fmt, '/');
325 if (stat(softlink_fmt, &st) != 0 ||
326 !S_ISDIR(st.st_mode)) {
327 err(2, "Unable to determine softlink dir %s",
330 if (statfs(softlink_fmt, &buf) != 0) {
331 err(2, "Unable to determine filesystem of %s",
334 filesystem = buf.f_mntonname;
345 bzero(&synctid, sizeof(synctid));
346 synctid.op = HAMMER_SYNCTID_SYNC2;
348 int fd = open(filesystem, O_RDONLY);
350 err(2, "Unable to open %s", filesystem);
351 if (ioctl(fd, HAMMERIOC_SYNCTID, &synctid) < 0)
352 err(2, "Synctid %s failed", filesystem);
354 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
356 err(2, "Couldn't generate string");
358 int sz = strlen(softlink_fmt) + 50;
361 err(2, "Failed to allocate string");
363 time_t t = time(NULL);
364 if (strftime(to, sz, softlink_fmt, localtime(&t)) == 0)
365 err(2, "String buffer too small");
367 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
369 snapshot_add(fd, from, to, note, synctid.tid);
381 snapshot_add(int fd, const char *fsym, const char *tsym, const char *label,
384 struct hammer_ioc_version version;
385 struct hammer_ioc_snapshot snapshot;
387 bzero(&version, sizeof(version));
388 bzero(&snapshot, sizeof(snapshot));
391 * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
393 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0 &&
394 version.cur_version >= 3) {
397 snapshot.snaps[0].tid = tid;
398 snapshot.snaps[0].ts = time(NULL) * 1000000ULL;
400 snprintf(snapshot.snaps[0].label,
401 sizeof(snapshot.snaps[0].label),
405 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, &snapshot) < 0) {
406 err(2, "Unable to create snapshot");
407 } else if (snapshot.head.error &&
408 snapshot.head.error != EEXIST) {
409 errx(2, "Unable to create snapshot: %s\n",
410 strerror(snapshot.head.error));
415 * Create a symlink for the snapshot. If a file exists with the same
416 * name the new symlink will replace it.
420 if (symlink(fsym, tsym) < 0) {
421 err(2, "Unable to create symlink %s", tsym);
428 snapshot_ls(const char *path)
430 /*struct hammer_ioc_version version;*/
431 struct hammer_ioc_snapshot snapshot;
432 struct hammer_ioc_pseudofs_rw pfs;
433 struct hammer_snapshot_data *snap;
440 fd = open(path, O_RDONLY);
442 err(2, "hammer snapls: cannot open %s", path);
446 bzero(&pfs, sizeof(pfs));
448 pfs.bytes = sizeof(struct hammer_pseudofs_data);
449 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
450 err(2, "hammer snapls: cannot retrieve PFS info on %s", path);
454 printf("snapshots on pfs %d\n", pfs.pfs_id);
456 bzero(&snapshot, sizeof(snapshot));
458 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
459 err(2, "hammer snapls: %s: not HAMMER fs or "
460 "version < 3", path);
463 for (i = 0; i < snapshot.count; ++i) {
464 snap = &snapshot.snaps[i];
466 t = snap->ts / 1000000ULL;
468 strftime(snapts, sizeof(snapts),
469 "%Y-%m-%d %H:%M:%S %Z", tp);
470 printf("0x%016jx\t%s\t%s\n",
471 (uintmax_t)snap->tid, snapts, snap->label);
473 } while (snapshot.head.error == 0 && snapshot.count);
478 snapshot_del(int fsfd, hammer_tid_t tid)
480 struct hammer_ioc_snapshot snapshot;
481 struct hammer_ioc_version version;
483 bzero(&version, sizeof(version));
485 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
486 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
488 if (version.cur_version < 3) {
489 errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
490 " 3 to use this directive", (uintmax_t)tid);
493 bzero(&snapshot, sizeof(snapshot));
495 snapshot.snaps[0].tid = tid;
497 if (ioctl(fsfd, HAMMERIOC_DEL_SNAPSHOT, &snapshot) < 0) {
498 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
499 } else if (snapshot.head.error) {
500 fprintf(stderr, "hammer snaprm 0x%016jx: %s\n",
501 (uintmax_t)tid, strerror(snapshot.head.error));
507 snapshot_usage(int exit_code)
510 "hammer snap path [\"note\"]\t- create snapshot & link, "
511 "points to base of PFS mount\n"
512 "hammer snaplo path [\"note\"]\t- create snapshot & link, "
513 "points to target dir\n"
514 "hammer snapq path [\"note\"]\t- create snapshot, output path to stdout\n"
515 "hammer snapls [<fs>]\t\t- list available snapshots.\n"
516 "hammer snaprm [<fs>] [path/transid]*\t- delete snapshots.\n"
518 "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
519 " in a HAMMER filesystem or PFS may be specified. If the path\n"
520 " specified does not exist this function will also create a\n"
523 " When deleting snapshots transaction ids may be directly specified\n"
524 " or file paths to snapshoft softlinks may be specified. If a\n"
525 " softlink is specified the softlink will also be deleted\n"
527 "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
528 " is still accepted but is a deprecated form. This form will\n"
529 " work for older hammer versions. The new forms only work for\n"
530 " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
531 " to version 3 in-place\n"
538 dirpart(const char *path)
543 ptr = strrchr(path, '/');
545 while (ptr > path && ptr[-1] == '/')
554 res = malloc(ptr - path + 1);
555 bcopy(path, res, ptr - path);