HAMMER 15/many - user utility infrastructure, refactor alists, misc
[dragonfly.git] / sbin / hammer / buffer_alist.c
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  * $DragonFly: src/sbin/hammer/Attic/buffer_alist.c,v 1.3 2008/01/03 06:48:45 dillon Exp $
35  */
36 /*
37  * Implement the super-cluster A-list recursion for the cluster allocator.
38  *
39  * Volume A-list -> supercluster A-list -> cluster
40  */
41
42 #include <sys/types.h>
43 #include <assert.h>
44 #include "hammer_util.h"
45
46 /*
47  * Buffers are already initialized by alloc_new_buffer() so our init/destroy
48  * code shouldn't do anything.
49  */
50 static int
51 buffer_alist_init(void *info __unused, int32_t blk __unused,
52                   int32_t radix __unused, hammer_alloc_state_t state __unused)
53 {
54         return(0);
55 }
56
57 static int
58 buffer_alist_destroy(void *info __unused, int32_t blk __unused,
59                      int32_t radix __unused)
60 {
61         return(0);
62 }
63
64 static int
65 buffer_alist_alloc_fwd(void *info, int32_t blk, int32_t radix __unused,
66                       int32_t count, int32_t atblk, int32_t *fullp)
67 {
68         struct cluster_info *cluster = info;
69         struct buffer_info *buf;
70         int32_t buf_no;
71         int32_t r;
72
73         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
74         buf = get_buffer(cluster, buf_no, 0);
75         assert(buf->ondisk->head.buf_type != 0);
76
77         r = hammer_alist_alloc_fwd(&buf->alist, count, atblk - blk);
78         if (r != HAMMER_ALIST_BLOCK_NONE)
79                 r += blk;
80         *fullp = hammer_alist_isfull(&buf->alist);
81         rel_buffer(buf);
82         return(r);
83 }
84
85 static int
86 buffer_alist_alloc_rev(void *info, int32_t blk, int32_t radix __unused,
87                       int32_t count, int32_t atblk, int32_t *fullp)
88 {
89         struct cluster_info *cluster = info;
90         struct buffer_info *buf;
91         int32_t buf_no;
92         int32_t r;
93
94         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
95         buf = get_buffer(cluster, buf_no, 0);
96         assert(buf->ondisk->head.buf_type != 0);
97
98         r = hammer_alist_alloc_rev(&buf->alist, count, atblk - blk);
99         if (r != HAMMER_ALIST_BLOCK_NONE)
100                 r += blk;
101         *fullp = hammer_alist_isfull(&buf->alist);
102         rel_buffer(buf);
103         return(r);
104 }
105
106 static void
107 buffer_alist_free(void *info, int32_t blk, int32_t radix __unused,
108                  int32_t base_blk, int32_t count, int32_t *emptyp)
109 {
110         struct cluster_info *cluster = info;
111         struct buffer_info *buf;
112         int32_t buf_no;
113
114         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
115         buf = get_buffer(cluster, buf_no, 0);
116         assert(buf->ondisk->head.buf_type != 0);
117         hammer_alist_free(&buf->alist, base_blk, count);
118         *emptyp = hammer_alist_isempty(&buf->alist);
119         rel_buffer(buf);
120 }
121
122 static void
123 buffer_alist_print(void *info __unused, int32_t blk __unused,
124                    int32_t radix __unused, int tab __unused)
125 {
126 }
127
128 void
129 hammer_buffer_alist_template(hammer_alist_config_t config)
130 {
131         config->bl_radix_init = buffer_alist_init;
132         config->bl_radix_destroy = buffer_alist_destroy;
133         config->bl_radix_alloc_fwd = buffer_alist_alloc_fwd;
134         config->bl_radix_alloc_rev = buffer_alist_alloc_rev;
135         config->bl_radix_free = buffer_alist_free;
136         config->bl_radix_print = buffer_alist_print;
137 }
138