Remove the RCSID macro
[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
21 #ifdef AFS_NT40_ENV
22 #include <afs/afsutil.h>
23 #else
24 #include <sys/time.h>
25 #endif
26
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <string.h>
31 #ifdef AFS_NT40_ENV
32 #include <fcntl.h>
33 #include <winsock2.h>
34 #else
35 #include <sys/file.h>
36 #include <netinet/in.h>
37 #include <unistd.h>
38 #endif
39 #include <dirent.h>
40 #include <sys/stat.h>
41 #include <afs/afsint.h>
42 #include <signal.h>
43 #ifdef AFS_PTHREAD_ENV
44 #include <assert.h>
45 #else /* AFS_PTHREAD_ENV */
46 #include <afs/assert.h>
47 #endif /* AFS_PTHREAD_ENV */
48 #include <afs/prs_fs.h>
49 #include <afs/nfs.h>
50 #include <lwp.h>
51 #include <lock.h>
52 #include <afs/cellconfig.h>
53 #include <afs/keys.h>
54 #include <rx/rx.h>
55 #include <ubik.h>
56 #include <afs/ihandle.h>
57 #ifdef AFS_NT40_ENV
58 #include <afs/ntops.h>
59 #endif
60 #include <afs/vnode.h>
61 #include <afs/volume.h>
62
63 #include "volser.h"
64
65 /*@printflike@*/ extern void Log(const char *format, ...);
66
67 static struct volser_trans *allTrans = 0;
68 static afs_int32 transCounter = 1;
69
70 /* create a new transaction, returning ptr to same with high ref count */
71 struct volser_trans *
72 NewTrans(afs_int32 avol, afs_int32 apart)
73 {
74     /* set volid, next, partition */
75     struct volser_trans *tt, *newtt;
76     struct timeval tp;
77     struct timezone tzp;
78
79     newtt = (struct volser_trans *)malloc(sizeof(struct volser_trans));
80     VTRANS_LOCK;
81     /* don't allow the same volume to be attached twice */
82     for (tt = allTrans; tt; tt = tt->next) {
83         if ((tt->volid == avol) && (tt->partition == apart)) {
84             VTRANS_UNLOCK;
85             free(newtt);
86             return (struct volser_trans *)0;    /* volume busy */
87         }
88     }
89     tt = newtt;
90     memset(tt, 0, sizeof(struct volser_trans));
91     tt->volid = avol;
92     tt->partition = apart;
93     tt->refCount = 1;
94     tt->rxCallPtr = (struct rx_call *)0;
95     strcpy(tt->lastProcName, "");
96     gettimeofday(&tp, &tzp);
97     tt->creationTime = tp.tv_sec;
98     tt->time = FT_ApproxTime();
99     tt->tid = transCounter++;
100     tt->next = allTrans;
101     allTrans = tt;
102     VTRANS_UNLOCK;
103     return tt;
104 }
105
106 /* find a trans, again returning with high ref count */
107 struct volser_trans *
108 FindTrans(register afs_int32 atrans)
109 {
110     register struct volser_trans *tt;
111     VTRANS_LOCK;
112     for (tt = allTrans; tt; tt = tt->next) {
113         if (tt->tid == atrans) {
114             tt->time = FT_ApproxTime();
115             tt->refCount++;
116             VTRANS_UNLOCK;
117             return tt;
118         }
119     }
120     VTRANS_UNLOCK;
121     return (struct volser_trans *)0;
122 }
123
124 /* delete transaction if refcount == 1, otherwise queue delete for later.  Does implicit TRELE */
125 afs_int32 
126 DeleteTrans(register struct volser_trans *atrans, afs_int32 lock)
127 {
128     register struct volser_trans *tt, **lt;
129     afs_int32 error;
130
131     if (lock) VTRANS_LOCK;
132     if (atrans->refCount > 1) {
133         /* someone else is using it now */
134         atrans->refCount--;
135         atrans->tflags |= TTDeleted;
136         if (lock) VTRANS_UNLOCK;
137         return 0;
138     }
139
140     /* otherwise we zap it ourselves */
141     lt = &allTrans;
142     for (tt = *lt; tt; lt = &tt->next, tt = *lt) {
143         if (tt == atrans) {
144             if (tt->volume)
145                 VDetachVolume(&error, tt->volume);
146             tt->volume = NULL;
147             if (tt->rxCallPtr)
148                 rxi_CallError(tt->rxCallPtr, RX_CALL_DEAD);
149             *lt = tt->next;
150             free(tt);
151             if (lock) VTRANS_UNLOCK;
152             return 0;
153         }
154     }
155     if (lock) VTRANS_UNLOCK;
156     return -1;                  /* failed to find the transaction in the generic list */
157 }
158
159 /* THOLD is a macro defined in volser.h */
160
161 /* put a transaction back */
162 afs_int32 
163 TRELE(register struct volser_trans *at)
164 {
165     VTRANS_LOCK;
166     if (at->refCount == 0) {
167         Log("TRELE: bad refcount\n");
168         VTRANS_UNLOCK;
169         return VOLSERTRELE_ERROR;
170     }
171
172     at->time = FT_ApproxTime(); /* we're still using it */
173     if (at->refCount == 1 && (at->tflags & TTDeleted)) {
174         DeleteTrans(at, 0);
175         VTRANS_UNLOCK;
176         return 0;
177     }
178     /* otherwise simply drop refcount */
179     at->refCount--;
180     VTRANS_UNLOCK;
181     return 0;
182 }
183
184 /* look for old transactions and delete them */
185 #define OLDTRANSTIME        600 /* seconds */
186 #define OLDTRANSWARN        300 /* seconds */
187 static int GCDeletes = 0;
188 afs_int32
189 GCTrans()
190 {
191     register struct volser_trans *tt, *nt;
192     afs_int32 now;
193
194     now = FT_ApproxTime();
195
196     VTRANS_LOCK;
197     for (tt = allTrans; tt; tt = nt) {
198         nt = tt->next;          /* remember in case we zap it */
199         if (tt->time + OLDTRANSWARN < now) {
200             Log("trans %u on volume %u %s than %d seconds\n", tt->tid,
201                 tt->volid,
202                 ((tt->refCount > 0) ? "is older" : "has been idle for more"),
203                 (((now - tt->time) / GCWAKEUP) * GCWAKEUP));
204         }
205         if (tt->refCount > 0)
206             continue;
207         if (tt->time + OLDTRANSTIME < now) {
208             Log("trans %u on volume %u has timed out\n", tt->tid, tt->volid);
209             tt->refCount++;     /* we're using it now */
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()
221 {
222     return (allTrans);
223 }