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;
64 struct rx_connection *rxconn;
66 if (!AFS_IS_DISCONNECTED) {
68 tc = afs_Conn(&adp->f.fid, treqp, SHARED_LOCK, &rxconn);
70 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
73 RXAFS_RemoveFile(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
74 aname, &OutDirStatus, &tsync);
80 (tc, rxconn, code, &adp->f.fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
84 osi_dnlc_remove(adp, aname, tvc);
88 ReleaseSharedLock(&tdc->lock);
96 ObtainWriteLock(&afs_xcbhash, 497);
97 afs_DequeueCallback(adp);
98 adp->f.states &= ~CStatd;
99 ReleaseWriteLock(&afs_xcbhash);
100 osi_dnlc_purgedp(adp);
102 ReleaseWriteLock(&adp->lock);
103 code = afs_CheckCode(code, treqp, 21);
107 UpgradeSToWLock(&tdc->lock, 637);
108 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
109 /* we can do it locally */
110 code = afs_dir_Delete(tdc, aname);
112 ZapDCE(tdc); /* surprise error -- invalid value */
117 ReleaseWriteLock(&tdc->lock);
118 afs_PutDCache(tdc); /* drop ref count */
120 ReleaseWriteLock(&adp->lock);
122 /* now, get vnode for unlinked dude, and see if we should force it
123 * from cache. adp is now the deleted files vnode. Note that we
124 * call FindVCache instead of GetVCache since if the file's really
125 * gone, we won't be able to fetch the status info anyway. */
127 afs_MarinerLog("store$Removing", tvc);
128 #ifdef AFS_BOZONLOCK_ENV
129 afs_BozonLock(&tvc->pvnLock, tvc);
130 /* Since afs_TryToSmush will do a pvn_vptrunc */
132 ObtainWriteLock(&tvc->lock, 141);
133 /* note that callback will be broken on the deleted file if there are
134 * still >0 links left to it, so we'll get the stat right */
135 tvc->f.m.LinkCount--;
136 tvc->f.states &= ~CUnique; /* For the dfs xlator */
137 if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
138 if (!AFS_NFSXLATORREQ(acred))
139 afs_TryToSmush(tvc, acred, 0);
141 ReleaseWriteLock(&tvc->lock);
142 #ifdef AFS_BOZONLOCK_ENV
143 afs_BozonUnlock(&tvc->pvnLock, tvc);
153 char *name, *sp, *p = ".__afs";
154 afs_int32 rd = afs_random() & 0xffff;
156 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
160 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
167 /* these variables appear to exist for debugging purposes */
168 struct vcache *Tadp1, *Ttvc;
173 /* Note that we don't set CDirty here, this is OK because the unlink
174 * RPC is called synchronously */
176 afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
178 struct vrequest treq;
180 struct VenusFid unlinkFid;
183 afs_size_t offset, len;
184 struct afs_fakestat_state fakestate;
187 AFS_STATCNT(afs_remove);
188 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
189 ICL_TYPE_STRING, aname);
192 if ((code = afs_InitReq(&treq, acred))) {
196 afs_InitFakeStat(&fakestate);
198 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
202 /* Check if this is dynroot */
203 if (afs_IsDynroot(adp)) {
204 code = afs_DynrootVOPRemove(adp, acred, aname);
207 if (afs_IsDynrootMount(adp)) {
212 if (strlen(aname) > AFSNAMEMAX) {
217 code = afs_VerifyVCache(adp, &treq);
220 code = afs_CheckCode(code, &treq, 23);
224 /** If the volume is read-only, return error without making an RPC to the
227 if (adp->f.states & CRO) {
232 /* If we're running disconnected without logging, go no further... */
233 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
238 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
239 ObtainWriteLock(&adp->lock, 142);
241 ObtainSharedLock(&tdc->lock, 638);
244 * Make sure that the data in the cache is current. We may have
245 * received a callback while we were waiting for the write lock.
247 if (!(adp->f.states & CStatd)
248 || (tdc && !hsame(adp->f.m.DataVersion, tdc->f.versionNo))) {
249 ReleaseWriteLock(&adp->lock);
251 ReleaseSharedLock(&tdc->lock);
257 unlinkFid.Fid.Vnode = 0;
259 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
261 /* This should not be necessary since afs_lookup() has already
266 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
268 afs_int32 cached = 0;
270 unlinkFid.Cell = adp->f.fid.Cell;
271 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
272 if (unlinkFid.Fid.Unique == 0) {
274 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
277 ObtainReadLock(&afs_xvcache);
278 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
279 ReleaseReadLock(&afs_xvcache);
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);
290 /* Can't hold a dcache lock whilst we're getting a vcache one */
292 ReleaseSharedLock(&tdc->lock);
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);
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);
303 /* Add removed file vcache to dirty list. */
304 afs_DisconAddDirty(tvc, VDisconRemove, 1);
306 adp->f.m.LinkCount--;
307 ReleaseWriteLock(&tvc->lock);
309 ObtainSharedLock(&tdc->lock, 714);
312 if (tvc && osi_Active(tvc)) {
313 /* about to delete whole file, prefetch it first */
314 ReleaseWriteLock(&adp->lock);
316 ReleaseSharedLock(&tdc->lock);
317 ObtainWriteLock(&tvc->lock, 143);
318 FetchWholeEnchilada(tvc, &treq);
319 ReleaseWriteLock(&tvc->lock);
320 ObtainWriteLock(&adp->lock, 144);
321 /* Technically I don't think we need this back, but let's hold it
322 anyway; The "got" reference should actually be sufficient. */
324 ObtainSharedLock(&tdc->lock, 640);
327 osi_dnlc_remove(adp, aname, tvc);
330 #ifndef AFS_DARWIN80_ENV
331 Tadpr = VREFCOUNT(adp);
336 #ifndef AFS_DARWIN80_ENV
338 Ttvcr = VREFCOUNT(tvc);
341 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
342 && !(tvc->f.states & CUnlinked)) {
344 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
345 && !(tvc->f.states & CUnlinked)) {
347 char *unlname = afs_newname();
349 ReleaseWriteLock(&adp->lock);
351 ReleaseSharedLock(&tdc->lock);
352 code = afsrename(adp, aname, adp, unlname, acred, &treq);
355 struct VenusFid *oldmvid = NULL;
358 tvc->mvid = (struct VenusFid *)unlname;
360 osi_FreeSmallSpace(oldmvid);
366 tvc->f.states |= CUnlinked;
367 /* if rename succeeded, remove should not */
368 ObtainWriteLock(&tvc->lock, 715);
369 if (tvc->f.ddirty_flags & VDisconRemove) {
370 tvc->f.ddirty_flags &= ~VDisconRemove;
372 ReleaseWriteLock(&tvc->lock);
374 osi_FreeSmallSpace(unlname);
380 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
383 afs_PutFakeStat(&fakestate);
384 #ifndef AFS_DARWIN80_ENV
385 /* we can't track by thread, it's not exported in the KPI; only do
387 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
394 /* afs_remunlink -- This tries to delete the file at the server after it has
395 * been renamed when unlinked locally but now has been finally released.
397 * CAUTION -- may be called with avc unheld. */
400 afs_remunlink(struct vcache *avc, int doit)
405 struct vrequest treq;
406 struct VenusFid dirFid;
410 if (NBObtainWriteLock(&avc->lock, 423))
412 #if defined(AFS_DARWIN80_ENV)
413 if (vnode_get(AFSTOV(avc))) {
414 ReleaseWriteLock(&avc->lock);
419 if (avc->mvid && (doit || (avc->f.states & CUnlinkedDel))) {
420 if ((code = afs_InitReq(&treq, avc->uncred))) {
421 ReleaseWriteLock(&avc->lock);
423 /* Must bump the refCount because GetVCache may block.
424 * Also clear mvid so no other thread comes here if we block.
426 unlname = (char *)avc->mvid;
431 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
437 /* We'll only try this once. If it fails, just release the vnode.
438 * Clear after doing hold so that NewVCache doesn't find us yet.
440 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
442 ReleaseWriteLock(&avc->lock);
444 dirFid.Cell = avc->f.fid.Cell;
445 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
446 dirFid.Fid.Vnode = avc->f.parent.vnode;
447 dirFid.Fid.Unique = avc->f.parent.unique;
448 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
451 tdc = afs_FindDCache(adp, (afs_size_t) 0);
452 ObtainWriteLock(&adp->lock, 159);
454 ObtainSharedLock(&tdc->lock, 639);
456 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
457 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
460 /* we failed - and won't be back to try again. */
463 osi_FreeSmallSpace(unlname);
467 #if defined(AFS_DARWIN80_ENV)
468 vnode_put(AFSTOV(avc));
470 ReleaseWriteLock(&avc->lock);