xref: /dragonfly/sbin/hammer/cmd_softprune.c (revision 68732d8f)
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  *
34  * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.7 2008/08/21 23:28:43 thomas Exp $
35  */
36 
37 #include "hammer.h"
38 
39 struct softprune {
40 	struct softprune *next;
41 	struct statfs fs;
42 	char *filesystem;
43 	struct hammer_ioc_prune prune;
44 	int maxelms;
45 	int prune_min;
46 };
47 
48 static void hammer_softprune_scandir(struct softprune **basep,
49 			struct hammer_ioc_prune *template,
50 			const char *dirname);
51 static int hammer_softprune_scanmeta(int fd, struct softprune *scan,
52 			int delete_all);
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,
57 			struct stat *st,
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);
63 
64 /*
65  * prune <softlink-dir>
66  * prune-everything <filesystem>
67  */
68 void
69 hammer_cmd_softprune(char **av, int ac, int everything_opt)
70 {
71 	struct hammer_ioc_prune template;
72 	struct hammer_ioc_pseudofs_rw pfs;
73 	struct softprune *base, *scan;
74 	int fd;
75 	int rcode;
76 
77 	base = NULL;
78 	rcode = 0;
79 	if (TimeoutOpt > 0)
80 		alarm(TimeoutOpt);
81 
82 	clrpfs(&pfs, NULL, -1);
83 
84 	/*
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.
89 	 */
90 
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;
98 
99 	/*
100 	 * For now just allow one directory
101 	 */
102 	if (ac == 0 || ac > 1) {
103 		softprune_usage(1);
104 		/* not reached */
105 	}
106 
107 	/*
108 	 * Scan the softlink directory.
109 	 */
110 	if (everything_opt) {
111 		const char *dummylink = "";
112 		scan = hammer_softprune_addentry(&base, &template,
113 						 *av, NULL, NULL,
114 						 dummylink, dummylink);
115 		if (scan == NULL) {
116 			softprune_usage(1);
117 			/* not reached */
118 		}
119 		scan->prune.nelms = 0;
120 		scan->prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
121 	} else {
122 		hammer_softprune_scandir(&base, &template, *av);
123 		if (base == NULL) {
124 			const char *dummylink = "";
125 			scan = hammer_softprune_addentry(&base, &template,
126 							 *av, NULL, NULL,
127 							 dummylink, dummylink);
128 			if (scan == NULL) {
129 				softprune_usage(1);
130 				/* not reached */
131 			}
132 			scan->prune.nelms = 0;
133 		}
134 		++av;
135 		--ac;
136 	}
137 
138 	/*
139 	 * XXX future (need to store separate cycles for each filesystem)
140 	 */
141 	if (base->next) {
142 		errx(1, "Currently only one HAMMER filesystem may "
143 			"be specified in the softlink scan");
144 		/* not reached */
145 	}
146 
147 	/*
148 	 * Issue the prunes
149 	 */
150 	for (scan = base; scan; scan = scan->next) {
151 		/*
152 		 * Open the filesystem for ioctl calls and extract the
153 		 * PFS.
154 		 */
155 		fd = open(scan->filesystem, O_RDONLY);
156 		if (fd < 0) {
157 			warn("Unable to open %s", scan->filesystem);
158 			rcode = 1;
159 			continue;
160 		}
161 
162 		if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
163 			warn("Filesystem %s is not HAMMER", scan->filesystem);
164 			rcode = 1;
165 			close(fd);
166 			continue;
167 		}
168 		scan->prune_min = pfs.ondisk->prune_min;
169 
170 		/*
171 		 * Incorporate meta-data snapshots into the pruning regimen.
172 		 * If pruning everything we delete the meta-data snapshots.
173 		 */
174 		if (hammer_softprune_scanmeta(fd, scan, everything_opt) < 0) {
175 			warn("Filesystem %s could not scan meta-data snaps",
176 			     scan->filesystem);
177 			rcode = 1;
178 			close(fd);
179 			continue;
180 		}
181 
182 		/*
183 		 * Finalize operations
184 		 */
185 		hammer_softprune_finalize(scan);
186 		if (everything_opt) {
187 			printf("Prune %s: EVERYTHING\n",
188 			       scan->filesystem);
189 		} else {
190 			printf("Prune %s: %d snapshots\n",
191 			       scan->filesystem, scan->prune.nelms);
192 		}
193 		if (scan->prune.nelms == 0 &&
194 		    (scan->prune.head.flags & HAMMER_IOC_PRUNE_ALL) == 0) {
195 			fprintf(stderr, "No snapshots found\n");
196 			continue;
197 		}
198 
199 		printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
200 		       "pfs_id %d\n",
201 		       scan->filesystem,
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,
206 		       pfs.pfs_id);
207 		printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
208 		       scan->filesystem,
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);
213 
214 		RunningIoctl = 1;
215 		if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) {
216 			printf("Prune %s failed: %s\n",
217 			       scan->filesystem, strerror(errno));
218 			rcode = 2;
219 		} else if (scan->prune.head.flags & HAMMER_IOC_HEAD_INTR) {
220 			printf("Prune %s interrupted by timer at "
221 			       "%016jx %04x\n",
222 			       scan->filesystem,
223 			       (uintmax_t)scan->prune.key_cur.obj_id,
224 			       scan->prune.key_cur.localization);
225 			if (CyclePath)
226 				hammer_set_cycle(&scan->prune.key_cur, 0);
227 			rcode = 0;
228 		} else {
229 			if (CyclePath)
230 				hammer_reset_cycle();
231 			printf("Prune %s succeeded\n", scan->filesystem);
232 		}
233 		printf("Pruned %jd/%jd records (%jd directory entries) "
234 		       "and %jd bytes\n",
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
239 		);
240 		RunningIoctl = 0;
241 		close(fd);
242 	}
243 	if (rcode)
244 		exit(rcode);
245 }
246 
247 /*
248  * Scan a directory for softlinks representing snapshots and build
249  * associated softprune structures.
250  *
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.
254  */
255 static
256 void
257 hammer_softprune_scandir(struct softprune **basep,
258 			 struct hammer_ioc_prune *template,
259 			 const char *dirname)
260 {
261 	struct stat st;
262 	struct dirent *den;
263 	DIR *dir;
264 	char *path;
265 	int len;
266 	char *linkbuf;
267 	char *ptr;
268 
269 	path = NULL;
270 	linkbuf = malloc(MAXPATHLEN);
271 
272 	if ((dir = opendir(dirname)) == NULL) {
273 		err(1, "Cannot open directory %s", dirname);
274 		/* not reached */
275 	}
276 	while ((den = readdir(dir)) != NULL) {
277 		if (strcmp(den->d_name, ".") == 0)
278 			continue;
279 		if (strcmp(den->d_name, "..") == 0)
280 			continue;
281 		if (path)
282 			free(path);
283 		asprintf(&path, "%s/%s", dirname, den->d_name);
284 		if (lstat(path, &st) < 0)
285 			continue;
286 		if (!S_ISLNK(st.st_mode))
287 			continue;
288 		if ((len = readlink(path, linkbuf, MAXPATHLEN - 1)) < 0)
289 			continue;
290 		linkbuf[len] = 0;
291 		if ((ptr = strrchr(linkbuf, '@')) &&
292 		    ptr > linkbuf && ptr[-1] == '@') {
293 			hammer_softprune_addentry(basep, template,
294 						  dirname, den->d_name, &st,
295 						  linkbuf, ptr - 1);
296 		}
297 	}
298 	free(linkbuf);
299 	if (path)
300 		free(path);
301 }
302 
303 /*
304  * Scan a directory for softlinks representing snapshots.
305  * Return 1 if the directory contains snapshots, otherwise 0.
306  */
307 int
308 hammer_softprune_testdir(const char *dirname)
309 {
310 	struct softprune *base = NULL;
311 	struct hammer_ioc_prune dummy_template;
312 
313 	bzero(&dummy_template, sizeof(dummy_template));
314 	hammer_softprune_scandir(&base, &dummy_template, dirname);
315 
316 	if (base)
317 		return(1);
318 	return(0);
319 }
320 
321 /*
322  * Scan the metadata snapshots for the filesystem and either delete them
323  * or add them to the pruning list.
324  */
325 static
326 int
327 hammer_softprune_scanmeta(int fd, struct softprune *scan, int delete_all)
328 {
329 	struct hammer_ioc_version	version;
330 	struct hammer_ioc_snapshot	snapshot;
331 	struct hammer_ioc_snapshot	dsnapshot;
332 	hammer_snapshot_data_t		snap;
333 	time_t ct;
334 
335 	/*
336 	 * Stop if we can't get the version.  Meta-data snapshots only
337 	 * exist for HAMMER version 3 or greater.
338 	 */
339 	bzero(&version, sizeof(version));
340 	if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
341 		return(-1);
342 	HammerVersion = version.cur_version;
343 	if (version.cur_version < 3)
344 		return(0);
345 
346 	bzero(&snapshot, sizeof(snapshot));
347 	bzero(&dsnapshot, sizeof(dsnapshot));
348 
349 	/*
350 	 * Scan meta-data snapshots, either add them to the prune list or
351 	 * delete them.  When deleting, just skip any entries which cannot
352 	 * be deleted.
353 	 */
354 	for (;;) {
355 		if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
356 			printf("hammer prune: Unable to access "
357 			       "meta-data snaps: %s\n", strerror(errno));
358 			return(-1);
359 		}
360 		while (snapshot.index < snapshot.count) {
361 			snap = &snapshot.snaps[snapshot.index];
362 			if (delete_all) {
363 				dsnapshot.snaps[dsnapshot.count++] = *snap;
364 				if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
365 					hammer_meta_flushdelete(fd, &dsnapshot);
366 			} else {
367 				ct = snap->ts / 1000000ULL;
368 				hammer_softprune_addelm(scan, snap->tid,
369 							ct, ct);
370 			}
371 			++snapshot.index;
372 		}
373 		if (snapshot.head.flags & HAMMER_IOC_SNAPSHOT_EOF)
374 			break;
375 		snapshot.index = 0;
376 	}
377 	if (delete_all)
378 		hammer_meta_flushdelete(fd, &dsnapshot);
379 	return(0);
380 }
381 
382 /*
383  * Flush any entries built up in the deletion snapshot ioctl structure.
384  * Used during a prune-everything.
385  */
386 static
387 void
388 hammer_meta_flushdelete(int fd, struct hammer_ioc_snapshot *dsnap)
389 {
390 	while (dsnap->index < dsnap->count) {
391 		if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnap) < 0)
392 			break;
393 		if (dsnap->head.error == 0)
394 			break;
395 		++dsnap->index;
396 	}
397 	dsnap->index = 0;
398 	dsnap->count = 0;
399 }
400 
401 /*
402  * Add the softlink to the appropriate softprune structure, creating a new
403  * one if necessary.
404  */
405 static
406 struct softprune *
407 hammer_softprune_addentry(struct softprune **basep,
408 			 struct hammer_ioc_prune *template,
409 			 const char *dirpath, const char *denname __unused,
410 			 struct stat *st,
411 			 const char *linkbuf, const char *tidptr)
412 {
413 	struct softprune *scan;
414 	struct statfs fs;
415 	char *fspath;
416 
417 	/*
418 	 * Calculate filesystem path.
419 	 */
420 	if (linkbuf[0] == '/') {
421 		asprintf(&fspath, "%*.*s",
422 			 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
423 			 linkbuf);
424 	} else {
425 		asprintf(&fspath, "%s/%*.*s", dirpath,
426 			 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
427 			 linkbuf);
428 	}
429 	if (statfs(fspath, &fs) < 0) {
430 		free(fspath);
431 		return(NULL);
432 	}
433 
434 	/*
435 	 * Locate the filesystem in an existing softprune structure
436 	 */
437 	for (scan = *basep; scan; scan = scan->next) {
438 		if (bcmp(&fs.f_fsid, &scan->fs.f_fsid, sizeof(fs.f_fsid)) != 0)
439 			continue;
440 		if (strcmp(fs.f_mntonname, scan->fs.f_mntonname) != 0)
441 			continue;
442 		break;
443 	}
444 
445 	/*
446 	 * Create a new softprune structure if necessasry
447 	 */
448 	if (scan == NULL) {
449 		scan = malloc(sizeof(*scan));
450 		bzero(scan, sizeof(*scan));
451 
452 		scan->fs = fs;
453 		scan->filesystem = fspath;
454 		scan->prune = *template;
455 		scan->maxelms = 32;
456 		scan->prune.elms = malloc(sizeof(struct hammer_ioc_prune_elm) *
457 					  scan->maxelms);
458 		scan->next = *basep;
459 		*basep = scan;
460 	} else {
461 		free(fspath);
462 	}
463 	hammer_softprune_addelm(scan,
464 				(hammer_tid_t)strtoull(tidptr + 2, NULL, 0),
465 				(st ? st->st_ctime : 0),
466 				(st ? st->st_mtime : 0));
467 	return(scan);
468 }
469 
470 /*
471  * Add the entry (unsorted).  Just set the beg_tid, we will sort
472  * and set the remaining entries later.
473  *
474  * Always leave one entry free for our terminator.
475  */
476 static
477 void
478 hammer_softprune_addelm(struct softprune *scan, hammer_tid_t tid,
479 			time_t ct, time_t mt)
480 {
481 	struct hammer_ioc_prune_elm *elm;
482 
483 	if (scan->prune.nelms >= scan->maxelms - 1) {
484 		scan->maxelms = (scan->maxelms * 3 / 2);
485 		scan->prune.elms = realloc(scan->prune.elms,
486 					   sizeof(*elm) * scan->maxelms);
487 	}
488 
489 	/*
490 	 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
491 	 *	 This will be cleaned up in the finalization phase.
492 	 */
493 	elm = &scan->prune.elms[scan->prune.nelms];
494 	elm->beg_tid = tid;
495 	elm->end_tid = 0;
496 	elm->mod_tid = 0;
497 	if (ct < mt)
498 		elm->mod_tid = ct;
499 	else
500 		elm->mod_tid = mt;
501 	++scan->prune.nelms;
502 }
503 
504 /*
505  * Finalize a softprune structure after scanning in its softlinks.
506  * Sort the elements, remove duplicates, and then fill in end_tid and
507  * mod_tid.
508  *
509  * The array must end up in descending order.
510  */
511 static
512 int
513 hammer_softprune_qsort_cmp(const void *arg1, const void *arg2)
514 {
515 	const struct hammer_ioc_prune_elm *elm1 = arg1;
516 	const struct hammer_ioc_prune_elm *elm2 = arg2;
517 
518 	if (elm1->beg_tid < elm2->beg_tid)
519 		return(1);
520 	if (elm1->beg_tid > elm2->beg_tid)
521 		return(-1);
522 	return(0);
523 }
524 
525 static
526 void
527 hammer_softprune_finalize(struct softprune *scan)
528 {
529 	struct hammer_ioc_prune_elm *elm;
530 	time_t t;
531 	long delta;
532 	int i;
533 
534 	/*
535 	 * Don't do anything if there are no elements.
536 	 */
537 	if (scan->prune.nelms == 0)
538 		return;
539 
540 	/*
541 	 * Sort the elements in descending order, remove duplicates, and
542 	 * fill in any missing bits.
543 	 */
544 	qsort(scan->prune.elms, scan->prune.nelms, sizeof(*elm),
545 	      hammer_softprune_qsort_cmp);
546 
547 	for (i = 0; i < scan->prune.nelms; ++i) {
548 		elm = &scan->prune.elms[i];
549 		if (i == 0) {
550 			/*
551 			 * First (highest TID) (also last if only one element)
552 			 */
553 			elm->end_tid = HAMMER_MAX_TID;
554 		} else if (elm[0].beg_tid == elm[-1].beg_tid) {
555 			/*
556 			 * Remove duplicate
557 			 */
558 			--scan->prune.nelms;
559 			if (i != scan->prune.nelms) {
560 				bcopy(elm + 1, elm,
561 				      (scan->prune.nelms - i) * sizeof(*elm));
562 			}
563 			--i;
564 			continue;
565 		} else {
566 			/*
567 			 * Middle or last.
568 			 */
569 			elm->end_tid = elm[-1].beg_tid;
570 		}
571 	}
572 
573 	/*
574 	 * If a minimum retention time (in seconds) is configured for the
575 	 * PFS, remove any snapshots from the pruning list that are within
576 	 * the period.
577 	 */
578 	if (scan->prune_min) {
579 		t = time(NULL);
580 		for (i = scan->prune.nelms - 1; i >= 0; --i) {
581 			elm = &scan->prune.elms[i];
582 			if (elm->mod_tid == 0)
583 				continue;
584 			delta = (long)(t - (time_t)elm->mod_tid);
585 			if (delta < scan->prune_min)
586 				break;
587 		}
588 		++i;
589 		if (i) {
590 			printf("Prune %s: prune_min: Will not clean between "
591 			       "the teeth of the first %d snapshots\n",
592 			       scan->filesystem, i);
593 			bcopy(&scan->prune.elms[i], &scan->prune.elms[0],
594 			      (scan->prune.nelms - i) * sizeof(scan->prune.elms[0]));
595 			scan->prune.elms[0].end_tid = HAMMER_MAX_TID;
596 			scan->prune.nelms -= i;
597 		}
598 	}
599 
600 	/*
601 	 * Remove the first entry.  This entry represents the prune from
602 	 * the most recent snapshot to current.  We wish to retain the
603 	 * fine-grained history for this region.
604 	 */
605 	if (scan->prune.nelms) {
606 		bcopy(&scan->prune.elms[1], &scan->prune.elms[0],
607 		      (scan->prune.nelms - 1) * sizeof(scan->prune.elms[0]));
608 		--scan->prune.nelms;
609 	}
610 
611 	/*
612 	 * Add a final element to prune everything from transaction id
613 	 * 0 to the lowest transaction id (aka last so far).
614 	 */
615 	if (scan->prune.nelms) {
616 		assert(scan->prune.nelms < scan->maxelms);
617 		elm = &scan->prune.elms[scan->prune.nelms];
618 		elm->beg_tid = 1;
619 		elm->end_tid = elm[-1].beg_tid;
620 		++scan->prune.nelms;
621 	}
622 
623 	/*
624 	 * Adjust mod_tid to what the ioctl() expects.
625 	 */
626 	for (i = 0; i < scan->prune.nelms; ++i) {
627 		elm = &scan->prune.elms[i];
628 		elm->mod_tid = elm->end_tid - elm->beg_tid;
629 		printf("TID %016jx - %016jx\n",
630 		       (uintmax_t)elm->beg_tid, (uintmax_t)elm->end_tid);
631 	}
632 }
633 
634 static
635 void
636 softprune_usage(int code)
637 {
638 	fprintf(stderr, "Badly formed prune command, use:\n");
639 	fprintf(stderr, "hammer prune <softlink-dir>\n");
640 	fprintf(stderr, "hammer prune-everything <filesystem>\n");
641 	exit(code);
642 }
643 
644