xref: /dflybsd-src/sbin/hammer/cmd_softprune.c (revision 005a4da7c7e9f1ce1486991786575353476f9282)
1e8969ef0SMatthew Dillon /*
2e8969ef0SMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3e8969ef0SMatthew Dillon  *
4e8969ef0SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5e8969ef0SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6e8969ef0SMatthew Dillon  *
7e8969ef0SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8e8969ef0SMatthew Dillon  * modification, are permitted provided that the following conditions
9e8969ef0SMatthew Dillon  * are met:
10e8969ef0SMatthew Dillon  *
11e8969ef0SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12e8969ef0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13e8969ef0SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14e8969ef0SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15e8969ef0SMatthew Dillon  *    the documentation and/or other materials provided with the
16e8969ef0SMatthew Dillon  *    distribution.
17e8969ef0SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18e8969ef0SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19e8969ef0SMatthew Dillon  *    from this software without specific, prior written permission.
20e8969ef0SMatthew Dillon  *
21e8969ef0SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22e8969ef0SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23e8969ef0SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24e8969ef0SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25e8969ef0SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26e8969ef0SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27e8969ef0SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28e8969ef0SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29e8969ef0SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30e8969ef0SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31e8969ef0SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e8969ef0SMatthew Dillon  * SUCH DAMAGE.
33e8969ef0SMatthew Dillon  *
3434bb69d8SThomas Nikolajsen  * $DragonFly: src/sbin/hammer/cmd_softprune.c,v 1.7 2008/08/21 23:28:43 thomas Exp $
35e8969ef0SMatthew Dillon  */
36e8969ef0SMatthew Dillon 
37e8969ef0SMatthew Dillon #include "hammer.h"
38e8969ef0SMatthew Dillon 
39ce6ddec9STomohiro Kusumi struct softprune {
40ce6ddec9STomohiro Kusumi 	struct softprune *next;
41ce6ddec9STomohiro Kusumi 	struct statfs fs;
42ce6ddec9STomohiro Kusumi 	char *filesystem;
43ce6ddec9STomohiro Kusumi 	struct hammer_ioc_prune prune;
44ce6ddec9STomohiro Kusumi 	int maxelms;
45ce6ddec9STomohiro Kusumi 	int prune_min;
46ce6ddec9STomohiro Kusumi };
47ce6ddec9STomohiro Kusumi 
48ce6ddec9STomohiro Kusumi static void hammer_softprune_scandir(struct softprune **basep,
49ce6ddec9STomohiro Kusumi 			struct hammer_ioc_prune *template,
50ce6ddec9STomohiro Kusumi 			const char *dirname);
5183f2a3aaSMatthew Dillon static int hammer_softprune_scanmeta(int fd, struct softprune *scan,
5283f2a3aaSMatthew Dillon 			int delete_all);
5383f2a3aaSMatthew Dillon static void hammer_meta_flushdelete(int fd, struct hammer_ioc_snapshot *dsnap);
54b5aaba7fSMatthew Dillon static struct softprune *hammer_softprune_addentry(struct softprune **basep,
55e8969ef0SMatthew Dillon 			struct hammer_ioc_prune *template,
56e7f926a5SMatthew Dillon 			const char *dirpath, const char *denname,
57e7f926a5SMatthew Dillon 			struct stat *st,
58b5aaba7fSMatthew Dillon 			const char *linkbuf, const char *tidptr);
5983f2a3aaSMatthew Dillon static void hammer_softprune_addelm(struct softprune *scan, hammer_tid_t tid,
6083f2a3aaSMatthew Dillon 			time_t ct, time_t mt);
61e8969ef0SMatthew Dillon static void hammer_softprune_finalize(struct softprune *scan);
62ce6ddec9STomohiro Kusumi static void softprune_usage(int code);
63e8969ef0SMatthew Dillon 
64e8969ef0SMatthew Dillon /*
65b5aaba7fSMatthew Dillon  * prune <softlink-dir>
66b5aaba7fSMatthew Dillon  * prune-everything <filesystem>
67e8969ef0SMatthew Dillon  */
68e8969ef0SMatthew Dillon void
hammer_cmd_softprune(char ** av,int ac,int everything_opt)69b5aaba7fSMatthew Dillon hammer_cmd_softprune(char **av, int ac, int everything_opt)
70e8969ef0SMatthew Dillon {
71e8969ef0SMatthew Dillon 	struct hammer_ioc_prune template;
72e7f926a5SMatthew Dillon 	struct hammer_ioc_pseudofs_rw pfs;
73e8969ef0SMatthew Dillon 	struct softprune *base, *scan;
74e8969ef0SMatthew Dillon 	int fd;
75e8969ef0SMatthew Dillon 	int rcode;
76e8969ef0SMatthew Dillon 
77e8969ef0SMatthew Dillon 	base = NULL;
78e8969ef0SMatthew Dillon 	rcode = 0;
79243ca327SMatthew Dillon 	if (TimeoutOpt > 0)
80243ca327SMatthew Dillon 		alarm(TimeoutOpt);
81e8969ef0SMatthew Dillon 
821b23fc22STomohiro Kusumi 	clrpfs(&pfs, NULL, -1);
83e7f926a5SMatthew Dillon 
84ab378f79SMatthew Dillon 	/*
851d9f6aa1SMatthew Dillon 	 * NOTE: To restrict to a single file XXX we have to set
86ab378f79SMatthew Dillon 	 * the localization the same (not yet implemented).  Typically
87ab378f79SMatthew Dillon 	 * two passes would be needed, one using HAMMER_LOCALIZE_MISC
88ab378f79SMatthew Dillon 	 * and one using HAMMER_LOCALIZE_INODE.
89ab378f79SMatthew Dillon 	 */
90ab378f79SMatthew Dillon 
91e8969ef0SMatthew Dillon 	bzero(&template, sizeof(template));
921d9f6aa1SMatthew Dillon 	template.key_beg.localization = HAMMER_MIN_LOCALIZATION;
931d9f6aa1SMatthew Dillon 	template.key_beg.obj_id = HAMMER_MIN_OBJID;
941d9f6aa1SMatthew Dillon 	template.key_end.localization = HAMMER_MAX_LOCALIZATION;
951d9f6aa1SMatthew Dillon 	template.key_end.obj_id = HAMMER_MAX_OBJID;
96243ca327SMatthew Dillon 	hammer_get_cycle(&template.key_end, NULL);
97e8969ef0SMatthew Dillon 	template.stat_oldest_tid = HAMMER_MAX_TID;
98e8969ef0SMatthew Dillon 
99e8969ef0SMatthew Dillon 	/*
100e8969ef0SMatthew Dillon 	 * For now just allow one directory
101e8969ef0SMatthew Dillon 	 */
10288cdee70STomohiro Kusumi 	if (ac == 0 || ac > 1) {
103e8969ef0SMatthew Dillon 		softprune_usage(1);
10488cdee70STomohiro Kusumi 		/* not reached */
10588cdee70STomohiro Kusumi 	}
106e8969ef0SMatthew Dillon 
107e8969ef0SMatthew Dillon 	/*
108ab378f79SMatthew Dillon 	 * Scan the softlink directory.
109e8969ef0SMatthew Dillon 	 */
110b5aaba7fSMatthew Dillon 	if (everything_opt) {
111b5aaba7fSMatthew Dillon 		const char *dummylink = "";
112e7f926a5SMatthew Dillon 		scan = hammer_softprune_addentry(&base, &template,
113e7f926a5SMatthew Dillon 						 *av, NULL, NULL,
114b5aaba7fSMatthew Dillon 						 dummylink, dummylink);
11588cdee70STomohiro Kusumi 		if (scan == NULL) {
116b5aaba7fSMatthew Dillon 			softprune_usage(1);
11788cdee70STomohiro Kusumi 			/* not reached */
11888cdee70STomohiro Kusumi 		}
119b5aaba7fSMatthew Dillon 		scan->prune.nelms = 0;
120b5aaba7fSMatthew Dillon 		scan->prune.head.flags |= HAMMER_IOC_PRUNE_ALL;
121b5aaba7fSMatthew Dillon 	} else {
122e8969ef0SMatthew Dillon 		hammer_softprune_scandir(&base, &template, *av);
1230551025bSMatthew Dillon 		if (base == NULL) {
1240551025bSMatthew Dillon 			const char *dummylink = "";
1250551025bSMatthew Dillon 			scan = hammer_softprune_addentry(&base, &template,
1260551025bSMatthew Dillon 							 *av, NULL, NULL,
1270551025bSMatthew Dillon 							 dummylink, dummylink);
12888cdee70STomohiro Kusumi 			if (scan == NULL) {
1290551025bSMatthew Dillon 				softprune_usage(1);
13088cdee70STomohiro Kusumi 				/* not reached */
13188cdee70STomohiro Kusumi 			}
1320551025bSMatthew Dillon 			scan->prune.nelms = 0;
1330551025bSMatthew Dillon 		}
134e8969ef0SMatthew Dillon 		++av;
135e8969ef0SMatthew Dillon 		--ac;
136b5aaba7fSMatthew Dillon 	}
137e8969ef0SMatthew Dillon 
138e8969ef0SMatthew Dillon 	/*
139e8969ef0SMatthew Dillon 	 * XXX future (need to store separate cycles for each filesystem)
140e8969ef0SMatthew Dillon 	 */
141052fd72bSTomohiro Kusumi 	if (base->next) {
14202318f07STomohiro Kusumi 		errx(1, "Currently only one HAMMER filesystem may "
14302318f07STomohiro Kusumi 			"be specified in the softlink scan");
144052fd72bSTomohiro Kusumi 		/* not reached */
145052fd72bSTomohiro Kusumi 	}
146e8969ef0SMatthew Dillon 
147e8969ef0SMatthew Dillon 	/*
148e8969ef0SMatthew Dillon 	 * Issue the prunes
149e8969ef0SMatthew Dillon 	 */
150e8969ef0SMatthew Dillon 	for (scan = base; scan; scan = scan->next) {
151e7f926a5SMatthew Dillon 		/*
152e7f926a5SMatthew Dillon 		 * Open the filesystem for ioctl calls and extract the
153e7f926a5SMatthew Dillon 		 * PFS.
154e7f926a5SMatthew Dillon 		 */
155e7f926a5SMatthew Dillon 		fd = open(scan->filesystem, O_RDONLY);
156e7f926a5SMatthew Dillon 		if (fd < 0) {
157e7f926a5SMatthew Dillon 			warn("Unable to open %s", scan->filesystem);
158e7f926a5SMatthew Dillon 			rcode = 1;
159e7f926a5SMatthew Dillon 			continue;
160e7f926a5SMatthew Dillon 		}
161e7f926a5SMatthew Dillon 
162e7f926a5SMatthew Dillon 		if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) < 0) {
163e7f926a5SMatthew Dillon 			warn("Filesystem %s is not HAMMER", scan->filesystem);
164e7f926a5SMatthew Dillon 			rcode = 1;
165e7f926a5SMatthew Dillon 			close(fd);
166e7f926a5SMatthew Dillon 			continue;
167e7f926a5SMatthew Dillon 		}
168e7f926a5SMatthew Dillon 		scan->prune_min = pfs.ondisk->prune_min;
169e7f926a5SMatthew Dillon 
170e7f926a5SMatthew Dillon 		/*
17183f2a3aaSMatthew Dillon 		 * Incorporate meta-data snapshots into the pruning regimen.
17283f2a3aaSMatthew Dillon 		 * If pruning everything we delete the meta-data snapshots.
17383f2a3aaSMatthew Dillon 		 */
17483f2a3aaSMatthew Dillon 		if (hammer_softprune_scanmeta(fd, scan, everything_opt) < 0) {
17583f2a3aaSMatthew Dillon 			warn("Filesystem %s could not scan meta-data snaps",
17683f2a3aaSMatthew Dillon 			     scan->filesystem);
17783f2a3aaSMatthew Dillon 			rcode = 1;
17883f2a3aaSMatthew Dillon 			close(fd);
17983f2a3aaSMatthew Dillon 			continue;
18083f2a3aaSMatthew Dillon 		}
18183f2a3aaSMatthew Dillon 
18283f2a3aaSMatthew Dillon 		/*
183e7f926a5SMatthew Dillon 		 * Finalize operations
184e7f926a5SMatthew Dillon 		 */
185e8969ef0SMatthew Dillon 		hammer_softprune_finalize(scan);
186f254e677STomohiro Kusumi 		if (everything_opt) {
187b5aaba7fSMatthew Dillon 			printf("Prune %s: EVERYTHING\n",
188b5aaba7fSMatthew Dillon 			       scan->filesystem);
189f254e677STomohiro Kusumi 		} else {
190e8969ef0SMatthew Dillon 			printf("Prune %s: %d snapshots\n",
191e8969ef0SMatthew Dillon 			       scan->filesystem, scan->prune.nelms);
192f254e677STomohiro Kusumi 		}
193b5aaba7fSMatthew Dillon 		if (scan->prune.nelms == 0 &&
194b5aaba7fSMatthew Dillon 		    (scan->prune.head.flags & HAMMER_IOC_PRUNE_ALL) == 0) {
1950551025bSMatthew Dillon 			fprintf(stderr, "No snapshots found\n");
196e8969ef0SMatthew Dillon 			continue;
197b5aaba7fSMatthew Dillon 		}
198e7f926a5SMatthew Dillon 
199a276dc6bSMatthew Dillon 		printf("Prune %s: objspace %016jx:%04x %016jx:%04x "
200e7f926a5SMatthew Dillon 		       "pfs_id %d\n",
201e7f926a5SMatthew Dillon 		       scan->filesystem,
202a276dc6bSMatthew Dillon 		       (uintmax_t)scan->prune.key_beg.obj_id,
203445faa69SMatthew Dillon 		       scan->prune.key_beg.localization,
204a276dc6bSMatthew Dillon 		       (uintmax_t)scan->prune.key_end.obj_id,
205e7f926a5SMatthew Dillon 		       scan->prune.key_end.localization,
206e7f926a5SMatthew Dillon 		       pfs.pfs_id);
207e7f926a5SMatthew Dillon 		printf("Prune %s: prune_min is %dd/%02d:%02d:%02d\n",
208e7f926a5SMatthew Dillon 		       scan->filesystem,
209e7f926a5SMatthew Dillon 			pfs.ondisk->prune_min / (24 * 60 * 60),
210e7f926a5SMatthew Dillon 			pfs.ondisk->prune_min / 60 / 60 % 24,
211e7f926a5SMatthew Dillon 			pfs.ondisk->prune_min / 60 % 60,
212e7f926a5SMatthew Dillon 			pfs.ondisk->prune_min % 60);
213e8969ef0SMatthew Dillon 
214445faa69SMatthew Dillon 		RunningIoctl = 1;
215e8969ef0SMatthew Dillon 		if (ioctl(fd, HAMMERIOC_PRUNE, &scan->prune) < 0) {
216e8969ef0SMatthew Dillon 			printf("Prune %s failed: %s\n",
217e8969ef0SMatthew Dillon 			       scan->filesystem, strerror(errno));
218e8969ef0SMatthew Dillon 			rcode = 2;
219e8969ef0SMatthew Dillon 		} else if (scan->prune.head.flags & HAMMER_IOC_HEAD_INTR) {
220e8969ef0SMatthew Dillon 			printf("Prune %s interrupted by timer at "
221a276dc6bSMatthew Dillon 			       "%016jx %04x\n",
222e8969ef0SMatthew Dillon 			       scan->filesystem,
223a276dc6bSMatthew Dillon 			       (uintmax_t)scan->prune.key_cur.obj_id,
2241d9f6aa1SMatthew Dillon 			       scan->prune.key_cur.localization);
225a7fbbf91SMatthew Dillon 			if (CyclePath)
226243ca327SMatthew Dillon 				hammer_set_cycle(&scan->prune.key_cur, 0);
227e8969ef0SMatthew Dillon 			rcode = 0;
228e8969ef0SMatthew Dillon 		} else {
229e8969ef0SMatthew Dillon 			if (CyclePath)
230e8969ef0SMatthew Dillon 				hammer_reset_cycle();
231e8969ef0SMatthew Dillon 			printf("Prune %s succeeded\n", scan->filesystem);
232e8969ef0SMatthew Dillon 		}
233a276dc6bSMatthew Dillon 		printf("Pruned %jd/%jd records (%jd directory entries) "
234a276dc6bSMatthew Dillon 		       "and %jd bytes\n",
235a276dc6bSMatthew Dillon 			(uintmax_t)scan->prune.stat_rawrecords,
236a276dc6bSMatthew Dillon 			(uintmax_t)scan->prune.stat_scanrecords,
237a276dc6bSMatthew Dillon 			(uintmax_t)scan->prune.stat_dirrecords,
238a276dc6bSMatthew Dillon 			(uintmax_t)scan->prune.stat_bytes
239e8969ef0SMatthew Dillon 		);
240445faa69SMatthew Dillon 		RunningIoctl = 0;
241e8969ef0SMatthew Dillon 		close(fd);
242e8969ef0SMatthew Dillon 	}
243e8969ef0SMatthew Dillon 	if (rcode)
244e8969ef0SMatthew Dillon 		exit(rcode);
245e8969ef0SMatthew Dillon }
246e8969ef0SMatthew Dillon 
247e8969ef0SMatthew Dillon /*
248e8969ef0SMatthew Dillon  * Scan a directory for softlinks representing snapshots and build
249e8969ef0SMatthew Dillon  * associated softprune structures.
25083f2a3aaSMatthew Dillon  *
25183f2a3aaSMatthew Dillon  * NOTE: Once a filesystem is completely converted to the meta-data
25283f2a3aaSMatthew Dillon  *	 snapshot mechanic we don't have to scan softlinks any more
25383f2a3aaSMatthew Dillon  *	 and can just use the meta-data.  But for now we do both.
254e8969ef0SMatthew Dillon  */
255*005a4da7STomohiro Kusumi static
256*005a4da7STomohiro Kusumi void
hammer_softprune_scandir(struct softprune ** basep,struct hammer_ioc_prune * template,const char * dirname)257e8969ef0SMatthew Dillon hammer_softprune_scandir(struct softprune **basep,
258e8969ef0SMatthew Dillon 			 struct hammer_ioc_prune *template,
259e8969ef0SMatthew Dillon 			 const char *dirname)
260e8969ef0SMatthew Dillon {
261e8969ef0SMatthew Dillon 	struct stat st;
262e8969ef0SMatthew Dillon 	struct dirent *den;
263e8969ef0SMatthew Dillon 	DIR *dir;
264e8969ef0SMatthew Dillon 	char *path;
265e8969ef0SMatthew Dillon 	int len;
266e8969ef0SMatthew Dillon 	char *linkbuf;
267e8969ef0SMatthew Dillon 	char *ptr;
268e8969ef0SMatthew Dillon 
269e8969ef0SMatthew Dillon 	path = NULL;
270e8969ef0SMatthew Dillon 	linkbuf = malloc(MAXPATHLEN);
271e8969ef0SMatthew Dillon 
272052fd72bSTomohiro Kusumi 	if ((dir = opendir(dirname)) == NULL) {
273e8969ef0SMatthew Dillon 		err(1, "Cannot open directory %s", dirname);
274052fd72bSTomohiro Kusumi 		/* not reached */
275052fd72bSTomohiro Kusumi 	}
276e8969ef0SMatthew Dillon 	while ((den = readdir(dir)) != NULL) {
277e8969ef0SMatthew Dillon 		if (strcmp(den->d_name, ".") == 0)
278e8969ef0SMatthew Dillon 			continue;
279e8969ef0SMatthew Dillon 		if (strcmp(den->d_name, "..") == 0)
280e8969ef0SMatthew Dillon 			continue;
281e8969ef0SMatthew Dillon 		if (path)
282e8969ef0SMatthew Dillon 			free(path);
283e8969ef0SMatthew Dillon 		asprintf(&path, "%s/%s", dirname, den->d_name);
284e8969ef0SMatthew Dillon 		if (lstat(path, &st) < 0)
285e8969ef0SMatthew Dillon 			continue;
286e8969ef0SMatthew Dillon 		if (!S_ISLNK(st.st_mode))
287e8969ef0SMatthew Dillon 			continue;
288e8969ef0SMatthew Dillon 		if ((len = readlink(path, linkbuf, MAXPATHLEN - 1)) < 0)
289e8969ef0SMatthew Dillon 			continue;
290e8969ef0SMatthew Dillon 		linkbuf[len] = 0;
291e8969ef0SMatthew Dillon 		if ((ptr = strrchr(linkbuf, '@')) &&
292f254e677STomohiro Kusumi 		    ptr > linkbuf && ptr[-1] == '@') {
293e8969ef0SMatthew Dillon 			hammer_softprune_addentry(basep, template,
294e7f926a5SMatthew Dillon 						  dirname, den->d_name, &st,
295e7f926a5SMatthew Dillon 						  linkbuf, ptr - 1);
296e8969ef0SMatthew Dillon 		}
297f254e677STomohiro Kusumi 	}
298e8969ef0SMatthew Dillon 	free(linkbuf);
299e8969ef0SMatthew Dillon 	if (path)
300e8969ef0SMatthew Dillon 		free(path);
301e8969ef0SMatthew Dillon }
302e8969ef0SMatthew Dillon 
303e8969ef0SMatthew Dillon /*
304ce6ddec9STomohiro Kusumi  * Scan a directory for softlinks representing snapshots.
305ce6ddec9STomohiro Kusumi  * Return 1 if the directory contains snapshots, otherwise 0.
306ce6ddec9STomohiro Kusumi  */
307ce6ddec9STomohiro Kusumi int
hammer_softprune_testdir(const char * dirname)308ce6ddec9STomohiro Kusumi hammer_softprune_testdir(const char *dirname)
309ce6ddec9STomohiro Kusumi {
310ce6ddec9STomohiro Kusumi 	struct softprune *base = NULL;
311ce6ddec9STomohiro Kusumi 	struct hammer_ioc_prune dummy_template;
312ce6ddec9STomohiro Kusumi 
313ce6ddec9STomohiro Kusumi 	bzero(&dummy_template, sizeof(dummy_template));
314ce6ddec9STomohiro Kusumi 	hammer_softprune_scandir(&base, &dummy_template, dirname);
315ce6ddec9STomohiro Kusumi 
316ce6ddec9STomohiro Kusumi 	if (base)
317ce6ddec9STomohiro Kusumi 		return(1);
318ce6ddec9STomohiro Kusumi 	return(0);
319ce6ddec9STomohiro Kusumi }
320ce6ddec9STomohiro Kusumi 
321ce6ddec9STomohiro Kusumi /*
32283f2a3aaSMatthew Dillon  * Scan the metadata snapshots for the filesystem and either delete them
32383f2a3aaSMatthew Dillon  * or add them to the pruning list.
32483f2a3aaSMatthew Dillon  */
32583f2a3aaSMatthew Dillon static
32683f2a3aaSMatthew Dillon int
hammer_softprune_scanmeta(int fd,struct softprune * scan,int delete_all)32783f2a3aaSMatthew Dillon hammer_softprune_scanmeta(int fd, struct softprune *scan, int delete_all)
32883f2a3aaSMatthew Dillon {
32983f2a3aaSMatthew Dillon 	struct hammer_ioc_version	version;
33083f2a3aaSMatthew Dillon 	struct hammer_ioc_snapshot	snapshot;
33183f2a3aaSMatthew Dillon 	struct hammer_ioc_snapshot	dsnapshot;
33246e9f340STomohiro Kusumi 	hammer_snapshot_data_t		snap;
33383f2a3aaSMatthew Dillon 	time_t ct;
33483f2a3aaSMatthew Dillon 
33583f2a3aaSMatthew Dillon 	/*
33683f2a3aaSMatthew Dillon 	 * Stop if we can't get the version.  Meta-data snapshots only
33783f2a3aaSMatthew Dillon 	 * exist for HAMMER version 3 or greater.
33883f2a3aaSMatthew Dillon 	 */
33983f2a3aaSMatthew Dillon 	bzero(&version, sizeof(version));
34083f2a3aaSMatthew Dillon 	if (ioctl(fd, HAMMERIOC_GET_VERSION, &version) < 0)
34183f2a3aaSMatthew Dillon 		return(-1);
3424c09d9c4SMatthew Dillon 	HammerVersion = version.cur_version;
34383f2a3aaSMatthew Dillon 	if (version.cur_version < 3)
34483f2a3aaSMatthew Dillon 		return(0);
34583f2a3aaSMatthew Dillon 
34683f2a3aaSMatthew Dillon 	bzero(&snapshot, sizeof(snapshot));
34783f2a3aaSMatthew Dillon 	bzero(&dsnapshot, sizeof(dsnapshot));
34883f2a3aaSMatthew Dillon 
34983f2a3aaSMatthew Dillon 	/*
35083f2a3aaSMatthew Dillon 	 * Scan meta-data snapshots, either add them to the prune list or
35183f2a3aaSMatthew Dillon 	 * delete them.  When deleting, just skip any entries which cannot
35283f2a3aaSMatthew Dillon 	 * be deleted.
35383f2a3aaSMatthew Dillon 	 */
35483f2a3aaSMatthew Dillon 	for (;;) {
35583f2a3aaSMatthew Dillon 		if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapshot) < 0) {
35683f2a3aaSMatthew Dillon 			printf("hammer prune: Unable to access "
35783f2a3aaSMatthew Dillon 			       "meta-data snaps: %s\n", strerror(errno));
35883f2a3aaSMatthew Dillon 			return(-1);
35983f2a3aaSMatthew Dillon 		}
36083f2a3aaSMatthew Dillon 		while (snapshot.index < snapshot.count) {
36183f2a3aaSMatthew Dillon 			snap = &snapshot.snaps[snapshot.index];
36283f2a3aaSMatthew Dillon 			if (delete_all) {
36383f2a3aaSMatthew Dillon 				dsnapshot.snaps[dsnapshot.count++] = *snap;
36483f2a3aaSMatthew Dillon 				if (dsnapshot.count == HAMMER_SNAPS_PER_IOCTL)
36583f2a3aaSMatthew Dillon 					hammer_meta_flushdelete(fd, &dsnapshot);
36683f2a3aaSMatthew Dillon 			} else {
36783f2a3aaSMatthew Dillon 				ct = snap->ts / 1000000ULL;
36883f2a3aaSMatthew Dillon 				hammer_softprune_addelm(scan, snap->tid,
36983f2a3aaSMatthew Dillon 							ct, ct);
37083f2a3aaSMatthew Dillon 			}
37183f2a3aaSMatthew Dillon 			++snapshot.index;
37283f2a3aaSMatthew Dillon 		}
37383f2a3aaSMatthew Dillon 		if (snapshot.head.flags & HAMMER_IOC_SNAPSHOT_EOF)
37483f2a3aaSMatthew Dillon 			break;
37583f2a3aaSMatthew Dillon 		snapshot.index = 0;
37683f2a3aaSMatthew Dillon 	}
37783f2a3aaSMatthew Dillon 	if (delete_all)
37883f2a3aaSMatthew Dillon 		hammer_meta_flushdelete(fd, &dsnapshot);
37983f2a3aaSMatthew Dillon 	return(0);
38083f2a3aaSMatthew Dillon }
38183f2a3aaSMatthew Dillon 
38283f2a3aaSMatthew Dillon /*
38383f2a3aaSMatthew Dillon  * Flush any entries built up in the deletion snapshot ioctl structure.
38483f2a3aaSMatthew Dillon  * Used during a prune-everything.
38583f2a3aaSMatthew Dillon  */
386*005a4da7STomohiro Kusumi static
387*005a4da7STomohiro Kusumi void
hammer_meta_flushdelete(int fd,struct hammer_ioc_snapshot * dsnap)38883f2a3aaSMatthew Dillon hammer_meta_flushdelete(int fd, struct hammer_ioc_snapshot *dsnap)
38983f2a3aaSMatthew Dillon {
39083f2a3aaSMatthew Dillon 	while (dsnap->index < dsnap->count) {
39183f2a3aaSMatthew Dillon 		if (ioctl(fd, HAMMERIOC_DEL_SNAPSHOT, dsnap) < 0)
39283f2a3aaSMatthew Dillon 			break;
39383f2a3aaSMatthew Dillon 		if (dsnap->head.error == 0)
39483f2a3aaSMatthew Dillon 			break;
39583f2a3aaSMatthew Dillon 		++dsnap->index;
39683f2a3aaSMatthew Dillon 	}
39783f2a3aaSMatthew Dillon 	dsnap->index = 0;
39883f2a3aaSMatthew Dillon 	dsnap->count = 0;
39983f2a3aaSMatthew Dillon }
40083f2a3aaSMatthew Dillon 
40183f2a3aaSMatthew Dillon /*
402e8969ef0SMatthew Dillon  * Add the softlink to the appropriate softprune structure, creating a new
403e7f926a5SMatthew Dillon  * one if necessary.
404e8969ef0SMatthew Dillon  */
405b5aaba7fSMatthew Dillon static
406b5aaba7fSMatthew Dillon struct softprune *
hammer_softprune_addentry(struct softprune ** basep,struct hammer_ioc_prune * template,const char * dirpath,const char * denname __unused,struct stat * st,const char * linkbuf,const char * tidptr)407e8969ef0SMatthew Dillon hammer_softprune_addentry(struct softprune **basep,
408e8969ef0SMatthew Dillon 			 struct hammer_ioc_prune *template,
409e7f926a5SMatthew Dillon 			 const char *dirpath, const char *denname __unused,
410e7f926a5SMatthew Dillon 			 struct stat *st,
411b5aaba7fSMatthew Dillon 			 const char *linkbuf, const char *tidptr)
412e8969ef0SMatthew Dillon {
413e8969ef0SMatthew Dillon 	struct softprune *scan;
414e8969ef0SMatthew Dillon 	struct statfs fs;
415e8969ef0SMatthew Dillon 	char *fspath;
416e8969ef0SMatthew Dillon 
417e7f926a5SMatthew Dillon 	/*
418e7f926a5SMatthew Dillon 	 * Calculate filesystem path.
419e7f926a5SMatthew Dillon 	 */
420f254e677STomohiro Kusumi 	if (linkbuf[0] == '/') {
421e8969ef0SMatthew Dillon 		asprintf(&fspath, "%*.*s",
422e11cf02dSSascha Wildner 			 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
423e11cf02dSSascha Wildner 			 linkbuf);
424f254e677STomohiro Kusumi 	} else {
425e8969ef0SMatthew Dillon 		asprintf(&fspath, "%s/%*.*s", dirpath,
426e11cf02dSSascha Wildner 			 (int)(tidptr - linkbuf), (int)(tidptr - linkbuf),
427e11cf02dSSascha Wildner 			 linkbuf);
428f254e677STomohiro Kusumi 	}
429e8969ef0SMatthew Dillon 	if (statfs(fspath, &fs) < 0) {
430e8969ef0SMatthew Dillon 		free(fspath);
431b5aaba7fSMatthew Dillon 		return(NULL);
432e8969ef0SMatthew Dillon 	}
433e8969ef0SMatthew Dillon 
434e8969ef0SMatthew Dillon 	/*
435e8969ef0SMatthew Dillon 	 * Locate the filesystem in an existing softprune structure
436e8969ef0SMatthew Dillon 	 */
437e8969ef0SMatthew Dillon 	for (scan = *basep; scan; scan = scan->next) {
438e8969ef0SMatthew Dillon 		if (bcmp(&fs.f_fsid, &scan->fs.f_fsid, sizeof(fs.f_fsid)) != 0)
439e8969ef0SMatthew Dillon 			continue;
440e8969ef0SMatthew Dillon 		if (strcmp(fs.f_mntonname, scan->fs.f_mntonname) != 0)
441e8969ef0SMatthew Dillon 			continue;
442e8969ef0SMatthew Dillon 		break;
443e8969ef0SMatthew Dillon 	}
444e8969ef0SMatthew Dillon 
445e8969ef0SMatthew Dillon 	/*
446e8969ef0SMatthew Dillon 	 * Create a new softprune structure if necessasry
447e8969ef0SMatthew Dillon 	 */
448e8969ef0SMatthew Dillon 	if (scan == NULL) {
449e8969ef0SMatthew Dillon 		scan = malloc(sizeof(*scan));
450e8969ef0SMatthew Dillon 		bzero(scan, sizeof(*scan));
451e8969ef0SMatthew Dillon 
452e8969ef0SMatthew Dillon 		scan->fs = fs;
453e8969ef0SMatthew Dillon 		scan->filesystem = fspath;
454e8969ef0SMatthew Dillon 		scan->prune = *template;
455e8969ef0SMatthew Dillon 		scan->maxelms = 32;
45683f2a3aaSMatthew Dillon 		scan->prune.elms = malloc(sizeof(struct hammer_ioc_prune_elm) *
45783f2a3aaSMatthew Dillon 					  scan->maxelms);
458ab378f79SMatthew Dillon 		scan->next = *basep;
459e8969ef0SMatthew Dillon 		*basep = scan;
460e8969ef0SMatthew Dillon 	} else {
461e8969ef0SMatthew Dillon 		free(fspath);
462e8969ef0SMatthew Dillon 	}
46383f2a3aaSMatthew Dillon 	hammer_softprune_addelm(scan,
46483f2a3aaSMatthew Dillon 				(hammer_tid_t)strtoull(tidptr + 2, NULL, 0),
46583f2a3aaSMatthew Dillon 				(st ? st->st_ctime : 0),
46683f2a3aaSMatthew Dillon 				(st ? st->st_mtime : 0));
46783f2a3aaSMatthew Dillon 	return(scan);
46883f2a3aaSMatthew Dillon }
469e8969ef0SMatthew Dillon 
470e8969ef0SMatthew Dillon /*
471e8969ef0SMatthew Dillon  * Add the entry (unsorted).  Just set the beg_tid, we will sort
472e8969ef0SMatthew Dillon  * and set the remaining entries later.
473e8969ef0SMatthew Dillon  *
474e8969ef0SMatthew Dillon  * Always leave one entry free for our terminator.
475e8969ef0SMatthew Dillon  */
476*005a4da7STomohiro Kusumi static
477*005a4da7STomohiro Kusumi void
hammer_softprune_addelm(struct softprune * scan,hammer_tid_t tid,time_t ct,time_t mt)47883f2a3aaSMatthew Dillon hammer_softprune_addelm(struct softprune *scan, hammer_tid_t tid,
47983f2a3aaSMatthew Dillon 			time_t ct, time_t mt)
48083f2a3aaSMatthew Dillon {
48183f2a3aaSMatthew Dillon 	struct hammer_ioc_prune_elm *elm;
48283f2a3aaSMatthew Dillon 
483e8969ef0SMatthew Dillon 	if (scan->prune.nelms >= scan->maxelms - 1) {
484e8969ef0SMatthew Dillon 		scan->maxelms = (scan->maxelms * 3 / 2);
485e8969ef0SMatthew Dillon 		scan->prune.elms = realloc(scan->prune.elms,
486e8969ef0SMatthew Dillon 					   sizeof(*elm) * scan->maxelms);
487e8969ef0SMatthew Dillon 	}
488e7f926a5SMatthew Dillon 
489e7f926a5SMatthew Dillon 	/*
490e7f926a5SMatthew Dillon 	 * NOTE: Temporarily store the snapshot timestamp in mod_tid.
491e7f926a5SMatthew Dillon 	 *	 This will be cleaned up in the finalization phase.
492e7f926a5SMatthew Dillon 	 */
493e8969ef0SMatthew Dillon 	elm = &scan->prune.elms[scan->prune.nelms];
49483f2a3aaSMatthew Dillon 	elm->beg_tid = tid;
495e8969ef0SMatthew Dillon 	elm->end_tid = 0;
4960bd7a37cSMatthew Dillon 	elm->mod_tid = 0;
49783f2a3aaSMatthew Dillon 	if (ct < mt)
49883f2a3aaSMatthew Dillon 		elm->mod_tid = ct;
4990bd7a37cSMatthew Dillon 	else
50083f2a3aaSMatthew Dillon 		elm->mod_tid = mt;
501e8969ef0SMatthew Dillon 	++scan->prune.nelms;
502e8969ef0SMatthew Dillon }
503e8969ef0SMatthew Dillon 
504e8969ef0SMatthew Dillon /*
505e8969ef0SMatthew Dillon  * Finalize a softprune structure after scanning in its softlinks.
506e8969ef0SMatthew Dillon  * Sort the elements, remove duplicates, and then fill in end_tid and
507e8969ef0SMatthew Dillon  * mod_tid.
508e8969ef0SMatthew Dillon  *
509e8969ef0SMatthew Dillon  * The array must end up in descending order.
510e8969ef0SMatthew Dillon  */
511*005a4da7STomohiro Kusumi static
512*005a4da7STomohiro Kusumi int
hammer_softprune_qsort_cmp(const void * arg1,const void * arg2)513e8969ef0SMatthew Dillon hammer_softprune_qsort_cmp(const void *arg1, const void *arg2)
514e8969ef0SMatthew Dillon {
515e8969ef0SMatthew Dillon 	const struct hammer_ioc_prune_elm *elm1 = arg1;
516e8969ef0SMatthew Dillon 	const struct hammer_ioc_prune_elm *elm2 = arg2;
517e8969ef0SMatthew Dillon 
518e8969ef0SMatthew Dillon 	if (elm1->beg_tid < elm2->beg_tid)
519e8969ef0SMatthew Dillon 		return(1);
520e8969ef0SMatthew Dillon 	if (elm1->beg_tid > elm2->beg_tid)
521e8969ef0SMatthew Dillon 		return(-1);
522e8969ef0SMatthew Dillon 	return(0);
523e8969ef0SMatthew Dillon }
524e8969ef0SMatthew Dillon 
525*005a4da7STomohiro Kusumi static
526*005a4da7STomohiro Kusumi void
hammer_softprune_finalize(struct softprune * scan)527e8969ef0SMatthew Dillon hammer_softprune_finalize(struct softprune *scan)
528e8969ef0SMatthew Dillon {
529e8969ef0SMatthew Dillon 	struct hammer_ioc_prune_elm *elm;
530e7f926a5SMatthew Dillon 	time_t t;
531e7f926a5SMatthew Dillon 	long delta;
532e8969ef0SMatthew Dillon 	int i;
533e8969ef0SMatthew Dillon 
534e8969ef0SMatthew Dillon 	/*
535e8969ef0SMatthew Dillon 	 * Don't do anything if there are no elements.
536e8969ef0SMatthew Dillon 	 */
537e8969ef0SMatthew Dillon 	if (scan->prune.nelms == 0)
538e8969ef0SMatthew Dillon 		return;
539e8969ef0SMatthew Dillon 
540e8969ef0SMatthew Dillon 	/*
541e8969ef0SMatthew Dillon 	 * Sort the elements in descending order, remove duplicates, and
542e8969ef0SMatthew Dillon 	 * fill in any missing bits.
543e8969ef0SMatthew Dillon 	 */
544e8969ef0SMatthew Dillon 	qsort(scan->prune.elms, scan->prune.nelms, sizeof(*elm),
545e8969ef0SMatthew Dillon 	      hammer_softprune_qsort_cmp);
546e8969ef0SMatthew Dillon 
547e8969ef0SMatthew Dillon 	for (i = 0; i < scan->prune.nelms; ++i) {
548e8969ef0SMatthew Dillon 		elm = &scan->prune.elms[i];
549e8969ef0SMatthew Dillon 		if (i == 0) {
550e8969ef0SMatthew Dillon 			/*
551e8969ef0SMatthew Dillon 			 * First (highest TID) (also last if only one element)
552e8969ef0SMatthew Dillon 			 */
553e8969ef0SMatthew Dillon 			elm->end_tid = HAMMER_MAX_TID;
554e8969ef0SMatthew Dillon 		} else if (elm[0].beg_tid == elm[-1].beg_tid) {
555e8969ef0SMatthew Dillon 			/*
556e8969ef0SMatthew Dillon 			 * Remove duplicate
557e8969ef0SMatthew Dillon 			 */
558e8969ef0SMatthew Dillon 			--scan->prune.nelms;
559f254e677STomohiro Kusumi 			if (i != scan->prune.nelms) {
560e8969ef0SMatthew Dillon 				bcopy(elm + 1, elm,
561e8969ef0SMatthew Dillon 				      (scan->prune.nelms - i) * sizeof(*elm));
562f254e677STomohiro Kusumi 			}
563e8969ef0SMatthew Dillon 			--i;
564e8969ef0SMatthew Dillon 			continue;
565e8969ef0SMatthew Dillon 		} else {
566e8969ef0SMatthew Dillon 			/*
567e8969ef0SMatthew Dillon 			 * Middle or last.
568e8969ef0SMatthew Dillon 			 */
569e8969ef0SMatthew Dillon 			elm->end_tid = elm[-1].beg_tid;
570e8969ef0SMatthew Dillon 		}
571e7f926a5SMatthew Dillon 	}
572e7f926a5SMatthew Dillon 
573e7f926a5SMatthew Dillon 	/*
574e7f926a5SMatthew Dillon 	 * If a minimum retention time (in seconds) is configured for the
575e7f926a5SMatthew Dillon 	 * PFS, remove any snapshots from the pruning list that are within
576e7f926a5SMatthew Dillon 	 * the period.
577e7f926a5SMatthew Dillon 	 */
578e7f926a5SMatthew Dillon 	if (scan->prune_min) {
579e7f926a5SMatthew Dillon 		t = time(NULL);
580e7f926a5SMatthew Dillon 		for (i = scan->prune.nelms - 1; i >= 0; --i) {
581e7f926a5SMatthew Dillon 			elm = &scan->prune.elms[i];
582e7f926a5SMatthew Dillon 			if (elm->mod_tid == 0)
583e7f926a5SMatthew Dillon 				continue;
584e7f926a5SMatthew Dillon 			delta = (long)(t - (time_t)elm->mod_tid);
585e7f926a5SMatthew Dillon 			if (delta < scan->prune_min)
586e7f926a5SMatthew Dillon 				break;
587e7f926a5SMatthew Dillon 		}
588e7f926a5SMatthew Dillon 		++i;
589e7f926a5SMatthew Dillon 		if (i) {
590e7f926a5SMatthew Dillon 			printf("Prune %s: prune_min: Will not clean between "
591e7f926a5SMatthew Dillon 			       "the teeth of the first %d snapshots\n",
592e7f926a5SMatthew Dillon 			       scan->filesystem, i);
593e7f926a5SMatthew Dillon 			bcopy(&scan->prune.elms[i], &scan->prune.elms[0],
594e7f926a5SMatthew Dillon 			      (scan->prune.nelms - i) * sizeof(scan->prune.elms[0]));
595e7f926a5SMatthew Dillon 			scan->prune.elms[0].end_tid = HAMMER_MAX_TID;
596e7f926a5SMatthew Dillon 			scan->prune.nelms -= i;
597e7f926a5SMatthew Dillon 		}
598e7f926a5SMatthew Dillon 	}
599e7f926a5SMatthew Dillon 
600e7f926a5SMatthew Dillon 	/*
601e7f926a5SMatthew Dillon 	 * Remove the first entry.  This entry represents the prune from
602e7f926a5SMatthew Dillon 	 * the most recent snapshot to current.  We wish to retain the
603e7f926a5SMatthew Dillon 	 * fine-grained history for this region.
604e7f926a5SMatthew Dillon 	 */
605e7f926a5SMatthew Dillon 	if (scan->prune.nelms) {
606e7f926a5SMatthew Dillon 		bcopy(&scan->prune.elms[1], &scan->prune.elms[0],
607e7f926a5SMatthew Dillon 		      (scan->prune.nelms - 1) * sizeof(scan->prune.elms[0]));
608e7f926a5SMatthew Dillon 		--scan->prune.nelms;
609e8969ef0SMatthew Dillon 	}
610e8969ef0SMatthew Dillon 
611e8969ef0SMatthew Dillon 	/*
612e8969ef0SMatthew Dillon 	 * Add a final element to prune everything from transaction id
613e8969ef0SMatthew Dillon 	 * 0 to the lowest transaction id (aka last so far).
614e8969ef0SMatthew Dillon 	 */
615e7f926a5SMatthew Dillon 	if (scan->prune.nelms) {
616e8969ef0SMatthew Dillon 		assert(scan->prune.nelms < scan->maxelms);
617e7f926a5SMatthew Dillon 		elm = &scan->prune.elms[scan->prune.nelms];
618ab378f79SMatthew Dillon 		elm->beg_tid = 1;
619e8969ef0SMatthew Dillon 		elm->end_tid = elm[-1].beg_tid;
620e7f926a5SMatthew Dillon 		++scan->prune.nelms;
621e7f926a5SMatthew Dillon 	}
622e7f926a5SMatthew Dillon 
623e7f926a5SMatthew Dillon 	/*
624e7f926a5SMatthew Dillon 	 * Adjust mod_tid to what the ioctl() expects.
625e7f926a5SMatthew Dillon 	 */
626e7f926a5SMatthew Dillon 	for (i = 0; i < scan->prune.nelms; ++i) {
627e7f926a5SMatthew Dillon 		elm = &scan->prune.elms[i];
628e8969ef0SMatthew Dillon 		elm->mod_tid = elm->end_tid - elm->beg_tid;
629a276dc6bSMatthew Dillon 		printf("TID %016jx - %016jx\n",
630a276dc6bSMatthew Dillon 		       (uintmax_t)elm->beg_tid, (uintmax_t)elm->end_tid);
631e7f926a5SMatthew Dillon 	}
632e8969ef0SMatthew Dillon }
633e8969ef0SMatthew Dillon 
634e8969ef0SMatthew Dillon static
635e8969ef0SMatthew Dillon void
softprune_usage(int code)636e8969ef0SMatthew Dillon softprune_usage(int code)
637e8969ef0SMatthew Dillon {
638b5aaba7fSMatthew Dillon 	fprintf(stderr, "Badly formed prune command, use:\n");
63934bb69d8SThomas Nikolajsen 	fprintf(stderr, "hammer prune <softlink-dir>\n");
640b5aaba7fSMatthew Dillon 	fprintf(stderr, "hammer prune-everything <filesystem>\n");
641e8969ef0SMatthew Dillon 	exit(code);
642e8969ef0SMatthew Dillon }
643e8969ef0SMatthew Dillon 
644