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);
98 ReleaseWriteLock(&adp->lock);
99 code = afs_CheckCode(code, treqp, 21);
103 UpgradeSToWLock(&tdc->lock, 637);
104 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
105 /* we can do it locally */
106 code = afs_dir_Delete(tdc, aname);
108 ZapDCE(tdc); /* surprise error -- invalid value */
113 ReleaseWriteLock(&tdc->lock);
114 afs_PutDCache(tdc); /* drop ref count */
116 ReleaseWriteLock(&adp->lock);
118 /* now, get vnode for unlinked dude, and see if we should force it
119 * from cache. adp is now the deleted files vnode. Note that we
120 * call FindVCache instead of GetVCache since if the file's really
121 * gone, we won't be able to fetch the status info anyway. */
124 afs_MarinerLog("store$Removing", tvc);
125 ObtainWriteLock(&tvc->lock, 141);
126 /* note that callback will be broken on the deleted file if there are
127 * still >0 links left to it, so we'll get the stat right */
128 tvc->f.m.LinkCount--;
129 tvc->f.states &= ~CUnique; /* For the dfs xlator */
130 if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
131 if (!AFS_NFSXLATORREQ(acred))
132 afs_TryToSmush(tvc, acred, 0);
134 ReleaseWriteLock(&tvc->lock);
143 char *name, *sp, *p = ".__afs";
144 afs_int32 rd = afs_random() & 0xffff;
146 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
150 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
157 /* these variables appear to exist for debugging purposes */
158 struct vcache *Tadp1, *Ttvc;
163 /* Note that we don't set CDirty here, this is OK because the unlink
164 * RPC is called synchronously */
166 afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
168 struct vrequest *treq = NULL;
170 struct VenusFid unlinkFid;
173 afs_size_t offset, len;
174 struct afs_fakestat_state fakestate;
177 AFS_STATCNT(afs_remove);
178 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
179 ICL_TYPE_STRING, aname);
182 if ((code = afs_CreateReq(&treq, acred))) {
186 afs_InitFakeStat(&fakestate);
188 code = afs_EvalFakeStat(&adp, &fakestate, treq);
192 /* Check if this is dynroot */
193 if (afs_IsDynroot(adp)) {
194 code = afs_DynrootVOPRemove(adp, acred, aname);
197 if (afs_IsDynrootMount(adp)) {
202 if (strlen(aname) > AFSNAMEMAX) {
207 code = afs_VerifyVCache(adp, treq);
210 code = afs_CheckCode(code, treq, 23);
214 /** If the volume is read-only, return error without making an RPC to the
217 if (adp->f.states & CRO) {
222 /* If we're running disconnected without logging, go no further... */
223 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
228 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1); /* test for error below */
229 ObtainWriteLock(&adp->lock, 142);
231 ObtainSharedLock(&tdc->lock, 638);
234 * Make sure that the data in the cache is current. We may have
235 * received a callback while we were waiting for the write lock.
237 if (!(adp->f.states & CStatd)
238 || (tdc && !afs_IsDCacheFresh(tdc, adp))) {
239 ReleaseWriteLock(&adp->lock);
241 ReleaseSharedLock(&tdc->lock);
247 unlinkFid.Fid.Vnode = 0;
249 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
251 /* This should not be necessary since afs_lookup() has already
256 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
258 unlinkFid.Cell = adp->f.fid.Cell;
259 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
260 if (unlinkFid.Fid.Unique == 0) {
262 afs_LookupVCache(&unlinkFid, treq, adp,
265 ObtainReadLock(&afs_xvcache);
266 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
267 ReleaseReadLock(&afs_xvcache);
272 if (AFS_IS_DISCON_RW) {
273 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
274 /* Make shadow copy of parent dir. */
275 afs_MakeShadowDir(adp, tdc);
278 /* Can't hold a dcache lock whilst we're getting a vcache one */
280 ReleaseSharedLock(&tdc->lock);
282 /* XXX - We're holding adp->lock still, and we've got no
283 * guarantee about whether the ordering matches the lock hierarchy */
284 ObtainWriteLock(&tvc->lock, 713);
286 /* If we were locally created, then we don't need to do very
287 * much beyond ensuring that we don't exist anymore */
288 if (tvc->f.ddirty_flags & VDisconCreate) {
289 afs_DisconRemoveDirty(tvc);
291 /* Add removed file vcache to dirty list. */
292 afs_DisconAddDirty(tvc, VDisconRemove, 1);
294 adp->f.m.LinkCount--;
295 ReleaseWriteLock(&tvc->lock);
297 ObtainSharedLock(&tdc->lock, 714);
300 if (tvc && osi_Active(tvc)) {
301 /* about to delete whole file, prefetch it first */
302 ReleaseWriteLock(&adp->lock);
304 ReleaseSharedLock(&tdc->lock);
305 ObtainWriteLock(&tvc->lock, 143);
306 FetchWholeEnchilada(tvc, treq);
307 ReleaseWriteLock(&tvc->lock);
308 ObtainWriteLock(&adp->lock, 144);
309 /* Technically I don't think we need this back, but let's hold it
310 anyway; The "got" reference should actually be sufficient. */
312 ObtainSharedLock(&tdc->lock, 640);
315 osi_dnlc_remove(adp, aname, tvc);
318 #ifndef AFS_DARWIN80_ENV
319 Tadpr = VREFCOUNT(adp);
324 #ifndef AFS_DARWIN80_ENV
326 Ttvcr = VREFCOUNT(tvc);
329 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
330 && !(tvc->f.states & CUnlinked)) {
332 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
333 && !(tvc->f.states & CUnlinked)) {
335 char *unlname = afs_newname();
337 ReleaseWriteLock(&adp->lock);
339 ReleaseSharedLock(&tdc->lock);
340 code = afsrename(adp, aname, adp, unlname, acred, treq);
343 void *oldmvid = NULL;
344 if (tvc->mvid.silly_name)
345 oldmvid = tvc->mvid.silly_name;
346 tvc->mvid.silly_name = unlname;
348 osi_FreeSmallSpace(oldmvid);
354 tvc->f.states |= CUnlinked;
355 /* if rename succeeded, remove should not */
356 ObtainWriteLock(&tvc->lock, 715);
357 if (tvc->f.ddirty_flags & VDisconRemove) {
358 tvc->f.ddirty_flags &= ~VDisconRemove;
360 ReleaseWriteLock(&tvc->lock);
362 osi_FreeSmallSpace(unlname);
368 code = afsremove(adp, tdc, tvc, aname, acred, treq);
371 afs_PutFakeStat(&fakestate);
372 #if !defined(AFS_DARWIN80_ENV) && !defined(UKERNEL)
373 /* we can't track by thread, it's not exported in the KPI; only do
375 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
378 afs_DestroyReq(treq);
383 /* afs_remunlink -- This tries to delete the file at the server after it has
384 * been renamed when unlinked locally but now has been finally released.
386 * CAUTION -- may be called with avc unheld. */
389 afs_remunlink(struct vcache *avc, int doit)
394 struct VenusFid dirFid;
398 if (NBObtainWriteLock(&avc->lock, 423))
400 #if defined(AFS_DARWIN80_ENV)
401 if (vnode_get(AFSTOV(avc))) {
402 ReleaseWriteLock(&avc->lock);
407 if (avc->mvid.silly_name && (doit || (avc->f.states & CUnlinkedDel))) {
408 struct vrequest *treq = NULL;
410 if ((code = afs_CreateReq(&treq, avc->uncred))) {
411 ReleaseWriteLock(&avc->lock);
413 /* Must bump the refCount because GetVCache may block.
414 * Also clear mvid so no other thread comes here if we block.
416 unlname = avc->mvid.silly_name;
417 avc->mvid.silly_name = NULL;
421 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
424 osi_Assert(osi_vnhold(avc) == 0);
427 /* We'll only try this once. If it fails, just release the vnode.
428 * Clear after doing hold so that NewVCache doesn't find us yet.
430 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
432 ReleaseWriteLock(&avc->lock);
434 dirFid.Cell = avc->f.fid.Cell;
435 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
436 dirFid.Fid.Vnode = avc->f.parent.vnode;
437 dirFid.Fid.Unique = avc->f.parent.unique;
438 adp = afs_GetVCache(&dirFid, treq);
441 tdc = afs_FindDCache(adp, (afs_size_t) 0);
442 ObtainWriteLock(&adp->lock, 159);
444 ObtainSharedLock(&tdc->lock, 639);
446 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
447 code = afsremove(adp, tdc, avc, unlname, cred, treq);
450 /* we failed - and won't be back to try again. */
453 osi_FreeSmallSpace(unlname);
455 afs_DestroyReq(treq);
458 #if defined(AFS_DARWIN80_ENV)
459 vnode_put(AFSTOV(avc));
461 ReleaseWriteLock(&avc->lock);