HAMMER 46/Many: Performance pass, media changes, bug fixes.
[dragonfly.git] / sys / vfs / hammer / hammer_transaction.c
CommitLineData
66325755 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
66325755
MD
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 *
2f85fa4d 34 * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.15 2008/05/18 01:48:50 dillon Exp $
66325755
MD
35 */
36
37#include "hammer.h"
38
0729c8c8
MD
39static hammer_tid_t hammer_alloc_tid(hammer_transaction_t trans, int count);
40
41
b84de5af
MD
42/*
43 * Start a standard transaction.
44 */
66325755 45void
8cd0a023
MD
46hammer_start_transaction(struct hammer_transaction *trans,
47 struct hammer_mount *hmp)
66325755 48{
a89aec1b 49 int error;
66325755 50
b84de5af 51 trans->type = HAMMER_TRANS_STD;
66325755 52 trans->hmp = hmp;
a89aec1b
MD
53 trans->rootvol = hammer_get_root_volume(hmp, &error);
54 KKASSERT(error == 0);
b84de5af 55 trans->tid = 0;
0729c8c8 56 trans->time = hammer_alloc_tid(trans, 1);
2f85fa4d 57 trans->sync_lock_refs = 0;
66325755
MD
58}
59
b84de5af
MD
60/*
61 * Start a simple read-only transaction. This will not stall.
62 */
36f82b23
MD
63void
64hammer_simple_transaction(struct hammer_transaction *trans,
65 struct hammer_mount *hmp)
66{
67 int error;
68
b84de5af 69 trans->type = HAMMER_TRANS_RO;
36f82b23
MD
70 trans->hmp = hmp;
71 trans->rootvol = hammer_get_root_volume(hmp, &error);
36f82b23 72 KKASSERT(error == 0);
b84de5af 73 trans->tid = 0;
0729c8c8 74 trans->time = hammer_alloc_tid(trans, 1);
2f85fa4d 75 trans->sync_lock_refs = 0;
36f82b23
MD
76}
77
b84de5af
MD
78/*
79 * Start a transaction using a particular TID. Used by the sync code.
80 * This does not stall.
2f85fa4d
MD
81 *
82 * This routine may only be called from the flusher thread. We predispose
83 * sync_lock_refs, implying serialization against the synchronization stage
84 * (which the flusher is responsible for).
b84de5af 85 */
d113fda1 86void
b84de5af
MD
87hammer_start_transaction_fls(struct hammer_transaction *trans,
88 struct hammer_mount *hmp)
d113fda1
MD
89{
90 int error;
91
2f85fa4d
MD
92 KKASSERT(curthread == hmp->flusher_td);
93
b84de5af 94 trans->type = HAMMER_TRANS_FLS;
d113fda1
MD
95 trans->hmp = hmp;
96 trans->rootvol = hammer_get_root_volume(hmp, &error);
97 KKASSERT(error == 0);
0729c8c8 98 trans->tid = hammer_alloc_tid(trans, 1);
b84de5af 99 trans->time = trans->tid;
2f85fa4d 100 trans->sync_lock_refs = 1;
66325755
MD
101}
102
103void
b84de5af 104hammer_done_transaction(struct hammer_transaction *trans)
66325755 105{
2f85fa4d
MD
106 int expected_lock_refs;
107
a89aec1b 108 hammer_rel_volume(trans->rootvol, 0);
b84de5af 109 trans->rootvol = NULL;
2f85fa4d
MD
110 expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0;
111 KKASSERT(trans->sync_lock_refs == expected_lock_refs);
112 trans->sync_lock_refs = 0;
66325755
MD
113}
114
d113fda1
MD
115/*
116 * Note: Successive transaction ids must be at least 2 apart so the
117 * B-Tree code can make a separator that does not match either the
118 * left or right hand sides.
119 */
0729c8c8
MD
120static hammer_tid_t
121hammer_alloc_tid(hammer_transaction_t trans, int count)
a89aec1b 122{
a89aec1b
MD
123 struct timespec ts;
124 hammer_tid_t tid;
125
126 getnanotime(&ts);
127 tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
b84de5af
MD
128 if (tid < trans->hmp->next_tid)
129 tid = trans->hmp->next_tid;
0729c8c8 130 if (tid >= 0xFFFFFFFFFFFFF000ULL)
a89aec1b 131 panic("hammer_start_transaction: Ran out of TIDs!");
0729c8c8 132 trans->hmp->next_tid = tid + count * 2;
d113fda1
MD
133 if (hammer_debug_tid) {
134 kprintf("alloc_tid %016llx (0x%08x)\n",
135 tid, (int)(tid / 1000000000LL));
136 }
a89aec1b
MD
137 return(tid);
138}
139
0729c8c8
MD
140/*
141 * Allocate an object id
142 */
143hammer_tid_t
144hammer_alloc_objid(hammer_transaction_t trans, hammer_inode_t dip)
145{
146 hammer_objid_cache_t ocp;
147 hammer_tid_t tid;
148
149 while ((ocp = dip->objid_cache) == NULL) {
150 if (trans->hmp->objid_cache_count < OBJID_CACHE_SIZE) {
151 ocp = kmalloc(sizeof(*ocp), M_HAMMER, M_WAITOK|M_ZERO);
152 ocp->next_tid = hammer_alloc_tid(trans,
153 OBJID_CACHE_BULK);
154 ocp->count = OBJID_CACHE_BULK;
155 TAILQ_INSERT_HEAD(&trans->hmp->objid_cache_list, ocp,
156 entry);
157 ++trans->hmp->objid_cache_count;
158 /* may have blocked, recheck */
159 if (dip->objid_cache == NULL) {
160 dip->objid_cache = ocp;
161 ocp->dip = dip;
162 }
163 } else {
164 ocp = TAILQ_FIRST(&trans->hmp->objid_cache_list);
165 if (ocp->dip)
166 ocp->dip->objid_cache = NULL;
167 dip->objid_cache = ocp;
168 ocp->dip = dip;
169 }
170 }
171 TAILQ_REMOVE(&trans->hmp->objid_cache_list, ocp, entry);
172 tid = ocp->next_tid;
173 ocp->next_tid += 2;
174 if (--ocp->count == 0) {
175 dip->objid_cache = NULL;
176 --trans->hmp->objid_cache_count;
177 ocp->dip = NULL;
178 kfree(ocp, M_HAMMER);
179 } else {
180 TAILQ_INSERT_TAIL(&trans->hmp->objid_cache_list, ocp, entry);
181 }
182 return(tid);
183}
184
185void
186hammer_clear_objid(hammer_inode_t dip)
187{
188 hammer_objid_cache_t ocp;
189
190 if ((ocp = dip->objid_cache) != NULL) {
191 dip->objid_cache = NULL;
192 ocp->dip = NULL;
193 TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
194 TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
195 }
196}
197
198void
199hammer_destroy_objid_cache(hammer_mount_t hmp)
200{
201 hammer_objid_cache_t ocp;
202
203 while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
204 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
205 kfree(ocp, M_HAMMER);
206 }
207}
208