99a3b3e294b4e5ec56e4596919c1a6c56fafd9d7
[dragonfly.git] / sys / vfs / hammer / hammer_freemap.c
1 /*
2  * Copyright (c) 2008 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/sys/vfs/hammer/hammer_freemap.c,v 1.2 2008/02/20 00:55:51 dillon Exp $
35  */
36
37 /*
38  * HAMMER freemap - bigblock allocator.  The freemap is a 2-layer blockmap
39  * with one layer2 entry for each big-block in the filesystem.  Big blocks
40  * are 8MB blocks.
41  *
42  * Our allocator is fairly straightforward, we just iterate through available
43  * blocks looking for a free one.  We shortcut the iteration based on
44  * layer1 availability.
45  */
46
47 #include "hammer.h"
48
49 hammer_off_t
50 hammer_freemap_alloc(hammer_mount_t hmp, hammer_off_t owner, int *errorp)
51 {
52         hammer_volume_t root_volume;
53         hammer_volume_ondisk_t ondisk;
54         hammer_off_t layer1_offset;
55         hammer_off_t layer2_offset;
56         hammer_off_t result_offset;
57         hammer_blockmap_t blockmap;
58         hammer_buffer_t buffer1 = NULL;
59         hammer_buffer_t buffer2 = NULL;
60         struct hammer_blockmap_layer1 *layer1;
61         struct hammer_blockmap_layer2 *layer2;
62         int vol_no;
63         int loops = 0;
64
65         root_volume = hammer_get_root_volume(hmp, errorp);
66         if (*errorp)
67                 return(0);
68         ondisk = root_volume->ondisk;
69
70         blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
71         result_offset = blockmap->next_offset;
72         vol_no = HAMMER_VOL_DECODE(result_offset);
73         for (;;) { 
74                 layer1_offset = blockmap->phys_offset +
75                                 HAMMER_BLOCKMAP_LAYER1_OFFSET(result_offset);
76
77                 layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
78                 if (layer1->phys_offset == HAMMER_BLOCKMAP_UNAVAIL) {
79                         /*
80                          * End-of-volume, try next volume.
81                          */
82 new_volume:
83                         ++vol_no;
84                         if (vol_no >= hmp->nvolumes)
85                                 vol_no = 0;
86                         result_offset = HAMMER_ENCODE_RAW_BUFFER(vol_no, 0);
87                         if (++loops == 2) {
88                                 *errorp = ENOSPC;
89                                 result_offset = 0;
90                                 goto done;
91                         }
92                 } else if (layer1->blocks_free == 0) {
93                         /*
94                          * layer2 has no free blocks, skip to the next layer.
95                          */
96                         result_offset = (result_offset + HAMMER_BLOCKMAP_LAYER2_MASK) & ~HAMMER_BLOCKMAP_LAYER2_MASK;
97                         if (HAMMER_VOL_DECODE(result_offset) != vol_no)
98                                 goto new_volume;
99                 } else {
100                         layer2_offset = layer1->phys_offset +
101                                 HAMMER_BLOCKMAP_LAYER2_OFFSET(result_offset);
102                         layer2 = hammer_bread(hmp, layer2_offset, errorp,
103                                               &buffer2);
104                         if (layer2->u.owner == HAMMER_BLOCKMAP_FREE) {
105                                 hammer_modify_buffer(buffer2, layer2,
106                                                      sizeof(*layer2));
107                                 layer2->u.owner = owner &
108                                                 ~HAMMER_LARGEBLOCK_MASK64;
109                                 hammer_modify_buffer(buffer1, layer1,
110                                                      sizeof(*layer1));
111                                 --layer1->blocks_free;
112                                 hammer_modify_volume(root_volume, &ondisk->vol0_stat_freebigblocks,
113                                                      sizeof(ondisk->vol0_stat_freebigblocks));
114                                 --ondisk->vol0_stat_freebigblocks;
115                                 break;
116                         }
117                         result_offset += HAMMER_LARGEBLOCK_SIZE;
118                         if (HAMMER_VOL_DECODE(result_offset) != vol_no)
119                                 goto new_volume;
120                 }
121         }
122         kprintf("hammer_freemap_alloc %016llx\n", result_offset);
123         hammer_modify_volume(root_volume, blockmap, sizeof(*blockmap));
124         blockmap->next_offset = result_offset + HAMMER_LARGEBLOCK_SIZE;
125 done:
126         if (buffer1)
127                 hammer_rel_buffer(buffer1, 0);
128         if (buffer2)
129                 hammer_rel_buffer(buffer2, 0);
130         hammer_rel_volume(root_volume, 0);
131         return(result_offset);
132 }
133
134 void
135 hammer_freemap_free(hammer_mount_t hmp, hammer_off_t phys_offset, 
136                     hammer_off_t owner, int *errorp)
137 {
138         hammer_volume_t root_volume;
139         hammer_volume_ondisk_t ondisk;
140         hammer_off_t layer1_offset;
141         hammer_off_t layer2_offset;
142         hammer_blockmap_t blockmap;
143         hammer_buffer_t buffer1 = NULL;
144         hammer_buffer_t buffer2 = NULL;
145         struct hammer_blockmap_layer1 *layer1;
146         struct hammer_blockmap_layer2 *layer2;
147
148         KKASSERT((phys_offset & HAMMER_LARGEBLOCK_MASK64) == 0);
149
150         kprintf("hammer_freemap_free %016llx\n", phys_offset);
151
152         root_volume = hammer_get_root_volume(hmp, errorp);
153         if (*errorp)
154                 return;
155         ondisk = root_volume->ondisk;
156
157         blockmap = &ondisk->vol0_blockmap[HAMMER_ZONE_FREEMAP_INDEX];
158         layer1_offset = blockmap->phys_offset +
159                         HAMMER_BLOCKMAP_LAYER1_OFFSET(phys_offset);
160         layer1 = hammer_bread(hmp, layer1_offset, errorp, &buffer1);
161
162         KKASSERT(layer1->phys_offset != HAMMER_BLOCKMAP_UNAVAIL);
163
164         layer2_offset = layer1->phys_offset +
165                         HAMMER_BLOCKMAP_LAYER2_OFFSET(phys_offset);
166         layer2 = hammer_bread(hmp, layer2_offset, errorp, &buffer2);
167
168         KKASSERT(layer2->u.owner == (owner & ~HAMMER_LARGEBLOCK_MASK64));
169         hammer_modify_buffer(buffer1, layer1, sizeof(*layer1));
170         ++layer1->blocks_free;
171         hammer_modify_buffer(buffer2, layer2, sizeof(*layer2));
172         layer2->u.owner = HAMMER_BLOCKMAP_FREE;
173
174         hammer_modify_volume(root_volume, &ondisk->vol0_stat_freebigblocks,
175                              sizeof(ondisk->vol0_stat_freebigblocks));
176         ++ondisk->vol0_stat_freebigblocks;
177
178         if (buffer1)
179                 hammer_rel_buffer(buffer1, 0);
180         if (buffer2)
181                 hammer_rel_buffer(buffer2, 0);
182         hammer_rel_volume(root_volume, 0);
183 }
184