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 extern char *find_pfs_mount(int pfsid
, uuid_t parentuuid
, int ismaster
);
48 #define DEFAULT_SNAPSHOT_NAME "snap-%Y%m%d-%H%M"
50 static void snapshot_usage(int exit_code
);
51 static void snapshot_add(int fd
, const char *fsym
, const char *tsym
,
52 const char *label
, hammer_tid_t tid
);
53 static void snapshot_ls(const char *path
);
54 static void snapshot_del(int fsfd
, hammer_tid_t tid
);
55 static char *dirpart(const char *path
);
58 * hammer snap <path> [<note>]
60 * Path may be a directory, softlink, or non-existent (a softlink will be
64 hammer_cmd_snap(char **av
, int ac
, int tostdout
, int fsbase
)
66 struct hammer_ioc_synctid synctid
;
67 struct hammer_ioc_version version
;
75 if (ac
== 0 || ac
> 2) {
82 snprintf(note
, sizeof(note
), "%s", av
[1]);
87 * Figure out the softlink path and directory path
89 if (stat(av
[0], &st
) < 0) {
90 dirpath
= dirpart(av
[0]);
92 } else if (S_ISLNK(st
.st_mode
)) {
93 dirpath
= dirpart(av
[0]);
95 } else if (S_ISDIR(st
.st_mode
)) {
96 time_t t
= time(NULL
);
101 strftime(extbuf
, sizeof(extbuf
), DEFAULT_SNAPSHOT_NAME
, tp
);
103 dirpath
= strdup(av
[0]);
104 asprintf(&tsym
, "%s/%s", dirpath
, extbuf
);
106 err(2, "hammer snap: File %s exists and is not a softlink\n",
112 * Get a handle on some directory in the filesystem for the
113 * ioctl (so it is stored in the correct PFS).
115 fsfd
= open(dirpath
, O_RDONLY
);
117 err(2, "hammer snap: Cannot open directory %s\n", dirpath
);
122 * Must be at least version 3 to use this command.
124 bzero(&version
, sizeof(version
));
126 if (ioctl(fsfd
, HAMMERIOC_GET_VERSION
, &version
) < 0) {
127 err(2, "Unable to create snapshot");
129 } else if (version
.cur_version
< 3) {
130 errx(2, "Unable to create snapshot: This directive requires "
132 "the filesystem to version 3. "
133 "Use 'hammer snapshot' for legacy operation.");
138 * Synctid to get a transaction id for the snapshot.
140 bzero(&synctid
, sizeof(synctid
));
141 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
142 if (ioctl(fsfd
, HAMMERIOC_SYNCTID
, &synctid
) < 0) {
143 err(2, "hammer snap: Synctid %s failed",
147 if (strcmp(dirpath
, ".") == 0 || strcmp(dirpath
, "..") == 0) {
148 printf("%s/@@0x%016jx\n",
149 dirpath
, (uintmax_t)synctid
.tid
);
151 printf("%s@@0x%016jx\n",
152 dirpath
, (uintmax_t)synctid
.tid
);
159 * Contents of the symlink being created.
164 if (statfs(dirpath
, &buf
) < 0) {
165 err(2, "hammer snap: Cannot determine mount for %s",
168 asprintf(&fsym
, "%s/@@0x%016jx",
169 buf
.f_mntonname
, (uintmax_t)synctid
.tid
);
170 } else if (strcmp(dirpath
, ".") == 0 || strcmp(dirpath
, "..") == 0) {
171 asprintf(&fsym
, "%s/@@0x%016jx",
172 dirpath
, (uintmax_t)synctid
.tid
);
174 asprintf(&fsym
, "%s@@0x%016jx",
175 dirpath
, (uintmax_t)synctid
.tid
);
179 * Create the snapshot.
181 snapshot_add(fsfd
, fsym
, tsym
, note
, synctid
.tid
);
186 * hammer snapls [<path> ...]
188 * If no arguments are specified snapshots for the PFS containing the
189 * current directory are listed.
192 hammer_cmd_snapls(char **av
, int ac
)
196 for (i
= 0; i
< ac
; ++i
)
203 * hammer snaprm {<path> | <transid>} ...
206 hammer_cmd_snaprm(char **av
, int ac
)
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",
225 fsfd
= open(".", O_RDONLY
);
226 snapshot_del(fsfd
, tid
);
227 } else if (S_ISDIR(st
.st_mode
)) {
230 fsfd
= open(av
[i
], O_RDONLY
);
232 err(2, "hammer snaprm: cannot open dir %s",
236 } else if (S_ISLNK(st
.st_mode
)) {
237 dirpath
= dirpart(av
[i
]);
238 bzero(linkbuf
, sizeof(linkbuf
));
239 if (readlink(av
[i
], linkbuf
, sizeof(linkbuf
) - 1) < 0) {
240 err(2, "hammer snaprm: cannot read softlink: "
244 if (linkbuf
[0] == '/') {
246 dirpath
= dirpart(linkbuf
);
248 asprintf(&ptr
, "%s/%s", dirpath
, linkbuf
);
250 dirpath
= dirpart(ptr
);
255 fsfd
= open(dirpath
, O_RDONLY
);
257 err(2, "hammer snaprm: cannot open dir %s",
262 if ((ptr
= strrchr(linkbuf
, '@')) &&
263 ptr
> linkbuf
&& ptr
[-1] == '@') {
264 tid
= strtoull(ptr
+ 1, NULL
, 16);
265 snapshot_del(fsfd
, tid
);
270 err(2, "hammer snaprm: not directory or snapshot "
271 "softlink: %s", av
[i
]);
280 * snapshot <softlink-dir>
281 * snapshot <filesystem> <softlink-dir> [<note>]
284 hammer_cmd_snapshot(char **av
, int ac
)
286 const char *filesystem
;
287 const char *softlink_dir
;
291 struct hammer_ioc_synctid synctid
;
298 softlink_dir
= av
[0];
299 } else if (ac
== 2) {
301 softlink_dir
= av
[1];
302 } else if (ac
== 3) {
304 softlink_dir
= av
[1];
313 if (stat(softlink_dir
, &st
) == 0) {
314 if (!S_ISDIR(st
.st_mode
))
315 err(2, "File %s already exists", softlink_dir
);
317 if (filesystem
== NULL
) {
318 if (statfs(softlink_dir
, &buf
) != 0) {
319 err(2, "Unable to determine filesystem of %s",
322 filesystem
= buf
.f_mntonname
;
325 softlink_fmt
= malloc(strlen(softlink_dir
) + 1 + 1 +
326 sizeof(DEFAULT_SNAPSHOT_NAME
));
327 if (softlink_fmt
== NULL
)
328 err(2, "Failed to allocate string");
330 strcpy(softlink_fmt
, softlink_dir
);
331 if (softlink_fmt
[strlen(softlink_fmt
)-1] != '/')
332 strcat(softlink_fmt
, "/");
333 strcat(softlink_fmt
, DEFAULT_SNAPSHOT_NAME
);
335 softlink_fmt
= strdup(softlink_dir
);
337 if (filesystem
== NULL
) {
339 * strip-off last '/path' segment to get the softlink
340 * directory, which we need to determine the filesystem
343 char *pos
= strrchr(softlink_fmt
, '/');
347 if (stat(softlink_fmt
, &st
) != 0 ||
348 !S_ISDIR(st
.st_mode
)) {
349 err(2, "Unable to determine softlink dir %s",
352 if (statfs(softlink_fmt
, &buf
) != 0) {
353 err(2, "Unable to determine filesystem of %s",
356 filesystem
= buf
.f_mntonname
;
367 bzero(&synctid
, sizeof(synctid
));
368 synctid
.op
= HAMMER_SYNCTID_SYNC2
;
370 int fd
= open(filesystem
, O_RDONLY
);
372 err(2, "Unable to open %s", filesystem
);
373 if (ioctl(fd
, HAMMERIOC_SYNCTID
, &synctid
) < 0)
374 err(2, "Synctid %s failed", filesystem
);
376 asprintf(&from
, "%s/@@0x%016jx", filesystem
, (uintmax_t)synctid
.tid
);
378 err(2, "Couldn't generate string");
380 int sz
= strlen(softlink_fmt
) + 50;
383 err(2, "Failed to allocate string");
385 time_t t
= time(NULL
);
386 if (strftime(to
, sz
, softlink_fmt
, localtime(&t
)) == 0)
387 err(2, "String buffer too small");
389 asprintf(&from
, "%s/@@0x%016jx", filesystem
, (uintmax_t)synctid
.tid
);
391 snapshot_add(fd
, from
, to
, note
, synctid
.tid
);
403 snapshot_add(int fd
, const char *fsym
, const char *tsym
, const char *label
,
406 struct hammer_ioc_version version
;
407 struct hammer_ioc_snapshot snapshot
;
409 bzero(&version
, sizeof(version
));
410 bzero(&snapshot
, sizeof(snapshot
));
413 * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
415 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) == 0 &&
416 version
.cur_version
>= 3) {
419 snapshot
.snaps
[0].tid
= tid
;
420 snapshot
.snaps
[0].ts
= time(NULL
) * 1000000ULL;
422 snprintf(snapshot
.snaps
[0].label
,
423 sizeof(snapshot
.snaps
[0].label
),
427 if (ioctl(fd
, HAMMERIOC_ADD_SNAPSHOT
, &snapshot
) < 0) {
428 err(2, "Unable to create snapshot");
429 } else if (snapshot
.head
.error
&&
430 snapshot
.head
.error
!= EEXIST
) {
431 errx(2, "Unable to create snapshot: %s\n",
432 strerror(snapshot
.head
.error
));
437 * Create a symlink for the snapshot. If a file exists with the same
438 * name the new symlink will replace it.
442 if (symlink(fsym
, tsym
) < 0) {
443 err(2, "Unable to create symlink %s", tsym
);
450 snapshot_ls(const char *path
)
452 /*struct hammer_ioc_version version;*/
453 struct hammer_ioc_info info
;
454 struct hammer_ioc_snapshot snapshot
;
455 struct hammer_ioc_pseudofs_rw pfs
;
456 struct hammer_pseudofs_data pfs_od
;
457 struct hammer_snapshot_data
*snap
;
465 fd
= open(path
, O_RDONLY
);
467 err(2, "hammer snapls: cannot open %s", path
);
471 bzero(&pfs
, sizeof(pfs
));
472 bzero(&pfs_od
, sizeof(pfs_od
));
474 pfs
.ondisk
= &pfs_od
;
475 pfs
.bytes
= sizeof(struct hammer_pseudofs_data
);
476 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
477 err(2, "hammer snapls: cannot retrieve PFS info on %s", path
);
481 bzero(&info
, sizeof(info
));
482 if ((ioctl(fd
, HAMMERIOC_GET_INFO
, &info
)) < 0) {
483 err(2, "hammer snapls: cannot retrieve HAMMER info");
487 ismaster
= (pfs_od
.mirror_flags
& HAMMER_PFSD_SLAVE
) ? 0 : 1;
488 mntpoint
= find_pfs_mount(pfs
.pfs_id
, info
.vol_fsid
, ismaster
);
490 /* Note the degenerate case of PFS #0 */
491 printf("Snapshots on %s\tPFS #%d\n",
492 pfs
.pfs_id
== 0 ? path
: mntpoint
, pfs
.pfs_id
);
493 printf("Transaction ID\t\tTimestamp\t\tNote\n");
498 bzero(&snapshot
, sizeof(snapshot
));
500 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
501 err(2, "hammer snapls: %s: not HAMMER fs or "
502 "version < 3", path
);
505 for (i
= 0; i
< snapshot
.count
; ++i
) {
506 snap
= &snapshot
.snaps
[i
];
508 t
= snap
->ts
/ 1000000ULL;
510 strftime(snapts
, sizeof(snapts
),
511 "%Y-%m-%d %H:%M:%S %Z", tp
);
512 printf("0x%016jx\t%s\t%s\n",
513 (uintmax_t)snap
->tid
, snapts
,
514 strlen(snap
->label
) ? snap
->label
: "-");
516 } while (snapshot
.head
.error
== 0 && snapshot
.count
);
521 snapshot_del(int fsfd
, hammer_tid_t tid
)
523 struct hammer_ioc_snapshot snapshot
;
524 struct hammer_ioc_version version
;
526 bzero(&version
, sizeof(version
));
528 if (ioctl(fsfd
, HAMMERIOC_GET_VERSION
, &version
) < 0) {
529 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid
);
531 if (version
.cur_version
< 3) {
532 errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
533 " 3 to use this directive", (uintmax_t)tid
);
536 bzero(&snapshot
, sizeof(snapshot
));
538 snapshot
.snaps
[0].tid
= tid
;
541 * Do not abort if we are unable to remove the meta-data.
543 if (ioctl(fsfd
, HAMMERIOC_DEL_SNAPSHOT
, &snapshot
) < 0) {
544 err(2, "hammer snaprm 0x%016jx",
546 } else if (snapshot
.head
.error
== ENOENT
) {
547 fprintf(stderr
, "Warning: hammer snaprm 0x%016jx: "
548 "meta-data not found\n",
550 } else if (snapshot
.head
.error
) {
551 fprintf(stderr
, "Warning: hammer snaprm 0x%016jx: %s\n",
552 (uintmax_t)tid
, strerror(snapshot
.head
.error
));
558 snapshot_usage(int exit_code
)
561 "hammer snap <path> [<note>]\t\tcreate snapshot & link, points to \n"
562 "\t\t\t\t\tbase of PFS mount\n"
563 "hammer snaplo <path> [<note>]\t\tcreate snapshot & link, points to \n"
564 "\t\t\t\t\ttarget dir\n"
565 "hammer snapq <dir> [<note>]\t\tcreate snapshot, output path to stdout\n"
566 "hammer snaprm {<path> | <transid>} ...\tdelete snapshots\n"
567 "hammer snapls [<path> ...]\t\tlist available snapshots\n"
569 "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
570 " in a HAMMER filesystem or PFS may be specified. If the path\n"
571 " specified does not exist this function will also create a\n"
574 " When deleting snapshots transaction ids may be directly specified\n"
575 " or file paths to snapshot softlinks may be specified. If a\n"
576 " softlink is specified the softlink will also be deleted.\n"
578 "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
579 " is still accepted but is a deprecated form. This form will\n"
580 " work for older hammer versions. The new forms only work for\n"
581 " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
582 " to version 3 in-place.\n"
589 dirpart(const char *path
)
594 ptr
= strrchr(path
, '/');
596 while (ptr
> path
&& ptr
[-1] == '/')
605 res
= malloc(ptr
- path
+ 1);
606 bcopy(path
, res
, ptr
- path
);