avoid use after free
[dragonfly.git] / sbin / hammer / hammer_util.h
blob0299fc6672251120686ef820303e4e55075e1fdf
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:
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.
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.
34 * $DragonFly: src/sbin/hammer/hammer_util.h,v 1.3 2007/11/20 07:16:27 dillon Exp $
37 #include <sys/types.h>
38 #include <sys/diskslice.h>
39 #include <sys/diskmbr.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <vfs/hammer/hammer_alist.h>
43 #include <vfs/hammer/hammer_disk.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdarg.h>
48 #include <stddef.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <uuid.h>
54 #include <assert.h>
55 #include <err.h>
57 struct supercl_info;
58 struct cluster_info;
59 struct buffer_info;
62 * These structures are used by newfs_hammer to track the filesystem
63 * buffers it constructs while building the filesystem. No attempt
64 * is made to try to make this efficient.
66 struct volume_info {
67 struct volume_info *next;
68 int vol_no;
69 int64_t vol_alloc;
71 const char *name;
72 int fd;
73 off_t size;
74 const char *type;
76 struct hammer_alist_live clu_alist; /* cluster allocator */
77 struct hammer_alist_live buf_alist; /* buffer head only */
78 struct hammer_volume_ondisk *ondisk;
80 struct supercl_info *supercl_base;
81 struct cluster_info *cluster_base;
84 struct supercl_info {
85 struct supercl_info *next;
86 int32_t scl_no;
87 int64_t scl_offset;
89 struct volume_info *volume;
91 struct hammer_alist_live clu_alist;
92 struct hammer_alist_live buf_alist;
93 struct hammer_supercl_ondisk *ondisk;
96 struct cluster_info {
97 struct cluster_info *next;
98 int32_t clu_no;
99 int64_t clu_offset;
101 struct supercl_info *supercl;
102 struct volume_info *volume;
104 struct hammer_alist_live alist_master;
105 struct hammer_alist_live alist_btree;
106 struct hammer_alist_live alist_record;
107 struct hammer_alist_live alist_mdata;
108 struct hammer_cluster_ondisk *ondisk;
110 struct buffer_info *buffer_base;
113 struct buffer_info {
114 struct buffer_info *next;
115 int32_t buf_no;
116 int64_t buf_offset;
118 struct cluster_info *cluster;
119 struct volume_info *volume;
121 struct hammer_alist_live alist;
122 hammer_fsbuf_ondisk_t ondisk;
125 extern struct hammer_alist_config Buf_alist_config;
126 extern struct hammer_alist_config Vol_normal_alist_config;
127 extern struct hammer_alist_config Vol_super_alist_config;
128 extern struct hammer_alist_config Supercl_alist_config;
129 extern struct hammer_alist_config Clu_master_alist_config;
130 extern struct hammer_alist_config Clu_slave_alist_config;
131 extern uuid_t Hammer_FSType;
132 extern uuid_t Hammer_FSId;
133 extern int64_t ClusterSize;
134 extern int UsingSuperClusters;
135 extern int NumVolumes;
136 extern struct volume_info *VolBase;
138 uint32_t crc32(const void *buf, size_t size);
140 struct volume_info *get_volume(int32_t vol_no);
141 struct supercl_info *get_supercl(struct volume_info *vol, int32_t scl_no);
142 struct cluster_info *get_cluster(struct volume_info *vol, int32_t clu_no);
143 struct buffer_info *get_buffer(struct cluster_info *cl, int32_t buf_no,
144 int64_t buf_type);
145 void *alloc_btree_element(struct cluster_info *cluster, int32_t *offp);
146 void *alloc_data_element(struct cluster_info *cluster,
147 int32_t bytes, int32_t *offp);
148 void *alloc_record_element(struct cluster_info *cluster, int32_t *offp);
150 void flush_all_volumes(void);
151 void flush_volume(struct volume_info *vol);
152 void flush_supercl(struct supercl_info *supercl);
153 void flush_cluster(struct cluster_info *cl);
154 void flush_buffer(struct buffer_info *buf);
156 void hammer_super_alist_template(struct hammer_alist_config *conf);
157 void hammer_buffer_alist_template(struct hammer_alist_config *conf);