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.7 2008/08/21 23:28:43 thomas Exp $
39 static void softprune_usage(int code);
40 static int hammer_softprune_scanmeta(int fd, struct softprune *scan,
42 static void hammer_meta_flushdelete(int fd, struct hammer_ioc_snapshot *dsnap);
43 static struct softprune *hammer_softprune_addentry(struct softprune **basep,
44 struct hammer_ioc_prune *template,
45 const char *dirpath, const char *denname,
47 const char *linkbuf, const char *tidptr);
48 static void hammer_softprune_addelm(struct softprune *scan, hammer_tid_t tid,
49 time_t ct, time_t mt);
50 static void hammer_softprune_finalize(struct softprune *scan);
53 * prune <softlink-dir>
54 * prune-everything <filesystem>
57 hammer_cmd_softprune(char **av, int ac, int everything_opt)
59 struct hammer_ioc_prune template;
60 struct hammer_ioc_pseudofs_rw pfs;
61 struct softprune *base, *scan;
70 bzero(&pfs, sizeof(pfs));
71 pfs.bytes = sizeof(*pfs.ondisk);
72 pfs.ondisk = malloc(pfs.bytes);
73 bzero(pfs.ondisk, pfs.bytes);
77 * NOTE: To restrict to a single file XXX we have to set
78 * the localization the same (not yet implemented). Typically
79 * two passes would be needed, one using HAMMER_LOCALIZE_MISC
80 * and one using HAMMER_LOCALIZE_INODE.
83 bzero(&template, sizeof(template));
84 template.key_beg.localization = HAMMER_MIN_LOCALIZATION;
85 template.key_beg.obj_id = HAMMER_MIN_OBJID;
86 template.key_end.localization = HAMMER_MAX_LOCALIZATION;
87 template.key_end.obj_id = HAMMER_MAX_OBJID;
88 hammer_get_cycle(&template.key_end, NULL);
89 template.stat_oldest_tid = HAMMER_MAX_TID;
92 * For now just allow one directory
94 if (ac == 0 || ac > 1)
98 * Scan the softlink directory.
100 if (everything_opt) {
101 const char *dummylink = "";
102 scan = hammer_softprune_addentry(&base, &template,
104 dummylink, dummylink);
107 scan->prune.nelms = 0;
108 scan->prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
110 hammer_softprune_scandir(&base, &template, *av);
112 const char *dummylink = "";
113 scan = hammer_softprune_addentry(&base, &template,
115 dummylink, dummylink);
118 scan->prune.nelms = 0;
125 * XXX future (need to store separate cycles for each filesystem)
128 fprintf(stderr, "Currently only one HAMMER filesystem may "
129 "be specified in the softlink scan\n");
136 for (scan = base; scan; scan = scan->next) {
138 * Open the filesystem for ioctl calls and extract the
141 fd = open(scan->filesystem, O_RDONLY);
143 warn("Unable to open %s", scan->filesystem);
148 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
149 warn("Filesystem %s is not HAMMER", scan->filesystem);
154 scan->prune_min = pfs.ondisk->prune_min;
157 * Incorporate meta-data snapshots into the pruning regimen.
158 * If pruning everything we delete the meta-data snapshots.
160 if (hammer_softprune_scanmeta(fd, scan, everything_opt) < 0) {
161 warn("Filesystem %s could not scan meta-data snaps",
169 * Finalize operations
171 hammer_softprune_finalize(scan);
172 if (everything_opt) {
173 printf("Prune %s: EVERYTHING\n",
176 printf("Prune %s: %d snapshots\n",
177 scan->filesystem, scan->prune.nelms);
179 if (scan->prune.nelms == 0 &&
180 (scan->prune.head.flags & HAMMER_IOC_PRUNE_ALL) == 0) {
181 fprintf(stderr, "No snapshots found\n");
185 printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
188 (uintmax_t)scan->prune.key_beg.obj_id,
189 scan->prune.key_beg.localization,
190 (uintmax_t)scan->prune.key_end.obj_id,
191 scan->prune.key_end.localization,
193 printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
195 pfs.ondisk->prune_min / (24 * 60 * 60),
196 pfs.ondisk->prune_min / 60 / 60 % 24,
197 pfs.ondisk->prune_min / 60 % 60,
198 pfs.ondisk->prune_min % 60);
201 if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) {
202 printf("Prune %s failed: %s\n",
203 scan->filesystem, strerror(errno));
205 } else if (scan->prune.head.flags & HAMMER_IOC_HEAD_INTR) {
206 printf("Prune %s interrupted by timer at "
209 (uintmax_t)scan->prune.key_cur.obj_id,
210 scan->prune.key_cur.localization);
212 hammer_set_cycle(&scan->prune.key_cur, 0);
216 hammer_reset_cycle();
217 printf("Prune %s succeeded\n", scan->filesystem);
219 printf("Pruned %jd/%jd records (%jd directory entries) "
221 (uintmax_t)scan->prune.stat_rawrecords,
222 (uintmax_t)scan->prune.stat_scanrecords,
223 (uintmax_t)scan->prune.stat_dirrecords,
224 (uintmax_t)scan->prune.stat_bytes
234 * Scan a directory for softlinks representing snapshots and build
235 * associated softprune structures.
237 * NOTE: Once a filesystem is completely converted to the meta-data
238 * snapshot mechanic we don't have to scan softlinks any more
239 * and can just use the meta-data. But for now we do both.
242 hammer_softprune_scandir(struct softprune **basep,
243 struct hammer_ioc_prune *template,
255 linkbuf = malloc(MAXPATHLEN);
257 if ((dir = opendir(dirname)) == NULL)
258 err(1, "Cannot open directory %s", dirname);
259 while ((den = readdir(dir)) != NULL) {
260 if (strcmp(den->d_name, ".") == 0)
262 if (strcmp(den->d_name, "..") == 0)
266 asprintf(&path, "%s/%s", dirname, den->d_name);
267 if (lstat(path, &st) < 0)
269 if (!S_ISLNK(st.st_mode))
271 if ((len = readlink(path, linkbuf, MAXPATHLEN - 1)) < 0)
274 if ((ptr = strrchr(linkbuf, '@')) &&
275 ptr > linkbuf && ptr[-1] == '@') {
276 hammer_softprune_addentry(basep, template,
277 dirname, den->d_name, &st,
287 * Scan the metadata snapshots for the filesystem and either delete them
288 * or add them to the pruning list.
292 hammer_softprune_scanmeta(int fd, struct softprune *scan, int delete_all)
294 struct hammer_ioc_version version;
295 struct hammer_ioc_snapshot snapshot;
296 struct hammer_ioc_snapshot dsnapshot;
297 struct hammer_snapshot_data *snap;
301 * Stop if we can't get the version. Meta-data snapshots only
302 * exist for HAMMER version 3 or greater.
304 bzero(&version, sizeof(version));
305 if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
307 if (version.cur_version < 3)
310 bzero(&snapshot, sizeof(snapshot));
311 bzero(&dsnapshot, sizeof(dsnapshot));
314 * Scan meta-data snapshots, either add them to the prune list or
315 * delete them. When deleting, just skip any entries which cannot
319 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
320 printf("hammer prune: Unable to access "
321 "meta-data snaps: %s\n", strerror(errno));
324 while (snapshot.index < snapshot.count) {
325 snap = &snapshot.snaps[snapshot.index];
327 dsnapshot.snaps[dsnapshot.count++] = *snap;
328 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
329 hammer_meta_flushdelete(fd, &dsnapshot);
331 ct = snap->ts / 1000000ULL;
332 hammer_softprune_addelm(scan, snap->tid,
337 if (snapshot.head.flags & HAMMER_IOC_SNAPSHOT_EOF)
342 hammer_meta_flushdelete(fd, &dsnapshot);
347 * Flush any entries built up in the deletion snapshot ioctl structure.
348 * Used during a prune-everything.
351 hammer_meta_flushdelete(int fd, struct hammer_ioc_snapshot *dsnap)
353 while (dsnap->index < dsnap->count) {
354 if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnap) < 0)
356 if (dsnap->head.error == 0)
365 * Add the softlink to the appropriate softprune structure, creating a new
370 hammer_softprune_addentry(struct softprune **basep,
371 struct hammer_ioc_prune *template,
372 const char *dirpath, const char *denname __unused,
374 const char *linkbuf, const char *tidptr)
376 struct softprune *scan;
381 * Calculate filesystem path.
383 if (linkbuf[0] == '/') {
384 asprintf(&fspath, "%*.*s",
385 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
388 asprintf(&fspath, "%s/%*.*s", dirpath,
389 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
392 if (statfs(fspath, &fs) < 0) {
398 * Locate the filesystem in an existing softprune structure
400 for (scan = *basep; scan; scan = scan->next) {
401 if (bcmp(&fs.f_fsid, &scan->fs.f_fsid, sizeof(fs.f_fsid)) != 0)
403 if (strcmp(fs.f_mntonname, scan->fs.f_mntonname) != 0)
409 * Create a new softprune structure if necessasry
412 scan = malloc(sizeof(*scan));
413 bzero(scan, sizeof(*scan));
416 scan->filesystem = fspath;
417 scan->prune = *template;
419 scan->prune.elms = malloc(sizeof(struct hammer_ioc_prune_elm) *
426 hammer_softprune_addelm(scan,
427 (hammer_tid_t)strtoull(tidptr + 2, NULL, 0),
428 (st ? st->st_ctime : 0),
429 (st ? st->st_mtime : 0));
434 * Add the entry (unsorted). Just set the beg_tid, we will sort
435 * and set the remaining entries later.
437 * Always leave one entry free for our terminator.
440 hammer_softprune_addelm(struct softprune *scan, hammer_tid_t tid,
441 time_t ct, time_t mt)
443 struct hammer_ioc_prune_elm *elm;
445 if (scan->prune.nelms >= scan->maxelms - 1) {
446 scan->maxelms = (scan->maxelms * 3 / 2);
447 scan->prune.elms = realloc(scan->prune.elms,
448 sizeof(*elm) * scan->maxelms);
452 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
453 * This will be cleaned up in the finalization phase.
455 elm = &scan->prune.elms[scan->prune.nelms];
467 * Finalize a softprune structure after scanning in its softlinks.
468 * Sort the elements, remove duplicates, and then fill in end_tid and
471 * The array must end up in descending order.
474 hammer_softprune_qsort_cmp(const void *arg1, const void *arg2)
476 const struct hammer_ioc_prune_elm *elm1 = arg1;
477 const struct hammer_ioc_prune_elm *elm2 = arg2;
479 if (elm1->beg_tid < elm2->beg_tid)
481 if (elm1->beg_tid > elm2->beg_tid)
487 hammer_softprune_finalize(struct softprune *scan)
489 struct hammer_ioc_prune_elm *elm;
495 * Don't do anything if there are no elements.
497 if (scan->prune.nelms == 0)
501 * Sort the elements in descending order, remove duplicates, and
502 * fill in any missing bits.
504 qsort(scan->prune.elms, scan->prune.nelms, sizeof(*elm),
505 hammer_softprune_qsort_cmp);
507 for (i = 0; i < scan->prune.nelms; ++i) {
508 elm = &scan->prune.elms[i];
511 * First (highest TID) (also last if only one element)
513 elm->end_tid = HAMMER_MAX_TID;
514 } else if (elm[0].beg_tid == elm[-1].beg_tid) {
519 if (i != scan->prune.nelms) {
521 (scan->prune.nelms - i) * sizeof(*elm));
529 elm->end_tid = elm[-1].beg_tid;
534 * If a minimum retention time (in seconds) is configured for the
535 * PFS, remove any snapshots from the pruning list that are within
538 if (scan->prune_min) {
540 for (i = scan->prune.nelms - 1; i >= 0; --i) {
541 elm = &scan->prune.elms[i];
542 if (elm->mod_tid == 0)
544 delta = (long)(t - (time_t)elm->mod_tid);
545 if (delta < scan->prune_min)
550 printf("Prune %s: prune_min: Will not clean between "
551 "the teeth of the first %d snapshots\n",
552 scan->filesystem, i);
553 bcopy(&scan->prune.elms[i], &scan->prune.elms[0],
554 (scan->prune.nelms - i) * sizeof(scan->prune.elms[0]));
555 scan->prune.elms[0].end_tid = HAMMER_MAX_TID;
556 scan->prune.nelms -= i;
561 * Remove the first entry. This entry represents the prune from
562 * the most recent snapshot to current. We wish to retain the
563 * fine-grained history for this region.
565 if (scan->prune.nelms) {
566 bcopy(&scan->prune.elms[1], &scan->prune.elms[0],
567 (scan->prune.nelms - 1) * sizeof(scan->prune.elms[0]));
572 * Add a final element to prune everything from transaction id
573 * 0 to the lowest transaction id (aka last so far).
575 if (scan->prune.nelms) {
576 assert(scan->prune.nelms < scan->maxelms);
577 elm = &scan->prune.elms[scan->prune.nelms];
579 elm->end_tid = elm[-1].beg_tid;
584 * Adjust mod_tid to what the ioctl() expects.
586 for (i = 0; i < scan->prune.nelms; ++i) {
587 elm = &scan->prune.elms[i];
588 elm->mod_tid = elm->end_tid - elm->beg_tid;
589 printf("TID %016jx - %016jx\n",
590 (uintmax_t)elm->beg_tid, (uintmax_t)elm->end_tid);
596 softprune_usage(int code)
598 fprintf(stderr, "Badly formed prune command, use:\n");
599 fprintf(stderr, "hammer prune <softlink-dir>\n");
600 fprintf(stderr, "hammer prune-everything <filesystem>\n");