2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.2 2008/06/01 01:33:58 dillon Exp $
40 struct softprune *next;
43 struct hammer_ioc_prune prune;
47 static void softprune_usage(int code);
48 static void hammer_softprune_scandir(struct softprune **basep,
49 struct hammer_ioc_prune *template,
51 static void hammer_softprune_addentry(struct softprune **basep,
52 struct hammer_ioc_prune *template,
53 const char *dirpath, char *linkbuf, char *tidptr);
54 static void hammer_softprune_finalize(struct softprune *scan);
57 * softprune <softlink-dir>
60 hammer_cmd_softprune(char **av, int ac)
62 struct hammer_ioc_prune template;
63 struct softprune *base, *scan;
71 * NOTE: To resitrct to a single file XXX we have to set
72 * the localization the same (not yet implemented). Typically
73 * two passes would be needed, one using HAMMER_LOCALIZE_MISC
74 * and one using HAMMER_LOCALIZE_INODE.
77 bzero(&template, sizeof(template));
78 template.beg_localization = HAMMER_MIN_LOCALIZATION;
79 template.beg_obj_id = HAMMER_MIN_OBJID;
80 template.end_localization = HAMMER_MAX_LOCALIZATION;
81 template.end_obj_id = HAMMER_MAX_OBJID;
82 hammer_get_cycle(&template.end_obj_id, &template.end_localization);
83 template.stat_oldest_tid = HAMMER_MAX_TID;
86 * For now just allow one directory
88 if (ac == 0 || ac > 1)
92 * Scan the softlink directory.
94 hammer_softprune_scandir(&base, &template, *av);
99 * XXX future (need to store separate cycles for each filesystem)
102 fprintf(stderr, "No snapshot softlinks found\n");
106 fprintf(stderr, "Currently only one HAMMER filesystem may "
107 "be specified in the softlink scan\n");
114 for (scan = base; scan; scan = scan->next) {
115 hammer_softprune_finalize(scan);
116 printf("Prune %s: %d snapshots\n",
117 scan->filesystem, scan->prune.nelms);
118 if (scan->prune.nelms == 0)
120 fd = open(scan->filesystem, O_RDONLY);
122 warn("Unable to open %s", scan->filesystem);
126 printf("objspace %016llx %016llx\n", scan->prune.beg_obj_id, scan->prune.end_obj_id);
128 if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) {
129 printf("Prune %s failed: %s\n",
130 scan->filesystem, strerror(errno));
132 } else if (scan->prune.head.flags & HAMMER_IOC_HEAD_INTR) {
133 printf("Prune %s interrupted by timer at "
136 scan->prune.cur_obj_id,
137 scan->prune.cur_localization);
139 hammer_set_cycle(scan->prune.cur_obj_id,
140 scan->prune.cur_localization);
145 hammer_reset_cycle();
146 printf("Prune %s succeeded\n", scan->filesystem);
148 printf("Pruned %lld/%lld records (%lld directory entries) "
150 scan->prune.stat_rawrecords,
151 scan->prune.stat_scanrecords,
152 scan->prune.stat_dirrecords,
153 scan->prune.stat_bytes
162 * Scan a directory for softlinks representing snapshots and build
163 * associated softprune structures.
166 hammer_softprune_scandir(struct softprune **basep,
167 struct hammer_ioc_prune *template,
179 linkbuf = malloc(MAXPATHLEN);
181 if ((dir = opendir(dirname)) == NULL)
182 err(1, "Cannot open directory %s", dirname);
183 while ((den = readdir(dir)) != NULL) {
184 if (strcmp(den->d_name, ".") == 0)
186 if (strcmp(den->d_name, "..") == 0)
190 asprintf(&path, "%s/%s", dirname, den->d_name);
191 if (lstat(path, &st) < 0)
193 if (!S_ISLNK(st.st_mode))
195 if ((len = readlink(path, linkbuf, MAXPATHLEN - 1)) < 0)
198 if ((ptr = strrchr(linkbuf, '@')) &&
199 ptr > linkbuf && ptr[-1] == '@') {
200 hammer_softprune_addentry(basep, template,
201 dirname, linkbuf, ptr - 1);
210 * Add the softlink to the appropriate softprune structure, creating a new
214 hammer_softprune_addentry(struct softprune **basep,
215 struct hammer_ioc_prune *template,
216 const char *dirpath, char *linkbuf, char *tidptr)
218 struct hammer_ioc_prune_elm *elm;
219 struct softprune *scan;
223 if (linkbuf[0] == '/') {
224 asprintf(&fspath, "%*.*s",
225 (tidptr - linkbuf), (tidptr - linkbuf), linkbuf);
227 asprintf(&fspath, "%s/%*.*s", dirpath,
228 (tidptr - linkbuf), (tidptr - linkbuf), linkbuf);
230 if (statfs(fspath, &fs) < 0) {
236 * Locate the filesystem in an existing softprune structure
238 for (scan = *basep; scan; scan = scan->next) {
239 if (bcmp(&fs.f_fsid, &scan->fs.f_fsid, sizeof(fs.f_fsid)) != 0)
241 if (strcmp(fs.f_mntonname, scan->fs.f_mntonname) != 0)
247 * Create a new softprune structure if necessasry
250 scan = malloc(sizeof(*scan));
251 bzero(scan, sizeof(*scan));
254 scan->filesystem = fspath;
255 scan->prune = *template;
257 scan->prune.elms = malloc(sizeof(*elm) * scan->maxelms);
265 * Add the entry (unsorted). Just set the beg_tid, we will sort
266 * and set the remaining entries later.
268 * Always leave one entry free for our terminator.
270 if (scan->prune.nelms >= scan->maxelms - 1) {
271 scan->maxelms = (scan->maxelms * 3 / 2);
272 scan->prune.elms = realloc(scan->prune.elms,
273 sizeof(*elm) * scan->maxelms);
275 elm = &scan->prune.elms[scan->prune.nelms];
276 elm->beg_tid = strtoull(tidptr + 2, NULL, 0);
283 * Finalize a softprune structure after scanning in its softlinks.
284 * Sort the elements, remove duplicates, and then fill in end_tid and
287 * The array must end up in descending order.
290 hammer_softprune_qsort_cmp(const void *arg1, const void *arg2)
292 const struct hammer_ioc_prune_elm *elm1 = arg1;
293 const struct hammer_ioc_prune_elm *elm2 = arg2;
295 if (elm1->beg_tid < elm2->beg_tid)
297 if (elm1->beg_tid > elm2->beg_tid)
303 hammer_softprune_finalize(struct softprune *scan)
305 struct hammer_ioc_prune_elm *elm;
309 * Don't do anything if there are no elements.
311 if (scan->prune.nelms == 0)
315 * Sort the elements in descending order, remove duplicates, and
316 * fill in any missing bits.
318 qsort(scan->prune.elms, scan->prune.nelms, sizeof(*elm),
319 hammer_softprune_qsort_cmp);
321 for (i = 0; i < scan->prune.nelms; ++i) {
322 elm = &scan->prune.elms[i];
325 * First (highest TID) (also last if only one element)
327 elm->end_tid = HAMMER_MAX_TID;
328 } else if (elm[0].beg_tid == elm[-1].beg_tid) {
333 if (i != scan->prune.nelms) {
335 (scan->prune.nelms - i) * sizeof(*elm));
343 elm->end_tid = elm[-1].beg_tid;
345 elm->mod_tid = elm->end_tid - elm->beg_tid;
349 * Add a final element to prune everything from transaction id
350 * 0 to the lowest transaction id (aka last so far).
352 assert(scan->prune.nelms < scan->maxelms);
353 elm = &scan->prune.elms[scan->prune.nelms++];
355 elm->end_tid = elm[-1].beg_tid;
356 elm->mod_tid = elm->end_tid - elm->beg_tid;
361 softprune_usage(int code)
363 fprintf(stderr, "Badly formed command, use:\n");
364 fprintf(stderr, "hammer softprune directory-containing-softlinks\n");