sys/vfs/hammer/hammer_transaction.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | /* * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "hammer.h" static uint32_t ocp_allocbit(hammer_objid_cache_t ocp, uint32_t n); /* * Start a standard transaction. * * May be called without fs_token */ void hammer_start_transaction(hammer_transaction_t trans, hammer_mount_t hmp) { struct timespec ts; int error; trans->type = HAMMER_TRANS_STD; trans->hmp = hmp; trans->rootvol = hammer_get_root_volume(hmp, &error); KKASSERT(error == 0); trans->tid = 0; trans->sync_lock_refs = 0; trans->flags = 0; vfs_timestamp(&ts); trans->time = (unsigned long)ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000; trans->time32 = (uint32_t)ts.tv_sec; } /* * Start a simple read-only transaction. This will not stall. * * May be called without fs_token */ void hammer_simple_transaction(hammer_transaction_t trans, hammer_mount_t hmp) { struct timespec ts; int error; trans->type = HAMMER_TRANS_RO; trans->hmp = hmp; trans->rootvol = hammer_get_root_volume(hmp, &error); KKASSERT(error == 0); trans->tid = 0; trans->sync_lock_refs = 0; trans->flags = 0; vfs_timestamp(&ts); trans->time = (unsigned long)ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000; trans->time32 = (uint32_t)ts.tv_sec; } /* * Start a transaction using a particular TID. Used by the sync code. * This does not stall. * * This routine may only be called from the flusher thread. We predispose * sync_lock_refs, implying serialization against the synchronization stage * (which the flusher is responsible for). */ void hammer_start_transaction_fls(hammer_transaction_t trans, hammer_mount_t hmp) { struct timespec ts; int error; bzero(trans, sizeof(*trans)); trans->type = HAMMER_TRANS_FLS; trans->hmp = hmp; trans->rootvol = hammer_get_root_volume(hmp, &error); KKASSERT(error == 0); trans->tid = hammer_alloc_tid(hmp, 1); trans->sync_lock_refs = 1; trans->flags = 0; vfs_timestamp(&ts); trans->time = (unsigned long)ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000; trans->time32 = (uint32_t)ts.tv_sec; } /* * May be called without fs_token */ void hammer_done_transaction(hammer_transaction_t trans) { int expected_lock_refs __debugvar; hammer_rel_volume(trans->rootvol, 0); trans->rootvol = NULL; expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0; KKASSERT(trans->sync_lock_refs == expected_lock_refs); trans->sync_lock_refs = 0; if (trans->type != HAMMER_TRANS_FLS) { if (trans->flags & HAMMER_TRANSF_NEWINODE) { lwkt_gettoken(&trans->hmp->fs_token); hammer_inode_waitreclaims(trans); lwkt_reltoken(&trans->hmp->fs_token); } } } /* * Allocate (count) TIDs. If running in multi-master mode the returned * base will be aligned to a 16-count plus the master id (0-15). * Multi-master mode allows non-conflicting to run and new objects to be * created on multiple masters in parallel. The transaction id identifies * the original master. The object_id is also subject to this rule in * order to allow objects to be created on multiple masters in parallel. * * Directories may pre-allocate a large number of object ids (100,000). * * NOTE: There is no longer a requirement that successive transaction * ids be 2 apart for separator generation. * * NOTE: When called by pseudo-backends such as ioctls the allocated * TID will be larger then the current flush TID, if a flush is running, * so any mirroring will pick the records up on a later flush. * * NOTE: HAMMER1 does not support multi-master clustering as of 2015. */ hammer_tid_t hammer_alloc_tid(hammer_mount_t hmp, int count) { hammer_tid_t tid; if (hmp->master_id < 0) { tid = hmp->next_tid + 1; hmp->next_tid = tid + count; } else { tid = (hmp->next_tid + HAMMER_MAX_MASTERS) & ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1); hmp->next_tid = tid + count * HAMMER_MAX_MASTERS; tid |= hmp->master_id; } if (tid >= 0xFFFFFFFFFF000000ULL) hpanic("Ran out of TIDs!"); if (hammer_debug_tid) hdkprintf("%016jx\n", (intmax_t)tid); return(tid); } /* * Allocate an object id. * * We use the upper OBJID_CACHE_BITS bits of the namekey to try to match * the low bits of the objid we allocate. */ hammer_tid_t hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip, int64_t namekey) { hammer_objid_cache_t ocp; hammer_tid_t tid; uint32_t n; while ((ocp = dip->objid_cache) == NULL) { if (hmp->objid_cache_count < OBJID_CACHE_SIZE) { ocp = kmalloc(sizeof(*ocp), hmp->m_misc, M_WAITOK|M_ZERO); ocp->base_tid = hammer_alloc_tid(hmp, OBJID_CACHE_BULK * 2); ocp->base_tid += OBJID_CACHE_BULK_MASK64; ocp->base_tid &= ~OBJID_CACHE_BULK_MASK64; /* may have blocked, recheck */ if (dip->objid_cache == NULL) { TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry); ++hmp->objid_cache_count; dip->objid_cache = ocp; ocp->dip = dip; } else { kfree(ocp, hmp->m_misc); } } else { /* * Steal one from another directory? * * Throw away ocp's that are more then half full, they * aren't worth stealing. */ ocp = TAILQ_FIRST(&hmp->objid_cache_list); if (ocp->dip) ocp->dip->objid_cache = NULL; if (ocp->count >= OBJID_CACHE_BULK / 2) { TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry); --hmp->objid_cache_count; kfree(ocp, hmp->m_misc); } else { dip->objid_cache = ocp; ocp->dip = dip; } } } TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry); /* * Allocate inode numbers uniformly. */ n = (namekey >> (63 - OBJID_CACHE_BULK_BITS)) & OBJID_CACHE_BULK_MASK; n = ocp_allocbit(ocp, n); tid = ocp->base_tid + n; #if 0 /* * The TID is incremented by 1 or by 16 depending what mode the * mount is operating in. */ ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS; #endif if (ocp->count >= OBJID_CACHE_BULK * 3 / 4) { dip->objid_cache = NULL; --hmp->objid_cache_count; ocp->dip = NULL; kfree(ocp, hmp->m_misc); } else { TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry); } return(tid); } /* * Allocate a bit starting with bit n. Wrap if necessary. * * This routine is only ever called if a bit is available somewhere * in the bitmap. */ static uint32_t ocp_allocbit(hammer_objid_cache_t ocp, uint32_t n) { uint32_t n0; n0 = (n >> 5) & 31; n &= 31; while (ocp->bm1[n0] & (1 << n)) { if (ocp->bm0 & (1 << n0)) { n0 = (n0 + 1) & 31; n = 0; } else if (++n == 32) { n0 = (n0 + 1) & 31; n = 0; } } ++ocp->count; ocp->bm1[n0] |= 1 << n; if (ocp->bm1[n0] == 0xFFFFFFFFU) ocp->bm0 |= 1 << n0; return((n0 << 5) + n); } void hammer_clear_objid(hammer_inode_t dip) { hammer_objid_cache_t ocp; if ((ocp = dip->objid_cache) != NULL) { dip->objid_cache = NULL; ocp->dip = NULL; TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry); TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry); } } void hammer_destroy_objid_cache(hammer_mount_t hmp) { hammer_objid_cache_t ocp; while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) { TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry); if (ocp->dip) ocp->dip->objid_cache = NULL; kfree(ocp, hmp->m_misc); --hmp->objid_cache_count; } KKASSERT(hmp->objid_cache_count == 0); } |