166325755SMatthew Dillon /*
2b84de5afSMatthew Dillon * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
366325755SMatthew Dillon *
466325755SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
566325755SMatthew Dillon * by Matthew Dillon <dillon@backplane.com>
666325755SMatthew Dillon *
766325755SMatthew Dillon * Redistribution and use in source and binary forms, with or without
866325755SMatthew Dillon * modification, are permitted provided that the following conditions
966325755SMatthew Dillon * are met:
1066325755SMatthew Dillon *
1166325755SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
1266325755SMatthew Dillon * notice, this list of conditions and the following disclaimer.
1366325755SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
1466325755SMatthew Dillon * notice, this list of conditions and the following disclaimer in
1566325755SMatthew Dillon * the documentation and/or other materials provided with the
1666325755SMatthew Dillon * distribution.
1766325755SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
1866325755SMatthew Dillon * contributors may be used to endorse or promote products derived
1966325755SMatthew Dillon * from this software without specific, prior written permission.
2066325755SMatthew Dillon *
2166325755SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2266325755SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2366325755SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2466325755SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2566325755SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2666325755SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2766325755SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2866325755SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2966325755SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3066325755SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3166325755SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3266325755SMatthew Dillon * SUCH DAMAGE.
3366325755SMatthew Dillon */
3466325755SMatthew Dillon
3566325755SMatthew Dillon #include "hammer.h"
3666325755SMatthew Dillon
3746137e17STomohiro Kusumi static uint32_t ocp_allocbit(hammer_objid_cache_t ocp, uint32_t n);
380729c8c8SMatthew Dillon
390729c8c8SMatthew Dillon
40b84de5afSMatthew Dillon /*
41b84de5afSMatthew Dillon * Start a standard transaction.
4232fcc103SMatthew Dillon *
4332fcc103SMatthew Dillon * May be called without fs_token
44b84de5afSMatthew Dillon */
4566325755SMatthew Dillon void
hammer_start_transaction(hammer_transaction_t trans,hammer_mount_t hmp)46ba2be8e9STomohiro Kusumi hammer_start_transaction(hammer_transaction_t trans, hammer_mount_t hmp)
4766325755SMatthew Dillon {
48*d489a79aSMatthew Dillon struct timespec ts;
49a89aec1bSMatthew Dillon int error;
5066325755SMatthew Dillon
51b84de5afSMatthew Dillon trans->type = HAMMER_TRANS_STD;
5266325755SMatthew Dillon trans->hmp = hmp;
53a89aec1bSMatthew Dillon trans->rootvol = hammer_get_root_volume(hmp, &error);
54a89aec1bSMatthew Dillon KKASSERT(error == 0);
55b84de5afSMatthew Dillon trans->tid = 0;
562f85fa4dSMatthew Dillon trans->sync_lock_refs = 0;
5721fde338SMatthew Dillon trans->flags = 0;
58ddfdf542SMatthew Dillon
59*d489a79aSMatthew Dillon vfs_timestamp(&ts);
60*d489a79aSMatthew Dillon trans->time = (unsigned long)ts.tv_sec * 1000000ULL +
61*d489a79aSMatthew Dillon ts.tv_nsec / 1000;
62*d489a79aSMatthew Dillon trans->time32 = (uint32_t)ts.tv_sec;
6366325755SMatthew Dillon }
6466325755SMatthew Dillon
65b84de5afSMatthew Dillon /*
66b84de5afSMatthew Dillon * Start a simple read-only transaction. This will not stall.
6732fcc103SMatthew Dillon *
6832fcc103SMatthew Dillon * May be called without fs_token
69b84de5afSMatthew Dillon */
7066325755SMatthew Dillon void
hammer_simple_transaction(hammer_transaction_t trans,hammer_mount_t hmp)71ba2be8e9STomohiro Kusumi hammer_simple_transaction(hammer_transaction_t trans, hammer_mount_t hmp)
7236f82b23SMatthew Dillon {
73*d489a79aSMatthew Dillon struct timespec ts;
7436f82b23SMatthew Dillon int error;
7536f82b23SMatthew Dillon
76b84de5afSMatthew Dillon trans->type = HAMMER_TRANS_RO;
7736f82b23SMatthew Dillon trans->hmp = hmp;
7836f82b23SMatthew Dillon trans->rootvol = hammer_get_root_volume(hmp, &error);
7936f82b23SMatthew Dillon KKASSERT(error == 0);
80b84de5afSMatthew Dillon trans->tid = 0;
812f85fa4dSMatthew Dillon trans->sync_lock_refs = 0;
8221fde338SMatthew Dillon trans->flags = 0;
83ddfdf542SMatthew Dillon
84*d489a79aSMatthew Dillon vfs_timestamp(&ts);
85*d489a79aSMatthew Dillon trans->time = (unsigned long)ts.tv_sec * 1000000ULL +
86*d489a79aSMatthew Dillon ts.tv_nsec / 1000;
87*d489a79aSMatthew Dillon trans->time32 = (uint32_t)ts.tv_sec;
8836f82b23SMatthew Dillon }
8936f82b23SMatthew Dillon
90b84de5afSMatthew Dillon /*
91b84de5afSMatthew Dillon * Start a transaction using a particular TID. Used by the sync code.
92b84de5afSMatthew Dillon * This does not stall.
932f85fa4dSMatthew Dillon *
942f85fa4dSMatthew Dillon * This routine may only be called from the flusher thread. We predispose
952f85fa4dSMatthew Dillon * sync_lock_refs, implying serialization against the synchronization stage
962f85fa4dSMatthew Dillon * (which the flusher is responsible for).
97b84de5afSMatthew Dillon */
9836f82b23SMatthew Dillon void
hammer_start_transaction_fls(hammer_transaction_t trans,hammer_mount_t hmp)99ba2be8e9STomohiro Kusumi hammer_start_transaction_fls(hammer_transaction_t trans, hammer_mount_t hmp)
100d113fda1SMatthew Dillon {
101*d489a79aSMatthew Dillon struct timespec ts;
102d113fda1SMatthew Dillon int error;
103d113fda1SMatthew Dillon
1049f5097dcSMatthew Dillon bzero(trans, sizeof(*trans));
1059f5097dcSMatthew Dillon
106b84de5afSMatthew Dillon trans->type = HAMMER_TRANS_FLS;
107d113fda1SMatthew Dillon trans->hmp = hmp;
108d113fda1SMatthew Dillon trans->rootvol = hammer_get_root_volume(hmp, &error);
109d113fda1SMatthew Dillon KKASSERT(error == 0);
1105de0c0e5SMatthew Dillon trans->tid = hammer_alloc_tid(hmp, 1);
1112f85fa4dSMatthew Dillon trans->sync_lock_refs = 1;
11221fde338SMatthew Dillon trans->flags = 0;
113ddfdf542SMatthew Dillon
114*d489a79aSMatthew Dillon vfs_timestamp(&ts);
115*d489a79aSMatthew Dillon trans->time = (unsigned long)ts.tv_sec * 1000000ULL +
116*d489a79aSMatthew Dillon ts.tv_nsec / 1000;
117*d489a79aSMatthew Dillon trans->time32 = (uint32_t)ts.tv_sec;
118d113fda1SMatthew Dillon }
119d113fda1SMatthew Dillon
12032fcc103SMatthew Dillon /*
12132fcc103SMatthew Dillon * May be called without fs_token
12232fcc103SMatthew Dillon */
123d113fda1SMatthew Dillon void
hammer_done_transaction(hammer_transaction_t trans)12487d20609STomohiro Kusumi hammer_done_transaction(hammer_transaction_t trans)
12566325755SMatthew Dillon {
126f31f6d84SSascha Wildner int expected_lock_refs __debugvar;
1272f85fa4dSMatthew Dillon
128a89aec1bSMatthew Dillon hammer_rel_volume(trans->rootvol, 0);
129b84de5afSMatthew Dillon trans->rootvol = NULL;
1302f85fa4dSMatthew Dillon expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0;
1312f85fa4dSMatthew Dillon KKASSERT(trans->sync_lock_refs == expected_lock_refs);
1322f85fa4dSMatthew Dillon trans->sync_lock_refs = 0;
13382010f9fSMatthew Dillon if (trans->type != HAMMER_TRANS_FLS) {
13432fcc103SMatthew Dillon if (trans->flags & HAMMER_TRANSF_NEWINODE) {
13532fcc103SMatthew Dillon lwkt_gettoken(&trans->hmp->fs_token);
136e98f1b96SMatthew Dillon hammer_inode_waitreclaims(trans);
13732fcc103SMatthew Dillon lwkt_reltoken(&trans->hmp->fs_token);
13832fcc103SMatthew Dillon }
13982010f9fSMatthew Dillon }
14066325755SMatthew Dillon }
14166325755SMatthew Dillon
142d113fda1SMatthew Dillon /*
1435de0c0e5SMatthew Dillon * Allocate (count) TIDs. If running in multi-master mode the returned
1445de0c0e5SMatthew Dillon * base will be aligned to a 16-count plus the master id (0-15).
1455de0c0e5SMatthew Dillon * Multi-master mode allows non-conflicting to run and new objects to be
1465de0c0e5SMatthew Dillon * created on multiple masters in parallel. The transaction id identifies
1475de0c0e5SMatthew Dillon * the original master. The object_id is also subject to this rule in
1485de0c0e5SMatthew Dillon * order to allow objects to be created on multiple masters in parallel.
1495de0c0e5SMatthew Dillon *
1505de0c0e5SMatthew Dillon * Directories may pre-allocate a large number of object ids (100,000).
1515de0c0e5SMatthew Dillon *
1525de0c0e5SMatthew Dillon * NOTE: There is no longer a requirement that successive transaction
1535de0c0e5SMatthew Dillon * ids be 2 apart for separator generation.
15483f2a3aaSMatthew Dillon *
15583f2a3aaSMatthew Dillon * NOTE: When called by pseudo-backends such as ioctls the allocated
15683f2a3aaSMatthew Dillon * TID will be larger then the current flush TID, if a flush is running,
15783f2a3aaSMatthew Dillon * so any mirroring will pick the records up on a later flush.
158c204ebbeSTomohiro Kusumi *
159c204ebbeSTomohiro Kusumi * NOTE: HAMMER1 does not support multi-master clustering as of 2015.
160d113fda1SMatthew Dillon */
16183f2a3aaSMatthew Dillon hammer_tid_t
hammer_alloc_tid(hammer_mount_t hmp,int count)1625de0c0e5SMatthew Dillon hammer_alloc_tid(hammer_mount_t hmp, int count)
163a89aec1bSMatthew Dillon {
164a89aec1bSMatthew Dillon hammer_tid_t tid;
165a89aec1bSMatthew Dillon
166732a1697SMatthew Dillon if (hmp->master_id < 0) {
167c82af904SMatthew Dillon tid = hmp->next_tid + 1;
168c82af904SMatthew Dillon hmp->next_tid = tid + count;
169c82af904SMatthew Dillon } else {
170c82af904SMatthew Dillon tid = (hmp->next_tid + HAMMER_MAX_MASTERS) &
171c82af904SMatthew Dillon ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1);
172c82af904SMatthew Dillon hmp->next_tid = tid + count * HAMMER_MAX_MASTERS;
173732a1697SMatthew Dillon tid |= hmp->master_id;
174c82af904SMatthew Dillon }
175c82af904SMatthew Dillon if (tid >= 0xFFFFFFFFFF000000ULL)
176903fdd05STomohiro Kusumi hpanic("Ran out of TIDs!");
1775de0c0e5SMatthew Dillon if (hammer_debug_tid)
17835a5249bSTomohiro Kusumi hdkprintf("%016jx\n", (intmax_t)tid);
179a89aec1bSMatthew Dillon return(tid);
180a89aec1bSMatthew Dillon }
181a89aec1bSMatthew Dillon
1820729c8c8SMatthew Dillon /*
1835a64efa1SMatthew Dillon * Allocate an object id.
1845a64efa1SMatthew Dillon *
1855a64efa1SMatthew Dillon * We use the upper OBJID_CACHE_BITS bits of the namekey to try to match
1865a64efa1SMatthew Dillon * the low bits of the objid we allocate.
1870729c8c8SMatthew Dillon */
1880729c8c8SMatthew Dillon hammer_tid_t
hammer_alloc_objid(hammer_mount_t hmp,hammer_inode_t dip,int64_t namekey)1895a64efa1SMatthew Dillon hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip, int64_t namekey)
1900729c8c8SMatthew Dillon {
1910729c8c8SMatthew Dillon hammer_objid_cache_t ocp;
1920729c8c8SMatthew Dillon hammer_tid_t tid;
19346137e17STomohiro Kusumi uint32_t n;
1940729c8c8SMatthew Dillon
1950729c8c8SMatthew Dillon while ((ocp = dip->objid_cache) == NULL) {
1965de0c0e5SMatthew Dillon if (hmp->objid_cache_count < OBJID_CACHE_SIZE) {
197bac808feSMatthew Dillon ocp = kmalloc(sizeof(*ocp), hmp->m_misc,
198bac808feSMatthew Dillon M_WAITOK|M_ZERO);
1995a64efa1SMatthew Dillon ocp->base_tid = hammer_alloc_tid(hmp,
2005a64efa1SMatthew Dillon OBJID_CACHE_BULK * 2);
2015a64efa1SMatthew Dillon ocp->base_tid += OBJID_CACHE_BULK_MASK64;
2025a64efa1SMatthew Dillon ocp->base_tid &= ~OBJID_CACHE_BULK_MASK64;
2030729c8c8SMatthew Dillon /* may have blocked, recheck */
2040729c8c8SMatthew Dillon if (dip->objid_cache == NULL) {
205d1ce1558SMatthew Dillon TAILQ_INSERT_TAIL(&hmp->objid_cache_list,
206d1ce1558SMatthew Dillon ocp, entry);
207d1ce1558SMatthew Dillon ++hmp->objid_cache_count;
2080729c8c8SMatthew Dillon dip->objid_cache = ocp;
2090729c8c8SMatthew Dillon ocp->dip = dip;
210d1ce1558SMatthew Dillon } else {
211d1ce1558SMatthew Dillon kfree(ocp, hmp->m_misc);
2120729c8c8SMatthew Dillon }
2130729c8c8SMatthew Dillon } else {
2145a64efa1SMatthew Dillon /*
2155a64efa1SMatthew Dillon * Steal one from another directory?
2165a64efa1SMatthew Dillon *
2175a64efa1SMatthew Dillon * Throw away ocp's that are more then half full, they
2185a64efa1SMatthew Dillon * aren't worth stealing.
2195a64efa1SMatthew Dillon */
2205de0c0e5SMatthew Dillon ocp = TAILQ_FIRST(&hmp->objid_cache_list);
2210729c8c8SMatthew Dillon if (ocp->dip)
2220729c8c8SMatthew Dillon ocp->dip->objid_cache = NULL;
2235a64efa1SMatthew Dillon if (ocp->count >= OBJID_CACHE_BULK / 2) {
224d1ce1558SMatthew Dillon TAILQ_REMOVE(&hmp->objid_cache_list,
225d1ce1558SMatthew Dillon ocp, entry);
2265a64efa1SMatthew Dillon --hmp->objid_cache_count;
2275a64efa1SMatthew Dillon kfree(ocp, hmp->m_misc);
2285a64efa1SMatthew Dillon } else {
2290729c8c8SMatthew Dillon dip->objid_cache = ocp;
2300729c8c8SMatthew Dillon ocp->dip = dip;
2310729c8c8SMatthew Dillon }
2320729c8c8SMatthew Dillon }
2335a64efa1SMatthew Dillon }
2345de0c0e5SMatthew Dillon TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
2355de0c0e5SMatthew Dillon
2365de0c0e5SMatthew Dillon /*
237d1ce1558SMatthew Dillon * Allocate inode numbers uniformly.
2385a64efa1SMatthew Dillon */
239d1ce1558SMatthew Dillon
2405a64efa1SMatthew Dillon n = (namekey >> (63 - OBJID_CACHE_BULK_BITS)) & OBJID_CACHE_BULK_MASK;
2415a64efa1SMatthew Dillon n = ocp_allocbit(ocp, n);
2425a64efa1SMatthew Dillon tid = ocp->base_tid + n;
2435a64efa1SMatthew Dillon
2445a64efa1SMatthew Dillon #if 0
2455a64efa1SMatthew Dillon /*
2465de0c0e5SMatthew Dillon * The TID is incremented by 1 or by 16 depending what mode the
2475de0c0e5SMatthew Dillon * mount is operating in.
2485de0c0e5SMatthew Dillon */
249732a1697SMatthew Dillon ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS;
2505a64efa1SMatthew Dillon #endif
251d1ce1558SMatthew Dillon if (ocp->count >= OBJID_CACHE_BULK * 3 / 4) {
2520729c8c8SMatthew Dillon dip->objid_cache = NULL;
2535de0c0e5SMatthew Dillon --hmp->objid_cache_count;
2540729c8c8SMatthew Dillon ocp->dip = NULL;
255bac808feSMatthew Dillon kfree(ocp, hmp->m_misc);
2560729c8c8SMatthew Dillon } else {
2575de0c0e5SMatthew Dillon TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry);
2580729c8c8SMatthew Dillon }
2590729c8c8SMatthew Dillon return(tid);
2600729c8c8SMatthew Dillon }
2610729c8c8SMatthew Dillon
2625a64efa1SMatthew Dillon /*
2635a64efa1SMatthew Dillon * Allocate a bit starting with bit n. Wrap if necessary.
2645a64efa1SMatthew Dillon *
2655a64efa1SMatthew Dillon * This routine is only ever called if a bit is available somewhere
2665a64efa1SMatthew Dillon * in the bitmap.
2675a64efa1SMatthew Dillon */
26846137e17STomohiro Kusumi static uint32_t
ocp_allocbit(hammer_objid_cache_t ocp,uint32_t n)26946137e17STomohiro Kusumi ocp_allocbit(hammer_objid_cache_t ocp, uint32_t n)
2705a64efa1SMatthew Dillon {
27146137e17STomohiro Kusumi uint32_t n0;
2725a64efa1SMatthew Dillon
2735a64efa1SMatthew Dillon n0 = (n >> 5) & 31;
2745a64efa1SMatthew Dillon n &= 31;
2755a64efa1SMatthew Dillon
2765a64efa1SMatthew Dillon while (ocp->bm1[n0] & (1 << n)) {
2775a64efa1SMatthew Dillon if (ocp->bm0 & (1 << n0)) {
2785a64efa1SMatthew Dillon n0 = (n0 + 1) & 31;
2795a64efa1SMatthew Dillon n = 0;
2805a64efa1SMatthew Dillon } else if (++n == 32) {
2815a64efa1SMatthew Dillon n0 = (n0 + 1) & 31;
2825a64efa1SMatthew Dillon n = 0;
2835a64efa1SMatthew Dillon }
2845a64efa1SMatthew Dillon }
2855a64efa1SMatthew Dillon ++ocp->count;
2865a64efa1SMatthew Dillon ocp->bm1[n0] |= 1 << n;
2875a64efa1SMatthew Dillon if (ocp->bm1[n0] == 0xFFFFFFFFU)
2885a64efa1SMatthew Dillon ocp->bm0 |= 1 << n0;
2895a64efa1SMatthew Dillon return((n0 << 5) + n);
2905a64efa1SMatthew Dillon }
2915a64efa1SMatthew Dillon
2920729c8c8SMatthew Dillon void
hammer_clear_objid(hammer_inode_t dip)2930729c8c8SMatthew Dillon hammer_clear_objid(hammer_inode_t dip)
2940729c8c8SMatthew Dillon {
2950729c8c8SMatthew Dillon hammer_objid_cache_t ocp;
2960729c8c8SMatthew Dillon
2970729c8c8SMatthew Dillon if ((ocp = dip->objid_cache) != NULL) {
2980729c8c8SMatthew Dillon dip->objid_cache = NULL;
2990729c8c8SMatthew Dillon ocp->dip = NULL;
3000729c8c8SMatthew Dillon TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
3010729c8c8SMatthew Dillon TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
3020729c8c8SMatthew Dillon }
3030729c8c8SMatthew Dillon }
3040729c8c8SMatthew Dillon
3050729c8c8SMatthew Dillon void
hammer_destroy_objid_cache(hammer_mount_t hmp)3060729c8c8SMatthew Dillon hammer_destroy_objid_cache(hammer_mount_t hmp)
3070729c8c8SMatthew Dillon {
3080729c8c8SMatthew Dillon hammer_objid_cache_t ocp;
3090729c8c8SMatthew Dillon
3100729c8c8SMatthew Dillon while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
3110729c8c8SMatthew Dillon TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
312f437a2abSMatthew Dillon if (ocp->dip)
313f437a2abSMatthew Dillon ocp->dip->objid_cache = NULL;
314bac808feSMatthew Dillon kfree(ocp, hmp->m_misc);
315d1ce1558SMatthew Dillon --hmp->objid_cache_count;
3160729c8c8SMatthew Dillon }
317d1ce1558SMatthew Dillon KKASSERT(hmp->objid_cache_count == 0);
3180729c8c8SMatthew Dillon }
3190729c8c8SMatthew Dillon
320