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