sbin/hammer: Add init_boot|mem_area_size()
[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 #include <sys/types.h>
36 #include <sys/tree.h>
37 #include <sys/queue.h>
38 #include <sys/mount.h>
39
40 #include <vfs/hammer/hammer_disk.h>
41 #include <vfs/hammer/hammer_ioctl.h>
42 #include <uuid.h>
43
44 /*
45  * pidfile management - common definitions so code is more robust
46  */
47
48 #define PIDFILE_BUFSIZE 64
49 static const char pidfile_loc[] = "/var/run";
50
51 /*
52  * Cache management - so the user code can keep its memory use under control
53  */
54 struct volume_info;
55 struct buffer_info;
56
57 TAILQ_HEAD(volume_list, volume_info);
58
59 struct cache_info {
60         TAILQ_ENTRY(cache_info) entry;
61         union {
62                 struct volume_info *volume;
63                 struct buffer_info *buffer;
64         } u;
65         enum cache_type { ISVOLUME, ISBUFFER } type;
66         int refs;       /* structural references */
67         int modified;   /* ondisk modified flag */
68         int delete;     /* delete flag - delete on last ref */
69 };
70
71 #define HAMMER_BUFLISTS         64
72 #define HAMMER_BUFLISTMASK      (HAMMER_BUFLISTS - 1)
73
74 /*
75  * These structures are used by newfs_hammer to track the filesystem
76  * buffers it constructs while building the filesystem.  No attempt
77  * is made to try to make this efficient.
78  */
79 struct volume_info {
80         struct cache_info       cache;
81         TAILQ_ENTRY(volume_info) entry;
82         int                     vol_no;
83         hammer_off_t            vol_alloc;      /* volume-relative offset */
84         hammer_off_t            vol_free_off;   /* zone-2 offset */
85         hammer_off_t            vol_free_end;   /* zone-2 offset */
86
87         char                    *name;
88         int                     fd;
89         off_t                   size;
90         off_t                   device_offset;
91         const char              *type;
92
93         struct hammer_volume_ondisk *ondisk;
94
95         TAILQ_HEAD(, buffer_info) buffer_lists[HAMMER_BUFLISTS];
96 };
97
98 struct buffer_info {
99         struct cache_info       cache;
100         TAILQ_ENTRY(buffer_info) entry;
101         hammer_off_t            buf_offset;     /* full hammer offset spec */
102         int64_t                 raw_offset;     /* physical offset */
103         int                     flags;          /* origination flags */
104         int                     use_count;      /* read count */
105         struct volume_info      *volume;
106         void                    *ondisk;
107 };
108
109 struct softprune {
110         struct softprune *next;
111         struct statfs fs;
112         char *filesystem;
113         struct hammer_ioc_prune prune;
114         int maxelms;
115         int prune_min;
116 };
117
118 /*
119  * Data structure for zone statistics.
120  */
121 struct zone_stat {
122         int                     zone;           /* zone index, not used */
123         hammer_off_t            blocks;         /* number of big-blocks */
124         hammer_off_t            items;          /* number of items */
125         hammer_off_t            used;           /* bytes used */
126 };
127
128 extern uuid_t Hammer_FSType;
129 extern uuid_t Hammer_FSId;
130 extern int64_t BootAreaSize;
131 extern int64_t MemAreaSize;
132 extern int64_t UndoBufferSize;
133 extern int DebugOpt;
134 extern const char *ScoreBoardFile;
135 extern const char *RestrictTarget;
136 extern int NumVolumes;
137 extern int RootVolNo;
138 extern struct volume_list VolList;
139 extern int UseReadBehind;
140 extern int UseReadAhead;
141 extern int AssertOnFailure;
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 *setup_volume(int32_t vol_no, const char *filename,
147                                 int isnew, int oflags);
148 struct volume_info *get_volume(int32_t vol_no);
149 struct buffer_info *get_buffer(hammer_off_t buf_offset, int isnew);
150 void *get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
151                                 int isnew);
152 hammer_node_ondisk_t get_node(hammer_off_t node_offset,
153                                 struct buffer_info **bufp);
154
155 void rel_volume(struct volume_info *volume);
156 void rel_buffer(struct buffer_info *buffer);
157
158 hammer_off_t blockmap_lookup(hammer_off_t bmap_off,
159                                 struct hammer_blockmap_layer1 *layer1,
160                                 struct hammer_blockmap_layer2 *layer2,
161                                 int *errorp);
162 void format_blockmap(hammer_blockmap_t blockmap, int zone, hammer_off_t offset);
163 void format_undomap(struct volume_info *root_vol);
164
165 void *alloc_btree_element(hammer_off_t *offp,
166                          struct buffer_info **data_bufferp);
167 void *alloc_meta_element(hammer_off_t *offp, int32_t data_len,
168                          struct buffer_info **data_bufferp);
169 void *alloc_data_element(hammer_off_t *offp, int32_t data_len,
170                          struct buffer_info **data_bufferp);
171
172 int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
173 void hammer_key_beg_init(hammer_base_elm_t base);
174 void hammer_key_end_init(hammer_base_elm_t base);
175 int hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf);
176
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, enum cache_type type);
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 void score_printf(size_t i, size_t w, const char *ctl, ...) __printflike(3, 4);
195
196 struct zone_stat *hammer_init_zone_stat(void);
197 struct zone_stat *hammer_init_zone_stat_bits(void);
198 void hammer_cleanup_zone_stat(struct zone_stat *stats);
199 void hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset,
200                         hammer_off_t bytes);
201 void hammer_add_zone_stat_layer2(struct zone_stat *stats,
202                         struct hammer_blockmap_layer2 *layer2);
203 void hammer_print_zone_stat(const struct zone_stat *stats);