xref: /dragonfly/sbin/hammer/hammer_util.h (revision dc861544)
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  * $DragonFly: src/sbin/hammer/hammer_util.h,v 1.12 2008/03/18 05:21:53 dillon Exp $
35  */
36 
37 #include <sys/types.h>
38 #include <sys/tree.h>
39 #include <sys/queue.h>
40 
41 #include <vfs/hammer/hammer_disk.h>
42 #include <uuid.h>
43 
44 /*
45  * Cache management - so the user code can keep its memory use under control
46  */
47 struct volume_info;
48 struct buffer_info;
49 
50 TAILQ_HEAD(volume_list, volume_info);
51 
52 struct cache_info {
53 	TAILQ_ENTRY(cache_info) entry;
54 	union {
55 		struct volume_info *volume;
56 		struct buffer_info *buffer;
57 	} u;
58 	enum cache_type { ISVOLUME, ISBUFFER } type;
59 	int refs;	/* structural references */
60 	int modified;	/* ondisk modified flag */
61 	int delete;	/* delete flag - delete on last ref */
62 };
63 
64 /*
65  * These structures are used by newfs_hammer to track the filesystem
66  * buffers it constructs while building the filesystem.  No attempt
67  * is made to try to make this efficient.
68  */
69 struct volume_info {
70 	struct cache_info	cache;
71 	TAILQ_ENTRY(volume_info) entry;
72 	int			vol_no;
73 	hammer_off_t		vol_alloc;	/* volume-relative offset */
74 	hammer_off_t		vol_free_off;	/* zone-2 offset */
75 	hammer_off_t		vol_free_end;	/* zone-2 offset */
76 
77 	char			*name;
78 	int			fd;
79 	off_t			size;
80 	const char		*type;
81 
82 	struct hammer_volume_ondisk *ondisk;
83 
84 	TAILQ_HEAD(, buffer_info) buffer_list;
85 };
86 
87 struct buffer_info {
88 	struct cache_info	cache;
89 	TAILQ_ENTRY(buffer_info) entry;
90 	hammer_off_t		buf_offset;	/* full hammer offset spec */
91 	int64_t			buf_disk_offset;/* relative to blkdev */
92 	struct volume_info	*volume;
93 	void			*ondisk;
94 };
95 
96 extern uuid_t Hammer_FSType;
97 extern uuid_t Hammer_FSId;
98 extern int64_t BootAreaSize;
99 extern int64_t MemAreaSize;
100 extern int NumVolumes;
101 extern int RootVolNo;
102 extern struct volume_list VolList;
103 
104 uint32_t crc32(const void *buf, size_t size);
105 
106 struct volume_info *setup_volume(int32_t vol_no, const char *filename,
107 				int isnew, int oflags);
108 struct volume_info *get_volume(int32_t vol_no);
109 struct buffer_info *get_buffer(hammer_off_t buf_offset, int isnew);
110 void *get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
111 				int isnew);
112 hammer_node_ondisk_t get_node(hammer_off_t node_offset,
113 				struct buffer_info **bufp);
114 
115 void rel_volume(struct volume_info *volume);
116 void rel_buffer(struct buffer_info *buffer);
117 
118 hammer_off_t blockmap_lookup(hammer_off_t bmap_off,
119 				struct hammer_blockmap_layer1 *layer1,
120 				struct hammer_blockmap_layer2 *layer2);
121 void format_blockmap(hammer_blockmap_t blockmap, hammer_off_t zone_off);
122 void format_undomap(hammer_volume_ondisk_t ondisk);
123 
124 void *alloc_btree_element(hammer_off_t *offp);
125 hammer_record_ondisk_t alloc_record_element(hammer_off_t *offp,
126 				int32_t data_len, void **datap);
127 int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
128 
129 
130 void format_freemap(struct volume_info *root_vol, hammer_blockmap_t blockmap);
131 int64_t initialize_freemap(struct volume_info *vol);
132 
133 void flush_all_volumes(void);
134 void flush_volume(struct volume_info *vol);
135 void flush_buffer(struct buffer_info *buf);
136 
137 void hammer_cache_add(struct cache_info *cache, enum cache_type type);
138 void hammer_cache_del(struct cache_info *cache);
139 void hammer_cache_flush(void);
140 
141 void panic(const char *ctl, ...);
142 
143