sbin/hammer: Move hammer_btree_cmp() to cmd_show.c
[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 <vfs/hammer/hammer_crc.h>
59 #include <uuid.h>
60
61 struct cache_info {
62         TAILQ_ENTRY(cache_info) entry;
63         int refs;       /* structural references */
64         int modified;   /* ondisk modified flag */
65         int delete;     /* delete flag - delete on last ref */
66 };
67
68 #define HAMMER_BUFLISTS         64
69 #define HAMMER_BUFLISTMASK      (HAMMER_BUFLISTS - 1)
70
71 /*
72  * These structures are used by hammer(8) and newfs_hammer(8)
73  * to track the filesystem buffers.
74  *
75  * vol_free_off and vol_free_end are zone-2 offsets.
76  * These two are initialized only when newly creating a filesystem.
77  */
78 struct volume_info {
79         TAILQ_ENTRY(volume_info) entry;
80         const char              *name;
81         const char              *type;
82         int                     vol_no;
83         int                     rdonly;
84         int                     fd;
85         off_t                   size;
86         off_t                   device_offset;
87
88         hammer_off_t            vol_free_off;
89         hammer_off_t            vol_free_end;
90
91         hammer_volume_ondisk_t ondisk;
92
93         TAILQ_HEAD(, buffer_info) buffer_lists[HAMMER_BUFLISTS];
94 };
95
96 struct buffer_info {
97         struct cache_info       cache;          /* must be at offset 0 */
98         TAILQ_ENTRY(buffer_info) entry;
99         hammer_off_t            buf_offset;     /* full hammer offset spec */
100         int64_t                 raw_offset;     /* physical offset */
101         struct volume_info      *volume;
102         void                    *ondisk;
103 };
104
105 /*
106  * Data structure for zone statistics.
107  */
108 struct zone_stat {
109         int64_t                 blocks;         /* number of big-blocks */
110         int64_t                 items;          /* number of items */
111         int64_t                 used;           /* bytes used */
112 };
113
114 extern uuid_t Hammer_FSType;
115 extern uuid_t Hammer_FSId;
116 extern int UseReadBehind;
117 extern int UseReadAhead;
118 extern int DebugOpt;
119 extern const char *zone_labels[];
120
121 struct volume_info *init_volume(int32_t vol_no, const char *filename,
122                                 int oflags);
123 struct volume_info *load_volume(const char *filename, int oflags);
124 void assert_volume_offset(struct volume_info *vol);
125 struct volume_info *get_volume(int32_t vol_no);
126 struct volume_info *get_root_volume(void);
127 void *get_buffer_data(hammer_off_t buf_offset, struct buffer_info **bufferp,
128                                 int isnew);
129 void rel_buffer(struct buffer_info *buffer);
130
131 hammer_off_t alloc_bigblock(struct volume_info *volume, int zone);
132 void *alloc_blockmap(int zone, int bytes, hammer_off_t *result_offp,
133                struct buffer_info **bufferp);
134 hammer_off_t blockmap_lookup(hammer_off_t bmap_off,
135                                 hammer_blockmap_layer1_t layer1,
136                                 hammer_blockmap_layer2_t layer2,
137                                 int *errorp);
138 void format_undomap(struct volume_info *root_vol, int64_t *undo_buffer_size);
139
140 hammer_node_ondisk_t alloc_btree_node(hammer_off_t *offp,
141                          struct buffer_info **data_bufferp);
142 void *alloc_meta_element(hammer_off_t *offp, int32_t data_len,
143                          struct buffer_info **data_bufferp);
144 void *alloc_data_element(hammer_off_t *offp, int32_t data_len,
145                          struct buffer_info **data_bufferp);
146
147 void format_blockmap(struct volume_info *vol, int zone, hammer_off_t offset);
148 void format_freemap(struct volume_info *root_vol);
149 int64_t initialize_freemap(struct volume_info *vol);
150 int64_t count_freemap(struct volume_info *vol);
151 void print_blockmap(const struct volume_info *root_vol);
152
153 void flush_all_volumes(void);
154 void flush_volume(struct volume_info *vol);
155 void flush_buffer(struct buffer_info *buf);
156
157 int64_t init_boot_area_size(int64_t value, off_t avg_vol_size);
158 int64_t init_mem_area_size(int64_t value, off_t avg_vol_size);
159
160 int hammer_parse_cache_size(const char *arg);
161 void hammer_cache_add(struct cache_info *cache);
162 void hammer_cache_del(struct cache_info *cache);
163 void hammer_cache_used(struct cache_info *cache);
164 void hammer_cache_flush(void);
165
166 void hammer_key_beg_init(hammer_base_elm_t base);
167 void hammer_key_end_init(hammer_base_elm_t base);
168 int getyn(void);
169 const char *sizetostr(off_t size);
170 int hammer_fs_to_vol(const char *fs, struct hammer_ioc_volume_list *iocp);
171 int hammer_fs_to_rootvol(const char *fs, char *buf, int len);
172
173 struct zone_stat *hammer_init_zone_stat(void);
174 struct zone_stat *hammer_init_zone_stat_bits(void);
175 void hammer_cleanup_zone_stat(struct zone_stat *stats);
176 void hammer_add_zone_stat(struct zone_stat *stats, hammer_off_t offset,
177                         int bytes);
178 void hammer_add_zone_stat_layer2(struct zone_stat *stats,
179                         hammer_blockmap_layer2_t layer2);
180 void hammer_print_zone_stat(const struct zone_stat *stats);
181
182 #endif /* !HAMMER_UTIL_H_ */