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. */
128 afs_MarinerLog("store$Removing", tvc);
129 ObtainWriteLock(&tvc->lock, 141);
130 /* note that callback will be broken on the deleted file if there are
131 * still >0 links left to it, so we'll get the stat right */
132 tvc->f.m.LinkCount--;
133 tvc->f.states &= ~CUnique; /* For the dfs xlator */
134 if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
135 if (!AFS_NFSXLATORREQ(acred))
136 afs_TryToSmush(tvc, acred, 0);
138 ReleaseWriteLock(&tvc->lock);
147 char *name, *sp, *p = ".__afs";
148 afs_int32 rd = afs_random() & 0xffff;
150 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
154 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
161 /* these variables appear to exist for debugging purposes */
162 struct vcache *Tadp1, *Ttvc;
167 /* Note that we don't set CDirty here, this is OK because the unlink
168 * RPC is called synchronously */
170 afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
172 struct vrequest *treq = NULL;
174 struct VenusFid unlinkFid;
177 afs_size_t offset, len;
178 struct afs_fakestat_state fakestate;
181 AFS_STATCNT(afs_remove);
182 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
183 ICL_TYPE_STRING, aname);
186 if ((code = afs_CreateReq(&treq, acred))) {
190 afs_InitFakeStat(&fakestate);
192 code = afs_EvalFakeStat(&adp, &fakestate, treq);
196 /* Check if this is dynroot */
197 if (afs_IsDynroot(adp)) {
198 code = afs_DynrootVOPRemove(adp, acred, aname);
201 if (afs_IsDynrootMount(adp)) {
206 if (strlen(aname) > AFSNAMEMAX) {
211 code = afs_VerifyVCache(adp, treq);
214 code = afs_CheckCode(code, treq, 23);
218 /** If the volume is read-only, return error without making an RPC to the
221 if (adp->f.states & CRO) {
226 /* If we're running disconnected without logging, go no further... */
227 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
232 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1); /* test for error below */
233 ObtainWriteLock(&adp->lock, 142);
235 ObtainSharedLock(&tdc->lock, 638);
238 * Make sure that the data in the cache is current. We may have
239 * received a callback while we were waiting for the write lock.
241 if (!(adp->f.states & CStatd)
242 || (tdc && !hsame(adp->f.m.DataVersion, tdc->f.versionNo))) {
243 ReleaseWriteLock(&adp->lock);
245 ReleaseSharedLock(&tdc->lock);
251 unlinkFid.Fid.Vnode = 0;
253 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
255 /* This should not be necessary since afs_lookup() has already
260 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
262 afs_int32 cached = 0;
264 unlinkFid.Cell = adp->f.fid.Cell;
265 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
266 if (unlinkFid.Fid.Unique == 0) {
268 afs_LookupVCache(&unlinkFid, treq, &cached, adp,
271 ObtainReadLock(&afs_xvcache);
272 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
273 ReleaseReadLock(&afs_xvcache);
278 if (AFS_IS_DISCON_RW) {
279 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
280 /* Make shadow copy of parent dir. */
281 afs_MakeShadowDir(adp, tdc);
284 /* Can't hold a dcache lock whilst we're getting a vcache one */
286 ReleaseSharedLock(&tdc->lock);
288 /* XXX - We're holding adp->lock still, and we've got no
289 * guarantee about whether the ordering matches the lock hierarchy */
290 ObtainWriteLock(&tvc->lock, 713);
292 /* If we were locally created, then we don't need to do very
293 * much beyond ensuring that we don't exist anymore */
294 if (tvc->f.ddirty_flags & VDisconCreate) {
295 afs_DisconRemoveDirty(tvc);
297 /* Add removed file vcache to dirty list. */
298 afs_DisconAddDirty(tvc, VDisconRemove, 1);
300 adp->f.m.LinkCount--;
301 ReleaseWriteLock(&tvc->lock);
303 ObtainSharedLock(&tdc->lock, 714);
306 if (tvc && osi_Active(tvc)) {
307 /* about to delete whole file, prefetch it first */
308 ReleaseWriteLock(&adp->lock);
310 ReleaseSharedLock(&tdc->lock);
311 ObtainWriteLock(&tvc->lock, 143);
312 FetchWholeEnchilada(tvc, treq);
313 ReleaseWriteLock(&tvc->lock);
314 ObtainWriteLock(&adp->lock, 144);
315 /* Technically I don't think we need this back, but let's hold it
316 anyway; The "got" reference should actually be sufficient. */
318 ObtainSharedLock(&tdc->lock, 640);
321 osi_dnlc_remove(adp, aname, tvc);
324 #ifndef AFS_DARWIN80_ENV
325 Tadpr = VREFCOUNT(adp);
330 #ifndef AFS_DARWIN80_ENV
332 Ttvcr = VREFCOUNT(tvc);
335 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
336 && !(tvc->f.states & CUnlinked)) {
338 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
339 && !(tvc->f.states & CUnlinked)) {
341 char *unlname = afs_newname();
343 ReleaseWriteLock(&adp->lock);
345 ReleaseSharedLock(&tdc->lock);
346 code = afsrename(adp, aname, adp, unlname, acred, treq);
349 struct VenusFid *oldmvid = NULL;
352 tvc->mvid = (struct VenusFid *)unlname;
354 osi_FreeSmallSpace(oldmvid);
360 tvc->f.states |= CUnlinked;
361 /* if rename succeeded, remove should not */
362 ObtainWriteLock(&tvc->lock, 715);
363 if (tvc->f.ddirty_flags & VDisconRemove) {
364 tvc->f.ddirty_flags &= ~VDisconRemove;
366 ReleaseWriteLock(&tvc->lock);
368 osi_FreeSmallSpace(unlname);
374 code = afsremove(adp, tdc, tvc, aname, acred, treq);
377 afs_PutFakeStat(&fakestate);
378 #if !defined(AFS_DARWIN80_ENV) && !defined(UKERNEL)
379 /* we can't track by thread, it's not exported in the KPI; only do
381 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
384 afs_DestroyReq(treq);
389 /* afs_remunlink -- This tries to delete the file at the server after it has
390 * been renamed when unlinked locally but now has been finally released.
392 * CAUTION -- may be called with avc unheld. */
395 afs_remunlink(struct vcache *avc, int doit)
400 struct VenusFid dirFid;
404 if (NBObtainWriteLock(&avc->lock, 423))
406 #if defined(AFS_DARWIN80_ENV)
407 if (vnode_get(AFSTOV(avc))) {
408 ReleaseWriteLock(&avc->lock);
413 if (avc->mvid && (doit || (avc->f.states & CUnlinkedDel))) {
414 struct vrequest *treq = NULL;
416 if ((code = afs_CreateReq(&treq, avc->uncred))) {
417 ReleaseWriteLock(&avc->lock);
419 /* Must bump the refCount because GetVCache may block.
420 * Also clear mvid so no other thread comes here if we block.
422 unlname = (char *)avc->mvid;
427 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
433 /* We'll only try this once. If it fails, just release the vnode.
434 * Clear after doing hold so that NewVCache doesn't find us yet.
436 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
438 ReleaseWriteLock(&avc->lock);
440 dirFid.Cell = avc->f.fid.Cell;
441 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
442 dirFid.Fid.Vnode = avc->f.parent.vnode;
443 dirFid.Fid.Unique = avc->f.parent.unique;
444 adp = afs_GetVCache(&dirFid, treq, NULL, NULL);
447 tdc = afs_FindDCache(adp, (afs_size_t) 0);
448 ObtainWriteLock(&adp->lock, 159);
450 ObtainSharedLock(&tdc->lock, 639);
452 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
453 code = afsremove(adp, tdc, avc, unlname, cred, treq);
456 /* we failed - and won't be back to try again. */
459 osi_FreeSmallSpace(unlname);
461 afs_DestroyReq(treq);
464 #if defined(AFS_DARWIN80_ENV)
465 vnode_put(AFSTOV(avc));
467 ReleaseWriteLock(&avc->lock);