xref: /dragonfly/sbin/hammer/cmd_reblock.c (revision 88cdee70)
13f673d5cSMatthew Dillon /*
23f673d5cSMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
33f673d5cSMatthew Dillon  *
43f673d5cSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
53f673d5cSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
63f673d5cSMatthew Dillon  *
73f673d5cSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
83f673d5cSMatthew Dillon  * modification, are permitted provided that the following conditions
93f673d5cSMatthew Dillon  * are met:
103f673d5cSMatthew Dillon  *
113f673d5cSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
123f673d5cSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
133f673d5cSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
143f673d5cSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
153f673d5cSMatthew Dillon  *    the documentation and/or other materials provided with the
163f673d5cSMatthew Dillon  *    distribution.
173f673d5cSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
183f673d5cSMatthew Dillon  *    contributors may be used to endorse or promote products derived
193f673d5cSMatthew Dillon  *    from this software without specific, prior written permission.
203f673d5cSMatthew Dillon  *
213f673d5cSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
223f673d5cSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
233f673d5cSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
243f673d5cSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
253f673d5cSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
263f673d5cSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
273f673d5cSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
283f673d5cSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
293f673d5cSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
303f673d5cSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
313f673d5cSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323f673d5cSMatthew Dillon  * SUCH DAMAGE.
333f673d5cSMatthew Dillon  *
3484082922SThomas Nikolajsen  * $DragonFly: src/sbin/hammer/cmd_reblock.c,v 1.11 2008/07/16 00:53:48 thomas Exp $
353f673d5cSMatthew Dillon  */
363f673d5cSMatthew Dillon 
373f673d5cSMatthew Dillon #include "hammer.h"
383f673d5cSMatthew Dillon 
393f673d5cSMatthew Dillon static void reblock_usage(int exit_code);
403f673d5cSMatthew Dillon 
413f673d5cSMatthew Dillon /*
42f6532f03SThomas Nikolajsen  * reblock <filesystem> [fill_percentage] (default 100%)
433f673d5cSMatthew Dillon  */
443f673d5cSMatthew Dillon void
hammer_cmd_reblock(char ** av,int ac,int flags)45ba7b52c9SMatthew Dillon hammer_cmd_reblock(char **av, int ac, int flags)
463f673d5cSMatthew Dillon {
473f673d5cSMatthew Dillon 	struct hammer_ioc_reblock reblock;
483f673d5cSMatthew Dillon 	const char *filesystem;
493f673d5cSMatthew Dillon 	int fd;
503f673d5cSMatthew Dillon 	int perc;
513f673d5cSMatthew Dillon 
52243ca327SMatthew Dillon 	if (TimeoutOpt > 0)
53243ca327SMatthew Dillon 		alarm(TimeoutOpt);
54243ca327SMatthew Dillon 
553f673d5cSMatthew Dillon 	bzero(&reblock, sizeof(reblock));
5658c17893SMatthew Dillon 
571d9f6aa1SMatthew Dillon 	reblock.key_beg.obj_id = HAMMER_MIN_OBJID;
581d9f6aa1SMatthew Dillon 	reblock.key_end.obj_id = HAMMER_MAX_OBJID;
59fca68e7bSTomohiro Kusumi 	hammer_get_cycle(&reblock.key_beg, NULL);
6058c17893SMatthew Dillon 
61ba7b52c9SMatthew Dillon 	reblock.head.flags = flags & HAMMER_IOC_DO_FLAGS;
625e1e1454STomohiro Kusumi 	reblock.allpfs = AllPFS;
637ef2d7b3STomohiro Kusumi 	reblock.vol_no = -1;	/* Don't select a volume */
643f673d5cSMatthew Dillon 
6558c17893SMatthew Dillon 	/*
6658c17893SMatthew Dillon 	 * Restrict the localization domain if asked to do inodes or data,
67fca68e7bSTomohiro Kusumi 	 * but not both.  Note that using INODE or MISC for localization
68fca68e7bSTomohiro Kusumi 	 * adds a limitation to reblock range of B-Tree node, if BTREE is
69fca68e7bSTomohiro Kusumi 	 * used with INODES or (DATA and/or DIRS) but not both.
7058c17893SMatthew Dillon 	 */
719e29c876SMatthew Dillon 	switch(flags & (HAMMER_IOC_DO_INODES|HAMMER_IOC_DO_DATA|HAMMER_IOC_DO_DIRS)) {
7258c17893SMatthew Dillon 	case HAMMER_IOC_DO_INODES:
731d9f6aa1SMatthew Dillon 		reblock.key_beg.localization = HAMMER_LOCALIZE_INODE;
741d9f6aa1SMatthew Dillon 		reblock.key_end.localization = HAMMER_LOCALIZE_INODE;
7558c17893SMatthew Dillon 		break;
7658c17893SMatthew Dillon 	case HAMMER_IOC_DO_DATA:
77fca68e7bSTomohiro Kusumi 	case HAMMER_IOC_DO_DIRS:
78fca68e7bSTomohiro Kusumi 	case HAMMER_IOC_DO_DATA | HAMMER_IOC_DO_DIRS:
791d9f6aa1SMatthew Dillon 		reblock.key_beg.localization = HAMMER_LOCALIZE_MISC;
801d9f6aa1SMatthew Dillon 		reblock.key_end.localization = HAMMER_LOCALIZE_MISC;
8158c17893SMatthew Dillon 		break;
82fca68e7bSTomohiro Kusumi 	default:
83fca68e7bSTomohiro Kusumi 		reblock.key_beg.localization = HAMMER_MIN_LOCALIZATION;
84fca68e7bSTomohiro Kusumi 		reblock.key_end.localization = HAMMER_MAX_LOCALIZATION;
85fca68e7bSTomohiro Kusumi 		break;
8658c17893SMatthew Dillon 	}
8758c17893SMatthew Dillon 
88*88cdee70STomohiro Kusumi 	if (ac == 0) {
893f673d5cSMatthew Dillon 		reblock_usage(1);
90*88cdee70STomohiro Kusumi 		/* not reached */
91*88cdee70STomohiro Kusumi 	}
923f673d5cSMatthew Dillon 	filesystem = av[0];
933f673d5cSMatthew Dillon 	if (ac == 1) {
949e29c876SMatthew Dillon 		perc = 100;
953f673d5cSMatthew Dillon 	} else {
963f673d5cSMatthew Dillon 		perc = strtol(av[1], NULL, 0);
97*88cdee70STomohiro Kusumi 		if (perc < 0 || perc > 100) {
983f673d5cSMatthew Dillon 			reblock_usage(1);
99*88cdee70STomohiro Kusumi 			/* not reached */
100*88cdee70STomohiro Kusumi 		}
1013f673d5cSMatthew Dillon 	}
102ba7b52c9SMatthew Dillon 	reblock.free_level = (int)((int64_t)perc *
103e04ee2deSTomohiro Kusumi 				   HAMMER_BIGBLOCK_SIZE / 100);
104e04ee2deSTomohiro Kusumi 	reblock.free_level = HAMMER_BIGBLOCK_SIZE - reblock.free_level;
105ba7b52c9SMatthew Dillon 	if (reblock.free_level < 0)
106ba7b52c9SMatthew Dillon 		reblock.free_level = 0;
107f02dcec0STomohiro Kusumi 	printf("reblock start %016jx:%04x\nfree level %d/%d\n",
108a276dc6bSMatthew Dillon 		(uintmax_t)reblock.key_beg.obj_id,
109b1c084cdSTomohiro Kusumi 		reblock.key_beg.localization,
110f02dcec0STomohiro Kusumi 		reblock.free_level,
111f02dcec0STomohiro Kusumi 		HAMMER_BIGBLOCK_SIZE);
1123f673d5cSMatthew Dillon 
1133f673d5cSMatthew Dillon 	fd = open(filesystem, O_RDONLY);
114052fd72bSTomohiro Kusumi 	if (fd < 0) {
1153f673d5cSMatthew Dillon 		err(1, "Unable to open %s", filesystem);
116052fd72bSTomohiro Kusumi 		/* not reached */
117052fd72bSTomohiro Kusumi 	}
118445faa69SMatthew Dillon 	RunningIoctl = 1;
1190006adaeSMatthew Dillon 	if (ioctl(fd, HAMMERIOC_REBLOCK, &reblock) < 0) {
120d7ae405cSMatthew Dillon 		printf("Reblock %s failed: %s\n", filesystem, strerror(errno));
121d7ae405cSMatthew Dillon 	} else if (reblock.head.flags & HAMMER_IOC_HEAD_INTR) {
122a276dc6bSMatthew Dillon 		printf("Reblock %s interrupted by timer at %016jx:%04x\n",
12358c17893SMatthew Dillon 			filesystem,
124a276dc6bSMatthew Dillon 			(uintmax_t)reblock.key_cur.obj_id,
1251d9f6aa1SMatthew Dillon 			reblock.key_cur.localization);
12652e2f1b5STomohiro Kusumi 		if (CyclePath)
127243ca327SMatthew Dillon 			hammer_set_cycle(&reblock.key_cur, 0);
1280006adaeSMatthew Dillon 	} else {
129d7ae405cSMatthew Dillon 		if (CyclePath)
130d7ae405cSMatthew Dillon 			hammer_reset_cycle();
1313f673d5cSMatthew Dillon 		printf("Reblock %s succeeded\n", filesystem);
1320006adaeSMatthew Dillon 	}
133445faa69SMatthew Dillon 	RunningIoctl = 0;
1343f673d5cSMatthew Dillon 	close(fd);
1353f673d5cSMatthew Dillon 	printf("Reblocked:\n"
13678c4be83STomohiro Kusumi 	       "    %jd/%jd B-Tree nodes\n"
13778c4be83STomohiro Kusumi 	       "    %jd/%jd B-Tree bytes\n"
138a276dc6bSMatthew Dillon 	       "    %jd/%jd data elements\n"
139a276dc6bSMatthew Dillon 	       "    %jd/%jd data bytes\n",
140a276dc6bSMatthew Dillon 	       (intmax_t)reblock.btree_moves, (intmax_t)reblock.btree_count,
1413ced8577STomohiro Kusumi 	       (intmax_t)(reblock.btree_moves *
1423ced8577STomohiro Kusumi 		       sizeof(struct hammer_node_ondisk)),
1433ced8577STomohiro Kusumi 	       (intmax_t)(reblock.btree_count *
1443ced8577STomohiro Kusumi 		       sizeof(struct hammer_node_ondisk)),
145a276dc6bSMatthew Dillon 	       (intmax_t)reblock.data_moves, (intmax_t)reblock.data_count,
146a276dc6bSMatthew Dillon 	       (intmax_t)reblock.data_byte_moves,
147a276dc6bSMatthew Dillon 	       (intmax_t)reblock.data_byte_count
1483f673d5cSMatthew Dillon 	);
1493f673d5cSMatthew Dillon }
1503f673d5cSMatthew Dillon 
1513f673d5cSMatthew Dillon static
1523f673d5cSMatthew Dillon void
reblock_usage(int exit_code)1533f673d5cSMatthew Dillon reblock_usage(int exit_code)
1543f673d5cSMatthew Dillon {
155f6532f03SThomas Nikolajsen 	fprintf(stderr, "hammer reblock <filesystem> [fill_percentage]\n");
156f6532f03SThomas Nikolajsen 	fprintf(stderr, "hammer reblock-btree <filesystem> [fill_percentage]\n");
157f6532f03SThomas Nikolajsen 	fprintf(stderr, "hammer reblock-inodes <filesystem> [fill_percentage]\n");
158f6532f03SThomas Nikolajsen 	fprintf(stderr, "hammer reblock-dirs <filesystem> [fill_percentage]\n");
159f6532f03SThomas Nikolajsen 	fprintf(stderr, "hammer reblock-data <filesystem> [fill_percentage]\n");
16084082922SThomas Nikolajsen 	fprintf(stderr, "By default 100%% is used.\n");
1613f673d5cSMatthew Dillon 	exit(exit_code);
1623f673d5cSMatthew Dillon }
1633f673d5cSMatthew Dillon 
164