snapshot.c revision 1.7 1 1.7 mrg /* $NetBSD: snapshot.c,v 1.7 2019/02/03 12:17:14 mrg Exp $ */
2 1.1 hannken
3 1.1 hannken /*-
4 1.2 hannken * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.1 hannken * All rights reserved.
6 1.1 hannken *
7 1.1 hannken * This code is derived from software contributed to The NetBSD Foundation
8 1.1 hannken * by Juergen Hannken-Illjes.
9 1.1 hannken *
10 1.1 hannken * Redistribution and use in source and binary forms, with or without
11 1.1 hannken * modification, are permitted provided that the following conditions
12 1.1 hannken * are met:
13 1.1 hannken * 1. Redistributions of source code must retain the above copyright
14 1.1 hannken * notice, this list of conditions and the following disclaimer.
15 1.1 hannken * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 hannken * notice, this list of conditions and the following disclaimer in the
17 1.1 hannken * documentation and/or other materials provided with the distribution.
18 1.1 hannken *
19 1.1 hannken * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 hannken * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 hannken * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 hannken * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 hannken * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 hannken * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 hannken * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 hannken * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 hannken * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 hannken * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 hannken * POSSIBILITY OF SUCH DAMAGE.
30 1.1 hannken */
31 1.1 hannken
32 1.1 hannken #include <sys/param.h>
33 1.1 hannken #include <sys/ioctl.h>
34 1.1 hannken #include <sys/mount.h>
35 1.1 hannken #include <sys/stat.h>
36 1.1 hannken
37 1.1 hannken #include <dev/fssvar.h>
38 1.1 hannken
39 1.1 hannken #include <errno.h>
40 1.1 hannken #include <fcntl.h>
41 1.1 hannken #include <stdio.h>
42 1.1 hannken #include <stdlib.h>
43 1.3 hannken #include <string.h>
44 1.1 hannken #include <unistd.h>
45 1.1 hannken
46 1.1 hannken #include "snapshot.h"
47 1.1 hannken
48 1.1 hannken /*
49 1.1 hannken * Create a snapshot of the file system currently mounted on the first argument
50 1.1 hannken * using the second argument as backing store and return an open file
51 1.1 hannken * descriptor for the snapshot. If the second argument is NULL, use the first
52 1.1 hannken * as backing store. If the third argument is not NULL, it gets the time the
53 1.3 hannken * snapshot was created. If the fourth argument is not NULL, it gets the
54 1.3 hannken * snapshot device path.
55 1.1 hannken */
56 1.1 hannken int
57 1.5 hannken snap_open(char *file, char *backup, time_t *snap_date, char **snap_dev)
58 1.1 hannken {
59 1.6 hannken int i, n, fd, israw, fsinternal, dounlink;
60 1.7 mrg char path[MAXPATHLEN], fss_dev[20], *cp;
61 1.1 hannken dev_t mountdev;
62 1.1 hannken struct fss_set fss;
63 1.1 hannken struct fss_get fsg;
64 1.1 hannken struct stat sb;
65 1.5 hannken struct statvfs *mntbuf, *fs, fsb;
66 1.1 hannken
67 1.1 hannken dounlink = 0;
68 1.1 hannken fd = -1;
69 1.5 hannken mntbuf = NULL;
70 1.1 hannken
71 1.5 hannken /*
72 1.5 hannken * Lookup the mount point. `file' is either a directory or a raw
73 1.5 hannken * character device.
74 1.5 hannken */
75 1.5 hannken if (lstat(file, &sb) < 0)
76 1.5 hannken goto fail;
77 1.5 hannken fss.fss_mount = NULL;
78 1.5 hannken if (S_ISCHR(sb.st_mode)) {
79 1.5 hannken if ((cp = strrchr(file, '/')) == NULL || cp[1] != 'r') {
80 1.5 hannken errno = EINVAL;
81 1.5 hannken goto fail;
82 1.5 hannken }
83 1.5 hannken snprintf(path, sizeof(path), "%.*s/%s",
84 1.5 hannken (int)(cp - file), file, cp + 2);
85 1.5 hannken n = getmntinfo(&mntbuf, MNT_NOWAIT);
86 1.5 hannken for (fs = mntbuf, i = 0; i < n; i++, fs++) {
87 1.5 hannken if (strcmp(fs->f_mntfromname, path) == 0) {
88 1.5 hannken fss.fss_mount = fs->f_mntonname;
89 1.5 hannken if (stat(fss.fss_mount, &sb) < 0)
90 1.5 hannken goto fail;
91 1.5 hannken break;
92 1.5 hannken }
93 1.5 hannken }
94 1.5 hannken } else if (S_ISDIR(sb.st_mode))
95 1.5 hannken fss.fss_mount = file;
96 1.5 hannken if (fss.fss_mount == NULL) {
97 1.5 hannken errno = EINVAL;
98 1.5 hannken goto fail;
99 1.5 hannken }
100 1.1 hannken fss.fss_bstore = backup ? backup : fss.fss_mount;
101 1.1 hannken fss.fss_csize = 0;
102 1.1 hannken mountdev = sb.st_dev;
103 1.1 hannken
104 1.1 hannken /*
105 1.1 hannken * Prepare the backing store. `backup' is either a raw device,
106 1.1 hannken * a file or a directory. If it is a file, it must not exist.
107 1.1 hannken */
108 1.1 hannken israw = 0;
109 1.1 hannken if (stat(fss.fss_bstore, &sb) == 0) {
110 1.1 hannken if (S_ISDIR(sb.st_mode)) {
111 1.1 hannken snprintf(path, sizeof(path),
112 1.1 hannken "%s/XXXXXXXXXX", fss.fss_bstore);
113 1.1 hannken fd = mkstemp(path);
114 1.1 hannken fss.fss_bstore = path;
115 1.1 hannken dounlink = 1;
116 1.1 hannken } else if (S_ISCHR(sb.st_mode)) {
117 1.1 hannken fd = open(fss.fss_bstore, O_RDWR);
118 1.1 hannken israw = 1;
119 1.1 hannken } else
120 1.1 hannken goto fail;
121 1.1 hannken } else {
122 1.1 hannken fd = open(fss.fss_bstore, O_CREAT|O_EXCL|O_WRONLY, 0600);
123 1.1 hannken dounlink = 1;
124 1.1 hannken }
125 1.1 hannken if (fd < 0)
126 1.1 hannken goto fail;
127 1.1 hannken
128 1.1 hannken if (fstat(fd, &sb) < 0)
129 1.1 hannken goto fail;
130 1.1 hannken fsinternal = (!israw && sb.st_dev == mountdev);
131 1.1 hannken
132 1.1 hannken /*
133 1.1 hannken * If the backing store is a plain file and the snapshot
134 1.1 hannken * is not file system internal, truncate to file system
135 1.1 hannken * free space.
136 1.1 hannken */
137 1.1 hannken if (!israw && !fsinternal) {
138 1.1 hannken if (statvfs(fss.fss_bstore, &fsb) < 0)
139 1.1 hannken goto fail;
140 1.1 hannken if (ftruncate(fd, (off_t)fsb.f_frsize*fsb.f_bavail) < 0)
141 1.1 hannken goto fail;
142 1.1 hannken }
143 1.1 hannken
144 1.1 hannken if (close(fd) < 0)
145 1.1 hannken goto fail;
146 1.1 hannken
147 1.6 hannken fss.fss_flags = FSS_UNCONFIG_ON_CLOSE;
148 1.6 hannken if (dounlink)
149 1.6 hannken fss.fss_flags |= FSS_UNLINK_ON_CREATE;
150 1.1 hannken /*
151 1.1 hannken * Create the snapshot on the first free snapshot device.
152 1.1 hannken */
153 1.1 hannken for (i = 0; ; i++) {
154 1.1 hannken snprintf(fss_dev, sizeof(fss_dev), "/dev/rfss%d", i);
155 1.1 hannken if ((fd = open(fss_dev, O_RDWR, 0)) < 0)
156 1.1 hannken goto fail;
157 1.1 hannken
158 1.1 hannken if (ioctl(fd, FSSIOCSET, &fss) < 0) {
159 1.1 hannken if (errno != EBUSY)
160 1.1 hannken goto fail;
161 1.1 hannken close(fd);
162 1.1 hannken fd = -1;
163 1.1 hannken continue;
164 1.1 hannken }
165 1.6 hannken dounlink = 0;
166 1.1 hannken
167 1.3 hannken if (snap_dev != NULL) {
168 1.3 hannken *snap_dev = strdup(fss_dev);
169 1.3 hannken if (*snap_dev == NULL) {
170 1.3 hannken ioctl(fd, FSSIOCCLR);
171 1.3 hannken goto fail;
172 1.3 hannken }
173 1.3 hannken }
174 1.3 hannken
175 1.6 hannken if (ioctl(fd, FSSIOCGET, &fsg) < 0) {
176 1.1 hannken ioctl(fd, FSSIOCCLR);
177 1.1 hannken goto fail;
178 1.1 hannken }
179 1.1 hannken
180 1.5 hannken if (mntbuf)
181 1.5 hannken free(mntbuf);
182 1.1 hannken if (snap_date != NULL)
183 1.1 hannken *snap_date = fsg.fsg_time.tv_sec;
184 1.1 hannken return fd;
185 1.1 hannken }
186 1.1 hannken
187 1.1 hannken fail:
188 1.5 hannken if (mntbuf)
189 1.5 hannken free(mntbuf);
190 1.1 hannken if (dounlink)
191 1.1 hannken unlink(fss.fss_bstore);
192 1.1 hannken if (fd >= 0)
193 1.1 hannken close(fd);
194 1.1 hannken
195 1.1 hannken return -1;
196 1.1 hannken }
197