HAMMER 56D/Many: Media structure finalization, atime/mtime, etc.
[dragonfly.git] / sys / vfs / hammer / hammer_transaction.c
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:
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  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_transaction.c,v 1.19 2008/06/20 21:24:53 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static hammer_tid_t hammer_alloc_tid(hammer_transaction_t trans, int count);
40
41
42 /*
43  * Start a standard transaction.
44  */
45 void
46 hammer_start_transaction(struct hammer_transaction *trans,
47                          struct hammer_mount *hmp)
48 {
49         struct timeval tv;
50         int error;
51
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
59         getmicrotime(&tv);
60         trans->time = (unsigned long)tv.tv_sec * 1000000ULL +
61                       tv.tv_usec;
62 }
63
64 /*
65  * Start a simple read-only transaction.  This will not stall.
66  */
67 void
68 hammer_simple_transaction(struct hammer_transaction *trans,
69                           struct hammer_mount *hmp)
70 {
71         struct timeval tv;
72         int error;
73
74         trans->type = HAMMER_TRANS_RO;
75         trans->hmp = hmp;
76         trans->rootvol = hammer_get_root_volume(hmp, &error);
77         KKASSERT(error == 0);
78         trans->tid = 0;
79         trans->sync_lock_refs = 0;
80
81         getmicrotime(&tv);
82         trans->time = (unsigned long)tv.tv_sec * 1000000ULL +
83                       tv.tv_usec;
84 }
85
86 /*
87  * Start a transaction using a particular TID.  Used by the sync code.
88  * This does not stall.
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).
93  */
94 void
95 hammer_start_transaction_fls(struct hammer_transaction *trans,
96                              struct hammer_mount *hmp)
97 {
98         struct timeval tv;
99         int error;
100
101         bzero(trans, sizeof(*trans));
102
103         trans->type = HAMMER_TRANS_FLS;
104         trans->hmp = hmp;
105         trans->rootvol = hammer_get_root_volume(hmp, &error);
106         KKASSERT(error == 0);
107         trans->tid = hammer_alloc_tid(trans, 1);
108         trans->sync_lock_refs = 1;
109
110         getmicrotime(&tv);
111         trans->time = (unsigned long)tv.tv_sec * 1000000ULL +
112                       tv.tv_usec;
113 }
114
115 void
116 hammer_done_transaction(struct hammer_transaction *trans)
117 {
118         int expected_lock_refs;
119
120         hammer_rel_volume(trans->rootvol, 0);
121         trans->rootvol = NULL;
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;
125 }
126
127 /*
128  * Note: Successive transaction ids must be at least 2 apart so the
129  * B-Tree code can make a separator that does not match either the
130  * left or right hand sides.
131  */
132 static hammer_tid_t
133 hammer_alloc_tid(hammer_transaction_t trans, int count)
134 {
135 #if 0
136         struct timespec ts;
137 #endif
138         hammer_tid_t tid;
139
140 #if 0
141         getnanotime(&ts);
142 #endif
143         tid = time_second * 1000000000LL;
144 #if 0
145         tid = ts.tv_sec * 1000000000LL + ts.tv_nsec;
146 #endif
147         if (tid < trans->hmp->next_tid)
148                 tid = trans->hmp->next_tid;
149         if (tid >= 0xFFFFFFFFFFFFF000ULL)
150                 panic("hammer_start_transaction: Ran out of TIDs!");
151         trans->hmp->next_tid = tid + count * 2;
152         if (hammer_debug_tid) {
153                 kprintf("alloc_tid %016llx (0x%08x)\n",
154                         tid, (int)(tid / 1000000000LL));
155         }
156         return(tid);
157 }
158
159 /*
160  * Allocate an object id
161  */
162 hammer_tid_t
163 hammer_alloc_objid(hammer_transaction_t trans, hammer_inode_t dip)
164 {
165         hammer_objid_cache_t ocp;
166         hammer_tid_t tid;
167
168         while ((ocp = dip->objid_cache) == NULL) {
169                 if (trans->hmp->objid_cache_count < OBJID_CACHE_SIZE) {
170                         ocp = kmalloc(sizeof(*ocp), M_HAMMER, M_WAITOK|M_ZERO);
171                         ocp->next_tid = hammer_alloc_tid(trans,
172                                                          OBJID_CACHE_BULK);
173                         ocp->count = OBJID_CACHE_BULK;
174                         TAILQ_INSERT_HEAD(&trans->hmp->objid_cache_list, ocp,
175                                           entry);
176                         ++trans->hmp->objid_cache_count;
177                         /* may have blocked, recheck */
178                         if (dip->objid_cache == NULL) {
179                                 dip->objid_cache = ocp;
180                                 ocp->dip = dip;
181                         }
182                 } else {
183                         ocp = TAILQ_FIRST(&trans->hmp->objid_cache_list);
184                         if (ocp->dip)
185                                 ocp->dip->objid_cache = NULL;
186                         dip->objid_cache = ocp;
187                         ocp->dip = dip;
188                 }
189         }
190         TAILQ_REMOVE(&trans->hmp->objid_cache_list, ocp, entry);
191         tid = ocp->next_tid;
192         ocp->next_tid += 2;
193         if (--ocp->count == 0) {
194                 dip->objid_cache = NULL;
195                 --trans->hmp->objid_cache_count;
196                 ocp->dip = NULL;
197                 kfree(ocp, M_HAMMER);
198         } else {
199                 TAILQ_INSERT_TAIL(&trans->hmp->objid_cache_list, ocp, entry);
200         }
201         return(tid);
202 }
203
204 void
205 hammer_clear_objid(hammer_inode_t dip)
206 {
207         hammer_objid_cache_t ocp;
208
209         if ((ocp = dip->objid_cache) != NULL) {
210                 dip->objid_cache = NULL;
211                 ocp->dip = NULL;
212                 TAILQ_REMOVE(&dip->hmp->objid_cache_list, ocp, entry);
213                 TAILQ_INSERT_HEAD(&dip->hmp->objid_cache_list, ocp, entry);
214         }
215 }
216
217 void
218 hammer_destroy_objid_cache(hammer_mount_t hmp)
219 {
220         hammer_objid_cache_t ocp;
221
222         while ((ocp = TAILQ_FIRST(&hmp->objid_cache_list)) != NULL) {
223                 TAILQ_REMOVE(&hmp->objid_cache_list, ocp, entry);
224                 kfree(ocp, M_HAMMER);
225         }
226 }
227