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