cb67306acb09821a75d374d5b7343a5affe21172
[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
35 #ifndef HAMMER_UTIL_H_
36 #define HAMMER_UTIL_H_
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/tree.h>
42 #include <sys/queue.h>
43 #include <sys/mount.h>
44
45 #include <assert.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <stddef.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55
56 #include <vfs/hammer/hammer_disk.h>
57 #include <vfs/hammer/hammer_ioctl.h>
58 #include <uuid.h>
59
60 /*
61  * pidfile management - common definitions so code is more robust
62  */
63
64 #define PIDFILE_BUFSIZE 64
65 static const char pidfile_loc[] = "/var/run";
66
67 /*
68  * Cache management - so the user code can keep its memory use under control
69  */
70 struct volume_info;
71 struct buffer_info;
72
73 TAILQ_HEAD(volume_list, volume_info);
74
75 struct cache_info {
76         TAILQ_ENTRY(cache_info) entry;
77         struct buffer_info *buffer;
78         int refs;       /* structural references */
79         int modified;   /* ondisk modified flag */
80         int delete;     /* delete flag - delete on last ref */
81 };
82
83 #define HAMMER_BUFLISTS         64
84 #define HAMMER_BUFLISTMASK      (HAMMER_BUFLISTS - 1)
85
86 /*
87  * These structures are used by newfs_hammer to track the filesystem
88  * buffers it constructs while building the filesystem.  No attempt
89  * is made to try to make this efficient.
90  */
91 struct volume_info {
92         TAILQ_ENTRY(volume_info) entry;
93         int                     vol_no;
94         int                     rdonly;
95
96         hammer_off_t            vol_free_off;   /* zone-2 offset */
97         hammer_off_t            vol_free_end;   /* zone-2 offset */
98
99         char                    *name;
100         int                     fd;
101         off_t                   size;
102         off_t                   device_offset;
103         const char              *type;
104
105         struct hammer_volume_ondisk *ondisk;
106
107         TAILQ_HEAD(, buffer_info) buffer_lists[HAMMER_BUFLISTS];
108 };
109
110 struct buffer_info {
111         struct cache_info       cache;
112         TAILQ_ENTRY(buffer_info) entry;
113         hammer_off_t            buf_offset;     /* full hammer offset spec */
114         int64_t                 raw_offset;     /* physical offset */
115         struct volume_info      *volume;
116         void                    *ondisk;
117 };
118
119 struct softprune {
120         struct softprune *next;
121         struct statfs fs;
122         char *filesystem;
123         struct hammer_ioc_prune prune;
124         int maxelms;
125         int prune_min;
126 };
127
128 /*
129  * Data structure for zone statistics.
130  */
131 struct zone_stat {
132         int                     zone;           /* zone index, not used */
133         hammer_off_t            blocks;         /* number of big-blocks */
134         hammer_off_t            items;          /* number of items */
135         hammer_off_t            used;           /* bytes used */
136 };
137
138 extern uuid_t Hammer_FSType;
139 extern uuid_t Hammer_FSId;
140 extern int DebugOpt;
141 extern struct volume_list VolList;
142
143 uint32_t crc32(const void *buf, size_t size);
144 uint32_t crc32_ext(const void *buf, size_t size, uint32_t ocrc);
145
146 struct volume_info *init_volume(int32_t vol_no, const char *filename,
147                                 int oflags);
148 struct volume_info *load_volume(const char *filename, int oflags);
149 void check_volume(struct volume_info *vol);
150 struct volume_info *get_volume(int32_t vol_no);
151 struct volume_info *get_root_volume(void);
152 struct buffer_info *get_buffer(hammer_off_t buf_offset, int isnew);
153 void *get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
154                                 int isnew);
155 hammer_node_ondisk_t get_node(hammer_off_t node_offset,
156                                 struct buffer_info **bufp);
157
158 void rel_buffer(struct buffer_info *buffer);
159
160 hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
161 void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
162                struct buffer_info **bufferp);
163 hammer_off_t blockmap_lookup(hammer_off_t bmap_off,
164                                 struct hammer_blockmap_layer1 *layer1,
165                                 struct hammer_blockmap_layer2 *layer2,
166                                 int *errorp);
167 void format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size);
168
169 void *alloc_btree_element(hammer_off_t *offp,
170                          struct buffer_info **data_bufferp);
171 void *alloc_meta_element(hammer_off_t *offp, int32_t data_len,
172                          struct buffer_info **data_bufferp);
173 void *alloc_data_element(hammer_off_t *offp, int32_t data_len,
174                          struct buffer_info **data_bufferp);
175
176 void format_blockmap(struct volume_info *vol, int zone, hammer_off_t offset);
177 void format_freemap(struct volume_info *root_vol);
178 int64_t initialize_freemap(struct volume_info *vol);
179 int64_t count_freemap(struct volume_info *vol);
180
181 void flush_all_volumes(void);
182 void flush_volume(struct volume_info *vol);
183 void flush_buffer(struct buffer_info *buf);
184
185 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size);
186 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size);
187
188 void hammer_cache_set(int bytes);
189 void hammer_cache_add(struct cache_info *cache);
190 void hammer_cache_del(struct cache_info *cache);
191 void hammer_cache_used(struct cache_info *cache);
192 void hammer_cache_flush(void);
193
194 int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
195 void hammer_key_beg_init(hammer_base_elm_t base);
196 void hammer_key_end_init(hammer_base_elm_t base);
197 int hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf);
198 int getyn(void);
199 const char *sizetostr(off_t size);
200 int hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *iocp);
201 int hammer_fs_to_rootvol(const char *fs, char *buf, int len);
202
203 struct zone_stat *hammer_init_zone_stat(void);
204 struct zone_stat *hammer_init_zone_stat_bits(void);
205 void hammer_cleanup_zone_stat(struct zone_stat *stats);
206 void hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset,
207                         hammer_off_t bytes);
208 void hammer_add_zone_stat_layer2(struct zone_stat *stats,
209                         struct hammer_blockmap_layer2 *layer2);
210 void hammer_print_zone_stat(const struct zone_stat *stats);
211
212 #endif /* !HAMMER_UTIL_H_ */