1 /* 2 * Copyright (c) 2007-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 35 #include "hammer.h" 36 37 static uint32_t ocp_allocbit(hammer_objid_cache_t ocp, uint32_t n); 38 39 40 /* 41 * Start a standard transaction. 42 * 43 * May be called without fs_token 44 */ 45 void 46 hammer_start_transaction(hammer_transaction_t trans, hammer_mount_t hmp) 47 { 48 struct timespec ts; 49 int error; 50 51 trans->type = HAMMER_TRANS_STD; 52 trans->hmp = hmp; 53 trans->rootvol = hammer_get_root_volume(hmp, &error); 54 KKASSERT(error == 0); 55 trans->tid = 0; 56 trans->sync_lock_refs = 0; 57 trans->flags = 0; 58 59 vfs_timestamp(&ts); 60 trans->time = (unsigned long)ts.tv_sec * 1000000ULL + 61 ts.tv_nsec / 1000; 62 trans->time32 = (uint32_t)ts.tv_sec; 63 } 64 65 /* 66 * Start a simple read-only transaction. This will not stall. 67 * 68 * May be called without fs_token 69 */ 70 void 71 hammer_simple_transaction(hammer_transaction_t trans, hammer_mount_t hmp) 72 { 73 struct timespec ts; 74 int error; 75 76 trans->type = HAMMER_TRANS_RO; 77 trans->hmp = hmp; 78 trans->rootvol = hammer_get_root_volume(hmp, &error); 79 KKASSERT(error == 0); 80 trans->tid = 0; 81 trans->sync_lock_refs = 0; 82 trans->flags = 0; 83 84 vfs_timestamp(&ts); 85 trans->time = (unsigned long)ts.tv_sec * 1000000ULL + 86 ts.tv_nsec / 1000; 87 trans->time32 = (uint32_t)ts.tv_sec; 88 } 89 90 /* 91 * Start a transaction using a particular TID. Used by the sync code. 92 * This does not stall. 93 * 94 * This routine may only be called from the flusher thread. We predispose 95 * sync_lock_refs, implying serialization against the synchronization stage 96 * (which the flusher is responsible for). 97 */ 98 void 99 hammer_start_transaction_fls(hammer_transaction_t trans, hammer_mount_t hmp) 100 { 101 struct timespec ts; 102 int error; 103 104 bzero(trans, sizeof(*trans)); 105 106 trans->type = HAMMER_TRANS_FLS; 107 trans->hmp = hmp; 108 trans->rootvol = hammer_get_root_volume(hmp, &error); 109 KKASSERT(error == 0); 110 trans->tid = hammer_alloc_tid(hmp, 1); 111 trans->sync_lock_refs = 1; 112 trans->flags = 0; 113 114 vfs_timestamp(&ts); 115 trans->time = (unsigned long)ts.tv_sec * 1000000ULL + 116 ts.tv_nsec / 1000; 117 trans->time32 = (uint32_t)ts.tv_sec; 118 } 119 120 /* 121 * May be called without fs_token 122 */ 123 void 124 hammer_done_transaction(hammer_transaction_t trans) 125 { 126 int expected_lock_refs __debugvar; 127 128 hammer_rel_volume(trans->rootvol, 0); 129 trans->rootvol = NULL; 130 expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0; 131 KKASSERT(trans->sync_lock_refs == expected_lock_refs); 132 trans->sync_lock_refs = 0; 133 if (trans->type != HAMMER_TRANS_FLS) { 134 if (trans->flags & HAMMER_TRANSF_NEWINODE) { 135 lwkt_gettoken(&trans->hmp->fs_token); 136 hammer_inode_waitreclaims(trans); 137 lwkt_reltoken(&trans->hmp->fs_token); 138 } 139 } 140 } 141 142 /* 143 * Allocate (count) TIDs. If running in multi-master mode the returned 144 * base will be aligned to a 16-count plus the master id (0-15). 145 * Multi-master mode allows non-conflicting to run and new objects to be 146 * created on multiple masters in parallel. The transaction id identifies 147 * the original master. The object_id is also subject to this rule in 148 * order to allow objects to be created on multiple masters in parallel. 149 * 150 * Directories may pre-allocate a large number of object ids (100,000). 151 * 152 * NOTE: There is no longer a requirement that successive transaction 153 * ids be 2 apart for separator generation. 154 * 155 * NOTE: When called by pseudo-backends such as ioctls the allocated 156 * TID will be larger then the current flush TID, if a flush is running, 157 * so any mirroring will pick the records up on a later flush. 158 * 159 * NOTE: HAMMER1 does not support multi-master clustering as of 2015. 160 */ 161 hammer_tid_t 162 hammer_alloc_tid(hammer_mount_t hmp, int count) 163 { 164 hammer_tid_t tid; 165 166 if (hmp->master_id < 0) { 167 tid = hmp->next_tid + 1; 168 hmp->next_tid = tid + count; 169 } else { 170 tid = (hmp->next_tid + HAMMER_MAX_MASTERS) & 171 ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1); 172 hmp->next_tid = tid + count * HAMMER_MAX_MASTERS; 173 tid |= hmp->master_id; 174 } 175 if (tid >= 0xFFFFFFFFFF000000ULL) 176 hpanic("Ran out of TIDs!"); 177 if (hammer_debug_tid) 178 hdkprintf("%016jx\n", (intmax_t)tid); 179 return(tid); 180 } 181 182 /* 183 * Allocate an object id. 184 * 185 * We use the upper OBJID_CACHE_BITS bits of the namekey to try to match 186 * the low bits of the objid we allocate. 187 */ 188 hammer_tid_t 189 hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip, int64_t namekey) 190 { 191 hammer_objid_cache_t ocp; 192 hammer_tid_t tid; 193 uint32_t n; 194 195 while ((ocp = dip->objid_cache) == NULL) { 196 if (hmp->objid_cache_count < OBJID_CACHE_SIZE) { 197 ocp = kmalloc(sizeof(*ocp), hmp->m_misc, 198 M_WAITOK|M_ZERO); 199 ocp->base_tid = hammer_alloc_tid(hmp, 200 OBJID_CACHE_BULK * 2); 201 ocp->base_tid += OBJID_CACHE_BULK_MASK64; 202 ocp->base_tid &= ~OBJID_CACHE_BULK_MASK64; 203 /* may have blocked, recheck */ 204 if (dip->objid_cache == NULL) { 205 TAILQ_INSERT_TAIL(&hmp->objid_cache_list, 206 ocp, entry); 207 ++hmp->objid_cache_count; 208 dip->objid_cache = ocp; 209 ocp->dip = dip; 210 } else { 211 kfree(ocp, hmp->m_misc); 212 } 213 } else { 214 /* 215 * Steal one from another directory? 216 * 217 * Throw away ocp's that are more then half full, they 218 * aren't worth stealing. 219 */ 220 ocp = TAILQ_FIRST(&hmp->objid_cache_list); 221 if (ocp->dip) 222 ocp->dip->objid_cache = NULL; 223 if (ocp->count >= OBJID_CACHE_BULK / 2) { 224 TAILQ_REMOVE(&hmp->objid_cache_list, 225 ocp, entry); 226 --hmp->objid_cache_count; 227 kfree(ocp, hmp->m_misc); 228 } else { 229 dip->objid_cache = ocp; 230 ocp->dip = dip; 231 } 232 } 233 } 234 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry); 235 236 /* 237 * Allocate inode numbers uniformly. 238 */ 239 240 n = (namekey >> (63 - OBJID_CACHE_BULK_BITS)) & OBJID_CACHE_BULK_MASK; 241 n = ocp_allocbit(ocp, n); 242 tid = ocp->base_tid + n; 243 244 #if 0 245 /* 246 * The TID is incremented by 1 or by 16 depending what mode the 247 * mount is operating in. 248 */ 249 ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS; 250 #endif 251 if (ocp->count >= OBJID_CACHE_BULK * 3 / 4) { 252 dip->objid_cache = NULL; 253 --hmp->objid_cache_count; 254 ocp->dip = NULL; 255 kfree(ocp, hmp->m_misc); 256 } else { 257 TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry); 258 } 259 return(tid); 260 } 261 262 /* 263 * Allocate a bit starting with bit n. Wrap if necessary. 264 * 265 * This routine is only ever called if a bit is available somewhere 266 * in the bitmap. 267 */ 268 static uint32_t 269 ocp_allocbit(hammer_objid_cache_t ocp, uint32_t n) 270 { 271 uint32_t n0; 272 273 n0 = (n >> 5) & 31; 274 n &= 31; 275 276 while (ocp->bm1[n0] & (1 << n)) { 277 if (ocp->bm0 & (1 << n0)) { 278 n0 = (n0 + 1) & 31; 279 n = 0; 280 } else if (++n == 32) { 281 n0 = (n0 + 1) & 31; 282 n = 0; 283 } 284 } 285 ++ocp->count; 286 ocp->bm1[n0] |= 1 << n; 287 if (ocp->bm1[n0] == 0xFFFFFFFFU) 288 ocp->bm0 |= 1 << n0; 289 return((n0 << 5) + n); 290 } 291 292 void 293 hammer_clear_objid(hammer_inode_t dip) 294 { 295 hammer_objid_cache_t ocp; 296 297 if ((ocp = dip->objid_cache) != NULL) { 298 dip->objid_cache = NULL; 299 ocp->dip = NULL; 300 TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry); 301 TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry); 302 } 303 } 304 305 void 306 hammer_destroy_objid_cache(hammer_mount_t hmp) 307 { 308 hammer_objid_cache_t ocp; 309 310 while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) { 311 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry); 312 if (ocp->dip) 313 ocp->dip->objid_cache = NULL; 314 kfree(ocp, hmp->m_misc); 315 --hmp->objid_cache_count; 316 } 317 KKASSERT(hmp->objid_cache_count == 0); 318 } 319 320