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 $
40 struct softprune
*next
;
43 struct hammer_ioc_prune prune
;
48 static void hammer_softprune_scandir(struct softprune
**basep
,
49 struct hammer_ioc_prune
*template,
51 static int hammer_softprune_scanmeta(int fd
, struct softprune
*scan
,
53 static void hammer_meta_flushdelete(int fd
, struct hammer_ioc_snapshot
*dsnap
);
54 static struct softprune
*hammer_softprune_addentry(struct softprune
**basep
,
55 struct hammer_ioc_prune
*template,
56 const char *dirpath
, const char *denname
,
58 const char *linkbuf
, const char *tidptr
);
59 static void hammer_softprune_addelm(struct softprune
*scan
, hammer_tid_t tid
,
60 time_t ct
, time_t mt
);
61 static void hammer_softprune_finalize(struct softprune
*scan
);
62 static void softprune_usage(int code
);
65 * prune <softlink-dir>
66 * prune-everything <filesystem>
69 hammer_cmd_softprune(char **av
, int ac
, int everything_opt
)
71 struct hammer_ioc_prune
template;
72 struct hammer_ioc_pseudofs_rw pfs
;
73 struct softprune
*base
, *scan
;
82 clrpfs(&pfs
, NULL
, -1);
85 * NOTE: To restrict to a single file XXX we have to set
86 * the localization the same (not yet implemented). Typically
87 * two passes would be needed, one using HAMMER_LOCALIZE_MISC
88 * and one using HAMMER_LOCALIZE_INODE.
91 bzero(&template, sizeof(template));
92 template.key_beg
.localization
= HAMMER_MIN_LOCALIZATION
;
93 template.key_beg
.obj_id
= HAMMER_MIN_OBJID
;
94 template.key_end
.localization
= HAMMER_MAX_LOCALIZATION
;
95 template.key_end
.obj_id
= HAMMER_MAX_OBJID
;
96 hammer_get_cycle(&template.key_end
, NULL
);
97 template.stat_oldest_tid
= HAMMER_MAX_TID
;
100 * For now just allow one directory
102 if (ac
== 0 || ac
> 1)
106 * Scan the softlink directory.
108 if (everything_opt
) {
109 const char *dummylink
= "";
110 scan
= hammer_softprune_addentry(&base
, &template,
112 dummylink
, dummylink
);
115 scan
->prune
.nelms
= 0;
116 scan
->prune
.head
.flags
|= HAMMER_IOC_PRUNE_ALL
;
118 hammer_softprune_scandir(&base
, &template, *av
);
120 const char *dummylink
= "";
121 scan
= hammer_softprune_addentry(&base
, &template,
123 dummylink
, dummylink
);
126 scan
->prune
.nelms
= 0;
133 * XXX future (need to store separate cycles for each filesystem)
136 fprintf(stderr
, "Currently only one HAMMER filesystem may "
137 "be specified in the softlink scan\n");
144 for (scan
= base
; scan
; scan
= scan
->next
) {
146 * Open the filesystem for ioctl calls and extract the
149 fd
= open(scan
->filesystem
, O_RDONLY
);
151 warn("Unable to open %s", scan
->filesystem
);
156 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
157 warn("Filesystem %s is not HAMMER", scan
->filesystem
);
162 scan
->prune_min
= pfs
.ondisk
->prune_min
;
165 * Incorporate meta-data snapshots into the pruning regimen.
166 * If pruning everything we delete the meta-data snapshots.
168 if (hammer_softprune_scanmeta(fd
, scan
, everything_opt
) < 0) {
169 warn("Filesystem %s could not scan meta-data snaps",
177 * Finalize operations
179 hammer_softprune_finalize(scan
);
180 if (everything_opt
) {
181 printf("Prune %s: EVERYTHING\n",
184 printf("Prune %s: %d snapshots\n",
185 scan
->filesystem
, scan
->prune
.nelms
);
187 if (scan
->prune
.nelms
== 0 &&
188 (scan
->prune
.head
.flags
& HAMMER_IOC_PRUNE_ALL
) == 0) {
189 fprintf(stderr
, "No snapshots found\n");
193 printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
196 (uintmax_t)scan
->prune
.key_beg
.obj_id
,
197 scan
->prune
.key_beg
.localization
,
198 (uintmax_t)scan
->prune
.key_end
.obj_id
,
199 scan
->prune
.key_end
.localization
,
201 printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
203 pfs
.ondisk
->prune_min
/ (24 * 60 * 60),
204 pfs
.ondisk
->prune_min
/ 60 / 60 % 24,
205 pfs
.ondisk
->prune_min
/ 60 % 60,
206 pfs
.ondisk
->prune_min
% 60);
209 if (ioctl(fd
, HAMMERIOC_PRUNE
, &scan
->prune
) < 0) {
210 printf("Prune %s failed: %s\n",
211 scan
->filesystem
, strerror(errno
));
213 } else if (scan
->prune
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
214 printf("Prune %s interrupted by timer at "
217 (uintmax_t)scan
->prune
.key_cur
.obj_id
,
218 scan
->prune
.key_cur
.localization
);
220 hammer_set_cycle(&scan
->prune
.key_cur
, 0);
224 hammer_reset_cycle();
225 printf("Prune %s succeeded\n", scan
->filesystem
);
227 printf("Pruned %jd/%jd records (%jd directory entries) "
229 (uintmax_t)scan
->prune
.stat_rawrecords
,
230 (uintmax_t)scan
->prune
.stat_scanrecords
,
231 (uintmax_t)scan
->prune
.stat_dirrecords
,
232 (uintmax_t)scan
->prune
.stat_bytes
242 * Scan a directory for softlinks representing snapshots and build
243 * associated softprune structures.
245 * NOTE: Once a filesystem is completely converted to the meta-data
246 * snapshot mechanic we don't have to scan softlinks any more
247 * and can just use the meta-data. But for now we do both.
250 hammer_softprune_scandir(struct softprune
**basep
,
251 struct hammer_ioc_prune
*template,
263 linkbuf
= malloc(MAXPATHLEN
);
265 if ((dir
= opendir(dirname
)) == NULL
)
266 err(1, "Cannot open directory %s", dirname
);
267 while ((den
= readdir(dir
)) != NULL
) {
268 if (strcmp(den
->d_name
, ".") == 0)
270 if (strcmp(den
->d_name
, "..") == 0)
274 asprintf(&path
, "%s/%s", dirname
, den
->d_name
);
275 if (lstat(path
, &st
) < 0)
277 if (!S_ISLNK(st
.st_mode
))
279 if ((len
= readlink(path
, linkbuf
, MAXPATHLEN
- 1)) < 0)
282 if ((ptr
= strrchr(linkbuf
, '@')) &&
283 ptr
> linkbuf
&& ptr
[-1] == '@') {
284 hammer_softprune_addentry(basep
, template,
285 dirname
, den
->d_name
, &st
,
295 * Scan a directory for softlinks representing snapshots.
296 * Return 1 if the directory contains snapshots, otherwise 0.
299 hammer_softprune_testdir(const char *dirname
)
301 struct softprune
*base
= NULL
;
302 struct hammer_ioc_prune dummy_template
;
304 bzero(&dummy_template
, sizeof(dummy_template
));
305 hammer_softprune_scandir(&base
, &dummy_template
, dirname
);
313 * Scan the metadata snapshots for the filesystem and either delete them
314 * or add them to the pruning list.
318 hammer_softprune_scanmeta(int fd
, struct softprune
*scan
, int delete_all
)
320 struct hammer_ioc_version version
;
321 struct hammer_ioc_snapshot snapshot
;
322 struct hammer_ioc_snapshot dsnapshot
;
323 hammer_snapshot_data_t snap
;
327 * Stop if we can't get the version. Meta-data snapshots only
328 * exist for HAMMER version 3 or greater.
330 bzero(&version
, sizeof(version
));
331 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0)
333 if (version
.cur_version
< 3)
336 bzero(&snapshot
, sizeof(snapshot
));
337 bzero(&dsnapshot
, sizeof(dsnapshot
));
340 * Scan meta-data snapshots, either add them to the prune list or
341 * delete them. When deleting, just skip any entries which cannot
345 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
346 printf("hammer prune: Unable to access "
347 "meta-data snaps: %s\n", strerror(errno
));
350 while (snapshot
.index
< snapshot
.count
) {
351 snap
= &snapshot
.snaps
[snapshot
.index
];
353 dsnapshot
.snaps
[dsnapshot
.count
++] = *snap
;
354 if (dsnapshot
.count
== HAMMER_SNAPS_PER_IOCTL
)
355 hammer_meta_flushdelete(fd
, &dsnapshot
);
357 ct
= snap
->ts
/ 1000000ULL;
358 hammer_softprune_addelm(scan
, snap
->tid
,
363 if (snapshot
.head
.flags
& HAMMER_IOC_SNAPSHOT_EOF
)
368 hammer_meta_flushdelete(fd
, &dsnapshot
);
373 * Flush any entries built up in the deletion snapshot ioctl structure.
374 * Used during a prune-everything.
377 hammer_meta_flushdelete(int fd
, struct hammer_ioc_snapshot
*dsnap
)
379 while (dsnap
->index
< dsnap
->count
) {
380 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, dsnap
) < 0)
382 if (dsnap
->head
.error
== 0)
391 * Add the softlink to the appropriate softprune structure, creating a new
396 hammer_softprune_addentry(struct softprune
**basep
,
397 struct hammer_ioc_prune
*template,
398 const char *dirpath
, const char *denname __unused
,
400 const char *linkbuf
, const char *tidptr
)
402 struct softprune
*scan
;
407 * Calculate filesystem path.
409 if (linkbuf
[0] == '/') {
410 asprintf(&fspath
, "%*.*s",
411 (int)(tidptr
- linkbuf
), (int)(tidptr
- linkbuf
),
414 asprintf(&fspath
, "%s/%*.*s", dirpath
,
415 (int)(tidptr
- linkbuf
), (int)(tidptr
- linkbuf
),
418 if (statfs(fspath
, &fs
) < 0) {
424 * Locate the filesystem in an existing softprune structure
426 for (scan
= *basep
; scan
; scan
= scan
->next
) {
427 if (bcmp(&fs
.f_fsid
, &scan
->fs
.f_fsid
, sizeof(fs
.f_fsid
)) != 0)
429 if (strcmp(fs
.f_mntonname
, scan
->fs
.f_mntonname
) != 0)
435 * Create a new softprune structure if necessasry
438 scan
= malloc(sizeof(*scan
));
439 bzero(scan
, sizeof(*scan
));
442 scan
->filesystem
= fspath
;
443 scan
->prune
= *template;
445 scan
->prune
.elms
= malloc(sizeof(struct hammer_ioc_prune_elm
) *
452 hammer_softprune_addelm(scan
,
453 (hammer_tid_t
)strtoull(tidptr
+ 2, NULL
, 0),
454 (st
? st
->st_ctime
: 0),
455 (st
? st
->st_mtime
: 0));
460 * Add the entry (unsorted). Just set the beg_tid, we will sort
461 * and set the remaining entries later.
463 * Always leave one entry free for our terminator.
466 hammer_softprune_addelm(struct softprune
*scan
, hammer_tid_t tid
,
467 time_t ct
, time_t mt
)
469 struct hammer_ioc_prune_elm
*elm
;
471 if (scan
->prune
.nelms
>= scan
->maxelms
- 1) {
472 scan
->maxelms
= (scan
->maxelms
* 3 / 2);
473 scan
->prune
.elms
= realloc(scan
->prune
.elms
,
474 sizeof(*elm
) * scan
->maxelms
);
478 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
479 * This will be cleaned up in the finalization phase.
481 elm
= &scan
->prune
.elms
[scan
->prune
.nelms
];
493 * Finalize a softprune structure after scanning in its softlinks.
494 * Sort the elements, remove duplicates, and then fill in end_tid and
497 * The array must end up in descending order.
500 hammer_softprune_qsort_cmp(const void *arg1
, const void *arg2
)
502 const struct hammer_ioc_prune_elm
*elm1
= arg1
;
503 const struct hammer_ioc_prune_elm
*elm2
= arg2
;
505 if (elm1
->beg_tid
< elm2
->beg_tid
)
507 if (elm1
->beg_tid
> elm2
->beg_tid
)
513 hammer_softprune_finalize(struct softprune
*scan
)
515 struct hammer_ioc_prune_elm
*elm
;
521 * Don't do anything if there are no elements.
523 if (scan
->prune
.nelms
== 0)
527 * Sort the elements in descending order, remove duplicates, and
528 * fill in any missing bits.
530 qsort(scan
->prune
.elms
, scan
->prune
.nelms
, sizeof(*elm
),
531 hammer_softprune_qsort_cmp
);
533 for (i
= 0; i
< scan
->prune
.nelms
; ++i
) {
534 elm
= &scan
->prune
.elms
[i
];
537 * First (highest TID) (also last if only one element)
539 elm
->end_tid
= HAMMER_MAX_TID
;
540 } else if (elm
[0].beg_tid
== elm
[-1].beg_tid
) {
545 if (i
!= scan
->prune
.nelms
) {
547 (scan
->prune
.nelms
- i
) * sizeof(*elm
));
555 elm
->end_tid
= elm
[-1].beg_tid
;
560 * If a minimum retention time (in seconds) is configured for the
561 * PFS, remove any snapshots from the pruning list that are within
564 if (scan
->prune_min
) {
566 for (i
= scan
->prune
.nelms
- 1; i
>= 0; --i
) {
567 elm
= &scan
->prune
.elms
[i
];
568 if (elm
->mod_tid
== 0)
570 delta
= (long)(t
- (time_t)elm
->mod_tid
);
571 if (delta
< scan
->prune_min
)
576 printf("Prune %s: prune_min: Will not clean between "
577 "the teeth of the first %d snapshots\n",
578 scan
->filesystem
, i
);
579 bcopy(&scan
->prune
.elms
[i
], &scan
->prune
.elms
[0],
580 (scan
->prune
.nelms
- i
) * sizeof(scan
->prune
.elms
[0]));
581 scan
->prune
.elms
[0].end_tid
= HAMMER_MAX_TID
;
582 scan
->prune
.nelms
-= i
;
587 * Remove the first entry. This entry represents the prune from
588 * the most recent snapshot to current. We wish to retain the
589 * fine-grained history for this region.
591 if (scan
->prune
.nelms
) {
592 bcopy(&scan
->prune
.elms
[1], &scan
->prune
.elms
[0],
593 (scan
->prune
.nelms
- 1) * sizeof(scan
->prune
.elms
[0]));
598 * Add a final element to prune everything from transaction id
599 * 0 to the lowest transaction id (aka last so far).
601 if (scan
->prune
.nelms
) {
602 assert(scan
->prune
.nelms
< scan
->maxelms
);
603 elm
= &scan
->prune
.elms
[scan
->prune
.nelms
];
605 elm
->end_tid
= elm
[-1].beg_tid
;
610 * Adjust mod_tid to what the ioctl() expects.
612 for (i
= 0; i
< scan
->prune
.nelms
; ++i
) {
613 elm
= &scan
->prune
.elms
[i
];
614 elm
->mod_tid
= elm
->end_tid
- elm
->beg_tid
;
615 printf("TID %016jx - %016jx\n",
616 (uintmax_t)elm
->beg_tid
, (uintmax_t)elm
->end_tid
);
622 softprune_usage(int code
)
624 fprintf(stderr
, "Badly formed prune command, use:\n");
625 fprintf(stderr
, "hammer prune <softlink-dir>\n");
626 fprintf(stderr
, "hammer prune-everything <filesystem>\n");