2 * Copyright 2000, International Business Machines Corporation and others.
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
18 #include <afsconfig.h>
19 #include "afs/param.h"
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"
30 extern afs_rwlock_t afs_xvcache;
31 extern afs_rwlock_t afs_xcbhash;
35 FetchWholeEnchilada(struct vcache *avc, struct vrequest *areq)
39 afs_size_t pos, offset, len;
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);
56 afsremove(struct vcache *adp, struct dcache *tdc,
57 struct vcache *tvc, char *aname, afs_ucred_t *acred,
58 struct vrequest *treqp)
62 struct AFSFetchStatus OutDirStatus;
63 struct AFSVolSync tsync;
65 if (!AFS_IS_DISCONNECTED) {
67 tc = afs_Conn(&adp->f.fid, treqp, SHARED_LOCK);
69 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
72 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
73 aname, &OutDirStatus, &tsync);
79 (tc, code, &adp->f.fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
83 osi_dnlc_remove(adp, aname, tvc);
87 ReleaseSharedLock(&tdc->lock);
95 ObtainWriteLock(&afs_xcbhash, 497);
96 afs_DequeueCallback(adp);
97 adp->f.states &= ~CStatd;
98 ReleaseWriteLock(&afs_xcbhash);
99 osi_dnlc_purgedp(adp);
101 ReleaseWriteLock(&adp->lock);
102 code = afs_CheckCode(code, treqp, 21);
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);
111 ZapDCE(tdc); /* surprise error -- invalid value */
116 ReleaseWriteLock(&tdc->lock);
117 afs_PutDCache(tdc); /* drop ref count */
119 ReleaseWriteLock(&adp->lock);
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. */
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 */
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);
140 ReleaseWriteLock(&tvc->lock);
141 #ifdef AFS_BOZONLOCK_ENV
142 afs_BozonUnlock(&tvc->pvnLock, tvc);
152 char *name, *sp, *p = ".__afs";
153 afs_int32 rd = afs_random() & 0xffff;
155 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
159 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
166 /* these variables appear to exist for debugging purposes */
167 struct vcache *Tadp1, *Ttvc;
172 /* Note that we don't set CDirty here, this is OK because the unlink
173 * RPC is called synchronously */
175 afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
177 struct vrequest treq;
179 struct VenusFid unlinkFid;
182 afs_size_t offset, len;
183 struct afs_fakestat_state fakestate;
186 AFS_STATCNT(afs_remove);
187 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
188 ICL_TYPE_STRING, aname);
191 if ((code = afs_InitReq(&treq, acred))) {
195 afs_InitFakeStat(&fakestate);
197 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
201 /* Check if this is dynroot */
202 if (afs_IsDynroot(adp)) {
203 code = afs_DynrootVOPRemove(adp, acred, aname);
206 if (afs_IsDynrootMount(adp)) {
211 if (strlen(aname) > AFSNAMEMAX) {
216 code = afs_VerifyVCache(adp, &treq);
219 code = afs_CheckCode(code, &treq, 23);
223 /** If the volume is read-only, return error without making an RPC to the
226 if (adp->f.states & CRO) {
231 /* If we're running disconnected without logging, go no further... */
232 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
237 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
238 ObtainWriteLock(&adp->lock, 142);
240 ObtainSharedLock(&tdc->lock, 638);
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.
246 if (!(adp->f.states & CStatd)
247 || (tdc && !hsame(adp->f.m.DataVersion, tdc->f.versionNo))) {
248 ReleaseWriteLock(&adp->lock);
250 ReleaseSharedLock(&tdc->lock);
256 unlinkFid.Fid.Vnode = 0;
258 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
260 /* This should not be necessary since afs_lookup() has already
265 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
267 afs_int32 cached = 0;
269 unlinkFid.Cell = adp->f.fid.Cell;
270 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
271 if (unlinkFid.Fid.Unique == 0) {
273 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
276 ObtainReadLock(&afs_xvcache);
277 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
278 ReleaseReadLock(&afs_xvcache);
283 if (AFS_IS_DISCON_RW) {
284 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
285 /* Make shadow copy of parent dir. */
286 afs_MakeShadowDir(adp, tdc);
289 /* Can't hold a dcache lock whilst we're getting a vcache one */
291 ReleaseSharedLock(&tdc->lock);
293 /* XXX - We're holding adp->lock still, and we've got no
294 * guarantee about whether the ordering matches the lock hierarchy */
295 ObtainWriteLock(&tvc->lock, 713);
297 /* If we were locally created, then we don't need to do very
298 * much beyond ensuring that we don't exist anymore */
299 if (tvc->f.ddirty_flags & VDisconCreate) {
300 afs_DisconRemoveDirty(tvc);
302 /* Add removed file vcache to dirty list. */
303 afs_DisconAddDirty(tvc, VDisconRemove, 1);
305 adp->f.m.LinkCount--;
306 ReleaseWriteLock(&tvc->lock);
308 ObtainSharedLock(&tdc->lock, 714);
311 if (tvc && osi_Active(tvc)) {
312 /* about to delete whole file, prefetch it first */
313 ReleaseWriteLock(&adp->lock);
315 ReleaseSharedLock(&tdc->lock);
316 ObtainWriteLock(&tvc->lock, 143);
317 FetchWholeEnchilada(tvc, &treq);
318 ReleaseWriteLock(&tvc->lock);
319 ObtainWriteLock(&adp->lock, 144);
320 /* Technically I don't think we need this back, but let's hold it
321 anyway; The "got" reference should actually be sufficient. */
323 ObtainSharedLock(&tdc->lock, 640);
326 osi_dnlc_remove(adp, aname, tvc);
329 #ifndef AFS_DARWIN80_ENV
330 Tadpr = VREFCOUNT(adp);
335 #ifndef AFS_DARWIN80_ENV
337 Ttvcr = VREFCOUNT(tvc);
340 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
341 && !(tvc->f.states & CUnlinked)) {
343 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
344 && !(tvc->f.states & CUnlinked)) {
346 char *unlname = afs_newname();
348 ReleaseWriteLock(&adp->lock);
350 ReleaseSharedLock(&tdc->lock);
351 code = afsrename(adp, aname, adp, unlname, acred, &treq);
354 struct VenusFid *oldmvid = NULL;
357 tvc->mvid = (struct VenusFid *)unlname;
359 osi_FreeSmallSpace(oldmvid);
365 tvc->f.states |= CUnlinked;
366 /* if rename succeeded, remove should not */
367 ObtainWriteLock(&tvc->lock, 715);
368 if (tvc->f.ddirty_flags & VDisconRemove) {
369 tvc->f.ddirty_flags &= ~VDisconRemove;
371 ReleaseWriteLock(&tvc->lock);
373 osi_FreeSmallSpace(unlname);
379 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
382 afs_PutFakeStat(&fakestate);
383 #ifndef AFS_DARWIN80_ENV
384 /* we can't track by thread, it's not exported in the KPI; only do
386 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
393 /* afs_remunlink -- This tries to delete the file at the server after it has
394 * been renamed when unlinked locally but now has been finally released.
396 * CAUTION -- may be called with avc unheld. */
399 afs_remunlink(struct vcache *avc, int doit)
404 struct vrequest treq;
405 struct VenusFid dirFid;
409 if (NBObtainWriteLock(&avc->lock, 423))
411 #if defined(AFS_DARWIN80_ENV)
412 if (vnode_get(AFSTOV(avc))) {
413 ReleaseWriteLock(&avc->lock);
418 if (avc->mvid && (doit || (avc->f.states & CUnlinkedDel))) {
419 if ((code = afs_InitReq(&treq, avc->uncred))) {
420 ReleaseWriteLock(&avc->lock);
422 /* Must bump the refCount because GetVCache may block.
423 * Also clear mvid so no other thread comes here if we block.
425 unlname = (char *)avc->mvid;
430 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
436 /* We'll only try this once. If it fails, just release the vnode.
437 * Clear after doing hold so that NewVCache doesn't find us yet.
439 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
441 ReleaseWriteLock(&avc->lock);
443 dirFid.Cell = avc->f.fid.Cell;
444 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
445 dirFid.Fid.Vnode = avc->f.parent.vnode;
446 dirFid.Fid.Unique = avc->f.parent.unique;
447 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
450 tdc = afs_FindDCache(adp, (afs_size_t) 0);
451 ObtainWriteLock(&adp->lock, 159);
453 ObtainSharedLock(&tdc->lock, 639);
455 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
456 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
459 /* we failed - and won't be back to try again. */
462 osi_FreeSmallSpace(unlname);
466 #if defined(AFS_DARWIN80_ENV)
467 vnode_put(AFSTOV(avc));
469 ReleaseWriteLock(&avc->lock);