inpcb: Use netisr_ncpus for listing inpcbs.
[dragonfly.git] / sbin / hammer / cmd_snapshot.c
blobbf98c48d54ee57c7ea4c62d45e5b3fb816dcf676
1 /*
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
9 * are met:
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.
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.
34 * $DragonFly: src/sbin/hammer/cmd_snapshot.c,v 1.7 2008/07/10 18:47:22 mneumann Exp $
37 #include "hammer.h"
39 #define DEFAULT_SNAPSHOT_NAME "snap-%Y%m%d-%H%M"
41 static void snapshot_usage(int exit_code);
42 static void snapshot_add(int fd, const char *fsym, const char *tsym,
43 const char *label, hammer_tid_t tid);
44 static void snapshot_ls(const char *path);
45 static void snapshot_del(int fsfd, hammer_tid_t tid);
46 static char *dirpart(const char *path);
49 * hammer snap <path> [<note>]
51 * Path may be a directory, softlink, or non-existent (a softlink will be
52 * created).
54 void
55 hammer_cmd_snap(char **av, int ac, int tostdout, int fsbase)
57 struct hammer_ioc_synctid synctid;
58 struct hammer_ioc_version version;
59 char *dirpath;
60 char *fsym = NULL;
61 char *tsym = NULL;
62 struct stat st;
63 char note[64];
64 int fsfd;
66 if (ac == 0 || ac > 2) {
67 snapshot_usage(1);
68 /* not reached */
71 if (ac == 2)
72 snprintf(note, sizeof(note), "%s", av[1]);
73 else
74 note[0] = 0;
77 * Figure out the softlink path and directory path
79 if (stat(av[0], &st) < 0) {
80 dirpath = dirpart(av[0]);
81 tsym = strdup(av[0]);
82 } else if (S_ISDIR(st.st_mode)) {
83 time_t t = time(NULL);
84 struct tm *tp;
85 char extbuf[64];
87 tp = localtime(&t);
88 strftime(extbuf, sizeof(extbuf), DEFAULT_SNAPSHOT_NAME, tp);
90 dirpath = strdup(av[0]);
91 asprintf(&tsym, "%s/%s", dirpath, extbuf);
92 } else {
93 err(2, "hammer snap: File %s exists and is not a directory",
94 av[0]);
95 /* not reached */
99 * Get a handle on some directory in the filesystem for the
100 * ioctl (so it is stored in the correct PFS).
102 fsfd = open(dirpath, O_RDONLY);
103 if (fsfd < 0) {
104 err(2, "hammer snap: Cannot open directory %s", dirpath);
105 /* not reached */
109 * Must be at least version 3 to use this command.
111 bzero(&version, sizeof(version));
113 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
114 err(2, "Unable to create snapshot");
115 /* not reached */
116 } else if (version.cur_version < 3) {
117 errx(2, "Unable to create snapshot: This directive requires "
118 "you to upgrade\n"
119 "the filesystem to version 3. "
120 "Use 'hammer snapshot' for legacy operation.");
121 /* not reached */
123 HammerVersion = version.cur_version;
126 * Synctid to get a transaction id for the snapshot.
128 bzero(&synctid, sizeof(synctid));
129 synctid.op = HAMMER_SYNCTID_SYNC2;
130 if (ioctl(fsfd, HAMMERIOC_SYNCTID, &synctid) < 0) {
131 err(2, "hammer snap: Synctid %s failed",
132 dirpath);
134 if (tostdout) {
135 if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
136 printf("%s/@@0x%016jx\n",
137 dirpath, (uintmax_t)synctid.tid);
138 } else {
139 printf("%s@@0x%016jx\n",
140 dirpath, (uintmax_t)synctid.tid);
142 fsym = NULL;
143 tsym = NULL;
147 * Contents of the symlink being created.
149 if (fsbase) {
150 struct statfs buf;
152 if (statfs(dirpath, &buf) < 0) {
153 err(2, "hammer snap: Cannot determine mount for %s",
154 dirpath);
156 asprintf(&fsym, "%s/@@0x%016jx",
157 buf.f_mntonname, (uintmax_t)synctid.tid);
158 } else if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
159 asprintf(&fsym, "%s/@@0x%016jx",
160 dirpath, (uintmax_t)synctid.tid);
161 } else {
162 asprintf(&fsym, "%s@@0x%016jx",
163 dirpath, (uintmax_t)synctid.tid);
167 * Create the snapshot.
169 snapshot_add(fsfd, fsym, tsym, note, synctid.tid);
170 free(dirpath);
171 free(fsym);
172 free(tsym);
176 * hammer snapls [<path> ...]
178 * If no arguments are specified snapshots for the PFS containing the
179 * current directory are listed.
181 void
182 hammer_cmd_snapls(char **av, int ac)
184 int i;
186 for (i = 0; i < ac; ++i)
187 snapshot_ls(av[i]);
188 if (ac == 0)
189 snapshot_ls(".");
193 * hammer snaprm <path> ...
194 * hammer snaprm <transid> ...
195 * hammer snaprm <filesystem> <transid> ...
197 void
198 hammer_cmd_snaprm(char **av, int ac)
200 struct stat st;
201 char linkbuf[1024];
202 intmax_t tid;
203 int fsfd = -1;
204 int i;
205 int delete;
206 enum snaprm_mode { none_m, path_m, tid_m } mode = none_m;
207 char *dirpath;
208 char *ptr, *ptr2;
210 if (ac == 0) {
211 snapshot_usage(1);
212 /* not reached */
215 for (i = 0; i < ac; ++i) {
216 if (lstat(av[i], &st) < 0) {
217 tid = strtoull(av[i], &ptr, 16);
218 if (*ptr) {
219 err(2, "hammer snaprm: not a file or tid: %s",
220 av[i]);
221 /* not reached */
223 if (mode == path_m) {
224 snapshot_usage(1);
225 /* not reached */
227 mode = tid_m;
228 if (fsfd < 0)
229 fsfd = open(".", O_RDONLY);
230 snapshot_del(fsfd, tid);
231 } else if (S_ISDIR(st.st_mode)) {
232 if (i != 0 || ac < 2) {
233 snapshot_usage(1);
234 /* not reached */
236 if (fsfd >= 0)
237 close(fsfd);
238 fsfd = open(av[i], O_RDONLY);
239 if (fsfd < 0) {
240 err(2, "hammer snaprm: cannot open dir %s",
241 av[i]);
242 /* not reached */
244 mode = tid_m;
245 } else if (S_ISLNK(st.st_mode)) {
246 dirpath = dirpart(av[i]);
247 bzero(linkbuf, sizeof(linkbuf));
248 if (readlink(av[i], linkbuf, sizeof(linkbuf) - 1) < 0) {
249 err(2, "hammer snaprm: cannot read softlink: "
250 "%s", av[i]);
251 /* not reached */
253 if (linkbuf[0] == '/') {
254 free(dirpath);
255 dirpath = dirpart(linkbuf);
256 } else {
257 asprintf(&ptr, "%s/%s", dirpath, linkbuf);
258 free(dirpath);
259 dirpath = dirpart(ptr);
260 free(ptr);
263 if (fsfd >= 0)
264 close(fsfd);
265 fsfd = open(dirpath, O_RDONLY);
266 if (fsfd < 0) {
267 err(2, "hammer snaprm: cannot open dir %s",
268 dirpath);
269 /* not reached */
272 delete = 1;
273 if (i == 0 && ac > 1) {
274 mode = path_m;
275 if (lstat(av[1], &st) < 0) {
276 tid = strtoull(av[1], &ptr, 16);
277 if (*ptr == '\0') {
278 delete = 0;
279 mode = tid_m;
282 } else {
283 if (mode == tid_m) {
284 snapshot_usage(1);
285 /* not reached */
287 mode = path_m;
289 if (delete && (ptr = strrchr(linkbuf, '@')) &&
290 ptr > linkbuf && ptr[-1] == '@' && ptr[1]) {
291 tid = strtoull(ptr + 1, &ptr2, 16);
292 if (*ptr2 == '\0') {
293 snapshot_del(fsfd, tid);
294 remove(av[i]);
297 free(dirpath);
298 } else {
299 err(2, "hammer snaprm: not directory or snapshot "
300 "softlink: %s", av[i]);
301 /* not reached */
304 if (fsfd >= 0)
305 close(fsfd);
309 * snapshot <softlink-dir>
310 * snapshot <filesystem> <softlink-dir> [<note>]
312 void
313 hammer_cmd_snapshot(char **av, int ac)
315 const char *filesystem;
316 const char *softlink_dir;
317 char *softlink_fmt;
318 struct statfs buf;
319 struct stat st;
320 struct hammer_ioc_synctid synctid;
321 char *from;
322 char *to;
323 char *note = NULL;
325 if (ac == 1) {
326 filesystem = NULL;
327 softlink_dir = av[0];
328 } else if (ac == 2) {
329 filesystem = av[0];
330 softlink_dir = av[1];
331 } else if (ac == 3) {
332 filesystem = av[0];
333 softlink_dir = av[1];
334 note = av[2];
335 } else {
336 snapshot_usage(1);
337 /* not reached */
340 if (stat(softlink_dir, &st) == 0) {
341 if (!S_ISDIR(st.st_mode))
342 err(2, "File %s already exists", softlink_dir);
344 if (filesystem == NULL) {
345 if (statfs(softlink_dir, &buf) != 0) {
346 err(2, "Unable to determine filesystem of %s",
347 softlink_dir);
349 filesystem = buf.f_mntonname;
352 softlink_fmt = malloc(strlen(softlink_dir) + 1 + 1 +
353 sizeof(DEFAULT_SNAPSHOT_NAME));
354 if (softlink_fmt == NULL)
355 err(2, "Failed to allocate string");
357 strcpy(softlink_fmt, softlink_dir);
358 if (softlink_fmt[strlen(softlink_fmt)-1] != '/')
359 strcat(softlink_fmt, "/");
360 strcat(softlink_fmt, DEFAULT_SNAPSHOT_NAME);
361 } else {
362 softlink_fmt = strdup(softlink_dir);
364 if (filesystem == NULL) {
366 * strip-off last '/path' segment to get the softlink
367 * directory, which we need to determine the filesystem
368 * we are on.
370 char *pos = strrchr(softlink_fmt, '/');
371 if (pos != NULL)
372 *pos = '\0';
374 if (stat(softlink_fmt, &st) != 0 ||
375 !S_ISDIR(st.st_mode)) {
376 err(2, "Unable to determine softlink dir %s",
377 softlink_fmt);
379 if (statfs(softlink_fmt, &buf) != 0) {
380 err(2, "Unable to determine filesystem of %s",
381 softlink_fmt);
383 filesystem = buf.f_mntonname;
385 /* restore '/' */
386 if (pos != NULL)
387 *pos = '/';
392 * Synctid
394 bzero(&synctid, sizeof(synctid));
395 synctid.op = HAMMER_SYNCTID_SYNC2;
397 int fd = open(filesystem, O_RDONLY);
398 if (fd < 0)
399 err(2, "Unable to open %s", filesystem);
400 if (ioctl(fd, HAMMERIOC_SYNCTID, &synctid) < 0)
401 err(2, "Synctid %s failed", filesystem);
403 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
404 if (from == NULL)
405 err(2, "Couldn't generate string");
407 int sz = strlen(softlink_fmt) + 50;
408 to = malloc(sz);
409 if (to == NULL)
410 err(2, "Failed to allocate string");
412 time_t t = time(NULL);
413 if (strftime(to, sz, softlink_fmt, localtime(&t)) == 0)
414 err(2, "String buffer too small");
416 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
418 snapshot_add(fd, from, to, note, synctid.tid);
420 close(fd);
421 printf("%s\n", to);
423 free(softlink_fmt);
424 free(from);
425 free(to);
428 static
429 void
430 snapshot_add(int fd, const char *fsym, const char *tsym, const char *label,
431 hammer_tid_t tid)
433 struct hammer_ioc_version version;
434 struct hammer_ioc_snapshot snapshot;
436 bzero(&version, sizeof(version));
437 bzero(&snapshot, sizeof(snapshot));
440 * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
442 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0 &&
443 version.cur_version >= 3) {
444 HammerVersion = version.cur_version;
445 snapshot.index = 0;
446 snapshot.count = 1;
447 snapshot.snaps[0].tid = tid;
448 snapshot.snaps[0].ts = time(NULL) * 1000000ULL;
449 if (label) {
450 snprintf(snapshot.snaps[0].label,
451 sizeof(snapshot.snaps[0].label),
452 "%s",
453 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",
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.
468 if (fsym && tsym) {
469 remove(tsym);
470 if (symlink(fsym, tsym) < 0) {
471 err(2, "Unable to create symlink %s", tsym);
476 static
477 void
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 hammer_snapshot_data_t snap;
485 struct tm *tp;
486 time_t t;
487 uint32_t i;
488 int fd;
489 char snapts[64];
491 fd = open(path, O_RDONLY);
492 if (fd < 0) {
493 err(2, "hammer snapls: cannot open %s", path);
494 /* not reached */
497 clrpfs(&pfs, &pfs_od, -1);
498 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
499 err(2, "hammer snapls: cannot retrieve PFS info on %s", path);
500 /* not reached */
503 bzero(&info, sizeof(info));
504 if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
505 err(2, "hammer snapls: cannot retrieve HAMMER info");
506 /* not reached */
509 printf("Snapshots on %s\tPFS#%d\n", path, pfs.pfs_id);
510 printf("Transaction ID\t\tTimestamp\t\tNote\n");
512 bzero(&snapshot, sizeof(snapshot));
513 do {
514 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
515 err(2, "hammer snapls: %s: not HAMMER fs or "
516 "version < 3", path);
517 /* not reached */
519 for (i = 0; i < snapshot.count; ++i) {
520 snap = &snapshot.snaps[i];
522 t = snap->ts / 1000000ULL;
523 tp = localtime(&t);
524 strftime(snapts, sizeof(snapts),
525 "%Y-%m-%d %H:%M:%S %Z", tp);
526 printf("0x%016jx\t%s\t%s\n",
527 (uintmax_t)snap->tid, snapts,
528 strlen(snap->label) ? snap->label : "-");
530 } while (snapshot.head.error == 0 && snapshot.count);
533 static
534 void
535 snapshot_del(int fsfd, hammer_tid_t tid)
537 struct hammer_ioc_snapshot snapshot;
538 struct hammer_ioc_version version;
540 bzero(&version, sizeof(version));
542 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
543 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
545 HammerVersion = version.cur_version;
546 if (version.cur_version < 3) {
547 errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
548 " 3 to use this directive", (uintmax_t)tid);
551 bzero(&snapshot, sizeof(snapshot));
552 snapshot.count = 1;
553 snapshot.snaps[0].tid = tid;
556 * Do not abort if we are unable to remove the meta-data.
558 if (ioctl(fsfd, HAMMERIOC_DEL_SNAPSHOT, &snapshot) < 0) {
559 err(2, "hammer snaprm 0x%016jx",
560 (uintmax_t)tid);
561 } else if (snapshot.head.error == ENOENT) {
562 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: "
563 "meta-data not found\n",
564 (uintmax_t)tid);
565 } else if (snapshot.head.error) {
566 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: %s\n",
567 (uintmax_t)tid, strerror(snapshot.head.error));
571 static
572 void
573 snapshot_usage(int exit_code)
575 fprintf(stderr,
576 "hammer snap <path> [<note>]\t\tcreate snapshot & link, points to\n"
577 "\t\t\t\t\tbase of PFS mount\n"
578 "hammer snaplo <path> [<note>]\t\tcreate snapshot & link, points to\n"
579 "\t\t\t\t\ttarget dir\n"
580 "hammer snapq <dir> [<note>]\t\tcreate snapshot, output path to stdout\n"
581 "hammer snaprm <path> ...\t\tdelete snapshots; filesystem is CWD\n"
582 "hammer snaprm <transid> ...\t\tdelete snapshots\n"
583 "hammer snaprm <filesystem> <transid> ...\tdelete snapshots\n"
584 "hammer snapls [<path> ...]\t\tlist available snapshots\n"
585 "\n"
586 "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
587 " in a HAMMER filesystem or PFS may be specified. If the path\n"
588 " specified does not exist this function will also create a\n"
589 " softlink.\n"
590 "\n"
591 " When deleting snapshots transaction ids may be directly specified\n"
592 " or file paths to snapshot softlinks may be specified. If a\n"
593 " softlink is specified the softlink will also be deleted.\n"
594 "\n"
595 "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
596 " is still accepted but is a deprecated form. This form will\n"
597 " work for older hammer versions. The new forms only work for\n"
598 " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
599 " to version 3 in-place.\n"
601 exit(exit_code);
604 static
605 char *
606 dirpart(const char *path)
608 const char *ptr;
609 char *res;
611 ptr = strrchr(path, '/');
612 if (ptr) {
613 while (ptr > path && ptr[-1] == '/')
614 --ptr;
615 if (ptr == path)
616 ptr = NULL;
618 if (ptr == NULL) {
619 path = ".";
620 ptr = path + 1;
622 res = malloc(ptr - path + 1);
623 bcopy(path, res, ptr - path);
624 res[ptr - path] = 0;
625 return(res);