xref: /dragonfly/sbin/hammer/cmd_softprune.c (revision d4ef6694)
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 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);
51 
52 /*
53  * prune <softlink-dir>
54  * prune-everything <filesystem>
55  */
56 void
57 hammer_cmd_softprune(char **av, int ac, int everything_opt)
58 {
59 	struct hammer_ioc_prune template;
60 	struct hammer_ioc_pseudofs_rw pfs;
61 	struct softprune *base, *scan;
62 	int fd;
63 	int rcode;
64 
65 	base = NULL;
66 	rcode = 0;
67 	if (TimeoutOpt > 0)
68 		alarm(TimeoutOpt);
69 
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;
75 
76 	/*
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.
81 	 */
82 
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;
90 
91 	/*
92 	 * For now just allow one directory
93 	 */
94 	if (ac == 0 || ac > 1)
95 		softprune_usage(1);
96 
97 	/*
98 	 * Scan the softlink directory.
99 	 */
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;
119 		}
120 		++av;
121 		--ac;
122 	}
123 
124 	/*
125 	 * XXX future (need to store separate cycles for each filesystem)
126 	 */
127 	if (base->next) {
128 		fprintf(stderr, "Currently only one HAMMER filesystem may "
129 				"be specified in the softlink scan\n");
130 		exit(1);
131 	}
132 
133 	/*
134 	 * Issue the prunes
135 	 */
136 	for (scan = base; scan; scan = scan->next) {
137 		/*
138 		 * Open the filesystem for ioctl calls and extract the
139 		 * PFS.
140 		 */
141 		fd = open(scan->filesystem, O_RDONLY);
142 		if (fd < 0) {
143 			warn("Unable to open %s", scan->filesystem);
144 			rcode = 1;
145 			continue;
146 		}
147 
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;
153 		}
154 		scan->prune_min = pfs.ondisk->prune_min;
155 
156 		/*
157 		 * Incorporate meta-data snapshots into the pruning regimen.
158 		 * If pruning everything we delete the meta-data snapshots.
159 		 */
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;
166 		}
167 
168 		/*
169 		 * Finalize operations
170 		 */
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);
178 		}
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;
183 		}
184 
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);
199 
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);
218 		}
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
225 		);
226 		RunningIoctl = 0;
227 		close(fd);
228 	}
229 	if (rcode)
230 		exit(rcode);
231 }
232 
233 /*
234  * Scan a directory for softlinks representing snapshots and build
235  * associated softprune structures.
236  *
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.
240  */
241 void
242 hammer_softprune_scandir(struct softprune **basep,
243 			 struct hammer_ioc_prune *template,
244 			 const char *dirname)
245 {
246 	struct stat st;
247 	struct dirent *den;
248 	DIR *dir;
249 	char *path;
250 	int len;
251 	char *linkbuf;
252 	char *ptr;
253 
254 	path = NULL;
255 	linkbuf = malloc(MAXPATHLEN);
256 
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);
279 		}
280 	}
281 	free(linkbuf);
282 	if (path)
283 		free(path);
284 }
285 
286 /*
287  * Scan the metadata snapshots for the filesystem and either delete them
288  * or add them to the pruning list.
289  */
290 static
291 int
292 hammer_softprune_scanmeta(int fd, struct softprune *scan, int delete_all)
293 {
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;
299 
300 	/*
301 	 * Stop if we can't get the version.  Meta-data snapshots only
302 	 * exist for HAMMER version 3 or greater.
303 	 */
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);
309 
310 	bzero(&snapshot, sizeof(snapshot));
311 	bzero(&dsnapshot, sizeof(dsnapshot));
312 
313 	/*
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.
317 	 */
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);
323 		}
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);
334 			}
335 			++snapshot.index;
336 		}
337 		if (snapshot.head.flags & HAMMER_IOC_SNAPSHOT_EOF)
338 			break;
339 		snapshot.index = 0;
340 	}
341 	if (delete_all)
342 		hammer_meta_flushdelete(fd, &dsnapshot);
343 	return(0);
344 }
345 
346 /*
347  * Flush any entries built up in the deletion snapshot ioctl structure.
348  * Used during a prune-everything.
349  */
350 static void
351 hammer_meta_flushdelete(int fd, struct hammer_ioc_snapshot *dsnap)
352 {
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;
359 	}
360 	dsnap->index = 0;
361 	dsnap->count = 0;
362 }
363 
364 /*
365  * Add the softlink to the appropriate softprune structure, creating a new
366  * one if necessary.
367  */
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)
375 {
376 	struct softprune *scan;
377 	struct statfs fs;
378 	char *fspath;
379 
380 	/*
381 	 * Calculate filesystem path.
382 	 */
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);
391 	}
392 	if (statfs(fspath, &fs) < 0) {
393 		free(fspath);
394 		return(NULL);
395 	}
396 
397 	/*
398 	 * Locate the filesystem in an existing softprune structure
399 	 */
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;
406 	}
407 
408 	/*
409 	 * Create a new softprune structure if necessasry
410 	 */
411 	if (scan == NULL) {
412 		scan = malloc(sizeof(*scan));
413 		bzero(scan, sizeof(*scan));
414 
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);
425 	}
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);
431 }
432 
433 /*
434  * Add the entry (unsorted).  Just set the beg_tid, we will sort
435  * and set the remaining entries later.
436  *
437  * Always leave one entry free for our terminator.
438  */
439 static void
440 hammer_softprune_addelm(struct softprune *scan, hammer_tid_t tid,
441 			time_t ct, time_t mt)
442 {
443 	struct hammer_ioc_prune_elm *elm;
444 
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);
449 	}
450 
451 	/*
452 	 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
453 	 *	 This will be cleaned up in the finalization phase.
454 	 */
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;
464 }
465 
466 /*
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.
470  *
471  * The array must end up in descending order.
472  */
473 static int
474 hammer_softprune_qsort_cmp(const void *arg1, const void *arg2)
475 {
476 	const struct hammer_ioc_prune_elm *elm1 = arg1;
477 	const struct hammer_ioc_prune_elm *elm2 = arg2;
478 
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);
484 }
485 
486 static void
487 hammer_softprune_finalize(struct softprune *scan)
488 {
489 	struct hammer_ioc_prune_elm *elm;
490 	time_t t;
491 	long delta;
492 	int i;
493 
494 	/*
495 	 * Don't do anything if there are no elements.
496 	 */
497 	if (scan->prune.nelms == 0)
498 		return;
499 
500 	/*
501 	 * Sort the elements in descending order, remove duplicates, and
502 	 * fill in any missing bits.
503 	 */
504 	qsort(scan->prune.elms, scan->prune.nelms, sizeof(*elm),
505 	      hammer_softprune_qsort_cmp);
506 
507 	for (i = 0; i < scan->prune.nelms; ++i) {
508 		elm = &scan->prune.elms[i];
509 		if (i == 0) {
510 			/*
511 			 * First (highest TID) (also last if only one element)
512 			 */
513 			elm->end_tid = HAMMER_MAX_TID;
514 		} else if (elm[0].beg_tid == elm[-1].beg_tid) {
515 			/*
516 			 * Remove duplicate
517 			 */
518 			--scan->prune.nelms;
519 			if (i != scan->prune.nelms) {
520 				bcopy(elm + 1, elm,
521 				      (scan->prune.nelms - i) * sizeof(*elm));
522 			}
523 			--i;
524 			continue;
525 		} else {
526 			/*
527 			 * Middle or last.
528 			 */
529 			elm->end_tid = elm[-1].beg_tid;
530 		}
531 	}
532 
533 	/*
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.
537 	 */
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;
547 		}
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;
557 		}
558 	}
559 
560 	/*
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.
564 	 */
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;
569 	}
570 
571 	/*
572 	 * Add a final element to prune everything from transaction id
573 	 * 0 to the lowest transaction id (aka last so far).
574 	 */
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;
581 	}
582 
583 	/*
584 	 * Adjust mod_tid to what the ioctl() expects.
585 	 */
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);
591 	}
592 }
593 
594 static
595 void
596 softprune_usage(int code)
597 {
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);
602 }
603 
604 
605