xref: /dflybsd-src/sbin/hammer/hammer_util.h (revision a74dc2505e953f1ecc2785705ab82cdd8045b81b)
1ed3afccaSMatthew Dillon /*
2ed3afccaSMatthew Dillon  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3ed3afccaSMatthew Dillon  *
4ed3afccaSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5ed3afccaSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6ed3afccaSMatthew Dillon  *
7ed3afccaSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8ed3afccaSMatthew Dillon  * modification, are permitted provided that the following conditions
9ed3afccaSMatthew Dillon  * are met:
10ed3afccaSMatthew Dillon  *
11ed3afccaSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12ed3afccaSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13ed3afccaSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14ed3afccaSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15ed3afccaSMatthew Dillon  *    the documentation and/or other materials provided with the
16ed3afccaSMatthew Dillon  *    distribution.
17ed3afccaSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18ed3afccaSMatthew Dillon  *    contributors may be used to endorse or promote products derived
19ed3afccaSMatthew Dillon  *    from this software without specific, prior written permission.
20ed3afccaSMatthew Dillon  *
21ed3afccaSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22ed3afccaSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23ed3afccaSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24ed3afccaSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25ed3afccaSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26ed3afccaSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27ed3afccaSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28ed3afccaSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29ed3afccaSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30ed3afccaSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31ed3afccaSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ed3afccaSMatthew Dillon  * SUCH DAMAGE.
33ed3afccaSMatthew Dillon  */
34ed3afccaSMatthew Dillon 
35*a74dc250STomohiro Kusumi #ifndef HAMMER_UTIL_H_
36*a74dc250STomohiro Kusumi #define HAMMER_UTIL_H_
37*a74dc250STomohiro Kusumi 
38ed3afccaSMatthew Dillon #include <sys/types.h>
3961aeeb33SMatthew Dillon #include <sys/tree.h>
4061aeeb33SMatthew Dillon #include <sys/queue.h>
4192d85f08STomohiro Kusumi #include <sys/mount.h>
4261aeeb33SMatthew Dillon 
43ed3afccaSMatthew Dillon #include <vfs/hammer/hammer_disk.h>
4492d85f08STomohiro Kusumi #include <vfs/hammer/hammer_ioctl.h>
45ed3afccaSMatthew Dillon #include <uuid.h>
46ed3afccaSMatthew Dillon 
4761aeeb33SMatthew Dillon /*
48a360fddeSJohn Marino  * pidfile management - common definitions so code is more robust
49a360fddeSJohn Marino  */
50a360fddeSJohn Marino 
51a360fddeSJohn Marino #define PIDFILE_BUFSIZE	64
52a360fddeSJohn Marino static const char pidfile_loc[] = "/var/run";
53a360fddeSJohn Marino 
54a360fddeSJohn Marino /*
5561aeeb33SMatthew Dillon  * Cache management - so the user code can keep its memory use under control
5661aeeb33SMatthew Dillon  */
5761aeeb33SMatthew Dillon struct volume_info;
58ed3afccaSMatthew Dillon struct buffer_info;
59ed3afccaSMatthew Dillon 
6061aeeb33SMatthew Dillon TAILQ_HEAD(volume_list, volume_info);
6161aeeb33SMatthew Dillon 
6261aeeb33SMatthew Dillon struct cache_info {
6361aeeb33SMatthew Dillon 	TAILQ_ENTRY(cache_info) entry;
6461aeeb33SMatthew Dillon 	union {
6561aeeb33SMatthew Dillon 		struct volume_info *volume;
6661aeeb33SMatthew Dillon 		struct buffer_info *buffer;
6761aeeb33SMatthew Dillon 	} u;
6847197d71SMatthew Dillon 	enum cache_type { ISVOLUME, ISBUFFER } type;
6961aeeb33SMatthew Dillon 	int refs;	/* structural references */
7061aeeb33SMatthew Dillon 	int modified;	/* ondisk modified flag */
7161aeeb33SMatthew Dillon 	int delete;	/* delete flag - delete on last ref */
7261aeeb33SMatthew Dillon };
7361aeeb33SMatthew Dillon 
74ba7b52c9SMatthew Dillon #define HAMMER_BUFLISTS		64
75ba7b52c9SMatthew Dillon #define HAMMER_BUFLISTMASK	(HAMMER_BUFLISTS - 1)
76ba7b52c9SMatthew Dillon 
77ed3afccaSMatthew Dillon /*
78ed3afccaSMatthew Dillon  * These structures are used by newfs_hammer to track the filesystem
79ed3afccaSMatthew Dillon  * buffers it constructs while building the filesystem.  No attempt
80ed3afccaSMatthew Dillon  * is made to try to make this efficient.
81ed3afccaSMatthew Dillon  */
82ed3afccaSMatthew Dillon struct volume_info {
8361aeeb33SMatthew Dillon 	struct cache_info	cache;
8461aeeb33SMatthew Dillon 	TAILQ_ENTRY(volume_info) entry;
85ed3afccaSMatthew Dillon 	int			vol_no;
86c3be93f2SMatthew Dillon 	hammer_off_t		vol_alloc;	/* volume-relative offset */
87c3be93f2SMatthew Dillon 	hammer_off_t		vol_free_off;	/* zone-2 offset */
88c3be93f2SMatthew Dillon 	hammer_off_t		vol_free_end;	/* zone-2 offset */
89ed3afccaSMatthew Dillon 
9061aeeb33SMatthew Dillon 	char			*name;
91ed3afccaSMatthew Dillon 	int			fd;
92ed3afccaSMatthew Dillon 	off_t			size;
93e0fb398bSTim 	off_t			device_offset;
94ed3afccaSMatthew Dillon 	const char		*type;
95ed3afccaSMatthew Dillon 
96ed3afccaSMatthew Dillon 	struct hammer_volume_ondisk *ondisk;
97ed3afccaSMatthew Dillon 
98ba7b52c9SMatthew Dillon 	TAILQ_HEAD(, buffer_info) buffer_lists[HAMMER_BUFLISTS];
99ed3afccaSMatthew Dillon };
100ed3afccaSMatthew Dillon 
101ed3afccaSMatthew Dillon struct buffer_info {
10261aeeb33SMatthew Dillon 	struct cache_info	cache;
10361aeeb33SMatthew Dillon 	TAILQ_ENTRY(buffer_info) entry;
10447197d71SMatthew Dillon 	hammer_off_t		buf_offset;	/* full hammer offset spec */
105b46b99bfSMatthew Dillon 	int64_t			raw_offset;	/* physical offset */
106b46b99bfSMatthew Dillon 	int			flags;		/* origination flags */
107b46b99bfSMatthew Dillon 	int			use_count;	/* read count */
108ed3afccaSMatthew Dillon 	struct volume_info	*volume;
10947197d71SMatthew Dillon 	void			*ondisk;
110ed3afccaSMatthew Dillon };
111ed3afccaSMatthew Dillon 
11292d85f08STomohiro Kusumi struct softprune {
11392d85f08STomohiro Kusumi 	struct softprune *next;
11492d85f08STomohiro Kusumi 	struct statfs fs;
11592d85f08STomohiro Kusumi 	char *filesystem;
11692d85f08STomohiro Kusumi 	struct hammer_ioc_prune prune;
11792d85f08STomohiro Kusumi 	int maxelms;
11892d85f08STomohiro Kusumi 	int prune_min;
11992d85f08STomohiro Kusumi };
12092d85f08STomohiro Kusumi 
121020339d5STomohiro Kusumi /*
122020339d5STomohiro Kusumi  * Data structure for zone statistics.
123020339d5STomohiro Kusumi  */
124723806f4STomohiro Kusumi struct zone_stat {
125723806f4STomohiro Kusumi 	int			zone;		/* zone index, not used */
126723806f4STomohiro Kusumi 	hammer_off_t		blocks;		/* number of big-blocks */
127e9976f43STomohiro Kusumi 	hammer_off_t		items;		/* number of items */
128020339d5STomohiro Kusumi 	hammer_off_t		used;		/* bytes used */
129723806f4STomohiro Kusumi };
130723806f4STomohiro Kusumi 
131ed3afccaSMatthew Dillon extern uuid_t Hammer_FSType;
132ed3afccaSMatthew Dillon extern uuid_t Hammer_FSId;
13361aeeb33SMatthew Dillon extern int64_t BootAreaSize;
13461aeeb33SMatthew Dillon extern int64_t MemAreaSize;
13564c21cf3SMatthew Dillon extern int64_t UndoBufferSize;
136ba7b52c9SMatthew Dillon extern int DebugOpt;
137269cdd19SMatthew Dillon extern const char *ScoreBoardFile;
13869f5a58cSMatthew Dillon extern const char *RestrictTarget;
139ed3afccaSMatthew Dillon extern int NumVolumes;
140d38ab092SMatthew Dillon extern int RootVolNo;
14161aeeb33SMatthew Dillon extern struct volume_list VolList;
142b46b99bfSMatthew Dillon extern int UseReadBehind;
143b46b99bfSMatthew Dillon extern int UseReadAhead;
144b9107f58SMatthew Dillon extern int AssertOnFailure;
145ed3afccaSMatthew Dillon 
146ed3afccaSMatthew Dillon uint32_t crc32(const void *buf, size_t size);
14717dd83bcSMatthew Dillon uint32_t crc32_ext(const void *buf, size_t size, uint32_t ocrc);
148ed3afccaSMatthew Dillon 
14961aeeb33SMatthew Dillon struct volume_info *setup_volume(int32_t vol_no, const char *filename,
15061aeeb33SMatthew Dillon 				int isnew, int oflags);
151ff8644cbSTomohiro Kusumi void check_volume(struct volume_info *vol);
152ed3afccaSMatthew Dillon struct volume_info *get_volume(int32_t vol_no);
15347197d71SMatthew Dillon struct buffer_info *get_buffer(hammer_off_t buf_offset, int isnew);
15440043e7fSMatthew Dillon void *get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
15540043e7fSMatthew Dillon 				int isnew);
15647197d71SMatthew Dillon hammer_node_ondisk_t get_node(hammer_off_t node_offset,
157d38ab092SMatthew Dillon 				struct buffer_info **bufp);
15861aeeb33SMatthew Dillon 
15961aeeb33SMatthew Dillon void rel_volume(struct volume_info *volume);
16061aeeb33SMatthew Dillon void rel_buffer(struct buffer_info *buffer);
16161aeeb33SMatthew Dillon 
16286872a2aSTomohiro Kusumi hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
16386872a2aSTomohiro Kusumi void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
16486872a2aSTomohiro Kusumi 	       struct buffer_info **bufferp);
165f03c9cf4SMatthew Dillon hammer_off_t blockmap_lookup(hammer_off_t bmap_off,
166f03c9cf4SMatthew Dillon 				struct hammer_blockmap_layer1 *layer1,
1676ed4c886SMatthew Dillon 				struct hammer_blockmap_layer2 *layer2,
1686ed4c886SMatthew Dillon 				int *errorp);
1696fdd989aSTomohiro Kusumi void format_blockmap(hammer_blockmap_t blockmap, int zone, hammer_off_t offset);
1703eccc362STomohiro Kusumi void format_undomap(struct volume_info *root_vol);
1713f673d5cSMatthew Dillon 
1724a2cb8bbSTomohiro Kusumi void *alloc_btree_element(hammer_off_t *offp,
1734a2cb8bbSTomohiro Kusumi 			 struct buffer_info **data_bufferp);
174baa8ac59STomohiro Kusumi void *alloc_meta_element(hammer_off_t *offp, int32_t data_len,
175baa8ac59STomohiro Kusumi 			 struct buffer_info **data_bufferp);
17611ad5adeSMatthew Dillon void *alloc_data_element(hammer_off_t *offp, int32_t data_len,
17711ad5adeSMatthew Dillon 			 struct buffer_info **data_bufferp);
17811ad5adeSMatthew Dillon 
1795a19cfc8SMatthew Dillon int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
180a7fbbf91SMatthew Dillon void hammer_key_beg_init(hammer_base_elm_t base);
181a7fbbf91SMatthew Dillon void hammer_key_end_init(hammer_base_elm_t base);
182a7fbbf91SMatthew Dillon int hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf);
183c3be93f2SMatthew Dillon 
184a360b0f5STomohiro Kusumi void format_freemap(struct volume_info *root_vol);
185c3be93f2SMatthew Dillon int64_t initialize_freemap(struct volume_info *vol);
18692d0a1c8STomohiro Kusumi int64_t count_freemap(struct volume_info *vol);
187c3be93f2SMatthew Dillon 
188ed3afccaSMatthew Dillon void flush_all_volumes(void);
189ed3afccaSMatthew Dillon void flush_volume(struct volume_info *vol);
190ed3afccaSMatthew Dillon void flush_buffer(struct buffer_info *buf);
191ed3afccaSMatthew Dillon 
192f819215cSTomohiro Kusumi int64_t init_boot_area_size(int64_t value, off_t avg_vol_size);
193f819215cSTomohiro Kusumi int64_t init_mem_area_size(int64_t value, off_t avg_vol_size);
194f819215cSTomohiro Kusumi 
1950faa08a1SMatthew Dillon void hammer_cache_set(int bytes);
19661aeeb33SMatthew Dillon void hammer_cache_add(struct cache_info *cache, enum cache_type type);
19761aeeb33SMatthew Dillon void hammer_cache_del(struct cache_info *cache);
198b46b99bfSMatthew Dillon void hammer_cache_used(struct cache_info *cache);
19961aeeb33SMatthew Dillon void hammer_cache_flush(void);
20061aeeb33SMatthew Dillon 
201f7eac9d2SSascha Wildner void score_printf(size_t i, size_t w, const char *ctl, ...) __printflike(3, 4);
202020339d5STomohiro Kusumi 
203020339d5STomohiro Kusumi struct zone_stat *hammer_init_zone_stat(void);
2042550036fSTomohiro Kusumi struct zone_stat *hammer_init_zone_stat_bits(void);
205020339d5STomohiro Kusumi void hammer_cleanup_zone_stat(struct zone_stat *stats);
206e9976f43STomohiro Kusumi void hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset,
207e9976f43STomohiro Kusumi 			hammer_off_t bytes);
208020339d5STomohiro Kusumi void hammer_add_zone_stat_layer2(struct zone_stat *stats,
209020339d5STomohiro Kusumi 			struct hammer_blockmap_layer2 *layer2);
210020339d5STomohiro Kusumi void hammer_print_zone_stat(const struct zone_stat *stats);
211*a74dc250STomohiro Kusumi 
212*a74dc250STomohiro Kusumi #endif /* !HAMMER_UTIL_H_ */
213