snapshot.c revision 1.3 1 1.3 hannken /* $NetBSD: snapshot.c,v 1.3 2006/10/26 20:02:30 hannken 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 * 3. All advertising materials mentioning features or use of this software
19 1.1 hannken * must display the following acknowledgement:
20 1.1 hannken * This product includes software developed by the NetBSD
21 1.1 hannken * Foundation, Inc. and its contributors.
22 1.1 hannken * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 hannken * contributors may be used to endorse or promote products derived
24 1.1 hannken * from this software without specific prior written permission.
25 1.1 hannken *
26 1.1 hannken * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 hannken * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 hannken * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 hannken * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 hannken * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 hannken * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 hannken * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 hannken * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 hannken * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 hannken * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 hannken * POSSIBILITY OF SUCH DAMAGE.
37 1.1 hannken */
38 1.1 hannken
39 1.1 hannken #include <sys/param.h>
40 1.1 hannken #include <sys/ioctl.h>
41 1.1 hannken #include <sys/mount.h>
42 1.1 hannken #include <sys/stat.h>
43 1.1 hannken
44 1.1 hannken #include <dev/fssvar.h>
45 1.1 hannken
46 1.1 hannken #include <errno.h>
47 1.1 hannken #include <fcntl.h>
48 1.1 hannken #include <stdio.h>
49 1.1 hannken #include <stdlib.h>
50 1.3 hannken #include <string.h>
51 1.1 hannken #include <unistd.h>
52 1.1 hannken
53 1.1 hannken #include "snapshot.h"
54 1.1 hannken
55 1.1 hannken /*
56 1.1 hannken * Create a snapshot of the file system currently mounted on the first argument
57 1.1 hannken * using the second argument as backing store and return an open file
58 1.1 hannken * descriptor for the snapshot. If the second argument is NULL, use the first
59 1.1 hannken * as backing store. If the third argument is not NULL, it gets the time the
60 1.3 hannken * snapshot was created. If the fourth argument is not NULL, it gets the
61 1.3 hannken * snapshot device path.
62 1.1 hannken */
63 1.1 hannken int
64 1.3 hannken snap_open(char *mountpoint, char *backup, time_t *snap_date, char **snap_dev)
65 1.1 hannken {
66 1.1 hannken int i, fd, israw, fsinternal, dounlink, flags;
67 1.1 hannken char path[MAXPATHLEN], fss_dev[14];
68 1.1 hannken dev_t mountdev;
69 1.1 hannken struct fss_set fss;
70 1.1 hannken struct fss_get fsg;
71 1.1 hannken struct stat sb;
72 1.1 hannken struct statvfs fsb;
73 1.1 hannken
74 1.1 hannken dounlink = 0;
75 1.1 hannken fd = -1;
76 1.1 hannken
77 1.1 hannken fss.fss_mount = mountpoint;
78 1.1 hannken fss.fss_bstore = backup ? backup : fss.fss_mount;
79 1.1 hannken fss.fss_csize = 0;
80 1.1 hannken
81 1.1 hannken if (stat(fss.fss_mount, &sb) < 0)
82 1.1 hannken goto fail;
83 1.1 hannken mountdev = sb.st_dev;
84 1.1 hannken
85 1.1 hannken /*
86 1.1 hannken * Prepare the backing store. `backup' is either a raw device,
87 1.1 hannken * a file or a directory. If it is a file, it must not exist.
88 1.1 hannken */
89 1.1 hannken israw = 0;
90 1.1 hannken if (stat(fss.fss_bstore, &sb) == 0) {
91 1.1 hannken if (S_ISDIR(sb.st_mode)) {
92 1.1 hannken snprintf(path, sizeof(path),
93 1.1 hannken "%s/XXXXXXXXXX", fss.fss_bstore);
94 1.1 hannken fd = mkstemp(path);
95 1.1 hannken fss.fss_bstore = path;
96 1.1 hannken dounlink = 1;
97 1.1 hannken } else if (S_ISCHR(sb.st_mode)) {
98 1.1 hannken fd = open(fss.fss_bstore, O_RDWR);
99 1.1 hannken israw = 1;
100 1.1 hannken } else
101 1.1 hannken goto fail;
102 1.1 hannken } else {
103 1.1 hannken fd = open(fss.fss_bstore, O_CREAT|O_EXCL|O_WRONLY, 0600);
104 1.1 hannken dounlink = 1;
105 1.1 hannken }
106 1.1 hannken if (fd < 0)
107 1.1 hannken goto fail;
108 1.1 hannken
109 1.1 hannken if (fstat(fd, &sb) < 0)
110 1.1 hannken goto fail;
111 1.1 hannken fsinternal = (!israw && sb.st_dev == mountdev);
112 1.1 hannken
113 1.1 hannken /*
114 1.1 hannken * If the backing store is a plain file and the snapshot
115 1.1 hannken * is not file system internal, truncate to file system
116 1.1 hannken * free space.
117 1.1 hannken */
118 1.1 hannken if (!israw && !fsinternal) {
119 1.1 hannken if (statvfs(fss.fss_bstore, &fsb) < 0)
120 1.1 hannken goto fail;
121 1.1 hannken if (ftruncate(fd, (off_t)fsb.f_frsize*fsb.f_bavail) < 0)
122 1.1 hannken goto fail;
123 1.1 hannken }
124 1.1 hannken
125 1.1 hannken if (close(fd) < 0)
126 1.1 hannken goto fail;
127 1.1 hannken
128 1.1 hannken /*
129 1.1 hannken * Create the snapshot on the first free snapshot device.
130 1.1 hannken */
131 1.1 hannken for (i = 0; ; i++) {
132 1.1 hannken snprintf(fss_dev, sizeof(fss_dev), "/dev/rfss%d", i);
133 1.1 hannken if ((fd = open(fss_dev, O_RDWR, 0)) < 0)
134 1.1 hannken goto fail;
135 1.1 hannken
136 1.1 hannken if (ioctl(fd, FSSIOFGET, &flags) < 0)
137 1.1 hannken goto fail;
138 1.1 hannken
139 1.1 hannken if (ioctl(fd, FSSIOCSET, &fss) < 0) {
140 1.1 hannken if (errno != EBUSY)
141 1.1 hannken goto fail;
142 1.1 hannken close(fd);
143 1.1 hannken fd = -1;
144 1.1 hannken continue;
145 1.1 hannken }
146 1.1 hannken
147 1.3 hannken if (snap_dev != NULL) {
148 1.3 hannken *snap_dev = strdup(fss_dev);
149 1.3 hannken if (*snap_dev == NULL) {
150 1.3 hannken ioctl(fd, FSSIOCCLR);
151 1.3 hannken goto fail;
152 1.3 hannken }
153 1.3 hannken }
154 1.3 hannken
155 1.1 hannken flags |= FSS_UNCONFIG_ON_CLOSE;
156 1.1 hannken if (ioctl(fd, FSSIOCGET, &fsg) < 0 ||
157 1.1 hannken ioctl(fd, FSSIOFSET, &flags) < 0 ||
158 1.1 hannken (!israw && unlink(fss.fss_bstore) < 0)) {
159 1.1 hannken ioctl(fd, FSSIOCCLR);
160 1.1 hannken goto fail;
161 1.1 hannken }
162 1.1 hannken
163 1.1 hannken if (snap_date != NULL)
164 1.1 hannken *snap_date = fsg.fsg_time.tv_sec;
165 1.1 hannken return fd;
166 1.1 hannken }
167 1.1 hannken
168 1.1 hannken fail:
169 1.1 hannken if (dounlink)
170 1.1 hannken unlink(fss.fss_bstore);
171 1.1 hannken if (fd >= 0)
172 1.1 hannken close(fd);
173 1.1 hannken
174 1.1 hannken return -1;
175 1.1 hannken }
176