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