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 softprune_usage(int code
);
49 static void hammer_softprune_scandir(struct softprune
**basep
,
50 struct hammer_ioc_prune
*template,
52 static struct softprune
*hammer_softprune_addentry(struct softprune
**basep
,
53 struct hammer_ioc_prune
*template,
54 const char *dirpath
, const char *denname
,
56 const char *linkbuf
, const char *tidptr
);
57 static void hammer_softprune_finalize(struct softprune
*scan
);
60 * prune <softlink-dir>
61 * prune-everything <filesystem>
64 hammer_cmd_softprune(char **av
, int ac
, int everything_opt
)
66 struct hammer_ioc_prune
template;
67 struct hammer_ioc_pseudofs_rw pfs
;
68 struct softprune
*base
, *scan
;
77 bzero(&pfs
, sizeof(pfs
));
78 pfs
.bytes
= sizeof(*pfs
.ondisk
);
79 pfs
.ondisk
= malloc(pfs
.bytes
);
80 bzero(pfs
.ondisk
, pfs
.bytes
);
84 * NOTE: To restrict to a single file XXX we have to set
85 * the localization the same (not yet implemented). Typically
86 * two passes would be needed, one using HAMMER_LOCALIZE_MISC
87 * and one using HAMMER_LOCALIZE_INODE.
90 bzero(&template, sizeof(template));
91 template.key_beg
.localization
= HAMMER_MIN_LOCALIZATION
;
92 template.key_beg
.obj_id
= HAMMER_MIN_OBJID
;
93 template.key_end
.localization
= HAMMER_MAX_LOCALIZATION
;
94 template.key_end
.obj_id
= HAMMER_MAX_OBJID
;
95 hammer_get_cycle(&template.key_end
, NULL
);
96 template.stat_oldest_tid
= HAMMER_MAX_TID
;
99 * For now just allow one directory
101 if (ac
== 0 || ac
> 1)
105 * Scan the softlink directory.
107 if (everything_opt
) {
108 const char *dummylink
= "";
109 scan
= hammer_softprune_addentry(&base
, &template,
111 dummylink
, dummylink
);
114 scan
->prune
.nelms
= 0;
115 scan
->prune
.head
.flags
|= HAMMER_IOC_PRUNE_ALL
;
117 hammer_softprune_scandir(&base
, &template, *av
);
123 * XXX future (need to store separate cycles for each filesystem)
126 fprintf(stderr
, "No snapshot softlinks found\n");
130 fprintf(stderr
, "Currently only one HAMMER filesystem may "
131 "be specified in the softlink scan\n");
138 for (scan
= base
; scan
; scan
= scan
->next
) {
140 * Open the filesystem for ioctl calls and extract the
143 fd
= open(scan
->filesystem
, O_RDONLY
);
145 warn("Unable to open %s", scan
->filesystem
);
150 if (ioctl(fd
, HAMMERIOC_GET_PSEUDOFS
, &pfs
) < 0) {
151 warn("Filesystem %s is not HAMMER", scan
->filesystem
);
156 scan
->prune_min
= pfs
.ondisk
->prune_min
;
159 * Finalize operations
161 hammer_softprune_finalize(scan
);
162 if (everything_opt
) {
163 printf("Prune %s: EVERYTHING\n",
166 printf("Prune %s: %d snapshots\n",
167 scan
->filesystem
, scan
->prune
.nelms
);
169 if (scan
->prune
.nelms
== 0 &&
170 (scan
->prune
.head
.flags
& HAMMER_IOC_PRUNE_ALL
) == 0) {
174 printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
177 (uintmax_t)scan
->prune
.key_beg
.obj_id
,
178 scan
->prune
.key_beg
.localization
,
179 (uintmax_t)scan
->prune
.key_end
.obj_id
,
180 scan
->prune
.key_end
.localization
,
182 printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
184 pfs
.ondisk
->prune_min
/ (24 * 60 * 60),
185 pfs
.ondisk
->prune_min
/ 60 / 60 % 24,
186 pfs
.ondisk
->prune_min
/ 60 % 60,
187 pfs
.ondisk
->prune_min
% 60);
190 if (ioctl(fd
, HAMMERIOC_PRUNE
, &scan
->prune
) < 0) {
191 printf("Prune %s failed: %s\n",
192 scan
->filesystem
, strerror(errno
));
194 } else if (scan
->prune
.head
.flags
& HAMMER_IOC_HEAD_INTR
) {
195 printf("Prune %s interrupted by timer at "
198 (uintmax_t)scan
->prune
.key_cur
.obj_id
,
199 scan
->prune
.key_cur
.localization
);
201 hammer_set_cycle(&scan
->prune
.key_cur
, 0);
205 hammer_reset_cycle();
206 printf("Prune %s succeeded\n", scan
->filesystem
);
208 printf("Pruned %jd/%jd records (%jd directory entries) "
210 (uintmax_t)scan
->prune
.stat_rawrecords
,
211 (uintmax_t)scan
->prune
.stat_scanrecords
,
212 (uintmax_t)scan
->prune
.stat_dirrecords
,
213 (uintmax_t)scan
->prune
.stat_bytes
223 * Scan a directory for softlinks representing snapshots and build
224 * associated softprune structures.
227 hammer_softprune_scandir(struct softprune
**basep
,
228 struct hammer_ioc_prune
*template,
240 linkbuf
= malloc(MAXPATHLEN
);
242 if ((dir
= opendir(dirname
)) == NULL
)
243 err(1, "Cannot open directory %s", dirname
);
244 while ((den
= readdir(dir
)) != NULL
) {
245 if (strcmp(den
->d_name
, ".") == 0)
247 if (strcmp(den
->d_name
, "..") == 0)
251 asprintf(&path
, "%s/%s", dirname
, den
->d_name
);
252 if (lstat(path
, &st
) < 0)
254 if (!S_ISLNK(st
.st_mode
))
256 if ((len
= readlink(path
, linkbuf
, MAXPATHLEN
- 1)) < 0)
259 if ((ptr
= strrchr(linkbuf
, '@')) &&
260 ptr
> linkbuf
&& ptr
[-1] == '@') {
261 hammer_softprune_addentry(basep
, template,
262 dirname
, den
->d_name
, &st
,
272 * Add the softlink to the appropriate softprune structure, creating a new
277 hammer_softprune_addentry(struct softprune
**basep
,
278 struct hammer_ioc_prune
*template,
279 const char *dirpath
, const char *denname __unused
,
281 const char *linkbuf
, const char *tidptr
)
283 struct hammer_ioc_prune_elm
*elm
;
284 struct softprune
*scan
;
289 * Calculate filesystem path.
291 if (linkbuf
[0] == '/') {
292 asprintf(&fspath
, "%*.*s",
293 (int)(tidptr
- linkbuf
), (int)(tidptr
- linkbuf
),
296 asprintf(&fspath
, "%s/%*.*s", dirpath
,
297 (int)(tidptr
- linkbuf
), (int)(tidptr
- linkbuf
),
300 if (statfs(fspath
, &fs
) < 0) {
306 * Locate the filesystem in an existing softprune structure
308 for (scan
= *basep
; scan
; scan
= scan
->next
) {
309 if (bcmp(&fs
.f_fsid
, &scan
->fs
.f_fsid
, sizeof(fs
.f_fsid
)) != 0)
311 if (strcmp(fs
.f_mntonname
, scan
->fs
.f_mntonname
) != 0)
317 * Create a new softprune structure if necessasry
320 scan
= malloc(sizeof(*scan
));
321 bzero(scan
, sizeof(*scan
));
324 scan
->filesystem
= fspath
;
325 scan
->prune
= *template;
327 scan
->prune
.elms
= malloc(sizeof(*elm
) * scan
->maxelms
);
335 * Add the entry (unsorted). Just set the beg_tid, we will sort
336 * and set the remaining entries later.
338 * Always leave one entry free for our terminator.
340 if (scan
->prune
.nelms
>= scan
->maxelms
- 1) {
341 scan
->maxelms
= (scan
->maxelms
* 3 / 2);
342 scan
->prune
.elms
= realloc(scan
->prune
.elms
,
343 sizeof(*elm
) * scan
->maxelms
);
347 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
348 * This will be cleaned up in the finalization phase.
350 elm
= &scan
->prune
.elms
[scan
->prune
.nelms
];
351 elm
->beg_tid
= strtoull(tidptr
+ 2, NULL
, 0);
355 if (st
->st_ctime
< st
->st_mtime
)
356 elm
->mod_tid
= st
->st_ctime
;
358 elm
->mod_tid
= st
->st_mtime
;
365 * Finalize a softprune structure after scanning in its softlinks.
366 * Sort the elements, remove duplicates, and then fill in end_tid and
369 * The array must end up in descending order.
372 hammer_softprune_qsort_cmp(const void *arg1
, const void *arg2
)
374 const struct hammer_ioc_prune_elm
*elm1
= arg1
;
375 const struct hammer_ioc_prune_elm
*elm2
= arg2
;
377 if (elm1
->beg_tid
< elm2
->beg_tid
)
379 if (elm1
->beg_tid
> elm2
->beg_tid
)
385 hammer_softprune_finalize(struct softprune
*scan
)
387 struct hammer_ioc_prune_elm
*elm
;
393 * Don't do anything if there are no elements.
395 if (scan
->prune
.nelms
== 0)
399 * Sort the elements in descending order, remove duplicates, and
400 * fill in any missing bits.
402 qsort(scan
->prune
.elms
, scan
->prune
.nelms
, sizeof(*elm
),
403 hammer_softprune_qsort_cmp
);
405 for (i
= 0; i
< scan
->prune
.nelms
; ++i
) {
406 elm
= &scan
->prune
.elms
[i
];
409 * First (highest TID) (also last if only one element)
411 elm
->end_tid
= HAMMER_MAX_TID
;
412 } else if (elm
[0].beg_tid
== elm
[-1].beg_tid
) {
417 if (i
!= scan
->prune
.nelms
) {
419 (scan
->prune
.nelms
- i
) * sizeof(*elm
));
427 elm
->end_tid
= elm
[-1].beg_tid
;
432 * If a minimum retention time (in seconds) is configured for the
433 * PFS, remove any snapshots from the pruning list that are within
436 if (scan
->prune_min
) {
438 for (i
= scan
->prune
.nelms
- 1; i
>= 0; --i
) {
439 elm
= &scan
->prune
.elms
[i
];
440 if (elm
->mod_tid
== 0)
442 delta
= (long)(t
- (time_t)elm
->mod_tid
);
443 if (delta
< scan
->prune_min
)
448 printf("Prune %s: prune_min: Will not clean between "
449 "the teeth of the first %d snapshots\n",
450 scan
->filesystem
, i
);
451 bcopy(&scan
->prune
.elms
[i
], &scan
->prune
.elms
[0],
452 (scan
->prune
.nelms
- i
) * sizeof(scan
->prune
.elms
[0]));
453 scan
->prune
.elms
[0].end_tid
= HAMMER_MAX_TID
;
454 scan
->prune
.nelms
-= i
;
459 * Remove the first entry. This entry represents the prune from
460 * the most recent snapshot to current. We wish to retain the
461 * fine-grained history for this region.
463 if (scan
->prune
.nelms
) {
464 bcopy(&scan
->prune
.elms
[1], &scan
->prune
.elms
[0],
465 (scan
->prune
.nelms
- 1) * sizeof(scan
->prune
.elms
[0]));
470 * Add a final element to prune everything from transaction id
471 * 0 to the lowest transaction id (aka last so far).
473 if (scan
->prune
.nelms
) {
474 assert(scan
->prune
.nelms
< scan
->maxelms
);
475 elm
= &scan
->prune
.elms
[scan
->prune
.nelms
];
477 elm
->end_tid
= elm
[-1].beg_tid
;
482 * Adjust mod_tid to what the ioctl() expects.
484 for (i
= 0; i
< scan
->prune
.nelms
; ++i
) {
485 elm
= &scan
->prune
.elms
[i
];
486 elm
->mod_tid
= elm
->end_tid
- elm
->beg_tid
;
487 printf("TID %016jx - %016jx\n",
488 (uintmax_t)elm
->beg_tid
, (uintmax_t)elm
->end_tid
);
494 softprune_usage(int code
)
496 fprintf(stderr
, "Badly formed prune command, use:\n");
497 fprintf(stderr
, "hammer prune <softlink-dir>\n");
498 fprintf(stderr
, "hammer prune-everything <filesystem>\n");