kernel - Refactor Xinvltlb (3)
[dragonfly.git] / sbin / hammer / cmd_softprune.c
blobd336da265cebe90e4a36abcb6001005ad3039ad5
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.7 2008/08/21 23:28:43 thomas Exp $
37 #include "hammer.h"
39 static void softprune_usage(int code);
40 static int hammer_softprune_scanmeta(int fd, struct softprune *scan,
41 int delete_all);
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,
46 struct stat *st,
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>
56 void
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;
62 int fd;
63 int rcode;
65 base = NULL;
66 rcode = 0;
67 if (TimeoutOpt > 0)
68 alarm(TimeoutOpt);
70 bzero(&pfs, sizeof(pfs));
71 pfs.bytes = sizeof(*pfs.ondisk);
72 pfs.ondisk = malloc(pfs.bytes);
73 bzero(pfs.ondisk, pfs.bytes);
74 pfs.pfs_id = -1;
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)
95 softprune_usage(1);
98 * Scan the softlink directory.
100 if (everything_opt) {
101 const char *dummylink = "";
102 scan = hammer_softprune_addentry(&base, &template,
103 *av, NULL, NULL,
104 dummylink, dummylink);
105 if (scan == NULL)
106 softprune_usage(1);
107 scan->prune.nelms = 0;
108 scan->prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
109 } else {
110 hammer_softprune_scandir(&base, &template, *av);
111 if (base == NULL) {
112 const char *dummylink = "";
113 scan = hammer_softprune_addentry(&base, &template,
114 *av, NULL, NULL,
115 dummylink, dummylink);
116 if (scan == NULL)
117 softprune_usage(1);
118 scan->prune.nelms = 0;
120 ++av;
121 --ac;
125 * XXX future (need to store separate cycles for each filesystem)
127 if (base->next) {
128 fprintf(stderr, "Currently only one HAMMER filesystem may "
129 "be specified in the softlink scan\n");
130 exit(1);
134 * Issue the prunes
136 for (scan = base; scan; scan = scan->next) {
138 * Open the filesystem for ioctl calls and extract the
139 * PFS.
141 fd = open(scan->filesystem, O_RDONLY);
142 if (fd < 0) {
143 warn("Unable to open %s", scan->filesystem);
144 rcode = 1;
145 continue;
148 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
149 warn("Filesystem %s is not HAMMER", scan->filesystem);
150 rcode = 1;
151 close(fd);
152 continue;
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",
162 scan->filesystem);
163 rcode = 1;
164 close(fd);
165 continue;
169 * Finalize operations
171 hammer_softprune_finalize(scan);
172 if (everything_opt) {
173 printf("Prune %s: EVERYTHING\n",
174 scan->filesystem);
175 } else {
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");
182 continue;
185 printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
186 "pfs_id %d\n",
187 scan->filesystem,
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,
192 pfs.pfs_id);
193 printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
194 scan->filesystem,
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);
200 RunningIoctl = 1;
201 if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) {
202 printf("Prune %s failed: %s\n",
203 scan->filesystem, strerror(errno));
204 rcode = 2;
205 } else if (scan->prune.head.flags & HAMMER_IOC_HEAD_INTR) {
206 printf("Prune %s interrupted by timer at "
207 "%016jx %04x\n",
208 scan->filesystem,
209 (uintmax_t)scan->prune.key_cur.obj_id,
210 scan->prune.key_cur.localization);
211 if (CyclePath)
212 hammer_set_cycle(&scan->prune.key_cur, 0);
213 rcode = 0;
214 } else {
215 if (CyclePath)
216 hammer_reset_cycle();
217 printf("Prune %s succeeded\n", scan->filesystem);
219 printf("Pruned %jd/%jd records (%jd directory entries) "
220 "and %jd bytes\n",
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
226 RunningIoctl = 0;
227 close(fd);
229 if (rcode)
230 exit(rcode);
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.
241 void
242 hammer_softprune_scandir(struct softprune **basep,
243 struct hammer_ioc_prune *template,
244 const char *dirname)
246 struct stat st;
247 struct dirent *den;
248 DIR *dir;
249 char *path;
250 int len;
251 char *linkbuf;
252 char *ptr;
254 path = NULL;
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)
261 continue;
262 if (strcmp(den->d_name, "..") == 0)
263 continue;
264 if (path)
265 free(path);
266 asprintf(&path, "%s/%s", dirname, den->d_name);
267 if (lstat(path, &st) < 0)
268 continue;
269 if (!S_ISLNK(st.st_mode))
270 continue;
271 if ((len = readlink(path, linkbuf, MAXPATHLEN - 1)) < 0)
272 continue;
273 linkbuf[len] = 0;
274 if ((ptr = strrchr(linkbuf, '@')) &&
275 ptr > linkbuf && ptr[-1] == '@') {
276 hammer_softprune_addentry(basep, template,
277 dirname, den->d_name, &st,
278 linkbuf, ptr - 1);
281 free(linkbuf);
282 if (path)
283 free(path);
287 * Scan the metadata snapshots for the filesystem and either delete them
288 * or add them to the pruning list.
290 static
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;
298 time_t ct;
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)
306 return(-1);
307 if (version.cur_version < 3)
308 return(0);
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
316 * be deleted.
318 for (;;) {
319 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
320 printf("hammer prune: Unable to access "
321 "meta-data snaps: %s\n", strerror(errno));
322 return(-1);
324 while (snapshot.index < snapshot.count) {
325 snap = &snapshot.snaps[snapshot.index];
326 if (delete_all) {
327 dsnapshot.snaps[dsnapshot.count++] = *snap;
328 if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
329 hammer_meta_flushdelete(fd, &dsnapshot);
330 } else {
331 ct = snap->ts / 1000000ULL;
332 hammer_softprune_addelm(scan, snap->tid,
333 ct, ct);
335 ++snapshot.index;
337 if (snapshot.head.flags & HAMMER_IOC_SNAPSHOT_EOF)
338 break;
339 snapshot.index = 0;
341 if (delete_all)
342 hammer_meta_flushdelete(fd, &dsnapshot);
343 return(0);
347 * Flush any entries built up in the deletion snapshot ioctl structure.
348 * Used during a prune-everything.
350 static void
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)
355 break;
356 if (dsnap->head.error == 0)
357 break;
358 ++dsnap->index;
360 dsnap->index = 0;
361 dsnap->count = 0;
365 * Add the softlink to the appropriate softprune structure, creating a new
366 * one if necessary.
368 static
369 struct softprune *
370 hammer_softprune_addentry(struct softprune **basep,
371 struct hammer_ioc_prune *template,
372 const char *dirpath, const char *denname __unused,
373 struct stat *st,
374 const char *linkbuf, const char *tidptr)
376 struct softprune *scan;
377 struct statfs fs;
378 char *fspath;
381 * Calculate filesystem path.
383 if (linkbuf[0] == '/') {
384 asprintf(&fspath, "%*.*s",
385 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
386 linkbuf);
387 } else {
388 asprintf(&fspath, "%s/%*.*s", dirpath,
389 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
390 linkbuf);
392 if (statfs(fspath, &fs) < 0) {
393 free(fspath);
394 return(NULL);
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)
402 continue;
403 if (strcmp(fs.f_mntonname, scan->fs.f_mntonname) != 0)
404 continue;
405 break;
409 * Create a new softprune structure if necessasry
411 if (scan == NULL) {
412 scan = malloc(sizeof(*scan));
413 bzero(scan, sizeof(*scan));
415 scan->fs = fs;
416 scan->filesystem = fspath;
417 scan->prune = *template;
418 scan->maxelms = 32;
419 scan->prune.elms = malloc(sizeof(struct hammer_ioc_prune_elm) *
420 scan->maxelms);
421 scan->next = *basep;
422 *basep = scan;
423 } else {
424 free(fspath);
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));
430 return(scan);
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.
439 static void
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];
456 elm->beg_tid = tid;
457 elm->end_tid = 0;
458 elm->mod_tid = 0;
459 if (ct < mt)
460 elm->mod_tid = ct;
461 else
462 elm->mod_tid = mt;
463 ++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
469 * mod_tid.
471 * The array must end up in descending order.
473 static int
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)
480 return(1);
481 if (elm1->beg_tid > elm2->beg_tid)
482 return(-1);
483 return(0);
486 static void
487 hammer_softprune_finalize(struct softprune *scan)
489 struct hammer_ioc_prune_elm *elm;
490 time_t t;
491 long delta;
492 int i;
495 * Don't do anything if there are no elements.
497 if (scan->prune.nelms == 0)
498 return;
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];
509 if (i == 0) {
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) {
516 * Remove duplicate
518 --scan->prune.nelms;
519 if (i != scan->prune.nelms) {
520 bcopy(elm + 1, elm,
521 (scan->prune.nelms - i) * sizeof(*elm));
523 --i;
524 continue;
525 } else {
527 * Middle or last.
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
536 * the period.
538 if (scan->prune_min) {
539 t = time(NULL);
540 for (i = scan->prune.nelms - 1; i >= 0; --i) {
541 elm = &scan->prune.elms[i];
542 if (elm->mod_tid == 0)
543 continue;
544 delta = (long)(t - (time_t)elm->mod_tid);
545 if (delta < scan->prune_min)
546 break;
548 ++i;
549 if (i) {
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]));
568 --scan->prune.nelms;
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];
578 elm->beg_tid = 1;
579 elm->end_tid = elm[-1].beg_tid;
580 ++scan->prune.nelms;
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);
594 static
595 void
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");
601 exit(code);