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) {
108 * Scan the softlink directory.
110 if (everything_opt
) {
111 const char *dummylink
= "";
112 scan
= hammer_softprune_addentry(&base
, &template,
114 dummylink
, dummylink
);
119 scan
->prune
.nelms
= 0;
120 scan
->prune
.head
.flags
|= HAMMER_IOC_PRUNE_ALL
;
122 hammer_softprune_scandir(&base
, &template, *av
);
124 const char *dummylink
= "";
125 scan
= hammer_softprune_addentry(&base
, &template,
127 dummylink
, dummylink
);
132 scan
->prune
.nelms
= 0;
139 * XXX future (need to store separate cycles for each filesystem)
142 errx(1, "Currently only one HAMMER filesystem may "
143 "be specified in the softlink scan");
150 for (scan
= base
; scan
; scan
= scan
->next
) {
152 * Open the filesystem for ioctl calls and extract the
155 fd
= open(scan
->filesystem
, O_RDONLY
);
157 warn("Unable to open %s", scan
->filesystem
);
162 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
163 warn("Filesystem %s is not HAMMER", scan
->filesystem
);
168 scan
->prune_min
= pfs
.ondisk
->prune_min
;
171 * Incorporate meta-data snapshots into the pruning regimen.
172 * If pruning everything we delete the meta-data snapshots.
174 if (hammer_softprune_scanmeta(fd
, scan
, everything_opt
) < 0) {
175 warn("Filesystem %s could not scan meta-data snaps",
183 * Finalize operations
185 hammer_softprune_finalize(scan
);
186 if (everything_opt
) {
187 printf("Prune %s: EVERYTHING\n",
190 printf("Prune %s: %d snapshots\n",
191 scan
->filesystem
, scan
->prune
.nelms
);
193 if (scan
->prune
.nelms
== 0 &&
194 (scan
->prune
.head
.flags
& HAMMER_IOC_PRUNE_ALL
) == 0) {
195 fprintf(stderr
, "No snapshots found\n");
199 printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
202 (uintmax_t)scan
->prune
.key_beg
.obj_id
,
203 scan
->prune
.key_beg
.localization
,
204 (uintmax_t)scan
->prune
.key_end
.obj_id
,
205 scan
->prune
.key_end
.localization
,
207 printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
209 pfs
.ondisk
->prune_min
/ (24 * 60 * 60),
210 pfs
.ondisk
->prune_min
/ 60 / 60 % 24,
211 pfs
.ondisk
->prune_min
/ 60 % 60,
212 pfs
.ondisk
->prune_min
% 60);
215 if (ioctl(fd
, HAMMERIOC_PRUNE
, &scan
->prune
) < 0) {
216 printf("Prune %s failed: %s\n",
217 scan
->filesystem
, strerror(errno
));
219 } else if (scan
->prune
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
220 printf("Prune %s interrupted by timer at "
223 (uintmax_t)scan
->prune
.key_cur
.obj_id
,
224 scan
->prune
.key_cur
.localization
);
226 hammer_set_cycle(&scan
->prune
.key_cur
, 0);
230 hammer_reset_cycle();
231 printf("Prune %s succeeded\n", scan
->filesystem
);
233 printf("Pruned %jd/%jd records (%jd directory entries) "
235 (uintmax_t)scan
->prune
.stat_rawrecords
,
236 (uintmax_t)scan
->prune
.stat_scanrecords
,
237 (uintmax_t)scan
->prune
.stat_dirrecords
,
238 (uintmax_t)scan
->prune
.stat_bytes
248 * Scan a directory for softlinks representing snapshots and build
249 * associated softprune structures.
251 * NOTE: Once a filesystem is completely converted to the meta-data
252 * snapshot mechanic we don't have to scan softlinks any more
253 * and can just use the meta-data. But for now we do both.
256 hammer_softprune_scandir(struct softprune
**basep
,
257 struct hammer_ioc_prune
*template,
269 linkbuf
= malloc(MAXPATHLEN
);
271 if ((dir
= opendir(dirname
)) == NULL
) {
272 err(1, "Cannot open directory %s", dirname
);
275 while ((den
= readdir(dir
)) != NULL
) {
276 if (strcmp(den
->d_name
, ".") == 0)
278 if (strcmp(den
->d_name
, "..") == 0)
282 asprintf(&path
, "%s/%s", dirname
, den
->d_name
);
283 if (lstat(path
, &st
) < 0)
285 if (!S_ISLNK(st
.st_mode
))
287 if ((len
= readlink(path
, linkbuf
, MAXPATHLEN
- 1)) < 0)
290 if ((ptr
= strrchr(linkbuf
, '@')) &&
291 ptr
> linkbuf
&& ptr
[-1] == '@') {
292 hammer_softprune_addentry(basep
, template,
293 dirname
, den
->d_name
, &st
,
303 * Scan a directory for softlinks representing snapshots.
304 * Return 1 if the directory contains snapshots, otherwise 0.
307 hammer_softprune_testdir(const char *dirname
)
309 struct softprune
*base
= NULL
;
310 struct hammer_ioc_prune dummy_template
;
312 bzero(&dummy_template
, sizeof(dummy_template
));
313 hammer_softprune_scandir(&base
, &dummy_template
, dirname
);
321 * Scan the metadata snapshots for the filesystem and either delete them
322 * or add them to the pruning list.
326 hammer_softprune_scanmeta(int fd
, struct softprune
*scan
, int delete_all
)
328 struct hammer_ioc_version version
;
329 struct hammer_ioc_snapshot snapshot
;
330 struct hammer_ioc_snapshot dsnapshot
;
331 hammer_snapshot_data_t snap
;
335 * Stop if we can't get the version. Meta-data snapshots only
336 * exist for HAMMER version 3 or greater.
338 bzero(&version
, sizeof(version
));
339 if (ioctl(fd
, HAMMERIOC_GET_VERSION
, &version
) < 0)
341 HammerVersion
= version
.cur_version
;
342 if (version
.cur_version
< 3)
345 bzero(&snapshot
, sizeof(snapshot
));
346 bzero(&dsnapshot
, sizeof(dsnapshot
));
349 * Scan meta-data snapshots, either add them to the prune list or
350 * delete them. When deleting, just skip any entries which cannot
354 if (ioctl(fd
, HAMMERIOC_GET_SNAPSHOT
, &snapshot
) < 0) {
355 printf("hammer prune: Unable to access "
356 "meta-data snaps: %s\n", strerror(errno
));
359 while (snapshot
.index
< snapshot
.count
) {
360 snap
= &snapshot
.snaps
[snapshot
.index
];
362 dsnapshot
.snaps
[dsnapshot
.count
++] = *snap
;
363 if (dsnapshot
.count
== HAMMER_SNAPS_PER_IOCTL
)
364 hammer_meta_flushdelete(fd
, &dsnapshot
);
366 ct
= snap
->ts
/ 1000000ULL;
367 hammer_softprune_addelm(scan
, snap
->tid
,
372 if (snapshot
.head
.flags
& HAMMER_IOC_SNAPSHOT_EOF
)
377 hammer_meta_flushdelete(fd
, &dsnapshot
);
382 * Flush any entries built up in the deletion snapshot ioctl structure.
383 * Used during a prune-everything.
386 hammer_meta_flushdelete(int fd
, struct hammer_ioc_snapshot
*dsnap
)
388 while (dsnap
->index
< dsnap
->count
) {
389 if (ioctl(fd
, HAMMERIOC_DEL_SNAPSHOT
, dsnap
) < 0)
391 if (dsnap
->head
.error
== 0)
400 * Add the softlink to the appropriate softprune structure, creating a new
405 hammer_softprune_addentry(struct softprune
**basep
,
406 struct hammer_ioc_prune
*template,
407 const char *dirpath
, const char *denname __unused
,
409 const char *linkbuf
, const char *tidptr
)
411 struct softprune
*scan
;
416 * Calculate filesystem path.
418 if (linkbuf
[0] == '/') {
419 asprintf(&fspath
, "%*.*s",
420 (int)(tidptr
- linkbuf
), (int)(tidptr
- linkbuf
),
423 asprintf(&fspath
, "%s/%*.*s", dirpath
,
424 (int)(tidptr
- linkbuf
), (int)(tidptr
- linkbuf
),
427 if (statfs(fspath
, &fs
) < 0) {
433 * Locate the filesystem in an existing softprune structure
435 for (scan
= *basep
; scan
; scan
= scan
->next
) {
436 if (bcmp(&fs
.f_fsid
, &scan
->fs
.f_fsid
, sizeof(fs
.f_fsid
)) != 0)
438 if (strcmp(fs
.f_mntonname
, scan
->fs
.f_mntonname
) != 0)
444 * Create a new softprune structure if necessasry
447 scan
= malloc(sizeof(*scan
));
448 bzero(scan
, sizeof(*scan
));
451 scan
->filesystem
= fspath
;
452 scan
->prune
= *template;
454 scan
->prune
.elms
= malloc(sizeof(struct hammer_ioc_prune_elm
) *
461 hammer_softprune_addelm(scan
,
462 (hammer_tid_t
)strtoull(tidptr
+ 2, NULL
, 0),
463 (st
? st
->st_ctime
: 0),
464 (st
? st
->st_mtime
: 0));
469 * Add the entry (unsorted). Just set the beg_tid, we will sort
470 * and set the remaining entries later.
472 * Always leave one entry free for our terminator.
475 hammer_softprune_addelm(struct softprune
*scan
, hammer_tid_t tid
,
476 time_t ct
, time_t mt
)
478 struct hammer_ioc_prune_elm
*elm
;
480 if (scan
->prune
.nelms
>= scan
->maxelms
- 1) {
481 scan
->maxelms
= (scan
->maxelms
* 3 / 2);
482 scan
->prune
.elms
= realloc(scan
->prune
.elms
,
483 sizeof(*elm
) * scan
->maxelms
);
487 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
488 * This will be cleaned up in the finalization phase.
490 elm
= &scan
->prune
.elms
[scan
->prune
.nelms
];
502 * Finalize a softprune structure after scanning in its softlinks.
503 * Sort the elements, remove duplicates, and then fill in end_tid and
506 * The array must end up in descending order.
509 hammer_softprune_qsort_cmp(const void *arg1
, const void *arg2
)
511 const struct hammer_ioc_prune_elm
*elm1
= arg1
;
512 const struct hammer_ioc_prune_elm
*elm2
= arg2
;
514 if (elm1
->beg_tid
< elm2
->beg_tid
)
516 if (elm1
->beg_tid
> elm2
->beg_tid
)
522 hammer_softprune_finalize(struct softprune
*scan
)
524 struct hammer_ioc_prune_elm
*elm
;
530 * Don't do anything if there are no elements.
532 if (scan
->prune
.nelms
== 0)
536 * Sort the elements in descending order, remove duplicates, and
537 * fill in any missing bits.
539 qsort(scan
->prune
.elms
, scan
->prune
.nelms
, sizeof(*elm
),
540 hammer_softprune_qsort_cmp
);
542 for (i
= 0; i
< scan
->prune
.nelms
; ++i
) {
543 elm
= &scan
->prune
.elms
[i
];
546 * First (highest TID) (also last if only one element)
548 elm
->end_tid
= HAMMER_MAX_TID
;
549 } else if (elm
[0].beg_tid
== elm
[-1].beg_tid
) {
554 if (i
!= scan
->prune
.nelms
) {
556 (scan
->prune
.nelms
- i
) * sizeof(*elm
));
564 elm
->end_tid
= elm
[-1].beg_tid
;
569 * If a minimum retention time (in seconds) is configured for the
570 * PFS, remove any snapshots from the pruning list that are within
573 if (scan
->prune_min
) {
575 for (i
= scan
->prune
.nelms
- 1; i
>= 0; --i
) {
576 elm
= &scan
->prune
.elms
[i
];
577 if (elm
->mod_tid
== 0)
579 delta
= (long)(t
- (time_t)elm
->mod_tid
);
580 if (delta
< scan
->prune_min
)
585 printf("Prune %s: prune_min: Will not clean between "
586 "the teeth of the first %d snapshots\n",
587 scan
->filesystem
, i
);
588 bcopy(&scan
->prune
.elms
[i
], &scan
->prune
.elms
[0],
589 (scan
->prune
.nelms
- i
) * sizeof(scan
->prune
.elms
[0]));
590 scan
->prune
.elms
[0].end_tid
= HAMMER_MAX_TID
;
591 scan
->prune
.nelms
-= i
;
596 * Remove the first entry. This entry represents the prune from
597 * the most recent snapshot to current. We wish to retain the
598 * fine-grained history for this region.
600 if (scan
->prune
.nelms
) {
601 bcopy(&scan
->prune
.elms
[1], &scan
->prune
.elms
[0],
602 (scan
->prune
.nelms
- 1) * sizeof(scan
->prune
.elms
[0]));
607 * Add a final element to prune everything from transaction id
608 * 0 to the lowest transaction id (aka last so far).
610 if (scan
->prune
.nelms
) {
611 assert(scan
->prune
.nelms
< scan
->maxelms
);
612 elm
= &scan
->prune
.elms
[scan
->prune
.nelms
];
614 elm
->end_tid
= elm
[-1].beg_tid
;
619 * Adjust mod_tid to what the ioctl() expects.
621 for (i
= 0; i
< scan
->prune
.nelms
; ++i
) {
622 elm
= &scan
->prune
.elms
[i
];
623 elm
->mod_tid
= elm
->end_tid
- elm
->beg_tid
;
624 printf("TID %016jx - %016jx\n",
625 (uintmax_t)elm
->beg_tid
, (uintmax_t)elm
->end_tid
);
631 softprune_usage(int code
)
633 fprintf(stderr
, "Badly formed prune command, use:\n");
634 fprintf(stderr
, "hammer prune <softlink-dir>\n");
635 fprintf(stderr
, "hammer prune-everything <filesystem>\n");