HAMMER - Add additional warnings for v2/v3 upgrades, fix bug in snaprm
[dragonfly.git] / sbin / hammer / cmd_snapshot.c
CommitLineData
6b669ab4
MN
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 *
e0af86f0 34 * $DragonFly: src/sbin/hammer/cmd_snapshot.c,v 1.7 2008/07/10 18:47:22 mneumann Exp $
6b669ab4
MN
35 */
36
37#include "hammer.h"
38#include <sys/param.h>
39#include <sys/mount.h>
96e6b862
MN
40#include <sys/types.h>
41#include <sys/stat.h>
6b669ab4 42#include <unistd.h>
f8052532
MN
43#include <string.h>
44#include <time.h>
6b669ab4 45
96e6b862
MN
46#define DEFAULT_SNAPSHOT_NAME "snap-%Y%m%d-%H%M"
47
6b669ab4 48static void snapshot_usage(int exit_code);
83f2a3aa
MD
49static void snapshot_add(int fd, const char *fsym, const char *tsym,
50 const char *label, hammer_tid_t tid);
51static void snapshot_ls(const char *path);
52static void snapshot_del(int fsfd, hammer_tid_t tid);
53static char *dirpart(const char *path);
54
55/*
56 * hammer snap path ["note"]
57 *
58 * Path may be a directory, softlink, or non-existant (a softlink will be
59 * created).
60 */
61void
62hammer_cmd_snap(char **av, int ac, int tostdout, int fsbase)
63{
64 struct hammer_ioc_synctid synctid;
65 struct hammer_ioc_version version;
66 char *dirpath;
67 char *fsym;
68 char *tsym;
69 struct stat st;
70 char note[64];
71 int fsfd;
72
73 if (ac == 0 || ac > 2) {
74 snapshot_usage(1);
75 /* not reached */
76 exit(1);
77 }
78
79 if (ac == 2)
80 snprintf(note, sizeof(note), "%s", av[1]);
81 else
82 note[0] = 0;
83
84 /*
85 * Figure out the softlink path and directory path
86 */
87 if (stat(av[0], &st) < 0) {
88 dirpath = dirpart(av[0]);
89 tsym = av[0];
90 } else if (S_ISLNK(st.st_mode)) {
91 dirpath = dirpart(av[0]);
92 tsym = av[0];
93 } else if (S_ISDIR(st.st_mode)) {
94 time_t t = time(NULL);
95 struct tm *tp;
96 char extbuf[64];
97
98 tp = localtime(&t);
99 strftime(extbuf, sizeof(extbuf), DEFAULT_SNAPSHOT_NAME, tp);
100
101 dirpath = strdup(av[0]);
102 asprintf(&tsym, "%s/%s", dirpath, extbuf);
103 } else {
104 err(2, "hammer snap: File %s exists and is not a softlink\n",
105 av[0]);
106 /* not reached */
107 }
108
109 /*
110 * Get a handle on some directory in the filesystem for the
111 * ioctl (so it is stored in the correct PFS).
112 */
113 fsfd = open(dirpath, O_RDONLY);
114 if (fsfd < 0) {
115 err(2, "hammder snap: Cannot open directory %s\n", dirpath);
116 /* not reached */
117 }
118
119 /*
120 * Must be at least version 3 to use this command.
121 */
122 bzero(&version, sizeof(version));
123
124 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
125 err(2, "Unable to create snapshot");
126 /* not reached */
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 "
131 "operation");
132 /* not reached */
133 }
134
135 /*
136 * Synctid to get a transaction id for the snapshot.
137 */
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",
142 dirpath);
143 }
144 if (tostdout) {
145 printf("%s/@@%016jx\n", dirpath, (uintmax_t)synctid.tid);
146 fsym = NULL;
147 tsym = NULL;
148 }
149
150 /*
151 * Contents of the symlink being created.
152 */
153 if (fsbase) {
154 struct statfs buf;
155
156 if (statfs(dirpath, &buf) < 0) {
157 err(2, "hammer snapfs: Cannot determine mount for %s",
158 dirpath);
159 }
160 asprintf(&fsym, "%s/@@0x%016jx",
161 buf.f_mntonname, (uintmax_t)synctid.tid);
162 } else {
163 asprintf(&fsym, "%s/@@0x%016jx",
164 dirpath, (uintmax_t)synctid.tid);
165 }
166
167 /*
168 * Create the snapshot.
169 */
170 snapshot_add(fsfd, fsym, tsym, note, synctid.tid);
171 free(dirpath);
172}
173
174/*
175 * hammer snapls [path]*
176 *
177 * If no arguments are specified snapshots for the PFS containing the
178 * current directory are listed.
179 */
180void
181hammer_cmd_snapls(char **av, int ac)
182{
183 int i;
184
185 for (i = 0; i < ac; ++i)
186 snapshot_ls(av[i]);
187 if (ac == 0)
188 snapshot_ls(".");
189}
190
191/*
192 * hammer snaprm [fsdir] [path/transid]*
193 */
194void
195hammer_cmd_snaprm(char **av, int ac)
196{
197 struct stat st;
198 char linkbuf[1024];
199 intmax_t tid;
200 int fsfd = -1;
201 int i;
202 char *dirpath;
203 char *ptr;
204
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",
210 av[i]);
211 /* not reached */
212 }
92ed14a3
MD
213 if (fsfd < 0)
214 fsfd = open(".", O_RDONLY);
83f2a3aa
MD
215 snapshot_del(fsfd, tid);
216 } else if (S_ISDIR(st.st_mode)) {
217 if (fsfd >= 0)
218 close(fsfd);
219 fsfd = open(av[i], O_RDONLY);
220 if (fsfd < 0) {
221 err(2, "hammer snaprm: cannot open dir %s",
222 av[i]);
223 /* not reached */
224 }
225 } else if (S_ISLNK(st.st_mode)) {
226 dirpath = dirpart(av[i]);
bf2c6489
MD
227 bzero(linkbuf, sizeof(linkbuf));
228 if (readlink(av[i], linkbuf, sizeof(linkbuf) - 1) < 0) {
229 err(2, "hammer snaprm: cannot read softlink: "
230 "%s", av[i]);
231 /* not reached */
232 }
233 if (linkbuf[0] == '/') {
234 free(dirpath);
235 dirpath = dirpart(linkbuf);
236 } else {
237 asprintf(&ptr, "%s/%s", dirpath, linkbuf);
238 free(dirpath);
239 dirpath = dirpart(ptr);
240 }
241
83f2a3aa
MD
242 if (fsfd >= 0)
243 close(fsfd);
244 fsfd = open(dirpath, O_RDONLY);
245 if (fsfd < 0) {
246 err(2, "hammer snaprm: cannot open dir %s",
247 dirpath);
248 /* not reached */
249 }
250
83f2a3aa
MD
251 if ((ptr = strrchr(linkbuf, '@')) &&
252 ptr > linkbuf && ptr[-1] == '@') {
253 tid = strtoull(ptr + 1, NULL, 16);
254 snapshot_del(fsfd, tid);
255 }
256 remove(av[i]);
257 free(dirpath);
258 } else {
259 err(2, "hammer snaprm: not directory or snapshot "
260 "softlink: %s", av[i]);
261 /* not reached */
262 }
263 }
264 if (fsfd >= 0)
265 close(fsfd);
266}
6b669ab4
MN
267
268/*
f8052532
MN
269 * snapshot <softlink-dir-in-filesystem>
270 * snapshot <filesystem> <softlink-dir>
6b669ab4
MN
271 */
272void
273hammer_cmd_snapshot(char **av, int ac)
274{
6b669ab4 275 const char *filesystem;
96e6b862
MN
276 const char *softlink_dir;
277 char *softlink_fmt;
6b669ab4 278 struct statfs buf;
96e6b862 279 struct stat st;
6b669ab4 280 struct hammer_ioc_synctid synctid;
6b669ab4
MN
281 char *from;
282 char *to;
b5ec5ad4 283 char *note = NULL;
6b669ab4 284
f8052532
MN
285 if (ac == 1) {
286 filesystem = NULL;
96e6b862 287 softlink_dir = av[0];
3267eb8a 288 } else if (ac == 2) {
f8052532 289 filesystem = av[0];
96e6b862 290 softlink_dir = av[1];
b5ec5ad4
MD
291 } else if (ac == 3) {
292 filesystem = av[0];
293 softlink_dir = av[1];
294 note = av[2];
3267eb8a 295 } else {
6b669ab4 296 snapshot_usage(1);
83f2a3aa
MD
297 /* not reached */
298 softlink_dir = NULL;
299 filesystem = NULL;
f8052532
MN
300 }
301
96e6b862
MN
302 if (stat(softlink_dir, &st) == 0) {
303 if (!S_ISDIR(st.st_mode))
304 err(2, "File %s already exists", softlink_dir);
305
306 if (filesystem == NULL) {
307 if (statfs(softlink_dir, &buf) != 0) {
308 err(2, "Unable to determine filesystem of %s",
309 softlink_dir);
310 }
311 filesystem = buf.f_mntonname;
312 }
313
314 softlink_fmt = malloc(strlen(softlink_dir) + 1 + 1 +
315 sizeof(DEFAULT_SNAPSHOT_NAME));
316 if (softlink_fmt == NULL)
317 err(2, "Failed to allocate string");
318
319 strcpy(softlink_fmt, softlink_dir);
320 if (softlink_fmt[strlen(softlink_fmt)-1] != '/')
321 strcat(softlink_fmt, "/");
322 strcat(softlink_fmt, DEFAULT_SNAPSHOT_NAME);
3267eb8a 323 } else {
96e6b862
MN
324 softlink_fmt = strdup(softlink_dir);
325
326 if (filesystem == NULL) {
327 /*
328 * strip-off last '/path' segment to get the softlink
329 * directory, which we need to determine the filesystem
330 * we are on.
331 */
332 char *pos = strrchr(softlink_fmt, '/');
a231693e
MN
333 if (pos != NULL)
334 *pos = '\0';
96e6b862
MN
335
336 if (stat(softlink_fmt, &st) != 0 ||
337 !S_ISDIR(st.st_mode)) {
a231693e 338 err(2, "Unable to determine softlink dir %s",
96e6b862 339 softlink_fmt);
a231693e 340 }
96e6b862
MN
341 if (statfs(softlink_fmt, &buf) != 0) {
342 err(2, "Unable to determine filesystem of %s",
343 softlink_fmt);
344 }
345 filesystem = buf.f_mntonname;
6b669ab4 346
e0af86f0 347 /* restore '/' */
96e6b862
MN
348 if (pos != NULL)
349 *pos = '/';
f8052532 350 }
f8052532 351 }
96e6b862 352
f8052532
MN
353 /*
354 * Synctid
355 */
6b669ab4
MN
356 bzero(&synctid, sizeof(synctid));
357 synctid.op = HAMMER_SYNCTID_SYNC2;
83f2a3aa 358
96e6b862 359 int fd = open(filesystem, O_RDONLY);
6b669ab4
MN
360 if (fd < 0)
361 err(2, "Unable to open %s", filesystem);
6764f177 362 if (ioctl(fd, HAMMERIOC_SYNCTID, &synctid) < 0)
6b669ab4 363 err(2, "Synctid %s failed", filesystem);
6b669ab4 364
a276dc6b 365 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
6764f177
MN
366 if (from == NULL)
367 err(2, "Couldn't generate string");
6764f177 368
f8052532
MN
369 int sz = strlen(softlink_fmt) + 50;
370 to = malloc(sz);
6764f177 371 if (to == NULL)
f8052532
MN
372 err(2, "Failed to allocate string");
373
374 time_t t = time(NULL);
375 if (strftime(to, sz, softlink_fmt, localtime(&t)) == 0)
376 err(2, "String buffer too small");
6764f177 377
83f2a3aa
MD
378 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
379
b5ec5ad4 380 snapshot_add(fd, from, to, note, synctid.tid);
83f2a3aa
MD
381
382 close(fd);
6764f177 383 printf("%s\n", to);
6b669ab4 384
96e6b862 385 free(softlink_fmt);
6764f177
MN
386 free(from);
387 free(to);
6b669ab4
MN
388}
389
390static
391void
83f2a3aa
MD
392snapshot_add(int fd, const char *fsym, const char *tsym, const char *label,
393 hammer_tid_t tid)
394{
395 struct hammer_ioc_version version;
396 struct hammer_ioc_snapshot snapshot;
397
398 bzero(&version, sizeof(version));
399 bzero(&snapshot, sizeof(snapshot));
400
b5ec5ad4
MD
401 /*
402 * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
403 */
83f2a3aa
MD
404 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0 &&
405 version.cur_version >= 3) {
406 snapshot.index = 0;
407 snapshot.count = 1;
408 snapshot.snaps[0].tid = tid;
409 snapshot.snaps[0].ts = time(NULL) * 1000000ULL;
410 if (label) {
411 snprintf(snapshot.snaps[0].label,
412 sizeof(snapshot.snaps[0].label),
413 "%s",
414 label);
415 }
416 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, &snapshot) < 0) {
417 err(2, "Unable to create snapshot");
418 } else if (snapshot.head.error &&
419 snapshot.head.error != EEXIST) {
420 errx(2, "Unable to create snapshot: %s\n",
421 strerror(snapshot.head.error));
422 }
423 }
b5ec5ad4
MD
424
425 /*
426 * Create a symlink for the snapshot. If a file exists with the same
427 * name the new symlink will replace it.
428 */
83f2a3aa
MD
429 if (fsym && tsym) {
430 remove(tsym);
431 if (symlink(fsym, tsym) < 0) {
432 err(2, "Unable to create symlink %s", tsym);
433 }
434 }
435}
436
437static
438void
439snapshot_ls(const char *path)
440{
441 /*struct hammer_ioc_version version;*/
442 struct hammer_ioc_snapshot snapshot;
443 struct hammer_ioc_pseudofs_rw pfs;
444 struct hammer_snapshot_data *snap;
445 struct tm *tp;
446 time_t t;
447 u_int32_t i;
448 int fd;
449 char snapts[64];
450
451 fd = open(path, O_RDONLY);
452 if (fd < 0) {
453 err(2, "hammer snapls: cannot open %s", path);
454 /* not reached */
455 }
456
457 bzero(&pfs, sizeof(pfs));
458 pfs.pfs_id = -1;
459 pfs.bytes = sizeof(struct hammer_pseudofs_data);
460 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
461 err(2, "hammer snapls: cannot retrieve PFS info on %s", path);
462 /* not reached */
463 }
464
465 printf("snapshots on pfs %d\n", pfs.pfs_id);
466
467 bzero(&snapshot, sizeof(snapshot));
468 do {
469 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
470 err(2, "hammer snapls: %s: not HAMMER fs or "
471 "version < 3", path);
472 /* not reached */
473 }
474 for (i = 0; i < snapshot.count; ++i) {
475 snap = &snapshot.snaps[i];
476
477 t = snap->ts / 1000000ULL;
478 tp = localtime(&t);
479 strftime(snapts, sizeof(snapts),
480 "%Y-%m-%d %H:%M:%S %Z", tp);
481 printf("0x%016jx\t%s\t%s\n",
482 (uintmax_t)snap->tid, snapts, snap->label);
483 }
484 } while (snapshot.head.error == 0 && snapshot.count);
485}
486
487static
488void
489snapshot_del(int fsfd, hammer_tid_t tid)
490{
491 struct hammer_ioc_snapshot snapshot;
492 struct hammer_ioc_version version;
493
494 bzero(&version, sizeof(version));
495
496 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
497 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
498 }
499 if (version.cur_version < 3) {
500 errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
501 " 3 to use this directive", (uintmax_t)tid);
502 }
503
504 bzero(&snapshot, sizeof(snapshot));
505 snapshot.count = 1;
506 snapshot.snaps[0].tid = tid;
507
92ed14a3
MD
508 /*
509 * Do not abort if we are unable to remove the meta-data.
510 */
83f2a3aa 511 if (ioctl(fsfd, HAMMERIOC_DEL_SNAPSHOT, &snapshot) < 0) {
92ed14a3
MD
512 err(2, "hammer snaprm 0x%016jx",
513 (uintmax_t)tid);
514 } else if (snapshot.head.error == ENOENT) {
515 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: "
516 "meta-data not found\n",
517 (uintmax_t)tid);
83f2a3aa 518 } else if (snapshot.head.error) {
92ed14a3 519 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: %s\n",
83f2a3aa
MD
520 (uintmax_t)tid, strerror(snapshot.head.error));
521 }
522}
523
524static
525void
6b669ab4
MN
526snapshot_usage(int exit_code)
527{
83f2a3aa
MD
528 fprintf(stderr,
529 "hammer snap path [\"note\"]\t- create snapshot & link, "
530 "points to base of PFS mount\n"
531 "hammer snaplo path [\"note\"]\t- create snapshot & link, "
532 "points to target dir\n"
533 "hammer snapq path [\"note\"]\t- create snapshot, output path to stdout\n"
534 "hammer snapls [<fs>]\t\t- list available snapshots.\n"
535 "hammer snaprm [<fs>] [path/transid]*\t- delete snapshots.\n"
536 "\n"
537 "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
538 " in a HAMMER filesystem or PFS may be specified. If the path\n"
539 " specified does not exist this function will also create a\n"
540 " softlink.\n"
541 "\n"
542 " When deleting snapshots transaction ids may be directly specified\n"
543 " or file paths to snapshoft softlinks may be specified. If a\n"
544 " softlink is specified the softlink will also be deleted\n"
545 "\n"
546 "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
547 " is still accepted but is a deprecated form. This form will\n"
548 " work for older hammer versions. The new forms only work for\n"
549 " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
550 " to version 3 in-place\n"
551 );
6b669ab4
MN
552 exit(exit_code);
553}
83f2a3aa
MD
554
555static
556char *
557dirpart(const char *path)
558{
559 const char *ptr;
560 char *res;
561
562 ptr = strrchr(path, '/');
563 if (ptr) {
564 while (ptr > path && ptr[-1] == '/')
565 --ptr;
566 if (ptr == path)
567 ptr = NULL;
568 }
569 if (ptr == NULL) {
570 path = ".";
571 ptr = path + 1;
572 }
573 res = malloc(ptr - path + 1);
574 bcopy(path, res, ptr - path);
575 res[ptr - path] = 0;
576 return(res);
577}