2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
41 #include <sys/namei.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/mountctl.h>
49 * Allocate media space, returning a combined data offset and radix.
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.
56 hammer2_freemap_alloc(hammer2_mount_t *hmp, int type, size_t bytes)
58 hammer2_off_t data_off;
59 hammer2_off_t data_next;
65 case HAMMER2_BREF_TYPE_INODE:
66 fctype = HAMMER2_FREECACHE_INODE;
68 case HAMMER2_BREF_TYPE_INDIRECT:
69 fctype = HAMMER2_FREECACHE_INODE;
71 case HAMMER2_BREF_TYPE_DATA:
72 fctype = HAMMER2_FREECACHE_DATA;
75 fctype = HAMMER2_FREECACHE_DATA;
80 * Figure out the base 2 radix of the allocation (rounded up)
82 radix = hammer2_bytes_to_radix(bytes);
85 lockmgr(&hmp->alloclk, LK_EXCLUSIVE);
86 if (radix <= HAMMER2_MAX_RADIX && hmp->freecache[fctype][radix]) {
88 * Allocate from our packing cache
90 data_off = hmp->freecache[fctype][radix];
91 hmp->freecache[fctype][radix] += bytes;
92 if ((hmp->freecache[fctype][radix] & HAMMER2_SEGMASK) == 0)
93 hmp->freecache[fctype][radix] = 0;
96 * Allocate from the allocation iterator using a SEGSIZE
97 * aligned block and reload the packing cache if possible.
99 data_off = hmp->voldata.allocator_beg;
100 data_off = (data_off + HAMMER2_SEGMASK64) & ~HAMMER2_SEGMASK64;
101 data_next = data_off + bytes;
103 if ((data_next & HAMMER2_SEGMASK) == 0) {
104 hmp->voldata.allocator_beg = data_next;
106 KKASSERT(radix <= HAMMER2_MAX_RADIX);
107 hmp->voldata.allocator_beg =
108 (data_next + HAMMER2_SEGMASK64) &
110 hmp->freecache[fctype][radix] = data_next;
113 lockmgr(&hmp->alloclk, LK_RELEASE);
117 * Allocations on-media are always in multiples of 64K but
118 * partial-block allocations can be tracked in-memory.
120 * We can reduce the need for read-modify-write IOs by
121 * telling the kernel that the contents of a new 64K block is
122 * initially good (before we use any of it).
124 * Worst case is the kernel evicts the buffer and causes HAMMER2's
125 * bread later on to actually issue a read I/O.
127 * XXX Maybe do this in SEGSIZE increments? Needs a lot of work.
128 * Also watch out for buffer size mismatches.
130 if (bytes < HAMMER2_MINIOSIZE &&
131 (data_off & (HAMMER2_MINIOSIZE - 1)) == 0) {
132 bp = getblk(hmp->devvp, data_off, HAMMER2_MINIOSIZE, 0, 0);
133 bp->b_flags |= B_CACHE;
139 if (hammer2_debug & 0x0001) {
140 kprintf("hammer2: allocate %d %016jx: %zd\n",
141 type, (intmax_t)data_off, bytes);
143 return (data_off | radix);
148 * Allocate media space, returning a combined data offset and radix.
149 * Also return the related (device) buffer cache buffer.
152 hammer2_freemap_alloc_bp(hammer2_mount_t *hmp, size_t bytes, struct buf **bpp)