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