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