Merge from vendor branch FILE:
[dragonfly.git] / sbin / hammer / hammer_util.h
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.5 2008/01/17 04:59:48 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_alist.h>
42 #include <vfs/hammer/hammer_disk.h>
43 #include <uuid.h>
44
45 /*
46  * Cache management - so the user code can keep its memory use under control
47  */
48 struct volume_info;
49 struct supercl_info;
50 struct cluster_info;
51 struct buffer_info;
52
53 TAILQ_HEAD(volume_list, volume_info);
54
55 struct cache_info {
56         TAILQ_ENTRY(cache_info) entry;
57         union {
58                 struct volume_info *volume;
59                 struct supercl_info *supercl;
60                 struct cluster_info *cluster;
61                 struct buffer_info *buffer;
62         } u;
63         enum cache_type { ISVOLUME, ISSUPERCL, ISCLUSTER, ISBUFFER } type;
64         int refs;       /* structural references */
65         int modified;   /* ondisk modified flag */
66         int delete;     /* delete flag - delete on last ref */
67 };
68
69 /*
70  * These structures are used by newfs_hammer to track the filesystem
71  * buffers it constructs while building the filesystem.  No attempt
72  * is made to try to make this efficient.
73  */
74 struct volume_info {
75         struct cache_info       cache;
76         TAILQ_ENTRY(volume_info) entry;
77         int                     vol_no;
78         int64_t                 vol_alloc;
79
80         char                    *name;
81         int                     fd;
82         off_t                   size;
83         const char              *type;
84         int                     using_supercl;
85
86         struct hammer_alist_live clu_alist;     /* cluster allocator */
87         struct hammer_alist_live buf_alist;     /* buffer head only */
88         struct hammer_volume_ondisk *ondisk;
89
90         TAILQ_HEAD(, cluster_info) cluster_list;
91         TAILQ_HEAD(, supercl_info) supercl_list;
92 };
93
94 struct supercl_info {
95         struct cache_info       cache;
96         TAILQ_ENTRY(supercl_info) entry;
97         int32_t                 scl_no;
98         int64_t                 scl_offset;
99
100         struct volume_info      *volume;
101
102         struct hammer_alist_live clu_alist;
103         struct hammer_alist_live buf_alist;
104         struct hammer_supercl_ondisk *ondisk;
105 };
106
107 struct cluster_info {
108         struct cache_info       cache;
109         TAILQ_ENTRY(cluster_info) entry;
110         int32_t                 clu_no;
111         int64_t                 clu_offset;
112
113         struct supercl_info     *supercl;
114         struct volume_info      *volume;
115
116         struct hammer_alist_live alist_master;
117         struct hammer_alist_live alist_btree;
118         struct hammer_alist_live alist_record;
119         struct hammer_alist_live alist_mdata;
120         struct hammer_cluster_ondisk *ondisk;
121
122         TAILQ_HEAD(, buffer_info) buffer_list;
123 };
124
125 struct buffer_info {
126         struct cache_info       cache;
127         TAILQ_ENTRY(buffer_info) entry;
128         int32_t                 buf_no;
129         int64_t                 buf_offset;
130
131         struct cluster_info     *cluster;
132         struct volume_info      *volume;
133
134         struct hammer_alist_live alist;
135         hammer_fsbuf_ondisk_t   ondisk;
136 };
137
138 extern struct hammer_alist_config Buf_alist_config;
139 extern struct hammer_alist_config Vol_normal_alist_config;
140 extern struct hammer_alist_config Vol_super_alist_config;
141 extern struct hammer_alist_config Supercl_alist_config;
142 extern struct hammer_alist_config Clu_master_alist_config;
143 extern struct hammer_alist_config Clu_slave_alist_config;
144 extern uuid_t Hammer_FSType;
145 extern uuid_t Hammer_FSId;
146 extern int64_t BootAreaSize;
147 extern int64_t MemAreaSize;
148 extern int UsingSuperClusters;
149 extern int NumVolumes;
150 extern int RootVolNo;
151 extern struct volume_list VolList;
152
153 uint32_t crc32(const void *buf, size_t size);
154
155 struct volume_info *setup_volume(int32_t vol_no, const char *filename,
156                                 int isnew, int oflags);
157 struct volume_info *get_volume(int32_t vol_no);
158 struct supercl_info *get_supercl(struct volume_info *vol, int32_t scl_no,
159                                 hammer_alloc_state_t isnew);
160 struct cluster_info *get_cluster(struct volume_info *vol, int32_t clu_no,
161                                 hammer_alloc_state_t isnew);
162 struct buffer_info *get_buffer(struct cluster_info *cl, int32_t buf_no,
163                                 int64_t buf_type);
164 hammer_node_ondisk_t get_node(struct cluster_info *cl, int32_t offset,
165                                 struct buffer_info **bufp);
166
167 void init_alist_templates(void);
168 void rel_volume(struct volume_info *volume);
169 void rel_supercl(struct supercl_info *supercl);
170 void rel_cluster(struct cluster_info *cluster);
171 void rel_buffer(struct buffer_info *buffer);
172
173 void *alloc_btree_element(struct cluster_info *cluster, int32_t *offp);
174 void *alloc_data_element(struct cluster_info *cluster,
175                                 int32_t bytes, int32_t *offp);
176 void *alloc_record_element(struct cluster_info *cluster, int32_t *offp);
177
178 void flush_all_volumes(void);
179 void flush_volume(struct volume_info *vol);
180 void flush_supercl(struct supercl_info *supercl);
181 void flush_cluster(struct cluster_info *cl);
182 void flush_buffer(struct buffer_info *buf);
183
184 void hammer_super_alist_template(struct hammer_alist_config *conf); 
185 void hammer_buffer_alist_template(struct hammer_alist_config *conf); 
186
187 void hammer_cache_add(struct cache_info *cache, enum cache_type type);
188 void hammer_cache_del(struct cache_info *cache);
189 void hammer_cache_flush(void);
190
191 void panic(const char *ctl, ...);
192