1 /* 2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@dragonflybsd.org> 6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/fcntl.h> 39 #include <sys/buf.h> 40 #include <sys/proc.h> 41 #include <sys/namei.h> 42 #include <sys/mount.h> 43 #include <sys/vnode.h> 44 #include <sys/mountctl.h> 45 46 #include "hammer2.h" 47 48 /* 49 * Allocate media space, returning a combined data offset and radix. 50 * 51 * XXX when diving a new full block create a clean empty buffer and bqrelse() 52 * it, so small data structures do not have to issue read-IO when they 53 * do the read-modify-write on the backing store. 54 */ 55 hammer2_off_t 56 hammer2_freemap_alloc(hammer2_mount_t *hmp, int type, size_t bytes) 57 { 58 hammer2_off_t data_off; 59 hammer2_off_t data_next; 60 hammer2_freecache_t *fc; 61 /*struct buf *bp;*/ 62 int radix; 63 int fctype; 64 65 switch(type) { 66 case HAMMER2_BREF_TYPE_INODE: 67 fctype = HAMMER2_FREECACHE_INODE; 68 break; 69 case HAMMER2_BREF_TYPE_INDIRECT: 70 fctype = HAMMER2_FREECACHE_INODE; 71 break; 72 case HAMMER2_BREF_TYPE_DATA: 73 fctype = HAMMER2_FREECACHE_DATA; 74 break; 75 default: 76 fctype = HAMMER2_FREECACHE_DATA; 77 break; 78 } 79 80 /* 81 * Figure out the base 2 radix of the allocation (rounded up) 82 */ 83 radix = hammer2_bytes_to_radix(bytes); 84 bytes = 1 << radix; 85 86 if (radix <= HAMMER2_MAX_RADIX) 87 fc = &hmp->freecache[fctype][radix]; 88 else 89 fc = NULL; 90 91 lockmgr(&hmp->alloclk, LK_EXCLUSIVE); 92 if (fc && fc->single) { 93 /* 94 * Allocate from our single-block cache. 95 */ 96 data_off = fc->single; 97 fc->single = 0; 98 } else if (fc && fc->bulk) { 99 /* 100 * Allocate from our packing cache. 101 */ 102 data_off = fc->bulk; 103 fc->bulk += bytes; 104 if ((fc->bulk & HAMMER2_SEGMASK) == 0) 105 fc->bulk = 0; 106 } else { 107 /* 108 * Allocate from the allocation iterator using a SEGSIZE 109 * aligned block and reload the packing cache if possible. 110 * 111 * Skip reserved areas at the beginning of each zone. 112 */ 113 hammer2_voldata_lock(hmp); 114 data_off = hmp->voldata.allocator_beg; 115 data_off = (data_off + HAMMER2_SEGMASK64) & ~HAMMER2_SEGMASK64; 116 if ((data_off & HAMMER2_ZONE_MASK64) < HAMMER2_ZONE_SEG) { 117 KKASSERT((data_off & HAMMER2_ZONE_MASK64) == 0); 118 data_off += HAMMER2_ZONE_SEG64; 119 } 120 data_next = data_off + bytes; 121 122 if ((data_next & HAMMER2_SEGMASK) == 0) { 123 hmp->voldata.allocator_beg = data_next; 124 } else { 125 KKASSERT(radix <= HAMMER2_MAX_RADIX); 126 hmp->voldata.allocator_beg = 127 (data_next + HAMMER2_SEGMASK64) & 128 ~HAMMER2_SEGMASK64; 129 fc->bulk = data_next; 130 } 131 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED_AUX); 132 hammer2_voldata_unlock(hmp); 133 } 134 lockmgr(&hmp->alloclk, LK_RELEASE); 135 136 #if 0 137 /* 138 * Allocations on-media are always in multiples of 64K but 139 * partial-block allocations can be tracked in-memory. 140 * 141 * We can reduce the need for read-modify-write IOs by 142 * telling the kernel that the contents of a new 64K block is 143 * initially good (before we use any of it). 144 * 145 * Worst case is the kernel evicts the buffer and causes HAMMER2's 146 * bread later on to actually issue a read I/O. 147 * 148 * XXX Maybe do this in SEGSIZE increments? Needs a lot of work. 149 * Also watch out for buffer size mismatches. 150 */ 151 if (bytes < HAMMER2_MINIOSIZE && 152 (data_off & (HAMMER2_MINIOSIZE - 1)) == 0) { 153 bp = getblk(hmp->devvp, data_off, HAMMER2_MINIOSIZE, 0, 0); 154 bp->b_flags |= B_CACHE; 155 bp->b_resid = 0; 156 bqrelse(bp); 157 } 158 #endif 159 160 if (hammer2_debug & 0x0001) { 161 kprintf("hammer2: allocate %d %016jx: %zd\n", 162 type, (intmax_t)data_off, bytes); 163 } 164 return (data_off | radix); 165 } 166 167 void 168 hammer2_freemap_free(hammer2_mount_t *hmp, hammer2_off_t data_off, int type) 169 { 170 hammer2_freecache_t *fc; 171 int radix; 172 int fctype; 173 174 switch(type) { 175 case HAMMER2_BREF_TYPE_INODE: 176 fctype = HAMMER2_FREECACHE_INODE; 177 break; 178 case HAMMER2_BREF_TYPE_INDIRECT: 179 fctype = HAMMER2_FREECACHE_INODE; 180 break; 181 case HAMMER2_BREF_TYPE_DATA: 182 fctype = HAMMER2_FREECACHE_DATA; 183 break; 184 default: 185 fctype = HAMMER2_FREECACHE_DATA; 186 break; 187 } 188 radix = (int)data_off & HAMMER2_OFF_MASK_RADIX; 189 data_off &= ~HAMMER2_OFF_MASK_RADIX; 190 if (radix >= HAMMER2_MAX_RADIX) 191 return; 192 193 fc = &hmp->freecache[fctype][radix]; 194 if (fc->single == 0) { 195 lockmgr(&hmp->alloclk, LK_EXCLUSIVE); 196 fc->single = data_off; 197 lockmgr(&hmp->alloclk, LK_RELEASE); 198 } 199 } 200 201 #if 0 202 /* 203 * Allocate media space, returning a combined data offset and radix. 204 * Also return the related (device) buffer cache buffer. 205 */ 206 hammer2_off_t 207 hammer2_freemap_alloc_bp(hammer2_mount_t *hmp, size_t bytes, struct buf **bpp) 208 { 209 } 210 211 #endif 212