fix-linux-atomic-t-lossage-by-macroizing-vnode-refcounts-20011008
[openafs.git] / src / afs / VNOPS / afs_vnop_remove.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  * Implements:
12  * afs_Wire (DUX)
13  * FetchWholeEnchilada
14  * afs_IsWired (DUX)
15  * afsremove
16  * afs_remove
17  *
18  * Local:
19  * newname
20  *
21  */
22 #include <afsconfig.h>
23 #include "../afs/param.h"
24
25 RCSID("$Header$");
26
27 #include "../afs/sysincludes.h" /* Standard vendor system headers */
28 #include "../afs/afsincludes.h" /* Afs-based standard headers */
29 #include "../afs/afs_stats.h" /* statistics */
30 #include "../afs/afs_cbqueue.h"
31 #include "../afs/nfsclient.h"
32 #include "../afs/afs_osidnlc.h"
33
34
35 extern afs_rwlock_t afs_xvcache;
36 extern afs_rwlock_t afs_xcbhash;
37
38
39 #ifdef  AFS_OSF_ENV
40 /*
41  *  Wire down file in cache: prefetch all data, and turn on CWired flag
42  *  so that callbacks/callback expirations are (temporarily) ignored
43  *  and cache file(s) are kept in cache. File will be unwired when
44  *  afs_inactive is called (ie no one has VN_HOLD on vnode), or when
45  *  afs_IsWired notices that the file is no longer Active.
46  */
47 afs_Wire(avc, areq)
48 #else   /* AFS_OSF_ENV */
49 static void FetchWholeEnchilada(avc, areq)
50 #endif
51 struct vrequest *areq;
52 register struct vcache *avc; {
53     register afs_int32 nextChunk, pos;
54     register struct dcache *tdc;
55     afs_int32 offset, len;
56
57     AFS_STATCNT(FetchWholeEnchilada);
58     if ((avc->states & CStatd) == 0) return;    /* don't know size */
59     for(nextChunk=0;nextChunk<1024;nextChunk++) {   /* sanity check on N chunks */
60         pos = AFS_CHUNKTOBASE(nextChunk);
61 #if     defined(AFS_OSF_ENV)
62         if (pos >= avc->m.Length) break;        /* all done */
63 #else   /* AFS_OSF_ENV */
64         if (pos >= avc->m.Length) return;       /* all done */
65 #endif
66         tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
67         if (!tdc) 
68 #if     defined(AFS_OSF_ENV)
69             break;
70 #else   /* AFS_OSF_ENV */
71             return;
72 #endif
73         afs_PutDCache(tdc);
74     }
75 #if defined(AFS_OSF_ENV)
76     avc->states |= CWired;
77 #endif  /* AFS_OSF_ENV */
78 }
79
80 #if     defined(AFS_OSF_ENV)
81 /*
82  *  Tests whether file is wired down, after unwiring the file if it
83  *  is found to be inactive (ie not open and not being paged from).
84  */
85 afs_IsWired(avc)
86     register struct vcache *avc; {
87     if (avc->states & CWired) {
88         if (osi_Active(avc)) {
89             return 1;
90         }
91         avc->states &= ~CWired;
92     }
93     return 0;
94 }
95 #endif  /* AFS_OSF_ENV */
96
97 afsremove(adp, tdc, tvc, aname, acred, treqp)
98     register struct vcache *adp;
99     register struct dcache *tdc;
100     register struct vcache *tvc;
101     char *aname;
102     struct vrequest *treqp;
103     struct AFS_UCRED *acred; {
104     register afs_int32 code;
105     register struct conn *tc;
106     afs_int32 offset, len;
107     struct AFSFetchStatus OutDirStatus;
108     struct AFSVolSync tsync;
109     XSTATS_DECLS
110
111     do {
112         tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
113         if (tc) {
114           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
115 #ifdef RX_ENABLE_LOCKS
116             AFS_GUNLOCK();
117 #endif /* RX_ENABLE_LOCKS */
118             code = RXAFS_RemoveFile(tc->id, (struct AFSFid *) &adp->fid.Fid,
119                                     aname, &OutDirStatus, &tsync);
120 #ifdef RX_ENABLE_LOCKS
121             AFS_GLOCK();
122 #endif /* RX_ENABLE_LOCKS */
123           XSTATS_END_TIME;
124       }
125         else code = -1;
126     } while
127       (afs_Analyze(tc, code, &adp->fid, treqp,
128                    AFS_STATS_FS_RPCIDX_REMOVEFILE, SHARED_LOCK, (struct cell *)0));
129
130     osi_dnlc_remove (adp, aname, tvc);
131     if (tvc) afs_symhint_inval(tvc);   /* XXX: don't really need to be so extreme */
132
133     if (code) {
134         if (tdc) afs_PutDCache(tdc);
135         if (tvc) afs_PutVCache(tvc, WRITE_LOCK);
136
137         if (code < 0) {
138           ObtainWriteLock(&afs_xcbhash, 497);
139           afs_DequeueCallback(adp);
140           adp->states &= ~CStatd;
141           ReleaseWriteLock(&afs_xcbhash);
142           osi_dnlc_purgedp(adp);
143         }
144         ReleaseWriteLock(&adp->lock);
145         code = afs_CheckCode(code, treqp, 21);
146         return code;
147     }
148     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
149         /* we can do it locally */
150         code = afs_dir_Delete(&tdc->f.inode, aname);
151         if (code) {
152             ZapDCE(tdc);        /* surprise error -- invalid value */
153             DZap(&tdc->f.inode);
154         }
155     }
156     if (tdc) {
157         afs_PutDCache(tdc);     /* drop ref count */
158     }
159     ReleaseWriteLock(&adp->lock);
160
161     /* now, get vnode for unlinked dude, and see if we should force it
162      * from cache.  adp is now the deleted files vnode.  Note that we
163      * call FindVCache instead of GetVCache since if the file's really
164      * gone, we won't be able to fetch the status info anyway.  */
165     if (tvc) {
166 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
167         afs_BozonLock(&tvc->pvnLock, tvc);      
168         /* Since afs_TryToSmush will do a pvn_vptrunc */
169 #endif
170         ObtainWriteLock(&tvc->lock,141);
171         /* note that callback will be broken on the deleted file if there are
172          * still >0 links left to it, so we'll get the stat right */
173         tvc->m.LinkCount--;
174         tvc->states &= ~CUnique;                /* For the dfs xlator */
175         if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
176             if (!AFS_NFSXLATORREQ(acred)) afs_TryToSmush(tvc, acred, 0);
177         } 
178         ReleaseWriteLock(&tvc->lock);
179 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
180         afs_BozonUnlock(&tvc->pvnLock, tvc);
181 #endif
182         afs_PutVCache(tvc, WRITE_LOCK);
183     }
184     return (0);
185 }
186
187 static char *newname() {
188     char *name, *sp, *p = ".__afs";
189     afs_int32 rd = afs_random() & 0xffff;
190
191     sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
192     while (*p != '\0') *sp++ = *p++;
193     while (rd) {
194         *sp++= "0123456789ABCDEF"[rd & 0x0f];
195         rd >>= 4;
196     }
197     *sp = '\0';
198     return (name);
199 }
200
201 /* these variables appear to exist for debugging purposes */
202 struct vcache * Tadp1, * Ttvc;
203 int Tadpr, Ttvcr;
204 char *Tnam;
205 char *Tnam1;
206
207 /* Note that we don't set CDirty here, this is OK because the unlink
208  * RPC is called synchronously */
209 #ifdef  AFS_OSF_ENV
210 afs_remove(ndp)
211     struct nameidata *ndp; {
212     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
213     char *aname = ndp->ni_dent.d_name;
214     struct ucred *acred = ndp->ni_cred;
215 #else   /* AFS_OSF_ENV */
216 afs_remove(OSI_VC_ARG(adp), aname, acred)
217     OSI_VC_DECL(adp);
218     char *aname;
219     struct AFS_UCRED *acred; {
220 #endif
221     struct vrequest treq;
222     register struct dcache *tdc;
223     struct VenusFid unlinkFid;
224     register afs_int32 code;
225     register struct vcache *tvc;
226     afs_int32 offset, len;
227     struct AFSFetchStatus OutDirStatus;
228     struct AFSVolSync tsync;
229     XSTATS_DECLS
230     OSI_VC_CONVERT(adp)
231
232     AFS_STATCNT(afs_remove);
233     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
234                ICL_TYPE_STRING, aname);
235
236     if (code = afs_InitReq(&treq, acred))
237       return code;
238 tagain:
239     code = afs_VerifyVCache(adp, &treq);
240 #ifdef  AFS_OSF_ENV
241     tvc = (struct vcache *)ndp->ni_vp;  /* should never be null */
242     if (code) {
243         afs_PutVCache(adp, 0);
244         afs_PutVCache(tvc, 0);
245         return afs_CheckCode(code, &treq, 22);
246     }
247 #else   /* AFS_OSF_ENV */
248     tvc = (struct vcache *) 0;
249     if (code) {
250       code = afs_CheckCode(code, &treq, 23);
251       return code;
252     }
253 #endif
254
255     /** If the volume is read-only, return error without making an RPC to the
256       * fileserver
257       */
258     if ( adp->states & CRO ) {
259         code = EROFS;
260         return code;
261     }
262
263     tdc = afs_GetDCache(adp, 0, &treq, &offset, &len, 1);  /* test for error below */
264     ObtainWriteLock(&adp->lock,142);
265
266     /*
267      * Make sure that the data in the cache is current. We may have
268      * received a callback while we were waiting for the write lock.
269      */
270     if (!(adp->states & CStatd)
271         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
272         ReleaseWriteLock(&adp->lock);
273         if (tdc)
274             afs_PutDCache(tdc);
275         goto tagain;
276     }
277
278     unlinkFid.Fid.Vnode = 0;
279     if (!tvc) {
280       tvc = osi_dnlc_lookup (adp, aname, WRITE_LOCK);
281     }
282     /* This should not be necessary since afs_lookup() has already
283      * done the work */
284     if (!tvc)
285       if (tdc) {
286         code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
287         if (code == 0) {        
288             afs_int32 cached=0;
289
290             unlinkFid.Cell = adp->fid.Cell;
291             unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
292             if (unlinkFid.Fid.Unique == 0) {
293                 tvc = afs_LookupVCache(&unlinkFid, &treq, &cached, 
294                                        WRITE_LOCK, adp, aname);
295             } else {
296                 ObtainReadLock(&afs_xvcache);
297                 tvc = afs_FindVCache(&unlinkFid, 1, WRITE_LOCK, 
298                                      0 , DO_STATS );
299                 ReleaseReadLock(&afs_xvcache);
300             }
301         }
302     }
303
304     if (tvc && osi_Active(tvc)) {
305         /* about to delete whole file, prefetch it first */
306         ReleaseWriteLock(&adp->lock);
307         ObtainWriteLock(&tvc->lock,143);
308 #if     defined(AFS_OSF_ENV)
309         afs_Wire(tvc, &treq);
310 #else   /* AFS_OSF_ENV */
311         FetchWholeEnchilada(tvc, &treq);
312 #endif
313         ReleaseWriteLock(&tvc->lock);
314         ObtainWriteLock(&adp->lock,144);
315     }
316
317     osi_dnlc_remove ( adp, aname, tvc);
318     if (tvc) afs_symhint_inval(tvc);
319
320     Tadp1 = adp; Tadpr = VREFCOUNT(adp); Ttvc = tvc; Tnam = aname; Tnam1 = 0;
321     if (tvc) Ttvcr = VREFCOUNT(tvc);
322 #ifdef  AFS_AIX_ENV
323     if (tvc && (VREFCOUNT(tvc) > 2) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
324 #else
325     if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
326 #endif
327         char *unlname = newname();
328
329         ReleaseWriteLock(&adp->lock);
330         code = afsrename(adp, aname, adp, unlname, acred);
331         Tnam1 = unlname;
332         if (!code) {
333             tvc->mvid = (struct VenusFid *)unlname;
334             crhold(acred);
335             if (tvc->uncred) {
336                 crfree(tvc->uncred);
337             }
338             tvc->uncred = acred;
339             tvc->states |= CUnlinked;
340         } else {
341             osi_FreeSmallSpace(unlname);            
342         }
343         if ( tdc )
344                 afs_PutDCache(tdc);
345         afs_PutVCache(tvc, WRITE_LOCK); 
346     } else {
347         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
348     }
349 #ifdef  AFS_OSF_ENV
350     afs_PutVCache(adp, WRITE_LOCK);
351 #endif  /* AFS_OSF_ENV */
352     return code;
353 }
354
355
356 /* afs_remunlink -- This tries to delete the file at the server after it has
357  *     been renamed when unlinked locally but now has been finally released.
358  *
359  * CAUTION -- may be called with avc unheld. */
360
361 afs_remunlink(avc, doit)
362     register struct vcache *avc;
363     register int doit;
364 {
365     struct AFS_UCRED *cred;
366     char *unlname;
367     struct vcache *adp;
368     struct vrequest treq;
369     struct VenusFid dirFid;
370     register struct dcache *tdc;
371     afs_int32 offset, len, code=0;
372
373     if (NBObtainWriteLock(&avc->lock, 423))
374         return 0;
375
376     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
377         if (code = afs_InitReq(&treq, avc->uncred)) {
378             ReleaseWriteLock(&avc->lock);
379         }
380         else {
381             /* Must bump the refCount because GetVCache may block.
382              * Also clear mvid so no other thread comes here if we block.
383              */
384             unlname = (char *)avc->mvid;
385             avc->mvid = NULL;
386             cred = avc->uncred;
387             avc->uncred = NULL;
388
389             VN_HOLD(&avc->v);
390
391             /* We'll only try this once. If it fails, just release the vnode.
392              * Clear after doing hold so that NewVCache doesn't find us yet.
393              */
394             avc->states  &= ~(CUnlinked | CUnlinkedDel);
395
396             ReleaseWriteLock(&avc->lock);
397
398             dirFid.Cell = avc->fid.Cell;
399             dirFid.Fid.Volume = avc->fid.Fid.Volume;
400             dirFid.Fid.Vnode = avc->parentVnode;
401             dirFid.Fid.Unique = avc->parentUnique;
402             adp = afs_GetVCache(&dirFid, &treq, (afs_int32 *)0, 
403                                 (struct vcache *)0, WRITE_LOCK);
404             
405             if (adp) {
406                 tdc = afs_FindDCache(adp, 0);
407                 ObtainWriteLock(&adp->lock, 159);
408
409                 /* afsremove releases the adp lock, and does vn_rele(avc) */
410                 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
411                 afs_PutVCache(adp, WRITE_LOCK);
412             } else {
413                 /* we failed - and won't be back to try again. */
414                 afs_PutVCache(avc, WRITE_LOCK);
415             }
416             osi_FreeSmallSpace(unlname);
417             crfree(cred);
418         }
419     }
420     else {
421         ReleaseWriteLock(&avc->lock);
422     }
423
424     return code;
425 }