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*a89aec1bSMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.3 2007/11/20 07:16:28 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 {
43*a89aec1bSMatthew Dillon 	int error;
4466325755SMatthew Dillon 
4566325755SMatthew Dillon 	trans->hmp = hmp;
46*a89aec1bSMatthew Dillon 	trans->rootvol = hammer_get_root_volume(hmp, &error);
47*a89aec1bSMatthew Dillon 	KKASSERT(error == 0);
48*a89aec1bSMatthew Dillon 	trans->tid = hammer_alloc_tid(trans);
4966325755SMatthew Dillon }
5066325755SMatthew Dillon 
5166325755SMatthew Dillon void
5266325755SMatthew Dillon hammer_abort_transaction(struct hammer_transaction *trans)
5366325755SMatthew Dillon {
54*a89aec1bSMatthew Dillon 	hammer_rel_volume(trans->rootvol, 0);
5566325755SMatthew Dillon 	KKASSERT(0);
5666325755SMatthew Dillon }
5766325755SMatthew Dillon 
5866325755SMatthew Dillon void
5966325755SMatthew Dillon hammer_commit_transaction(struct hammer_transaction *trans)
6066325755SMatthew Dillon {
61*a89aec1bSMatthew Dillon 	hammer_rel_volume(trans->rootvol, 0);
6266325755SMatthew Dillon }
6366325755SMatthew Dillon 
64*a89aec1bSMatthew Dillon hammer_tid_t
65*a89aec1bSMatthew Dillon hammer_alloc_tid(hammer_transaction_t trans)
66*a89aec1bSMatthew Dillon {
67*a89aec1bSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
68*a89aec1bSMatthew Dillon 	struct timespec ts;
69*a89aec1bSMatthew Dillon 	hammer_tid_t tid;
70*a89aec1bSMatthew Dillon 
71*a89aec1bSMatthew Dillon 	getnanotime(&ts);
72*a89aec1bSMatthew Dillon 	tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
73*a89aec1bSMatthew Dillon 	ondisk = trans->rootvol->ondisk;
74*a89aec1bSMatthew Dillon 	if (tid < ondisk->vol0_nexttid)
75*a89aec1bSMatthew Dillon 		tid = ondisk->vol0_nexttid;
76*a89aec1bSMatthew Dillon 	if (tid == 0xFFFFFFFFFFFFFFFFULL)
77*a89aec1bSMatthew Dillon 		panic("hammer_start_transaction: Ran out of TIDs!");
78*a89aec1bSMatthew Dillon 	ondisk->vol0_nexttid = tid + 1;
79*a89aec1bSMatthew Dillon 	hammer_modify_volume(trans->rootvol);
80*a89aec1bSMatthew Dillon 
81*a89aec1bSMatthew Dillon 	return(tid);
82*a89aec1bSMatthew Dillon }
83*a89aec1bSMatthew Dillon 
84*a89aec1bSMatthew Dillon hammer_tid_t
85*a89aec1bSMatthew Dillon hammer_alloc_recid(hammer_transaction_t trans)
86*a89aec1bSMatthew Dillon {
87*a89aec1bSMatthew Dillon 	hammer_volume_ondisk_t ondisk;
88*a89aec1bSMatthew Dillon 	hammer_tid_t recid;
89*a89aec1bSMatthew Dillon 
90*a89aec1bSMatthew Dillon 	ondisk = trans->rootvol->ondisk;
91*a89aec1bSMatthew Dillon 	recid = ++ondisk->vol0_recid;
92*a89aec1bSMatthew Dillon 	hammer_modify_volume(trans->rootvol);
93*a89aec1bSMatthew Dillon 	return(recid);
94*a89aec1bSMatthew Dillon }
95