fssconfig.c revision 1.1 1 /* $NetBSD: fssconfig.c,v 1.1 2003/12/10 11:40:12 hannken Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/mount.h>
42 #include <sys/stat.h>
43
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51 #include <util.h>
52
53 #include <dev/fssvar.h>
54
55 int vflag = 0;
56 int xflag = 0;
57
58 int mkfile(int, off_t);
59 void config(int, char **);
60 void unconfig(int, char **);
61 void list(int, char **);
62 void usage(void);
63
64 int
65 main(int argc, char **argv)
66 {
67 int ch;
68 void (*action)(int, char **);
69
70 action = config;
71
72 while ((ch = getopt(argc, argv, "cluvx")) != -1) {
73 switch (ch) {
74 case 'c':
75 action = config;
76 break;
77 case 'l':
78 action = list;
79 break;
80 case 'u':
81 action = unconfig;
82 break;
83 case 'v':
84 vflag++;
85 break;
86 case 'x':
87 xflag++;
88 break;
89 default:
90 case '?':
91 usage();
92 /* NOTREACHED */
93 }
94 }
95
96 argc -= optind;
97 argv += optind;
98
99 (*action)(argc, argv);
100
101 exit(0);
102 }
103
104 int
105 mkfile(int fd, off_t size)
106 {
107 char buf[64*1024];
108 ssize_t l;
109
110 memset(buf, 0, sizeof(buf));
111 while (size > 0) {
112 if ((l = write(fd, buf, sizeof(buf))) < 0)
113 return -1;
114 size -= l;
115 }
116
117 return 0;
118 }
119
120 void
121 config(int argc, char **argv)
122 {
123 int fd, isreg, istmp;
124 char full[64], path[MAXPATHLEN];
125 off_t bssize;
126 struct stat sbuf;
127 struct statfs fsbuf;
128 struct fss_set fss;
129
130 if (argc < 3)
131 usage();
132
133 istmp = 0;
134
135 if (statfs(argv[1], &fsbuf) != 0)
136 err(1, "statfs %s", argv[1]);
137
138 fss.fss_mount = argv[1];
139 fss.fss_bstore = argv[2];
140 if (argc > 3)
141 fss.fss_csize = strsuftoll("cluster size", argv[3], 0, INT_MAX);
142 else
143 fss.fss_csize = 0;
144 if (argc > 4)
145 bssize = strsuftoll("bs size", argv[4], 0, LLONG_MAX);
146 else
147 bssize = (off_t)fsbuf.f_blocks*fsbuf.f_bsize/10;
148
149 /*
150 * Create the backing store. If it is a directory, create a temporary
151 * file and set the unlink flag.
152 */
153 if ((fd = open(fss.fss_bstore, O_CREAT|O_TRUNC|O_WRONLY, 0600)) < 0) {
154 if (errno != EISDIR)
155 err(1, "create: %s", fss.fss_bstore);
156 snprintf(path, sizeof(path), "%s/XXXXXXXXXX", fss.fss_bstore);
157 if ((fd = mkstemp(path)) < 0)
158 err(1, "mkstemp: %s", path);
159 fss.fss_bstore = path;
160 istmp = 1;
161 }
162 if (fstat(fd, &sbuf) < 0)
163 err(1, "stat: %s", fss.fss_bstore);
164 isreg = S_ISREG(sbuf.st_mode);
165 if (isreg && mkfile(fd, bssize) < 0)
166 err(1, "write %s", fss.fss_bstore);
167 close(fd);
168
169 if ((fd = opendisk(argv[0], O_RDWR, full, sizeof(full), 0)) < 0) {
170 if (istmp)
171 unlink(fss.fss_bstore);
172 err(1, "open: %s", argv[0]);
173 }
174
175 if (ioctl(fd, FSSIOCSET, &fss) < 0) {
176 if (istmp)
177 unlink(fss.fss_bstore);
178 err(1, "%s: FSSIOCSET", full);
179 }
180
181 if ((xflag || istmp) && isreg && unlink(fss.fss_bstore) < 0)
182 err(1, "unlink: %s", fss.fss_bstore);
183 }
184
185 void
186 unconfig(int argc, char **argv)
187 {
188 int fd;
189 char full[64];
190
191 if (argc != 1)
192 usage();
193
194 if ((fd = opendisk(argv[0], O_RDWR, full, sizeof(full), 0)) < 0)
195 err(1, "open: %s", argv[0]);
196
197 if (ioctl(fd, FSSIOCCLR) < 0)
198 err(1, "%s: FSSIOCCLR", full);
199 }
200
201 void
202 list(int argc, char **argv)
203 {
204 int n, fd;
205 char *dev, path[64], full[64];
206 char clbuf[5], bsbuf[5], tmbuf[64];
207 time_t t;
208 struct fss_get fsg;
209
210 if (argc > 1)
211 usage();
212
213 if (argc > 0)
214 dev = argv[0];
215 else
216 dev = path;
217
218 for (n = 0; ; n++) {
219 if (argc == 0)
220 snprintf(path, sizeof(path), "fss%d", n);
221 if ((fd = opendisk(dev, O_RDONLY, full, sizeof(full), 0)) < 0) {
222 if (argc == 0 && (errno == ENOENT || errno == ENXIO))
223 break;
224 err(1, "open: %s", dev);
225 }
226
227 if (ioctl(fd, FSSIOCGET, &fsg) < 0) {
228 if (errno == ENXIO)
229 printf("%s: not in use\n", dev);
230 else
231 err(1, "%s: FSSIOCGET", full);
232 } else if (vflag) {
233 humanize_number(clbuf, sizeof(clbuf),
234 (int64_t)fsg.fsg_csize,
235 "", HN_AUTOSCALE, HN_B|HN_NOSPACE);
236
237 humanize_number(bsbuf, sizeof(bsbuf),
238 (int64_t)fsg.fsg_bs_size*fsg.fsg_csize,
239 "", HN_AUTOSCALE, HN_B|HN_NOSPACE);
240
241 t = fsg.fsg_time.tv_sec;
242 strftime(tmbuf, sizeof(tmbuf), "%F %T", localtime(&t));
243
244 printf("%s: %s, taken %s, %" PRId64 " clusters of %s"
245 ", %s backup\n", dev, fsg.fsg_mount, tmbuf,
246 fsg.fsg_mount_size, clbuf, bsbuf);
247 } else
248 printf("%s: %s\n", dev, fsg.fsg_mount);
249
250 close(fd);
251
252 if (argc > 0)
253 break;
254 }
255 }
256
257 void
258 usage(void)
259 {
260 fprintf(stderr, "%s",
261 "usage: fssconfig [-cx] device path backup [cluster [size]]\n"
262 " fssconfig -u device\n"
263 " fssconfig -l [-v] [device]\n");
264 exit(1);
265 }
266