HAMMER Utilities: Sync to 58B
[dragonfly.git] / sbin / hammer / cmd_softprune.c
blob298a78fac373a79aee4b07c6ae225651bfac45fc
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.4 2008/06/24 17:40:21 dillon Exp $
37 #include "hammer.h"
39 struct softprune {
40 struct softprune *next;
41 struct statfs fs;
42 char *filesystem;
43 struct hammer_ioc_prune prune;
44 int maxelms;
47 static void softprune_usage(int code);
48 static void hammer_softprune_scandir(struct softprune **basep,
49 struct hammer_ioc_prune *template,
50 const char *dirname);
51 static struct softprune *hammer_softprune_addentry(struct softprune **basep,
52 struct hammer_ioc_prune *template,
53 const char *dirpath,
54 const char *linkbuf, const char *tidptr);
55 static void hammer_softprune_finalize(struct softprune *scan);
58 * prune <softlink-dir>
59 * prune-everything <filesystem>
61 void
62 hammer_cmd_softprune(char **av, int ac, int everything_opt)
64 struct hammer_ioc_prune template;
65 struct softprune *base, *scan;
66 int fd;
67 int rcode;
69 base = NULL;
70 rcode = 0;
73 * NOTE: To restrict to a single file XXX we have to set
74 * the localization the same (not yet implemented). Typically
75 * two passes would be needed, one using HAMMER_LOCALIZE_MISC
76 * and one using HAMMER_LOCALIZE_INODE.
79 bzero(&template, sizeof(template));
80 template.key_beg.localization = HAMMER_MIN_LOCALIZATION;
81 template.key_beg.obj_id = HAMMER_MIN_OBJID;
82 template.key_end.localization = HAMMER_MAX_LOCALIZATION;
83 template.key_end.obj_id = HAMMER_MAX_OBJID;
84 hammer_get_cycle(&template.key_end.obj_id,
85 &template.key_end.localization);
86 template.stat_oldest_tid = HAMMER_MAX_TID;
89 * For now just allow one directory
91 if (ac == 0 || ac > 1)
92 softprune_usage(1);
95 * Scan the softlink directory.
97 if (everything_opt) {
98 const char *dummylink = "";
99 scan = hammer_softprune_addentry(&base, &template, *av,
100 dummylink, dummylink);
101 if (scan == NULL)
102 softprune_usage(1);
103 scan->prune.nelms = 0;
104 scan->prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
106 } else {
107 hammer_softprune_scandir(&base, &template, *av);
108 ++av;
109 --ac;
113 * XXX future (need to store separate cycles for each filesystem)
115 if (base == NULL) {
116 fprintf(stderr, "No snapshot softlinks found\n");
117 exit(1);
119 if (base->next) {
120 fprintf(stderr, "Currently only one HAMMER filesystem may "
121 "be specified in the softlink scan\n");
122 exit(1);
126 * Issue the prunes
128 for (scan = base; scan; scan = scan->next) {
129 hammer_softprune_finalize(scan);
130 if (everything_opt) {
131 printf("Prune %s: EVERYTHING\n",
132 scan->filesystem);
133 } else {
134 printf("Prune %s: %d snapshots\n",
135 scan->filesystem, scan->prune.nelms);
137 if (scan->prune.nelms == 0 &&
138 (scan->prune.head.flags & HAMMER_IOC_PRUNE_ALL) == 0) {
139 continue;
141 fd = open(scan->filesystem, O_RDONLY);
142 if (fd < 0) {
143 warn("Unable to open %s", scan->filesystem);
144 rcode = 1;
145 continue;
147 printf("objspace %016llx %016llx\n",
148 scan->prune.key_beg.obj_id,
149 scan->prune.key_end.obj_id);
151 if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) {
152 printf("Prune %s failed: %s\n",
153 scan->filesystem, strerror(errno));
154 rcode = 2;
155 } else if (scan->prune.head.flags & HAMMER_IOC_HEAD_INTR) {
156 printf("Prune %s interrupted by timer at "
157 "%016llx %04x\n",
158 scan->filesystem,
159 scan->prune.key_cur.obj_id,
160 scan->prune.key_cur.localization);
161 if (CyclePath) {
162 hammer_set_cycle(
163 scan->prune.key_cur.obj_id,
164 scan->prune.key_cur.localization);
166 rcode = 0;
167 } else {
168 if (CyclePath)
169 hammer_reset_cycle();
170 printf("Prune %s succeeded\n", scan->filesystem);
172 printf("Pruned %lld/%lld records (%lld directory entries) "
173 "and %lld bytes\n",
174 scan->prune.stat_rawrecords,
175 scan->prune.stat_scanrecords,
176 scan->prune.stat_dirrecords,
177 scan->prune.stat_bytes
179 close(fd);
181 if (rcode)
182 exit(rcode);
186 * Scan a directory for softlinks representing snapshots and build
187 * associated softprune structures.
189 static void
190 hammer_softprune_scandir(struct softprune **basep,
191 struct hammer_ioc_prune *template,
192 const char *dirname)
194 struct stat st;
195 struct dirent *den;
196 DIR *dir;
197 char *path;
198 int len;
199 char *linkbuf;
200 char *ptr;
202 path = NULL;
203 linkbuf = malloc(MAXPATHLEN);
205 if ((dir = opendir(dirname)) == NULL)
206 err(1, "Cannot open directory %s", dirname);
207 while ((den = readdir(dir)) != NULL) {
208 if (strcmp(den->d_name, ".") == 0)
209 continue;
210 if (strcmp(den->d_name, "..") == 0)
211 continue;
212 if (path)
213 free(path);
214 asprintf(&path, "%s/%s", dirname, den->d_name);
215 if (lstat(path, &st) < 0)
216 continue;
217 if (!S_ISLNK(st.st_mode))
218 continue;
219 if ((len = readlink(path, linkbuf, MAXPATHLEN - 1)) < 0)
220 continue;
221 linkbuf[len] = 0;
222 if ((ptr = strrchr(linkbuf, '@')) &&
223 ptr > linkbuf && ptr[-1] == '@') {
224 hammer_softprune_addentry(basep, template,
225 dirname, linkbuf, ptr - 1);
228 free(linkbuf);
229 if (path)
230 free(path);
234 * Add the softlink to the appropriate softprune structure, creating a new
235 * if necessary.
237 static
238 struct softprune *
239 hammer_softprune_addentry(struct softprune **basep,
240 struct hammer_ioc_prune *template,
241 const char *dirpath,
242 const char *linkbuf, const char *tidptr)
244 struct hammer_ioc_prune_elm *elm;
245 struct softprune *scan;
246 struct statfs fs;
247 char *fspath;
249 if (linkbuf[0] == '/') {
250 asprintf(&fspath, "%*.*s",
251 (tidptr - linkbuf), (tidptr - linkbuf), linkbuf);
252 } else {
253 asprintf(&fspath, "%s/%*.*s", dirpath,
254 (tidptr - linkbuf), (tidptr - linkbuf), linkbuf);
256 if (statfs(fspath, &fs) < 0) {
257 free(fspath);
258 return(NULL);
262 * Locate the filesystem in an existing softprune structure
264 for (scan = *basep; scan; scan = scan->next) {
265 if (bcmp(&fs.f_fsid, &scan->fs.f_fsid, sizeof(fs.f_fsid)) != 0)
266 continue;
267 if (strcmp(fs.f_mntonname, scan->fs.f_mntonname) != 0)
268 continue;
269 break;
273 * Create a new softprune structure if necessasry
275 if (scan == NULL) {
276 scan = malloc(sizeof(*scan));
277 bzero(scan, sizeof(*scan));
279 scan->fs = fs;
280 scan->filesystem = fspath;
281 scan->prune = *template;
282 scan->maxelms = 32;
283 scan->prune.elms = malloc(sizeof(*elm) * scan->maxelms);
284 scan->next = *basep;
285 *basep = scan;
286 } else {
287 free(fspath);
291 * Add the entry (unsorted). Just set the beg_tid, we will sort
292 * and set the remaining entries later.
294 * Always leave one entry free for our terminator.
296 if (scan->prune.nelms >= scan->maxelms - 1) {
297 scan->maxelms = (scan->maxelms * 3 / 2);
298 scan->prune.elms = realloc(scan->prune.elms,
299 sizeof(*elm) * scan->maxelms);
301 elm = &scan->prune.elms[scan->prune.nelms];
302 elm->beg_tid = strtoull(tidptr + 2, NULL, 0);
303 elm->end_tid = 0;
304 elm->mod_tid = 0;
305 ++scan->prune.nelms;
306 return(scan);
310 * Finalize a softprune structure after scanning in its softlinks.
311 * Sort the elements, remove duplicates, and then fill in end_tid and
312 * mod_tid.
314 * The array must end up in descending order.
316 static int
317 hammer_softprune_qsort_cmp(const void *arg1, const void *arg2)
319 const struct hammer_ioc_prune_elm *elm1 = arg1;
320 const struct hammer_ioc_prune_elm *elm2 = arg2;
322 if (elm1->beg_tid < elm2->beg_tid)
323 return(1);
324 if (elm1->beg_tid > elm2->beg_tid)
325 return(-1);
326 return(0);
329 static void
330 hammer_softprune_finalize(struct softprune *scan)
332 struct hammer_ioc_prune_elm *elm;
333 int i;
336 * Don't do anything if there are no elements.
338 if (scan->prune.nelms == 0)
339 return;
342 * Sort the elements in descending order, remove duplicates, and
343 * fill in any missing bits.
345 qsort(scan->prune.elms, scan->prune.nelms, sizeof(*elm),
346 hammer_softprune_qsort_cmp);
348 for (i = 0; i < scan->prune.nelms; ++i) {
349 elm = &scan->prune.elms[i];
350 if (i == 0) {
352 * First (highest TID) (also last if only one element)
354 elm->end_tid = HAMMER_MAX_TID;
355 } else if (elm[0].beg_tid == elm[-1].beg_tid) {
357 * Remove duplicate
359 --scan->prune.nelms;
360 if (i != scan->prune.nelms) {
361 bcopy(elm + 1, elm,
362 (scan->prune.nelms - i) * sizeof(*elm));
364 --i;
365 continue;
366 } else {
368 * Middle or last.
370 elm->end_tid = elm[-1].beg_tid;
372 elm->mod_tid = elm->end_tid - elm->beg_tid;
376 * Add a final element to prune everything from transaction id
377 * 0 to the lowest transaction id (aka last so far).
379 assert(scan->prune.nelms < scan->maxelms);
380 elm = &scan->prune.elms[scan->prune.nelms++];
381 elm->beg_tid = 1;
382 elm->end_tid = elm[-1].beg_tid;
383 elm->mod_tid = elm->end_tid - elm->beg_tid;
386 static
387 void
388 softprune_usage(int code)
390 fprintf(stderr, "Badly formed prune command, use:\n");
391 fprintf(stderr, "hammer prune <dir-holding-softlinks>\n");
392 fprintf(stderr, "hammer prune-everything <filesystem>\n");
393 exit(code);