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