Pre-2.0 release: Sync with HAMMER 64 - simplify PFS operations, fix pfs-upgrade
[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 *
ac14cb85 34 * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.22.2.2 2008/07/19 18:46:20 dillon Exp $
66325755
MD
35 */
36
37#include "hammer.h"
38
5de0c0e5 39static hammer_tid_t hammer_alloc_tid(hammer_mount_t hmp, int count);
0729c8c8
MD
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{
ddfdf542 49 struct timeval tv;
a89aec1b 50 int error;
66325755 51
b84de5af 52 trans->type = HAMMER_TRANS_STD;
66325755 53 trans->hmp = hmp;
a89aec1b
MD
54 trans->rootvol = hammer_get_root_volume(hmp, &error);
55 KKASSERT(error == 0);
b84de5af 56 trans->tid = 0;
2f85fa4d 57 trans->sync_lock_refs = 0;
ddfdf542
MD
58
59 getmicrotime(&tv);
dd94f1b1
MD
60 trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
61 trans->time32 = (u_int32_t)tv.tv_sec;
66325755
MD
62}
63
b84de5af
MD
64/*
65 * Start a simple read-only transaction. This will not stall.
66 */
36f82b23
MD
67void
68hammer_simple_transaction(struct hammer_transaction *trans,
69 struct hammer_mount *hmp)
70{
ddfdf542 71 struct timeval tv;
36f82b23
MD
72 int error;
73
b84de5af 74 trans->type = HAMMER_TRANS_RO;
36f82b23
MD
75 trans->hmp = hmp;
76 trans->rootvol = hammer_get_root_volume(hmp, &error);
36f82b23 77 KKASSERT(error == 0);
b84de5af 78 trans->tid = 0;
2f85fa4d 79 trans->sync_lock_refs = 0;
ddfdf542
MD
80
81 getmicrotime(&tv);
dd94f1b1
MD
82 trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
83 trans->time32 = (u_int32_t)tv.tv_sec;
36f82b23
MD
84}
85
b84de5af
MD
86/*
87 * Start a transaction using a particular TID. Used by the sync code.
88 * This does not stall.
2f85fa4d
MD
89 *
90 * This routine may only be called from the flusher thread. We predispose
91 * sync_lock_refs, implying serialization against the synchronization stage
92 * (which the flusher is responsible for).
b84de5af 93 */
d113fda1 94void
b84de5af
MD
95hammer_start_transaction_fls(struct hammer_transaction *trans,
96 struct hammer_mount *hmp)
d113fda1 97{
ddfdf542 98 struct timeval tv;
d113fda1
MD
99 int error;
100
9f5097dc
MD
101 bzero(trans, sizeof(*trans));
102
b84de5af 103 trans->type = HAMMER_TRANS_FLS;
d113fda1
MD
104 trans->hmp = hmp;
105 trans->rootvol = hammer_get_root_volume(hmp, &error);
106 KKASSERT(error == 0);
5de0c0e5 107 trans->tid = hammer_alloc_tid(hmp, 1);
2f85fa4d 108 trans->sync_lock_refs = 1;
ddfdf542
MD
109
110 getmicrotime(&tv);
dd94f1b1
MD
111 trans->time = (unsigned long)tv.tv_sec * 1000000ULL + tv.tv_usec;
112 trans->time32 = (u_int32_t)tv.tv_sec;
66325755
MD
113}
114
115void
b84de5af 116hammer_done_transaction(struct hammer_transaction *trans)
66325755 117{
2f85fa4d
MD
118 int expected_lock_refs;
119
a89aec1b 120 hammer_rel_volume(trans->rootvol, 0);
b84de5af 121 trans->rootvol = NULL;
2f85fa4d
MD
122 expected_lock_refs = (trans->type == HAMMER_TRANS_FLS) ? 1 : 0;
123 KKASSERT(trans->sync_lock_refs == expected_lock_refs);
124 trans->sync_lock_refs = 0;
66325755
MD
125}
126
d113fda1 127/*
5de0c0e5
MD
128 * Allocate (count) TIDs. If running in multi-master mode the returned
129 * base will be aligned to a 16-count plus the master id (0-15).
130 * Multi-master mode allows non-conflicting to run and new objects to be
131 * created on multiple masters in parallel. The transaction id identifies
132 * the original master. The object_id is also subject to this rule in
133 * order to allow objects to be created on multiple masters in parallel.
134 *
135 * Directories may pre-allocate a large number of object ids (100,000).
136 *
137 * NOTE: There is no longer a requirement that successive transaction
138 * ids be 2 apart for separator generation.
d113fda1 139 */
0729c8c8 140static hammer_tid_t
5de0c0e5 141hammer_alloc_tid(hammer_mount_t hmp, int count)
a89aec1b 142{
a89aec1b
MD
143 hammer_tid_t tid;
144
ac14cb85 145 if (hmp->master_id < 0) {
c82af904
MD
146 tid = hmp->next_tid + 1;
147 hmp->next_tid = tid + count;
148 } else {
149 tid = (hmp->next_tid + HAMMER_MAX_MASTERS) &
150 ~(hammer_tid_t)(HAMMER_MAX_MASTERS - 1);
151 hmp->next_tid = tid + count * HAMMER_MAX_MASTERS;
ac14cb85 152 tid |= hmp->master_id;
c82af904
MD
153 }
154 if (tid >= 0xFFFFFFFFFF000000ULL)
a89aec1b 155 panic("hammer_start_transaction: Ran out of TIDs!");
5de0c0e5
MD
156 if (hammer_debug_tid)
157 kprintf("alloc_tid %016llx\n", tid);
a89aec1b
MD
158 return(tid);
159}
160
0729c8c8
MD
161/*
162 * Allocate an object id
163 */
164hammer_tid_t
5de0c0e5 165hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip)
0729c8c8
MD
166{
167 hammer_objid_cache_t ocp;
168 hammer_tid_t tid;
169
170 while ((ocp = dip->objid_cache) == NULL) {
5de0c0e5 171 if (hmp->objid_cache_count < OBJID_CACHE_SIZE) {
0729c8c8 172 ocp = kmalloc(sizeof(*ocp), M_HAMMER, M_WAITOK|M_ZERO);
5de0c0e5 173 ocp->next_tid = hammer_alloc_tid(hmp, OBJID_CACHE_BULK);
0729c8c8 174 ocp->count = OBJID_CACHE_BULK;
5de0c0e5
MD
175 TAILQ_INSERT_HEAD(&hmp->objid_cache_list, ocp, entry);
176 ++hmp->objid_cache_count;
0729c8c8
MD
177 /* may have blocked, recheck */
178 if (dip->objid_cache == NULL) {
179 dip->objid_cache = ocp;
180 ocp->dip = dip;
181 }
182 } else {
5de0c0e5 183 ocp = TAILQ_FIRST(&hmp->objid_cache_list);
0729c8c8
MD
184 if (ocp->dip)
185 ocp->dip->objid_cache = NULL;
186 dip->objid_cache = ocp;
187 ocp->dip = dip;
188 }
189 }
5de0c0e5
MD
190 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
191
192 /*
193 * The TID is incremented by 1 or by 16 depending what mode the
194 * mount is operating in.
195 */
0729c8c8 196 tid = ocp->next_tid;
ac14cb85 197 ocp->next_tid += (hmp->master_id < 0) ? 1 : HAMMER_MAX_MASTERS;
5de0c0e5 198
0729c8c8
MD
199 if (--ocp->count == 0) {
200 dip->objid_cache = NULL;
5de0c0e5 201 --hmp->objid_cache_count;
0729c8c8
MD
202 ocp->dip = NULL;
203 kfree(ocp, M_HAMMER);
204 } else {
5de0c0e5 205 TAILQ_INSERT_TAIL(&hmp->objid_cache_list, ocp, entry);
0729c8c8
MD
206 }
207 return(tid);
208}
209
210void
211hammer_clear_objid(hammer_inode_t dip)
212{
213 hammer_objid_cache_t ocp;
214
215 if ((ocp = dip->objid_cache) != NULL) {
216 dip->objid_cache = NULL;
217 ocp->dip = NULL;
218 TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
219 TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
220 }
221}
222
223void
224hammer_destroy_objid_cache(hammer_mount_t hmp)
225{
226 hammer_objid_cache_t ocp;
227
228 while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
229 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
8c585a28
MD
230 if (ocp->dip)
231 ocp->dip->objid_cache = NULL;
0729c8c8
MD
232 kfree(ocp, M_HAMMER);
233 }
234}
235