HAMMER: MFC all changes through 20080924
[dragonfly.git] / sys / vfs / hammer / hammer_transaction.c
blob5f994f6f65c3bbfe0489760c986e7d2684eb1a6d
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:
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.
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.
34 * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.22.2.4 2008/09/25 01:42:52 dillon Exp $
37 #include "hammer.h"
39 static hammer_tid_t hammer_alloc_tid(hammer_mount_t hmp, int count);
43 * Start a standard transaction.
45 void
46 hammer_start_transaction(struct hammer_transaction *trans,
47 struct hammer_mount *hmp)
49 struct timeval tv;
50 int error;
52 trans->type = HAMMER_TRANS_STD;
53 trans->hmp = hmp;
54 trans->rootvol = hammer_get_root_volume(hmp, &error);
55 KKASSERT(error == 0);
56 trans->tid = 0;
57 trans->sync_lock_refs = 0;
58 trans->flags = 0;
60 getmicrotime(&tv);
61 trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
62 trans->time32 = (u_int32_t)tv.tv_sec;
66 * Start a simple read-only transaction. This will not stall.
68 void
69 hammer_simple_transaction(struct hammer_transaction *trans,
70 struct hammer_mount *hmp)
72 struct timeval tv;
73 int error;
75 trans->type = HAMMER_TRANS_RO;
76 trans->hmp = hmp;
77 trans->rootvol = hammer_get_root_volume(hmp, &error);
78 KKASSERT(error == 0);
79 trans->tid = 0;
80 trans->sync_lock_refs = 0;
81 trans->flags = 0;
83 getmicrotime(&tv);
84 trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
85 trans->time32 = (u_int32_t)tv.tv_sec;
89 * Start a transaction using a particular TID. Used by the sync code.
90 * This does not stall.
92 * This routine may only be called from the flusher thread. We predispose
93 * sync_lock_refs, implying serialization against the synchronization stage
94 * (which the flusher is responsible for).
96 void
97 hammer_start_transaction_fls(struct hammer_transaction *trans,
98 struct hammer_mount *hmp)
100 struct timeval tv;
101 int error;
103 bzero(trans, sizeof(*trans));
105 trans->type = HAMMER_TRANS_FLS;
106 trans->hmp = hmp;
107 trans->rootvol = hammer_get_root_volume(hmp, &error);
108 KKASSERT(error == 0);
109 trans->tid = hammer_alloc_tid(hmp, 1);
110 trans->sync_lock_refs = 1;
111 trans->flags = 0;
113 getmicrotime(&tv);
114 trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
115 trans->time32 = (u_int32_t)tv.tv_sec;
118 void
119 hammer_done_transaction(struct hammer_transaction *trans)
121 int expected_lock_refs;
123 hammer_rel_volume(trans->rootvol, 0);
124 trans->rootvol = NULL;
125 expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0;
126 KKASSERT(trans->sync_lock_refs == expected_lock_refs);
127 trans->sync_lock_refs = 0;
128 if (trans->flags & HAMMER_TRANSF_NEWINODE)
129 hammer_inode_waitreclaims(trans->hmp);
133 * Allocate (count) TIDs. If running in multi-master mode the returned
134 * base will be aligned to a 16-count plus the master id (0-15).
135 * Multi-master mode allows non-conflicting to run and new objects to be
136 * created on multiple masters in parallel. The transaction id identifies
137 * the original master. The object_id is also subject to this rule in
138 * order to allow objects to be created on multiple masters in parallel.
140 * Directories may pre-allocate a large number of object ids (100,000).
142 * NOTE: There is no longer a requirement that successive transaction
143 * ids be 2 apart for separator generation.
145 static hammer_tid_t
146 hammer_alloc_tid(hammer_mount_t hmp, int count)
148 hammer_tid_t tid;
150 if (hmp->master_id < 0) {
151 tid = hmp->next_tid + 1;
152 hmp->next_tid = tid + count;
153 } else {
154 tid = (hmp->next_tid + HAMMER_MAX_MASTERS) &
155 ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1);
156 hmp->next_tid = tid + count * HAMMER_MAX_MASTERS;
157 tid |= hmp->master_id;
159 if (tid >= 0xFFFFFFFFFF000000ULL)
160 panic("hammer_start_transaction: Ran out of TIDs!");
161 if (hammer_debug_tid)
162 kprintf("alloc_tid %016llx\n", tid);
163 return(tid);
167 * Allocate an object id
169 hammer_tid_t
170 hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip)
172 hammer_objid_cache_t ocp;
173 hammer_tid_t tid;
175 while ((ocp = dip->objid_cache) == NULL) {
176 if (hmp->objid_cache_count < OBJID_CACHE_SIZE) {
177 ocp = kmalloc(sizeof(*ocp), M_HAMMER, M_WAITOK|M_ZERO);
178 ocp->next_tid = hammer_alloc_tid(hmp, OBJID_CACHE_BULK);
179 ocp->count = OBJID_CACHE_BULK;
180 TAILQ_INSERT_HEAD(&hmp->objid_cache_list, ocp, entry);
181 ++hmp->objid_cache_count;
182 /* may have blocked, recheck */
183 if (dip->objid_cache == NULL) {
184 dip->objid_cache = ocp;
185 ocp->dip = dip;
187 } else {
188 ocp = TAILQ_FIRST(&hmp->objid_cache_list);
189 if (ocp->dip)
190 ocp->dip->objid_cache = NULL;
191 dip->objid_cache = ocp;
192 ocp->dip = dip;
195 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
198 * The TID is incremented by 1 or by 16 depending what mode the
199 * mount is operating in.
201 tid = ocp->next_tid;
202 ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS;
204 if (--ocp->count == 0) {
205 dip->objid_cache = NULL;
206 --hmp->objid_cache_count;
207 ocp->dip = NULL;
208 kfree(ocp, M_HAMMER);
209 } else {
210 TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry);
212 return(tid);
215 void
216 hammer_clear_objid(hammer_inode_t dip)
218 hammer_objid_cache_t ocp;
220 if ((ocp = dip->objid_cache) != NULL) {
221 dip->objid_cache = NULL;
222 ocp->dip = NULL;
223 TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
224 TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
228 void
229 hammer_destroy_objid_cache(hammer_mount_t hmp)
231 hammer_objid_cache_t ocp;
233 while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
234 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
235 if (ocp->dip)
236 ocp->dip->objid_cache = NULL;
237 kfree(ocp, M_HAMMER);