166325755SMatthew Dillon /*
266325755SMatthew Dillon  * Copyright (c) 2007 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  *
34*36f82b23SMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.10 2008/03/19 20:18:17 dillon Exp $
3566325755SMatthew Dillon  */
3666325755SMatthew Dillon 
3766325755SMatthew Dillon #include "hammer.h"
3866325755SMatthew Dillon 
3966325755SMatthew Dillon void
408cd0a023SMatthew Dillon hammer_start_transaction(struct hammer_transaction *trans,
418cd0a023SMatthew Dillon 			 struct hammer_mount *hmp)
4266325755SMatthew Dillon {
43a89aec1bSMatthew Dillon 	int error;
4466325755SMatthew Dillon 
4566325755SMatthew Dillon 	trans->hmp = hmp;
46a89aec1bSMatthew Dillon 	trans->rootvol = hammer_get_root_volume(hmp, &error);
47a89aec1bSMatthew Dillon 	KKASSERT(error == 0);
48a89aec1bSMatthew Dillon 	trans->tid = hammer_alloc_tid(trans);
4966325755SMatthew Dillon }
5066325755SMatthew Dillon 
5166325755SMatthew Dillon void
52*36f82b23SMatthew Dillon hammer_simple_transaction(struct hammer_transaction *trans,
53*36f82b23SMatthew Dillon 			  struct hammer_mount *hmp)
54*36f82b23SMatthew Dillon {
55*36f82b23SMatthew Dillon 	int error;
56*36f82b23SMatthew Dillon 
57*36f82b23SMatthew Dillon 	trans->hmp = hmp;
58*36f82b23SMatthew Dillon 	trans->rootvol = hammer_get_root_volume(hmp, &error);
59*36f82b23SMatthew Dillon 	trans->tid = 0;
60*36f82b23SMatthew Dillon 	KKASSERT(error == 0);
61*36f82b23SMatthew Dillon }
62*36f82b23SMatthew Dillon 
63*36f82b23SMatthew Dillon void
64d113fda1SMatthew Dillon hammer_start_transaction_tid(struct hammer_transaction *trans,
65d113fda1SMatthew Dillon 			     struct hammer_mount *hmp, hammer_tid_t tid)
66d113fda1SMatthew Dillon {
67d113fda1SMatthew Dillon 	int error;
68d113fda1SMatthew Dillon 
69d113fda1SMatthew Dillon 	trans->hmp = hmp;
70d113fda1SMatthew Dillon 	trans->rootvol = hammer_get_root_volume(hmp, &error);
71d113fda1SMatthew Dillon 	KKASSERT(error == 0);
72d113fda1SMatthew Dillon 	trans->tid = tid;
73d113fda1SMatthew Dillon }
74d113fda1SMatthew Dillon 
75d113fda1SMatthew Dillon void
7666325755SMatthew Dillon hammer_abort_transaction(struct hammer_transaction *trans)
7766325755SMatthew Dillon {
78a89aec1bSMatthew Dillon 	hammer_rel_volume(trans->rootvol, 0);
7966325755SMatthew Dillon }
8066325755SMatthew Dillon 
8166325755SMatthew Dillon void
8266325755SMatthew Dillon hammer_commit_transaction(struct hammer_transaction *trans)
8366325755SMatthew Dillon {
84a89aec1bSMatthew Dillon 	hammer_rel_volume(trans->rootvol, 0);
8566325755SMatthew Dillon }
8666325755SMatthew Dillon 
87d113fda1SMatthew Dillon /*
88d113fda1SMatthew Dillon  * Note: Successive transaction ids must be at least 2 apart so the
89d113fda1SMatthew Dillon  * B-Tree code can make a separator that does not match either the
90d113fda1SMatthew Dillon  * left or right hand sides.
91d113fda1SMatthew Dillon  */
92a89aec1bSMatthew Dillon hammer_tid_t
93a89aec1bSMatthew Dillon hammer_alloc_tid(hammer_transaction_t trans)
94a89aec1bSMatthew Dillon {
95a89aec1bSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
96a89aec1bSMatthew Dillon 	struct timespec ts;
97a89aec1bSMatthew Dillon 	hammer_tid_t tid;
98a89aec1bSMatthew Dillon 
99a89aec1bSMatthew Dillon 	getnanotime(&ts);
100a89aec1bSMatthew Dillon 	tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
101*36f82b23SMatthew Dillon 	hammer_modify_volume(trans, trans->rootvol, NULL, 0);
102a89aec1bSMatthew Dillon 	ondisk = trans->rootvol->ondisk;
10347197d71SMatthew Dillon 	if (tid < ondisk->vol0_next_tid)
10447197d71SMatthew Dillon 		tid = ondisk->vol0_next_tid;
105a89aec1bSMatthew Dillon 	if (tid == 0xFFFFFFFFFFFFFFFFULL)
106a89aec1bSMatthew Dillon 		panic("hammer_start_transaction: Ran out of TIDs!");
107d113fda1SMatthew Dillon 	if (hammer_debug_tid) {
108d113fda1SMatthew Dillon 		kprintf("alloc_tid %016llx (0x%08x)\n",
109d113fda1SMatthew Dillon 			tid, (int)(tid / 1000000000LL));
110d113fda1SMatthew Dillon 	}
11147197d71SMatthew Dillon 	ondisk->vol0_next_tid = tid + 2;
112a89aec1bSMatthew Dillon 
113a89aec1bSMatthew Dillon 	return(tid);
114a89aec1bSMatthew Dillon }
115a89aec1bSMatthew Dillon 
116