Further rationalise our usage of assert()
[openafs.git] / src / volser / voltrans.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  *  Module:         Voltrans.c
12  *  System:         Volser
13  *  Instituition:           ITC, CMU
14  *  Date:                   December, 88
15  */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 #include <roken.h>
21
22 #include <afs/opr.h>
23
24 #ifdef AFS_NT40_ENV
25 #include <afs/afsutil.h>
26 #endif
27
28 #include <afs/afsint.h>
29 #include <afs/prs_fs.h>
30 #include <afs/nfs.h>
31 #include <lwp.h>
32 #include <lock.h>
33 #include <afs/cellconfig.h>
34 #include <afs/keys.h>
35 #include <rx/rx.h>
36 #include <ubik.h>
37 #include <afs/ihandle.h>
38 #ifdef AFS_NT40_ENV
39 #include <afs/ntops.h>
40 #endif
41 #include <afs/vnode.h>
42 #include <afs/volume.h>
43
44 #include "volint.h"
45 #include "volser.h"
46 #include "volser_internal.h"
47
48 static struct volser_trans *allTrans = 0;
49 static afs_int32 transCounter = 1;
50
51 /* create a new transaction, returning ptr to same with high ref count */
52 struct volser_trans *
53 NewTrans(afs_uint32 avol, afs_int32 apart)
54 {
55     /* set volid, next, partition */
56     struct volser_trans *tt, *newtt;
57     struct timeval tp;
58
59     newtt = calloc(1, sizeof(struct volser_trans));
60     VTRANS_LOCK;
61     /* don't allow the same volume to be attached twice */
62     for (tt = allTrans; tt; tt = tt->next) {
63         if ((tt->volid == avol) && (tt->partition == apart)) {
64             VTRANS_UNLOCK;
65             free(newtt);
66             return (struct volser_trans *)0;    /* volume busy */
67         }
68     }
69     tt = newtt;
70     tt->volid = avol;
71     tt->partition = apart;
72     tt->refCount = 1;
73     tt->rxCallPtr = (struct rx_call *)0;
74     strcpy(tt->lastProcName, "");
75     gettimeofday(&tp, NULL);
76     tt->creationTime = tp.tv_sec;
77     tt->time = FT_ApproxTime();
78     tt->tid = transCounter++;
79     tt->next = allTrans;
80     VTRANS_OBJ_LOCK_INIT(tt);
81     allTrans = tt;
82     VTRANS_UNLOCK;
83     return tt;
84 }
85
86 /* find a trans, again returning with high ref count */
87 struct volser_trans *
88 FindTrans(afs_int32 atrans)
89 {
90     struct volser_trans *tt;
91     VTRANS_LOCK;
92     for (tt = allTrans; tt; tt = tt->next) {
93         if (tt->tid == atrans) {
94             tt->time = FT_ApproxTime();
95             tt->refCount++;
96             VTRANS_UNLOCK;
97             return tt;
98         }
99     }
100     VTRANS_UNLOCK;
101     return (struct volser_trans *)0;
102 }
103
104 /* delete transaction if refcount == 1, otherwise queue delete for later.  Does implicit TRELE */
105 afs_int32
106 DeleteTrans(struct volser_trans *atrans, afs_int32 lock)
107 {
108     struct volser_trans *tt, **lt;
109     Error error;
110
111     if (lock) VTRANS_LOCK;
112     if (atrans->refCount > 1) {
113         /* someone else is using it now */
114         atrans->refCount--;
115         atrans->tflags |= TTDeleted;
116         if (lock) VTRANS_UNLOCK;
117         return 0;
118     }
119
120     /* otherwise we zap it ourselves */
121     lt = &allTrans;
122     for (tt = *lt; tt; lt = &tt->next, tt = *lt) {
123         if (tt == atrans) {
124             if (tt->volume)
125                 VDetachVolume(&error, tt->volume);
126             tt->volume = NULL;
127             if (tt->rxCallPtr)
128                 rxi_CallError(tt->rxCallPtr, RX_CALL_DEAD);
129             *lt = tt->next;
130             VTRANS_OBJ_LOCK_DESTROY(tt);
131             free(tt);
132             if (lock) VTRANS_UNLOCK;
133             return 0;
134         }
135     }
136     if (lock) VTRANS_UNLOCK;
137     return -1;                  /* failed to find the transaction in the generic list */
138 }
139
140 /* THOLD is a macro defined in volser.h */
141
142 /* put a transaction back */
143 afs_int32
144 TRELE(struct volser_trans *at)
145 {
146     VTRANS_LOCK;
147     if (at->refCount == 0) {
148         Log("TRELE: bad refcount\n");
149         VTRANS_UNLOCK;
150         return VOLSERTRELE_ERROR;
151     }
152
153     at->time = FT_ApproxTime(); /* we're still using it */
154     if (at->refCount == 1 && (at->tflags & TTDeleted)) {
155         DeleteTrans(at, 0);
156         VTRANS_UNLOCK;
157         return 0;
158     }
159     /* otherwise simply drop refcount */
160     at->refCount--;
161     VTRANS_UNLOCK;
162     return 0;
163 }
164
165 /* look for old transactions and delete them */
166 #define OLDTRANSTIME        600 /* seconds */
167 #define OLDTRANSWARN        300 /* seconds */
168 static int GCDeletes = 0;
169 afs_int32
170 GCTrans(void)
171 {
172     struct volser_trans *tt, *nt;
173     afs_int32 now;
174
175     now = FT_ApproxTime();
176
177     VTRANS_LOCK;
178     for (tt = allTrans; tt; tt = nt) {
179         nt = tt->next;          /* remember in case we zap it */
180         if (tt->time + OLDTRANSWARN < now) {
181             Log("trans %u on volume %u %s than %d seconds\n", tt->tid,
182                 tt->volid,
183                 ((tt->refCount > 0) ? "is older" : "has been idle for more"),
184                 (((now - tt->time) / GCWAKEUP) * GCWAKEUP));
185         }
186         if (tt->refCount > 0)
187             continue;
188         if (tt->time + OLDTRANSTIME < now) {
189             Log("trans %u on volume %u has timed out\n", tt->tid, tt->volid);
190
191             tt->refCount++;     /* we're using it now */
192
193             if (tt->volume && V_destroyMe(tt->volume) == DESTROY_ME
194                 && !(tt->vflags & VTDeleted)) {
195
196                 Error error;
197
198                 Log("Deleting timed-out temporary volume %lu\n",
199                     (long unsigned) tt->volid);
200
201                 VTRANS_OBJ_LOCK(tt);
202                 tt->vflags |= VTDeleted;
203                 VTRANS_OBJ_UNLOCK(tt);
204
205                 VTRANS_UNLOCK;
206                 VPurgeVolume(&error, tt->volume);
207                 VTRANS_LOCK;
208             }
209
210             DeleteTrans(tt, 0); /* drops refCount or deletes it */
211             GCDeletes++;
212         }
213     }
214     VTRANS_UNLOCK;
215     return 0;
216 }
217
218 /*return the head of the transaction list */
219 struct volser_trans *
220 TransList(void)
221 {
222     return (allTrans);
223 }