33b4b5e385fb18d0cdc216c7cf652b2859660b6c
[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         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_DECL(adp), char *aname, struct AFS_UCRED *acred)
219 {
220     struct vrequest treq;
221     register struct dcache *tdc;
222     struct VenusFid unlinkFid;
223     register afs_int32 code;
224     register struct vcache *tvc;
225     afs_size_t offset, len;
226     struct afs_fakestat_state fakestate;
227     OSI_VC_CONVERT(adp);
228
229     AFS_STATCNT(afs_remove);
230     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
231                ICL_TYPE_STRING, aname);
232
233 #ifdef  AFS_OSF_ENV
234     tvc = (struct vcache *)ndp->ni_vp;  /* should never be null */
235 #endif
236
237     if ((code = afs_InitReq(&treq, acred))) {
238 #ifdef  AFS_OSF_ENV
239         afs_PutVCache(tvc);
240 #endif
241         return code;
242     }
243
244     afs_InitFakeStat(&fakestate);
245     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
246     if (code) {
247         afs_PutFakeStat(&fakestate);
248 #ifdef  AFS_OSF_ENV
249         afs_PutVCache(tvc);
250 #endif
251         return code;
252     }
253
254     /* Check if this is dynroot */
255     if (afs_IsDynroot(adp)) {
256         code = afs_DynrootVOPRemove(adp, acred, aname);
257         afs_PutFakeStat(&fakestate);
258 #ifdef  AFS_OSF_ENV
259         afs_PutVCache(tvc);
260 #endif
261         return code;
262     }
263     if (afs_IsDynrootMount(adp)) {
264         return ENOENT;
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     /* If we're running disconnected without logging, go no further... */
305     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
306 #ifdef  AFS_OSF_ENV
307         afs_PutVCache(tvc);
308 #endif
309         code = ENETDOWN;
310         afs_PutFakeStat(&fakestate);
311         return code;
312     }
313     
314     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
315     ObtainWriteLock(&adp->lock, 142);
316 #if defined(AFS_DISCON_ENV)
317     if (AFS_IS_DISCON_RW && !(adp->ddirty_flags & VDisconShadowed))
318         /* Make shadow copy of parent dir. */
319         afs_MakeShadowDir(adp);
320 #endif
321     if (tdc)
322         ObtainSharedLock(&tdc->lock, 638);
323
324     /*
325      * Make sure that the data in the cache is current. We may have
326      * received a callback while we were waiting for the write lock.
327      */
328     if (!(adp->states & CStatd)
329         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
330         ReleaseWriteLock(&adp->lock);
331         if (tdc) {
332             ReleaseSharedLock(&tdc->lock);
333             afs_PutDCache(tdc);
334         }
335         goto tagain;
336     }
337
338     unlinkFid.Fid.Vnode = 0;
339     if (!tvc) {
340         tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
341     }
342     /* This should not be necessary since afs_lookup() has already
343      * done the work.
344      */
345     if (!tvc)
346         if (tdc) {
347             code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
348             if (code == 0) {
349                 afs_int32 cached = 0;
350
351                 unlinkFid.Cell = adp->fid.Cell;
352                 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
353                 if (unlinkFid.Fid.Unique == 0) {
354                     tvc =
355                         afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
356                                          aname);
357                 } else {
358                     ObtainReadLock(&afs_xvcache);
359                     tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
360                     ReleaseReadLock(&afs_xvcache);
361                 }
362             }
363         }
364
365 #if defined(AFS_DISCON_ENV)
366     if (AFS_IS_DISCON_RW) {
367         /* Add removed file vcache to dirty list. */
368
369         if (!tvc->ddirty_flags ||
370                 (tvc->ddirty_flags == VDisconShadowed)) {
371             /* Add to list only if fresh. */
372             ObtainWriteLock(&afs_DDirtyVCListLock, 725);
373             AFS_DISCON_ADD_DIRTY(tvc, 1);
374             ReleaseWriteLock(&afs_DDirtyVCListLock);
375         }
376
377         ObtainWriteLock(&tvc->lock, 726);
378         tvc->ddirty_flags |= VDisconRemove;
379         ReleaseWriteLock(&tvc->lock);
380
381         //ObtainWriteLock(&adp->lock, 751);
382         adp->m.LinkCount--;
383         //ReleaseWriteLock(&adp->lock);
384     }
385 #endif
386
387     if (tvc && osi_Active(tvc)) {
388         /* about to delete whole file, prefetch it first */
389         ReleaseWriteLock(&adp->lock);
390         if (tdc)
391             ReleaseSharedLock(&tdc->lock);
392         ObtainWriteLock(&tvc->lock, 143);
393 #if     defined(AFS_OSF_ENV)
394         afs_Wire(tvc, &treq);
395 #else /* AFS_OSF_ENV */
396         FetchWholeEnchilada(tvc, &treq);
397 #endif
398         ReleaseWriteLock(&tvc->lock);
399         ObtainWriteLock(&adp->lock, 144);
400         /* Technically I don't think we need this back, but let's hold it 
401            anyway; The "got" reference should actually be sufficient. */
402         if (tdc) 
403             ObtainSharedLock(&tdc->lock, 640);
404     }
405
406     osi_dnlc_remove(adp, aname, tvc);
407
408     Tadp1 = adp;
409 #ifndef AFS_DARWIN80_ENV
410     Tadpr = VREFCOUNT(adp);
411 #endif
412     Ttvc = tvc;
413     Tnam = aname;
414     Tnam1 = 0;
415 #ifndef AFS_DARWIN80_ENV
416     if (tvc)
417         Ttvcr = VREFCOUNT(tvc);
418 #endif
419 #ifdef  AFS_AIX_ENV
420     if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
421         && !(tvc->states & CUnlinked)) {
422 #else
423     if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
424         && !(tvc->states & CUnlinked)) {
425 #endif
426         char *unlname = afs_newname();
427
428         ReleaseWriteLock(&adp->lock);
429         if (tdc)
430             ReleaseSharedLock(&tdc->lock);
431         code = afsrename(adp, aname, adp, unlname, acred, &treq);
432         Tnam1 = unlname;
433         if (!code) {
434             struct VenusFid *oldmvid = NULL;
435             if (tvc->mvid) 
436                 oldmvid = tvc->mvid;
437             tvc->mvid = (struct VenusFid *)unlname;
438             if (oldmvid)
439                 osi_FreeSmallSpace(oldmvid);
440             crhold(acred);
441             if (tvc->uncred) {
442                 crfree(tvc->uncred);
443             }
444             tvc->uncred = acred;
445             tvc->states |= CUnlinked;
446         } else {
447             osi_FreeSmallSpace(unlname);
448         }
449         if (tdc)
450             afs_PutDCache(tdc);
451         /* Don't decrease refcount for this vcache if disconnected, we will
452          * need it during replay.
453          */
454         if (!AFS_IS_DISCON_RW)
455             afs_PutVCache(tvc);
456     } else {
457         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
458     }
459     afs_PutFakeStat(&fakestate);
460 #ifndef AFS_DARWIN80_ENV
461     /* we can't track by thread, it's not exported in the KPI; only do
462        this on !macos */
463     osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
464 #endif
465     return code;
466 }
467
468
469 /* afs_remunlink -- This tries to delete the file at the server after it has
470  *     been renamed when unlinked locally but now has been finally released.
471  *
472  * CAUTION -- may be called with avc unheld. */
473
474 int
475 afs_remunlink(register struct vcache *avc, register int doit)
476 {
477     struct AFS_UCRED *cred;
478     char *unlname;
479     struct vcache *adp;
480     struct vrequest treq;
481     struct VenusFid dirFid;
482     register struct dcache *tdc;
483     afs_int32 code = 0;
484
485     if (NBObtainWriteLock(&avc->lock, 423))
486         return 0;
487 #if defined(AFS_DARWIN80_ENV)
488     if (vnode_get(AFSTOV(avc))) {
489         ReleaseWriteLock(&avc->lock);
490         return 0;
491     }
492 #endif
493
494     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
495         if ((code = afs_InitReq(&treq, avc->uncred))) {
496             ReleaseWriteLock(&avc->lock);
497         } else {
498             /* Must bump the refCount because GetVCache may block.
499              * Also clear mvid so no other thread comes here if we block.
500              */
501             unlname = (char *)avc->mvid;
502             avc->mvid = NULL;
503             cred = avc->uncred;
504             avc->uncred = NULL;
505
506 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
507             VREF(AFSTOV(avc));
508 #else
509             VN_HOLD(AFSTOV(avc));
510 #endif
511
512             /* We'll only try this once. If it fails, just release the vnode.
513              * Clear after doing hold so that NewVCache doesn't find us yet.
514              */
515             avc->states &= ~(CUnlinked | CUnlinkedDel);
516
517             ReleaseWriteLock(&avc->lock);
518
519             dirFid.Cell = avc->fid.Cell;
520             dirFid.Fid.Volume = avc->fid.Fid.Volume;
521             dirFid.Fid.Vnode = avc->parentVnode;
522             dirFid.Fid.Unique = avc->parentUnique;
523             adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
524
525             if (adp) {
526                 tdc = afs_FindDCache(adp, (afs_size_t) 0);
527                 ObtainWriteLock(&adp->lock, 159);
528                 if (tdc)
529                     ObtainSharedLock(&tdc->lock, 639);
530
531                 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
532                 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
533                 afs_PutVCache(adp);
534             } else {
535                 /* we failed - and won't be back to try again. */
536                 afs_PutVCache(avc);
537             }
538             osi_FreeSmallSpace(unlname);
539             crfree(cred);
540         }
541     } else {
542 #if defined(AFS_DARWIN80_ENV)
543         vnode_put(AFSTOV(avc));
544 #endif
545         ReleaseWriteLock(&avc->lock);
546     }
547
548     return code;
549 }