36cdf7b146708ed7ff5629c425c06052dc63bea6
[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(register struct vcache *avc, struct vrequest *areq)
50 #endif
51 {
52     register afs_int32 nextChunk;
53     register struct dcache *tdc;
54     afs_size_t pos, offset, len;
55
56     AFS_STATCNT(FetchWholeEnchilada);
57     if ((avc->states & CStatd) == 0)
58         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)
63             break;              /* all done */
64 #else /* AFS_OSF_ENV */
65         if (pos >= avc->m.Length)
66             return;             /* all done */
67 #endif
68         tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
69         if (!tdc)
70 #if     defined(AFS_OSF_ENV)
71             break;
72 #else /* AFS_OSF_ENV */
73             return;
74 #endif
75         afs_PutDCache(tdc);
76     }
77 #if defined(AFS_OSF_ENV)
78     avc->states |= CWired;
79 #endif /* AFS_OSF_ENV */
80 }
81
82 #if     defined(AFS_OSF_ENV)
83 /*
84  *  Tests whether file is wired down, after unwiring the file if it
85  *  is found to be inactive (ie not open and not being paged from).
86  */
87 afs_IsWired(register struct vcache *avc)
88 {
89     if (avc->states & CWired) {
90         if (osi_Active(avc)) {
91             return 1;
92         }
93         avc->states &= ~CWired;
94     }
95     return 0;
96 }
97 #endif /* AFS_OSF_ENV */
98
99 int
100 afsremove(register struct vcache *adp, register struct dcache *tdc,
101           register struct vcache *tvc, char *aname, struct AFS_UCRED *acred,
102           struct vrequest *treqp)
103 {
104     register afs_int32 code = 0;
105     register struct conn *tc;
106     struct AFSFetchStatus OutDirStatus;
107     struct AFSVolSync tsync;
108     XSTATS_DECLS;
109     if (!AFS_IS_DISCONNECTED) {
110         do {
111             tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
112             if (tc) {
113                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
114                 RX_AFS_GUNLOCK();
115                 code =
116                     RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
117                                      aname, &OutDirStatus, &tsync);
118                 RX_AFS_GLOCK();
119                 XSTATS_END_TIME;
120             } else
121                 code = -1;
122         } while (afs_Analyze
123                  (tc, code, &adp->fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
124                   SHARED_LOCK, NULL));
125     }
126
127     osi_dnlc_remove(adp, aname, tvc);
128
129     if (code) {
130         if (tdc) {
131             ReleaseSharedLock(&tdc->lock);
132             afs_PutDCache(tdc);
133         }
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_IS_DISCON_RW || 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         /* Don't decrease refcount for this vcache if disconnected, we will
188          * need it during replay.
189          */
190         if (!AFS_IS_DISCON_RW)
191             afs_PutVCache(tvc);
192     }
193     return (0);
194 }
195
196 char *
197 afs_newname(void)
198 {
199     char *name, *sp, *p = ".__afs";
200     afs_int32 rd = afs_random() & 0xffff;
201
202     sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
203     while (*p != '\0')
204         *sp++ = *p++;
205     while (rd) {
206         *sp++ = "0123456789ABCDEF"[rd & 0x0f];
207         rd >>= 4;
208     }
209     *sp = '\0';
210     return (name);
211 }
212
213 /* these variables appear to exist for debugging purposes */
214 struct vcache *Tadp1, *Ttvc;
215 int Tadpr, Ttvcr;
216 char *Tnam;
217 char *Tnam1;
218
219 /* Note that we don't set CDirty here, this is OK because the unlink
220  * RPC is called synchronously */
221 int
222 afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
223 {
224     struct vrequest treq;
225     register struct dcache *tdc;
226     struct VenusFid unlinkFid;
227     register afs_int32 code;
228     register struct vcache *tvc;
229     afs_size_t offset, len;
230     struct afs_fakestat_state fakestate;
231     OSI_VC_CONVERT(adp);
232
233     AFS_STATCNT(afs_remove);
234     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
235                ICL_TYPE_STRING, aname);
236
237 #ifdef  AFS_OSF_ENV
238     tvc = (struct vcache *)ndp->ni_vp;  /* should never be null */
239 #endif
240
241     if ((code = afs_InitReq(&treq, acred))) {
242 #ifdef  AFS_OSF_ENV
243         afs_PutVCache(tvc);
244 #endif
245         return code;
246     }
247
248     afs_InitFakeStat(&fakestate);
249     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
250     if (code) {
251         afs_PutFakeStat(&fakestate);
252 #ifdef  AFS_OSF_ENV
253         afs_PutVCache(tvc);
254 #endif
255         return code;
256     }
257
258     /* Check if this is dynroot */
259     if (afs_IsDynroot(adp)) {
260         code = afs_DynrootVOPRemove(adp, acred, aname);
261         afs_PutFakeStat(&fakestate);
262 #ifdef  AFS_OSF_ENV
263         afs_PutVCache(tvc);
264 #endif
265         return code;
266     }
267     if (afs_IsDynrootMount(adp)) {
268         return ENOENT;
269     }
270
271     if (strlen(aname) > AFSNAMEMAX) {
272         afs_PutFakeStat(&fakestate);
273 #ifdef  AFS_OSF_ENV
274         afs_PutVCache(tvc);
275 #endif
276         return ENAMETOOLONG;
277     }
278   tagain:
279     code = afs_VerifyVCache(adp, &treq);
280 #ifdef  AFS_OSF_ENV
281     tvc = VTOAFS(ndp->ni_vp);   /* should never be null */
282     if (code) {
283         afs_PutVCache(tvc);
284         afs_PutFakeStat(&fakestate);
285         return afs_CheckCode(code, &treq, 22);
286     }
287 #else /* AFS_OSF_ENV */
288     tvc = NULL;
289     if (code) {
290         code = afs_CheckCode(code, &treq, 23);
291         afs_PutFakeStat(&fakestate);
292         return code;
293     }
294 #endif
295
296     /** If the volume is read-only, return error without making an RPC to the
297       * fileserver
298       */
299     if (adp->states & CRO) {
300 #ifdef  AFS_OSF_ENV
301         afs_PutVCache(tvc);
302 #endif
303         code = EROFS;
304         afs_PutFakeStat(&fakestate);
305         return code;
306     }
307
308     /* If we're running disconnected without logging, go no further... */
309     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
310 #ifdef  AFS_OSF_ENV
311         afs_PutVCache(tvc);
312 #endif
313         code = ENETDOWN;
314         afs_PutFakeStat(&fakestate);
315         return code;
316     }
317     
318     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
319     ObtainWriteLock(&adp->lock, 142);
320 #if defined(AFS_DISCON_ENV)
321     if (AFS_IS_DISCON_RW && !(adp->ddirty_flags & VDisconShadowed))
322         /* Make shadow copy of parent dir. */
323         afs_MakeShadowDir(adp);
324 #endif
325     if (tdc)
326         ObtainSharedLock(&tdc->lock, 638);
327
328     /*
329      * Make sure that the data in the cache is current. We may have
330      * received a callback while we were waiting for the write lock.
331      */
332     if (!(adp->states & CStatd)
333         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
334         ReleaseWriteLock(&adp->lock);
335         if (tdc) {
336             ReleaseSharedLock(&tdc->lock);
337             afs_PutDCache(tdc);
338         }
339         goto tagain;
340     }
341
342     unlinkFid.Fid.Vnode = 0;
343     if (!tvc) {
344         tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
345     }
346     /* This should not be necessary since afs_lookup() has already
347      * done the work.
348      */
349     if (!tvc)
350         if (tdc) {
351             code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
352             if (code == 0) {
353                 afs_int32 cached = 0;
354
355                 unlinkFid.Cell = adp->fid.Cell;
356                 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
357                 if (unlinkFid.Fid.Unique == 0) {
358                     tvc =
359                         afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
360                                          aname);
361                 } else {
362                     ObtainReadLock(&afs_xvcache);
363                     tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
364                     ReleaseReadLock(&afs_xvcache);
365                 }
366             }
367         }
368
369 #if defined(AFS_DISCON_ENV)
370     if (AFS_IS_DISCON_RW) {
371         /* Add removed file vcache to dirty list. */
372
373         if (!tvc->ddirty_flags ||
374                 (tvc->ddirty_flags == VDisconShadowed)) {
375             /* Add to list only if fresh. */
376             ObtainWriteLock(&afs_DDirtyVCListLock, 725);
377             AFS_DISCON_ADD_DIRTY(tvc);
378             ReleaseWriteLock(&afs_DDirtyVCListLock);
379         }
380
381         ObtainWriteLock(&tvc->lock, 726);
382         tvc->ddirty_flags |= VDisconRemove;
383         ReleaseWriteLock(&tvc->lock);
384
385         //ObtainWriteLock(&adp->lock, 751);
386         adp->m.LinkCount--;
387         //ReleaseWriteLock(&adp->lock);
388     }
389 #endif
390
391     if (tvc && osi_Active(tvc)) {
392         /* about to delete whole file, prefetch it first */
393         ReleaseWriteLock(&adp->lock);
394         if (tdc)
395             ReleaseSharedLock(&tdc->lock);
396         ObtainWriteLock(&tvc->lock, 143);
397 #if     defined(AFS_OSF_ENV)
398         afs_Wire(tvc, &treq);
399 #else /* AFS_OSF_ENV */
400         FetchWholeEnchilada(tvc, &treq);
401 #endif
402         ReleaseWriteLock(&tvc->lock);
403         ObtainWriteLock(&adp->lock, 144);
404         /* Technically I don't think we need this back, but let's hold it 
405            anyway; The "got" reference should actually be sufficient. */
406         if (tdc) 
407             ObtainSharedLock(&tdc->lock, 640);
408     }
409
410     osi_dnlc_remove(adp, aname, tvc);
411
412     Tadp1 = adp;
413 #ifndef AFS_DARWIN80_ENV
414     Tadpr = VREFCOUNT(adp);
415 #endif
416     Ttvc = tvc;
417     Tnam = aname;
418     Tnam1 = 0;
419 #ifndef AFS_DARWIN80_ENV
420     if (tvc)
421         Ttvcr = VREFCOUNT(tvc);
422 #endif
423 #ifdef  AFS_AIX_ENV
424     if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
425         && !(tvc->states & CUnlinked)) {
426 #else
427     if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
428         && !(tvc->states & CUnlinked)) {
429 #endif
430         char *unlname = afs_newname();
431
432         ReleaseWriteLock(&adp->lock);
433         if (tdc)
434             ReleaseSharedLock(&tdc->lock);
435         code = afsrename(adp, aname, adp, unlname, acred, &treq);
436         Tnam1 = unlname;
437         if (!code) {
438             struct VenusFid *oldmvid = NULL;
439             if (tvc->mvid) 
440                 oldmvid = tvc->mvid;
441             tvc->mvid = (struct VenusFid *)unlname;
442             if (oldmvid)
443                 osi_FreeSmallSpace(oldmvid);
444             crhold(acred);
445             if (tvc->uncred) {
446                 crfree(tvc->uncred);
447             }
448             tvc->uncred = acred;
449             tvc->states |= CUnlinked;
450         } else {
451             osi_FreeSmallSpace(unlname);
452         }
453         if (tdc)
454             afs_PutDCache(tdc);
455         /* Don't decrease refcount for this vcache if disconnected, we will
456          * need it during replay.
457          */
458         if (!AFS_IS_DISCON_RW)
459             afs_PutVCache(tvc);
460     } else {
461         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
462     }
463     afs_PutFakeStat(&fakestate);
464 #ifndef AFS_DARWIN80_ENV
465     /* we can't track by thread, it's not exported in the KPI; only do
466        this on !macos */
467     osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
468 #endif
469     return code;
470 }
471
472
473 /* afs_remunlink -- This tries to delete the file at the server after it has
474  *     been renamed when unlinked locally but now has been finally released.
475  *
476  * CAUTION -- may be called with avc unheld. */
477
478 int
479 afs_remunlink(register struct vcache *avc, register int doit)
480 {
481     struct AFS_UCRED *cred;
482     char *unlname;
483     struct vcache *adp;
484     struct vrequest treq;
485     struct VenusFid dirFid;
486     register struct dcache *tdc;
487     afs_int32 code = 0;
488
489     if (NBObtainWriteLock(&avc->lock, 423))
490         return 0;
491 #if defined(AFS_DARWIN80_ENV)
492     if (vnode_get(AFSTOV(avc))) {
493         ReleaseWriteLock(&avc->lock);
494         return 0;
495     }
496 #endif
497
498     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
499         if ((code = afs_InitReq(&treq, avc->uncred))) {
500             ReleaseWriteLock(&avc->lock);
501         } else {
502             /* Must bump the refCount because GetVCache may block.
503              * Also clear mvid so no other thread comes here if we block.
504              */
505             unlname = (char *)avc->mvid;
506             avc->mvid = NULL;
507             cred = avc->uncred;
508             avc->uncred = NULL;
509
510 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
511             VREF(AFSTOV(avc));
512 #else
513             VN_HOLD(AFSTOV(avc));
514 #endif
515
516             /* We'll only try this once. If it fails, just release the vnode.
517              * Clear after doing hold so that NewVCache doesn't find us yet.
518              */
519             avc->states &= ~(CUnlinked | CUnlinkedDel);
520
521             ReleaseWriteLock(&avc->lock);
522
523             dirFid.Cell = avc->fid.Cell;
524             dirFid.Fid.Volume = avc->fid.Fid.Volume;
525             dirFid.Fid.Vnode = avc->parentVnode;
526             dirFid.Fid.Unique = avc->parentUnique;
527             adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
528
529             if (adp) {
530                 tdc = afs_FindDCache(adp, (afs_size_t) 0);
531                 ObtainWriteLock(&adp->lock, 159);
532                 if (tdc)
533                     ObtainSharedLock(&tdc->lock, 639);
534
535                 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
536                 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
537                 afs_PutVCache(adp);
538             } else {
539                 /* we failed - and won't be back to try again. */
540                 afs_PutVCache(avc);
541             }
542             osi_FreeSmallSpace(unlname);
543             crfree(cred);
544         }
545     } else {
546 #if defined(AFS_DARWIN80_ENV)
547         vnode_put(AFSTOV(avc));
548 #endif
549         ReleaseWriteLock(&avc->lock);
550     }
551
552     return code;
553 }