xref: /dragonfly/sbin/hammer/hammer_util.h (revision 6e5c5008)
1 /*
2  * Copyright (c) 2007 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 
35 #ifndef HAMMER_UTIL_H_
36 #define HAMMER_UTIL_H_
37 
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/queue.h>
42 #include <sys/mount.h>
43 #include <sys/wait.h>
44 #include <sys/ioctl.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <stddef.h>
50 #include <string.h>
51 #include <ctype.h>
52 #include <dirent.h>
53 #include <errno.h>
54 #include <err.h>
55 #include <fcntl.h>
56 #include <libgen.h>
57 #include <limits.h>
58 #include <signal.h>
59 #include <time.h>
60 #include <unistd.h>
61 #include <assert.h>
62 
63 #include <vfs/hammer/hammer_disk.h>
64 #include <vfs/hammer/hammer_ioctl.h>
65 #include <vfs/hammer/hammer_crc.h>
66 
67 #define HAMMER_BUFLISTS		64
68 #define HAMMER_BUFLISTMASK	(HAMMER_BUFLISTS - 1)
69 
70 /*
71  * These structures are used by hammer(8) and newfs_hammer(8)
72  * to track the filesystem buffers.
73  *
74  * vol_free_off and vol_free_end are zone-2 offsets.
75  * These two are initialized only when newly creating a filesystem.
76  */
77 typedef struct volume_info {
78 	TAILQ_ENTRY(volume_info) entry;
79 	const char		*name;
80 	const char		*type;
81 	int			vol_no;
82 	int			rdonly;
83 	int			fd;
84 	off_t			size;
85 	off_t			device_offset;
86 
87 	hammer_off_t		vol_free_off;
88 	hammer_off_t		vol_free_end;
89 
90 	hammer_volume_ondisk_t ondisk;
91 
92 	TAILQ_HEAD(, buffer_info) buffer_lists[HAMMER_BUFLISTS];
93 } *volume_info_t;
94 
95 typedef struct cache_info {
96 	TAILQ_ENTRY(cache_info) entry;
97 	int			refs;		/* structural references */
98 	int			modified;	/* ondisk modified flag */
99 	int			delete;		/* delete flag - delete on last ref */
100 } *cache_info_t;
101 
102 typedef struct buffer_info {
103 	struct cache_info	cache;		/* must be at offset 0 */
104 	TAILQ_ENTRY(buffer_info) entry;
105 	hammer_off_t		zone2_offset;	/* zone-2 offset */
106 	int64_t			raw_offset;	/* physical offset */
107 	volume_info_t		volume;
108 	void			*ondisk;
109 } *buffer_info_t;
110 
111 /*
112  * Data structure for zone statistics.
113  */
114 typedef struct zone_stat {
115 	int64_t			blocks;		/* number of big-blocks */
116 	int64_t			items;		/* number of items */
117 	int64_t			used;		/* bytes used */
118 } *zone_stat_t;
119 
120 extern hammer_uuid_t Hammer_FSType;
121 extern hammer_uuid_t Hammer_FSId;
122 extern int UseReadBehind;
123 extern int UseReadAhead;
124 extern int DebugOpt;
125 extern uint32_t HammerVersion;
126 extern const char *zone_labels[];
127 
128 volume_info_t init_volume(const char *filename, int oflags, int32_t vol_no);
129 volume_info_t load_volume(const char *filename, int oflags, int verify_volume);
130 int is_regfile(const volume_info_t volume);
131 void assert_volume_offset(const volume_info_t volume);
132 volume_info_t get_volume(int32_t vol_no);
133 volume_info_t get_root_volume(void);
134 void rel_buffer(buffer_info_t buffer);
135 void *get_buffer_data(hammer_off_t buf_offset, buffer_info_t *bufferp,
136 			int isnew);
137 hammer_node_ondisk_t alloc_btree_node(hammer_off_t *offp,
138 			buffer_info_t *data_bufferp);
139 void *alloc_meta_element(hammer_off_t *offp, int32_t data_len,
140 			buffer_info_t *data_bufferp);
141 void format_blockmap(volume_info_t root_vol, int zone, hammer_off_t offset);
142 void format_freemap(volume_info_t root_vol);
143 int64_t initialize_freemap(volume_info_t volume);
144 int64_t count_freemap(const volume_info_t volume);
145 void format_undomap(volume_info_t root_vol, int64_t *undo_buffer_size);
146 void print_blockmap(const volume_info_t volume);
147 void flush_all_volumes(void);
148 void flush_volume(volume_info_t volume);
149 void flush_buffer(buffer_info_t buffer);
150 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size);
151 int64_t init_memory_log_size(int64_t value, off_t avg_vol_size);
152 
153 hammer_off_t bootstrap_bigblock(volume_info_t volume);
154 hammer_off_t alloc_undo_bigblock(volume_info_t volume);
155 void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
156 			buffer_info_t *bufferp);
157 hammer_off_t blockmap_lookup(hammer_off_t bmap_off, int *errorp);
158 hammer_off_t blockmap_lookup_save(hammer_off_t bmap_off,
159 				hammer_blockmap_layer1_t layer1,
160 				hammer_blockmap_layer2_t layer2,
161 				int *errorp);
162 
163 int hammer_parse_cache_size(const char *arg);
164 void hammer_cache_add(cache_info_t cache);
165 void hammer_cache_del(cache_info_t cache);
166 void hammer_cache_used(cache_info_t cache);
167 void hammer_cache_flush(void);
168 
169 void hammer_key_beg_init(hammer_base_elm_t base);
170 void hammer_key_end_init(hammer_base_elm_t base);
171 int getyn(void);
172 const char *sizetostr(off_t size);
173 int hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *iocp);
174 int hammer_fs_to_rootvol(const char *fs, char *buf, int len);
175 zone_stat_t hammer_init_zone_stat(void);
176 zone_stat_t hammer_init_zone_stat_bits(void);
177 void hammer_cleanup_zone_stat(zone_stat_t stats);
178 void hammer_add_zone_stat(zone_stat_t stats, hammer_off_t offset, int bytes);
179 void hammer_add_zone_stat_layer2(zone_stat_t stats,
180 			hammer_blockmap_layer2_t layer2);
181 void hammer_print_zone_stat(const zone_stat_t stats);
182 
183 void hammer_uuid_create(hammer_uuid_t *uuid);
184 int hammer_uuid_from_string(const char *str, hammer_uuid_t *uuid);
185 int hammer_uuid_to_string(const hammer_uuid_t *uuid, char **str);
186 int hammer_uuid_name_lookup(hammer_uuid_t *uuid, const char *str);
187 int hammer_uuid_compare(const hammer_uuid_t *uuid1, const hammer_uuid_t *uuid2);
188 
189 #define hwarn(format, args...)	warn("WARNING: "format,## args)
190 #define hwarnx(format, args...)	warnx("WARNING: "format,## args)
191 
192 #endif /* !HAMMER_UTIL_H_ */
193