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