xref: /dflybsd-src/sbin/hammer/hammer_util.h (revision ba7b52c996532c051e314ff0e4a8ab2864a740c2)
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  *
34*ba7b52c9SMatthew Dillon  * $DragonFly: src/sbin/hammer/hammer_util.h,v 1.14 2008/05/12 05:13:48 dillon Exp $
35ed3afccaSMatthew Dillon  */
36ed3afccaSMatthew Dillon 
37ed3afccaSMatthew Dillon #include <sys/types.h>
3861aeeb33SMatthew Dillon #include <sys/tree.h>
3961aeeb33SMatthew Dillon #include <sys/queue.h>
4061aeeb33SMatthew Dillon 
41ed3afccaSMatthew Dillon #include <vfs/hammer/hammer_disk.h>
42ed3afccaSMatthew Dillon #include <uuid.h>
43ed3afccaSMatthew Dillon 
4461aeeb33SMatthew Dillon /*
4561aeeb33SMatthew Dillon  * Cache management - so the user code can keep its memory use under control
4661aeeb33SMatthew Dillon  */
4761aeeb33SMatthew Dillon struct volume_info;
48ed3afccaSMatthew Dillon struct buffer_info;
49ed3afccaSMatthew Dillon 
5061aeeb33SMatthew Dillon TAILQ_HEAD(volume_list, volume_info);
5161aeeb33SMatthew Dillon 
5261aeeb33SMatthew Dillon struct cache_info {
5361aeeb33SMatthew Dillon 	TAILQ_ENTRY(cache_info) entry;
5461aeeb33SMatthew Dillon 	union {
5561aeeb33SMatthew Dillon 		struct volume_info *volume;
5661aeeb33SMatthew Dillon 		struct buffer_info *buffer;
5761aeeb33SMatthew Dillon 	} u;
5847197d71SMatthew Dillon 	enum cache_type { ISVOLUME, ISBUFFER } type;
5961aeeb33SMatthew Dillon 	int refs;	/* structural references */
6061aeeb33SMatthew Dillon 	int modified;	/* ondisk modified flag */
6161aeeb33SMatthew Dillon 	int delete;	/* delete flag - delete on last ref */
6261aeeb33SMatthew Dillon };
6361aeeb33SMatthew Dillon 
64*ba7b52c9SMatthew Dillon #define HAMMER_BUFLISTS		64
65*ba7b52c9SMatthew Dillon #define HAMMER_BUFLISTMASK	(HAMMER_BUFLISTS - 1)
66*ba7b52c9SMatthew Dillon 
67ed3afccaSMatthew Dillon /*
68ed3afccaSMatthew Dillon  * These structures are used by newfs_hammer to track the filesystem
69ed3afccaSMatthew Dillon  * buffers it constructs while building the filesystem.  No attempt
70ed3afccaSMatthew Dillon  * is made to try to make this efficient.
71ed3afccaSMatthew Dillon  */
72ed3afccaSMatthew Dillon struct volume_info {
7361aeeb33SMatthew Dillon 	struct cache_info	cache;
7461aeeb33SMatthew Dillon 	TAILQ_ENTRY(volume_info) entry;
75ed3afccaSMatthew Dillon 	int			vol_no;
76c3be93f2SMatthew Dillon 	hammer_off_t		vol_alloc;	/* volume-relative offset */
77c3be93f2SMatthew Dillon 	hammer_off_t		vol_free_off;	/* zone-2 offset */
78c3be93f2SMatthew Dillon 	hammer_off_t		vol_free_end;	/* zone-2 offset */
79ed3afccaSMatthew Dillon 
8061aeeb33SMatthew Dillon 	char			*name;
81ed3afccaSMatthew Dillon 	int			fd;
82ed3afccaSMatthew Dillon 	off_t			size;
83ed3afccaSMatthew Dillon 	const char		*type;
84ed3afccaSMatthew Dillon 
85ed3afccaSMatthew Dillon 	struct hammer_volume_ondisk *ondisk;
86ed3afccaSMatthew Dillon 
87*ba7b52c9SMatthew Dillon 	TAILQ_HEAD(, buffer_info) buffer_lists[HAMMER_BUFLISTS];
88ed3afccaSMatthew Dillon };
89ed3afccaSMatthew Dillon 
90ed3afccaSMatthew Dillon struct buffer_info {
9161aeeb33SMatthew Dillon 	struct cache_info	cache;
9261aeeb33SMatthew Dillon 	TAILQ_ENTRY(buffer_info) entry;
9347197d71SMatthew Dillon 	hammer_off_t		buf_offset;	/* full hammer offset spec */
9447197d71SMatthew Dillon 	int64_t			buf_disk_offset;/* relative to blkdev */
95ed3afccaSMatthew Dillon 	struct volume_info	*volume;
9647197d71SMatthew Dillon 	void			*ondisk;
97ed3afccaSMatthew Dillon };
98ed3afccaSMatthew Dillon 
99ed3afccaSMatthew Dillon extern uuid_t Hammer_FSType;
100ed3afccaSMatthew Dillon extern uuid_t Hammer_FSId;
10161aeeb33SMatthew Dillon extern int64_t BootAreaSize;
10261aeeb33SMatthew Dillon extern int64_t MemAreaSize;
10364c21cf3SMatthew Dillon extern int64_t UndoBufferSize;
104*ba7b52c9SMatthew Dillon extern int DebugOpt;
105ed3afccaSMatthew Dillon extern int NumVolumes;
106d38ab092SMatthew Dillon extern int RootVolNo;
10761aeeb33SMatthew Dillon extern struct volume_list VolList;
108ed3afccaSMatthew Dillon 
109ed3afccaSMatthew Dillon uint32_t crc32(const void *buf, size_t size);
110ed3afccaSMatthew Dillon 
11161aeeb33SMatthew Dillon struct volume_info *setup_volume(int32_t vol_no, const char *filename,
11261aeeb33SMatthew Dillon 				int isnew, int oflags);
113ed3afccaSMatthew Dillon struct volume_info *get_volume(int32_t vol_no);
11447197d71SMatthew Dillon struct buffer_info *get_buffer(hammer_off_t buf_offset, int isnew);
11540043e7fSMatthew Dillon void *get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
11640043e7fSMatthew Dillon 				int isnew);
11747197d71SMatthew Dillon hammer_node_ondisk_t get_node(hammer_off_t node_offset,
118d38ab092SMatthew Dillon 				struct buffer_info **bufp);
11961aeeb33SMatthew Dillon 
12061aeeb33SMatthew Dillon void rel_volume(struct volume_info *volume);
12161aeeb33SMatthew Dillon void rel_buffer(struct buffer_info *buffer);
12261aeeb33SMatthew Dillon 
123f03c9cf4SMatthew Dillon hammer_off_t blockmap_lookup(hammer_off_t bmap_off,
124f03c9cf4SMatthew Dillon 				struct hammer_blockmap_layer1 *layer1,
125f03c9cf4SMatthew Dillon 				struct hammer_blockmap_layer2 *layer2);
126c3be93f2SMatthew Dillon void format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_off);
1273f673d5cSMatthew Dillon void format_undomap(hammer_volume_ondisk_t ondisk);
1283f673d5cSMatthew Dillon 
12947197d71SMatthew Dillon void *alloc_btree_element(hammer_off_t *offp);
13047197d71SMatthew Dillon hammer_record_ondisk_t alloc_record_element(hammer_off_t *offp,
13147197d71SMatthew Dillon 				int32_t data_len, void **datap);
1325a19cfc8SMatthew Dillon int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
1335a19cfc8SMatthew Dillon 
134c3be93f2SMatthew Dillon 
135c3be93f2SMatthew Dillon void format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap);
136c3be93f2SMatthew Dillon int64_t initialize_freemap(struct volume_info *vol);
137c3be93f2SMatthew Dillon 
138ed3afccaSMatthew Dillon void flush_all_volumes(void);
139ed3afccaSMatthew Dillon void flush_volume(struct volume_info *vol);
140ed3afccaSMatthew Dillon void flush_buffer(struct buffer_info *buf);
141ed3afccaSMatthew Dillon 
14261aeeb33SMatthew Dillon void hammer_cache_add(struct cache_info *cache, enum cache_type type);
14361aeeb33SMatthew Dillon void hammer_cache_del(struct cache_info *cache);
14461aeeb33SMatthew Dillon void hammer_cache_flush(void);
14561aeeb33SMatthew Dillon 
14661aeeb33SMatthew Dillon void panic(const char *ctl, ...);
14761aeeb33SMatthew Dillon 
148