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