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