kernel - Improve VM fault performance for sequential access
[dragonfly.git] / sbin / hammer / cmd_snapshot.c
blob42779eb4d7b85a85be003a48f9a8b0fc47b97631
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:
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"
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <time.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-existent (a softlink will be
59 * created).
61 void
62 hammer_cmd_snap(char **av, int ac, int tostdout, int fsbase)
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;
73 if (ac == 0 || ac > 2) {
74 snapshot_usage(1);
75 /* not reached */
76 exit(1);
79 if (ac == 2)
80 snprintf(note, sizeof(note), "%s", av[1]);
81 else
82 note[0] = 0;
85 * Figure out the softlink path and directory path
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];
98 tp = localtime(&t);
99 strftime(extbuf, sizeof(extbuf), DEFAULT_SNAPSHOT_NAME, tp);
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 */
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);
114 if (fsfd < 0) {
115 err(2, "hammer snap: Cannot open directory %s\n", dirpath);
116 /* not reached */
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");
126 /* not reached */
127 } else if (version.cur_version < 3) {
128 errx(2, "Unable to create snapshot: This directive requires "
129 "you to upgrade\n"
130 "the filesystem to version 3. "
131 "Use 'hammer snapshot' for legacy operation.");
132 /* not reached */
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",
142 dirpath);
144 if (tostdout) {
145 if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
146 printf("%s/@@0x%016jx\n",
147 dirpath, (uintmax_t)synctid.tid);
148 } else {
149 printf("%s@@0x%016jx\n",
150 dirpath, (uintmax_t)synctid.tid);
152 fsym = NULL;
153 tsym = NULL;
157 * Contents of the symlink being created.
159 if (fsbase) {
160 struct statfs buf;
162 if (statfs(dirpath, &buf) < 0) {
163 err(2, "hammer snap: Cannot determine mount for %s",
164 dirpath);
166 asprintf(&fsym, "%s/@@0x%016jx",
167 buf.f_mntonname, (uintmax_t)synctid.tid);
168 } else if (strcmp(dirpath, ".") == 0 || strcmp(dirpath, "..") == 0) {
169 asprintf(&fsym, "%s/@@0x%016jx",
170 dirpath, (uintmax_t)synctid.tid);
171 } else {
172 asprintf(&fsym, "%s@@0x%016jx",
173 dirpath, (uintmax_t)synctid.tid);
177 * Create the snapshot.
179 snapshot_add(fsfd, fsym, tsym, note, synctid.tid);
180 free(dirpath);
184 * hammer snapls [<path> ...]
186 * If no arguments are specified snapshots for the PFS containing the
187 * current directory are listed.
189 void
190 hammer_cmd_snapls(char **av, int ac)
192 int i;
194 for (i = 0; i < ac; ++i)
195 snapshot_ls(av[i]);
196 if (ac == 0)
197 snapshot_ls(".");
201 * hammer snaprm {<path> | <transid>} ...
203 void
204 hammer_cmd_snaprm(char **av, int ac)
206 struct stat st;
207 char linkbuf[1024];
208 intmax_t tid;
209 int fsfd = -1;
210 int i;
211 char *dirpath;
212 char *ptr;
214 for (i = 0; i < ac; ++i) {
215 if (lstat(av[i], &st) < 0) {
216 tid = strtoull(av[i], &ptr, 16);
217 if (*ptr) {
218 err(2, "hammer snaprm: not a file or tid: %s",
219 av[i]);
220 /* not reached */
222 if (fsfd < 0)
223 fsfd = open(".", O_RDONLY);
224 snapshot_del(fsfd, tid);
225 } else if (S_ISDIR(st.st_mode)) {
226 if (fsfd >= 0)
227 close(fsfd);
228 fsfd = open(av[i], O_RDONLY);
229 if (fsfd < 0) {
230 err(2, "hammer snaprm: cannot open dir %s",
231 av[i]);
232 /* not reached */
234 } else if (S_ISLNK(st.st_mode)) {
235 dirpath = dirpart(av[i]);
236 bzero(linkbuf, sizeof(linkbuf));
237 if (readlink(av[i], linkbuf, sizeof(linkbuf) - 1) < 0) {
238 err(2, "hammer snaprm: cannot read softlink: "
239 "%s", av[i]);
240 /* not reached */
242 if (linkbuf[0] == '/') {
243 free(dirpath);
244 dirpath = dirpart(linkbuf);
245 } else {
246 asprintf(&ptr, "%s/%s", dirpath, linkbuf);
247 free(dirpath);
248 dirpath = dirpart(ptr);
251 if (fsfd >= 0)
252 close(fsfd);
253 fsfd = open(dirpath, O_RDONLY);
254 if (fsfd < 0) {
255 err(2, "hammer snaprm: cannot open dir %s",
256 dirpath);
257 /* not reached */
260 if ((ptr = strrchr(linkbuf, '@')) &&
261 ptr > linkbuf && ptr[-1] == '@') {
262 tid = strtoull(ptr + 1, NULL, 16);
263 snapshot_del(fsfd, tid);
265 remove(av[i]);
266 free(dirpath);
267 } else {
268 err(2, "hammer snaprm: not directory or snapshot "
269 "softlink: %s", av[i]);
270 /* not reached */
273 if (fsfd >= 0)
274 close(fsfd);
278 * snapshot <softlink-dir>
279 * snapshot <filesystem> <softlink-dir> [<note>]
281 void
282 hammer_cmd_snapshot(char **av, int ac)
284 const char *filesystem;
285 const char *softlink_dir;
286 char *softlink_fmt;
287 struct statfs buf;
288 struct stat st;
289 struct hammer_ioc_synctid synctid;
290 char *from;
291 char *to;
292 char *note = NULL;
294 if (ac == 1) {
295 filesystem = NULL;
296 softlink_dir = av[0];
297 } else if (ac == 2) {
298 filesystem = av[0];
299 softlink_dir = av[1];
300 } else if (ac == 3) {
301 filesystem = av[0];
302 softlink_dir = av[1];
303 note = av[2];
304 } else {
305 snapshot_usage(1);
306 /* not reached */
307 softlink_dir = NULL;
308 filesystem = NULL;
311 if (stat(softlink_dir, &st) == 0) {
312 if (!S_ISDIR(st.st_mode))
313 err(2, "File %s already exists", softlink_dir);
315 if (filesystem == NULL) {
316 if (statfs(softlink_dir, &buf) != 0) {
317 err(2, "Unable to determine filesystem of %s",
318 softlink_dir);
320 filesystem = buf.f_mntonname;
323 softlink_fmt = malloc(strlen(softlink_dir) + 1 + 1 +
324 sizeof(DEFAULT_SNAPSHOT_NAME));
325 if (softlink_fmt == NULL)
326 err(2, "Failed to allocate string");
328 strcpy(softlink_fmt, softlink_dir);
329 if (softlink_fmt[strlen(softlink_fmt)-1] != '/')
330 strcat(softlink_fmt, "/");
331 strcat(softlink_fmt, DEFAULT_SNAPSHOT_NAME);
332 } else {
333 softlink_fmt = strdup(softlink_dir);
335 if (filesystem == NULL) {
337 * strip-off last '/path' segment to get the softlink
338 * directory, which we need to determine the filesystem
339 * we are on.
341 char *pos = strrchr(softlink_fmt, '/');
342 if (pos != NULL)
343 *pos = '\0';
345 if (stat(softlink_fmt, &st) != 0 ||
346 !S_ISDIR(st.st_mode)) {
347 err(2, "Unable to determine softlink dir %s",
348 softlink_fmt);
350 if (statfs(softlink_fmt, &buf) != 0) {
351 err(2, "Unable to determine filesystem of %s",
352 softlink_fmt);
354 filesystem = buf.f_mntonname;
356 /* restore '/' */
357 if (pos != NULL)
358 *pos = '/';
363 * Synctid
365 bzero(&synctid, sizeof(synctid));
366 synctid.op = HAMMER_SYNCTID_SYNC2;
368 int fd = open(filesystem, O_RDONLY);
369 if (fd < 0)
370 err(2, "Unable to open %s", filesystem);
371 if (ioctl(fd, HAMMERIOC_SYNCTID, &synctid) < 0)
372 err(2, "Synctid %s failed", filesystem);
374 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
375 if (from == NULL)
376 err(2, "Couldn't generate string");
378 int sz = strlen(softlink_fmt) + 50;
379 to = malloc(sz);
380 if (to == NULL)
381 err(2, "Failed to allocate string");
383 time_t t = time(NULL);
384 if (strftime(to, sz, softlink_fmt, localtime(&t)) == 0)
385 err(2, "String buffer too small");
387 asprintf(&from, "%s/@@0x%016jx", filesystem, (uintmax_t)synctid.tid);
389 snapshot_add(fd, from, to, note, synctid.tid);
391 close(fd);
392 printf("%s\n", to);
394 free(softlink_fmt);
395 free(from);
396 free(to);
399 static
400 void
401 snapshot_add(int fd, const char *fsym, const char *tsym, const char *label,
402 hammer_tid_t tid)
404 struct hammer_ioc_version version;
405 struct hammer_ioc_snapshot snapshot;
407 bzero(&version, sizeof(version));
408 bzero(&snapshot, sizeof(snapshot));
411 * For HAMMER filesystem v3+ the snapshot is recorded in meta-data.
413 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) == 0 &&
414 version.cur_version >= 3) {
415 snapshot.index = 0;
416 snapshot.count = 1;
417 snapshot.snaps[0].tid = tid;
418 snapshot.snaps[0].ts = time(NULL) * 1000000ULL;
419 if (label) {
420 snprintf(snapshot.snaps[0].label,
421 sizeof(snapshot.snaps[0].label),
422 "%s",
423 label);
425 if (ioctl(fd, HAMMERIOC_ADD_SNAPSHOT, &snapshot) < 0) {
426 err(2, "Unable to create snapshot");
427 } else if (snapshot.head.error &&
428 snapshot.head.error != EEXIST) {
429 errx(2, "Unable to create snapshot: %s\n",
430 strerror(snapshot.head.error));
435 * Create a symlink for the snapshot. If a file exists with the same
436 * name the new symlink will replace it.
438 if (fsym && tsym) {
439 remove(tsym);
440 if (symlink(fsym, tsym) < 0) {
441 err(2, "Unable to create symlink %s", tsym);
446 static
447 void
448 snapshot_ls(const char *path)
450 /*struct hammer_ioc_version version;*/
451 struct hammer_ioc_snapshot snapshot;
452 struct hammer_ioc_pseudofs_rw pfs;
453 struct hammer_snapshot_data *snap;
454 struct tm *tp;
455 time_t t;
456 u_int32_t i;
457 int fd;
458 char snapts[64];
460 fd = open(path, O_RDONLY);
461 if (fd < 0) {
462 err(2, "hammer snapls: cannot open %s", path);
463 /* not reached */
466 bzero(&pfs, sizeof(pfs));
467 pfs.pfs_id = -1;
468 pfs.bytes = sizeof(struct hammer_pseudofs_data);
469 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
470 err(2, "hammer snapls: cannot retrieve PFS info on %s", path);
471 /* not reached */
474 printf("Snapshots on PFS #%d\n", pfs.pfs_id);
476 bzero(&snapshot, sizeof(snapshot));
477 do {
478 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
479 err(2, "hammer snapls: %s: not HAMMER fs or "
480 "version < 3", path);
481 /* not reached */
483 for (i = 0; i < snapshot.count; ++i) {
484 snap = &snapshot.snaps[i];
486 t = snap->ts / 1000000ULL;
487 tp = localtime(&t);
488 strftime(snapts, sizeof(snapts),
489 "%Y-%m-%d %H:%M:%S %Z", tp);
490 printf("0x%016jx\t%s\t%s\n",
491 (uintmax_t)snap->tid, snapts, snap->label);
493 } while (snapshot.head.error == 0 && snapshot.count);
496 static
497 void
498 snapshot_del(int fsfd, hammer_tid_t tid)
500 struct hammer_ioc_snapshot snapshot;
501 struct hammer_ioc_version version;
503 bzero(&version, sizeof(version));
505 if (ioctl(fsfd, HAMMERIOC_GET_VERSION, &version) < 0) {
506 err(2, "hammer snaprm 0x%016jx", (uintmax_t)tid);
508 if (version.cur_version < 3) {
509 errx(2, "hammer snaprm 0x%016jx: You must upgrade to version "
510 " 3 to use this directive", (uintmax_t)tid);
513 bzero(&snapshot, sizeof(snapshot));
514 snapshot.count = 1;
515 snapshot.snaps[0].tid = tid;
518 * Do not abort if we are unable to remove the meta-data.
520 if (ioctl(fsfd, HAMMERIOC_DEL_SNAPSHOT, &snapshot) < 0) {
521 err(2, "hammer snaprm 0x%016jx",
522 (uintmax_t)tid);
523 } else if (snapshot.head.error == ENOENT) {
524 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: "
525 "meta-data not found\n",
526 (uintmax_t)tid);
527 } else if (snapshot.head.error) {
528 fprintf(stderr, "Warning: hammer snaprm 0x%016jx: %s\n",
529 (uintmax_t)tid, strerror(snapshot.head.error));
533 static
534 void
535 snapshot_usage(int exit_code)
537 fprintf(stderr,
538 "hammer snap <path> [<note>]\t\tcreate snapshot & link, points to \n"
539 "\t\t\t\t\tbase of PFS mount\n"
540 "hammer snaplo <path> [<note>]\t\tcreate snapshot & link, points to \n"
541 "\t\t\t\t\ttarget dir\n"
542 "hammer snapq <dir> [<note>]\t\tcreate snapshot, output path to stdout\n"
543 "hammer snaprm {<path> | <transid>} ...\tdelete snapshots\n"
544 "hammer snapls [<path> ...]\t\tlist available snapshots\n"
545 "\n"
546 "NOTE: Snapshots are created in filesystem meta-data, any directory\n"
547 " in a HAMMER filesystem or PFS may be specified. If the path\n"
548 " specified does not exist this function will also create a\n"
549 " softlink.\n"
550 "\n"
551 " When deleting snapshots transaction ids may be directly specified\n"
552 " or file paths to snapshot softlinks may be specified. If a\n"
553 " softlink is specified the softlink will also be deleted.\n"
554 "\n"
555 "NOTE: The old 'hammer snapshot [<filesystem>] <snapshot-dir>' form\n"
556 " is still accepted but is a deprecated form. This form will\n"
557 " work for older hammer versions. The new forms only work for\n"
558 " HAMMER version 3 or later filesystems. HAMMER can be upgraded\n"
559 " to version 3 in-place.\n"
561 exit(exit_code);
564 static
565 char *
566 dirpart(const char *path)
568 const char *ptr;
569 char *res;
571 ptr = strrchr(path, '/');
572 if (ptr) {
573 while (ptr > path && ptr[-1] == '/')
574 --ptr;
575 if (ptr == path)
576 ptr = NULL;
578 if (ptr == NULL) {
579 path = ".";
580 ptr = path + 1;
582 res = malloc(ptr - path + 1);
583 bcopy(path, res, ptr - path);
584 res[ptr - path] = 0;
585 return(res);