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*d113fda1SMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.6 2008/01/01 01:00:03 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*d113fda1SMatthew Dillon hammer_start_transaction_tid(struct hammer_transaction *trans,
53*d113fda1SMatthew Dillon 			     struct hammer_mount *hmp, hammer_tid_t tid)
54*d113fda1SMatthew Dillon {
55*d113fda1SMatthew Dillon 	int error;
56*d113fda1SMatthew Dillon 
57*d113fda1SMatthew Dillon 	trans->hmp = hmp;
58*d113fda1SMatthew Dillon 	trans->rootvol = hammer_get_root_volume(hmp, &error);
59*d113fda1SMatthew Dillon 	KKASSERT(error == 0);
60*d113fda1SMatthew Dillon 	trans->tid = tid;
61*d113fda1SMatthew Dillon }
62*d113fda1SMatthew Dillon 
63*d113fda1SMatthew Dillon void
6466325755SMatthew Dillon hammer_abort_transaction(struct hammer_transaction *trans)
6566325755SMatthew Dillon {
66a89aec1bSMatthew Dillon 	hammer_rel_volume(trans->rootvol, 0);
6766325755SMatthew Dillon }
6866325755SMatthew Dillon 
6966325755SMatthew Dillon void
7066325755SMatthew Dillon hammer_commit_transaction(struct hammer_transaction *trans)
7166325755SMatthew Dillon {
72a89aec1bSMatthew Dillon 	hammer_rel_volume(trans->rootvol, 0);
7366325755SMatthew Dillon }
7466325755SMatthew Dillon 
75*d113fda1SMatthew Dillon /*
76*d113fda1SMatthew Dillon  * Note: Successive transaction ids must be at least 2 apart so the
77*d113fda1SMatthew Dillon  * B-Tree code can make a separator that does not match either the
78*d113fda1SMatthew Dillon  * left or right hand sides.
79*d113fda1SMatthew Dillon  */
80a89aec1bSMatthew Dillon hammer_tid_t
81a89aec1bSMatthew Dillon hammer_alloc_tid(hammer_transaction_t trans)
82a89aec1bSMatthew Dillon {
83a89aec1bSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
84a89aec1bSMatthew Dillon 	struct timespec ts;
85a89aec1bSMatthew Dillon 	hammer_tid_t tid;
86a89aec1bSMatthew Dillon 
87a89aec1bSMatthew Dillon 	getnanotime(&ts);
88a89aec1bSMatthew Dillon 	tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
890b075555SMatthew Dillon 	hammer_modify_volume(trans->rootvol);
90a89aec1bSMatthew Dillon 	ondisk = trans->rootvol->ondisk;
91a89aec1bSMatthew Dillon 	if (tid < ondisk->vol0_nexttid)
92a89aec1bSMatthew Dillon 		tid = ondisk->vol0_nexttid;
93a89aec1bSMatthew Dillon 	if (tid == 0xFFFFFFFFFFFFFFFFULL)
94a89aec1bSMatthew Dillon 		panic("hammer_start_transaction: Ran out of TIDs!");
95*d113fda1SMatthew Dillon 	if (hammer_debug_tid) {
96*d113fda1SMatthew Dillon 		kprintf("alloc_tid %016llx (0x%08x)\n",
97*d113fda1SMatthew Dillon 			tid, (int)(tid / 1000000000LL));
98*d113fda1SMatthew Dillon 	}
99*d113fda1SMatthew Dillon 	ondisk->vol0_nexttid = tid + 2;
1000b075555SMatthew Dillon 	hammer_modify_volume_done(trans->rootvol);
101a89aec1bSMatthew Dillon 
102a89aec1bSMatthew Dillon 	return(tid);
103a89aec1bSMatthew Dillon }
104a89aec1bSMatthew Dillon 
105a89aec1bSMatthew Dillon hammer_tid_t
106a89aec1bSMatthew Dillon hammer_alloc_recid(hammer_transaction_t trans)
107a89aec1bSMatthew Dillon {
108a89aec1bSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
109a89aec1bSMatthew Dillon 	hammer_tid_t recid;
110a89aec1bSMatthew Dillon 
1110b075555SMatthew Dillon 	hammer_modify_volume(trans->rootvol);
112a89aec1bSMatthew Dillon 	ondisk = trans->rootvol->ondisk;
113a89aec1bSMatthew Dillon 	recid = ++ondisk->vol0_recid;
1140b075555SMatthew Dillon 	hammer_modify_volume_done(trans->rootvol);
115a89aec1bSMatthew Dillon 	return(recid);
116a89aec1bSMatthew Dillon }
117