|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: BSD-3-Clause |
| 3 | + * |
| 4 | + * Copyright (c) 2022-2023 Tomohiro Kusumi <tkusumi@netbsd.org> |
| 5 | + * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved. |
| 6 | + * |
| 7 | + * This code is derived from software contributed to The DragonFly Project |
| 8 | + * by Matthew Dillon <dillon@dragonflybsd.org> |
| 9 | + * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> |
| 10 | + * |
| 11 | + * Redistribution and use in source and binary forms, with or without |
| 12 | + * modification, are permitted provided that the following conditions |
| 13 | + * are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in |
| 19 | + * the documentation and/or other materials provided with the |
| 20 | + * distribution. |
| 21 | + * 3. Neither the name of The DragonFly Project nor the names of its |
| 22 | + * contributors may be used to endorse or promote products derived |
| 23 | + * from this software without specific, prior written permission. |
| 24 | + * |
| 25 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 | + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 28 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 29 | + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 31 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 33 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 34 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 35 | + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | + * SUCH DAMAGE. |
| 37 | + */ |
| 38 | + |
| 39 | +#include "hammer2.h" |
| 40 | + |
| 41 | +/* |
| 42 | + * The snapshot is named <PFSNAME>_<YYYYMMDD.HHMMSS.TRANSID> unless |
| 43 | + * overridden by a label. |
| 44 | + * |
| 45 | + * When local non-cache media is involved the media is |
| 46 | + * first synchronized and the snapshot is then based on |
| 47 | + * the media. |
| 48 | + * |
| 49 | + * If the media is remote the snapshot is created on the remote |
| 50 | + * end (if you have sufficient administrative rights) and a local |
| 51 | + * ADMIN or CACHE PFS is created with a connection to the snapshot |
| 52 | + * on the remote. |
| 53 | + * |
| 54 | + * If the client has snapshot rights to multiple remotes then TBD. |
| 55 | + */ |
| 56 | + |
| 57 | +int |
| 58 | +cmd_pfs_snapshot(const char *sel_path, const char *path, const char *label, |
| 59 | + uint32_t pfs_flags) |
| 60 | +{ |
| 61 | + hammer2_ioc_pfs_t pfs; |
| 62 | + int ecode = 0; |
| 63 | + int fd; |
| 64 | + char filename[NAME_MAX + 256]; |
| 65 | + time_t t; |
| 66 | + struct tm *tp; |
| 67 | + |
| 68 | + if (path == NULL) { |
| 69 | + fd = hammer2_ioctl_handle(sel_path); |
| 70 | + } else { |
| 71 | + fd = open(path, O_RDONLY); |
| 72 | + if (fd < 0) |
| 73 | + fprintf(stderr, "Unable to open %s\n", path); |
| 74 | + } |
| 75 | + if (fd < 0) |
| 76 | + return 1; |
| 77 | + |
| 78 | + if (label == NULL) { |
| 79 | + time(&t); |
| 80 | + tp = localtime(&t); |
| 81 | + bzero(&pfs, sizeof(pfs)); |
| 82 | + pfs.name_key = (hammer2_key_t)-1; |
| 83 | + if (ioctl(fd, HAMMER2IOC_PFS_GET, &pfs) < 0) { |
| 84 | + perror("ioctl"); |
| 85 | + } |
| 86 | + snprintf(filename, sizeof(filename), |
| 87 | + "%s.%04d%02d%02d.%02d%02d%02d", |
| 88 | + pfs.name, |
| 89 | + tp->tm_year + 1900, |
| 90 | + tp->tm_mon + 1, |
| 91 | + tp->tm_mday, |
| 92 | + tp->tm_hour, |
| 93 | + tp->tm_min, |
| 94 | + tp->tm_sec); |
| 95 | + label = filename; |
| 96 | + } |
| 97 | + |
| 98 | + bzero(&pfs, sizeof(pfs)); |
| 99 | + strlcpy(pfs.name, label, sizeof(pfs.name)); |
| 100 | + pfs.pfs_flags = pfs_flags; |
| 101 | + |
| 102 | + if (ioctl(fd, HAMMER2IOC_PFS_SNAPSHOT, &pfs) < 0) { |
| 103 | + perror("ioctl"); |
| 104 | + ecode = 1; |
| 105 | + } else { |
| 106 | + printf("created snapshot %s\n", label); |
| 107 | + } |
| 108 | + return ecode; |
| 109 | +} |
0 commit comments