Merge from vendor branch LESS:
[dragonfly.git] / sbin / newfs_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/newfs_hammer/Attic/buffer_alist.c,v 1.2 2007/11/01 22:26:37 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 "newfs_hammer.h"
43
44 static int
45 buffer_alist_init(void *info, int32_t blk, int32_t radix)
46 {
47         struct cluster_info *cluster = info;
48         struct buffer_info *buf;
49         int32_t buf_no;
50
51         /*
52          * Calculate the buffer number, initialize based on the buffer type.
53          * The buffer has already been allocated so assert that it has been
54          * initialized.
55          */
56         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
57         buf = get_buffer(cluster, buf_no, 0);
58         assert(buf->ondisk->head.buf_type != 0);
59
60         return(0);
61 }
62
63 static int
64 buffer_alist_destroy(void *info, int32_t blk, int32_t radix)
65 {
66         return(0);
67 }
68
69 static int
70 buffer_alist_alloc_fwd(void *info, int32_t blk, int32_t radix,
71                       int32_t count, int32_t atblk, int32_t *fullp)
72 {
73         struct cluster_info *cluster = info;
74         struct buffer_info *buf;
75         int32_t buf_no;
76         int32_t r;
77
78         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
79         buf = get_buffer(cluster, buf_no, 0);
80         assert(buf->ondisk->head.buf_type != 0);
81
82         r = hammer_alist_alloc_fwd(&buf->alist, count, atblk - blk);
83         if (r != HAMMER_ALIST_BLOCK_NONE)
84                 r += blk;
85         *fullp = hammer_alist_isfull(&buf->alist);
86         return(r);
87 }
88
89 static int
90 buffer_alist_alloc_rev(void *info, int32_t blk, int32_t radix,
91                       int32_t count, int32_t atblk, int32_t *fullp)
92 {
93         struct cluster_info *cluster = info;
94         struct buffer_info *buf;
95         int32_t buf_no;
96         int32_t r;
97
98         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
99         buf = get_buffer(cluster, buf_no, 0);
100         assert(buf->ondisk->head.buf_type != 0);
101
102         r = hammer_alist_alloc_rev(&buf->alist, count, atblk - blk);
103         if (r != HAMMER_ALIST_BLOCK_NONE)
104                 r += blk;
105         *fullp = hammer_alist_isfull(&buf->alist);
106         return(r);
107 }
108
109 static void
110 buffer_alist_free(void *info, int32_t blk, int32_t radix,
111                  int32_t base_blk, int32_t count, int32_t *emptyp)
112 {
113         struct cluster_info *cluster = info;
114         struct buffer_info *buf;
115         int32_t buf_no;
116
117         buf_no = blk / HAMMER_FSBUF_MAXBLKS;
118         buf = get_buffer(cluster, buf_no, 0);
119         assert(buf->ondisk->head.buf_type != 0);
120         hammer_alist_free(&buf->alist, base_blk, count);
121         *emptyp = hammer_alist_isempty(&buf->alist);
122 }
123
124 static void
125 buffer_alist_print(void *info, int32_t blk, int32_t radix, int tab)
126 {
127 }
128
129 void
130 hammer_buffer_alist_template(hammer_alist_config_t config)
131 {
132         config->bl_radix_init = buffer_alist_init;
133         config->bl_radix_destroy = buffer_alist_destroy;
134         config->bl_radix_alloc_fwd = buffer_alist_alloc_fwd;
135         config->bl_radix_alloc_rev = buffer_alist_alloc_rev;
136         config->bl_radix_free = buffer_alist_free;
137         config->bl_radix_print = buffer_alist_print;
138 }
139