efa964ab8d0deed0ad5edabf4da8a418454e2a8a
[dragonfly.git] / sbin / hammer / super_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/super_alist.c,v 1.4 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 "hammer_util.h"
44
45 static int
46 super_alist_init(void *info, int32_t blk, int32_t radix __unused,
47                  hammer_alloc_state_t state)
48 {
49         struct volume_info *vol = info;
50         struct supercl_info *supercl;
51         int32_t sclno;
52
53         /*
54          * Calculate the super-cluster number containing the cluster (blk)
55          * and obtain the super-cluster buffer.
56          */
57         sclno = blk / HAMMER_SCL_MAXCLUSTERS;
58         supercl = get_supercl(vol, sclno, state);
59         rel_supercl(supercl);
60         return(0);
61 }
62
63 static int
64 super_alist_destroy(void *info __unused, int32_t blk __unused,
65                     int32_t radix __unused)
66 {
67         return(0);
68 }
69
70 static int
71 super_alist_alloc_fwd(void *info, int32_t blk, int32_t radix __unused,
72                       int32_t count, int32_t atblk, int32_t *fullp)
73 {
74         struct volume_info *vol = info;
75         struct supercl_info *supercl;
76         int32_t sclno;
77         int32_t r;
78
79         sclno = blk / HAMMER_SCL_MAXCLUSTERS;
80         supercl = get_supercl(vol, sclno, 0);
81         r = hammer_alist_alloc_fwd(&supercl->clu_alist, count, atblk - blk);
82         if (r != HAMMER_ALIST_BLOCK_NONE)
83                 r += blk;
84         *fullp = hammer_alist_isfull(&supercl->clu_alist);
85         rel_supercl(supercl);
86         return(r);
87 }
88
89 static int
90 super_alist_alloc_rev(void *info, int32_t blk, int32_t radix __unused,
91                       int32_t count, int32_t atblk, int32_t *fullp)
92 {
93         struct volume_info *vol = info;
94         struct supercl_info *supercl;
95         int32_t sclno;
96         int32_t r;
97
98         sclno = blk / HAMMER_SCL_MAXCLUSTERS;
99         supercl = get_supercl(vol, sclno, 0);
100         r = hammer_alist_alloc_rev(&supercl->clu_alist, count, atblk - blk);
101         if (r != HAMMER_ALIST_BLOCK_NONE)
102                 r += blk;
103         *fullp = hammer_alist_isfull(&supercl->clu_alist);
104         rel_supercl(supercl);
105         return(r);
106 }
107
108 static void
109 super_alist_free(void *info, int32_t blk, int32_t radix __unused,
110                  int32_t base_blk, int32_t count, int32_t *emptyp)
111 {
112         struct volume_info *vol = info;
113         struct supercl_info *supercl;
114         int32_t sclno;
115
116         sclno = blk / HAMMER_SCL_MAXCLUSTERS;
117         supercl = get_supercl(vol, sclno, 0);
118
119         hammer_alist_free(&supercl->clu_alist, base_blk, count);
120         *emptyp = hammer_alist_isempty(&supercl->clu_alist);
121         rel_supercl(supercl);
122 }
123
124 static void
125 super_alist_print(void *info __unused, int32_t blk __unused,
126                   int32_t radix __unused, int tab __unused)
127 {
128 }
129
130 void
131 hammer_super_alist_template(hammer_alist_config_t config)
132 {
133         config->bl_radix_init = super_alist_init;
134         config->bl_radix_destroy = super_alist_destroy;
135         config->bl_radix_alloc_fwd = super_alist_alloc_fwd;
136         config->bl_radix_alloc_rev = super_alist_alloc_rev;
137         config->bl_radix_free = super_alist_free;
138         config->bl_radix_print = super_alist_print;
139 }
140