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;
34 FetchWholeEnchilada(struct vcache *avc, struct vrequest *areq)
38 afs_size_t pos, offset, len;
40 AFS_STATCNT(FetchWholeEnchilada);
41 if ((avc->f.states & CStatd) == 0)
42 return; /* don't know size */
43 for (nextChunk = 0; nextChunk < 1024; nextChunk++) { /* sanity check on N chunks */
44 pos = AFS_CHUNKTOBASE(nextChunk);
45 if (pos >= avc->f.m.Length)
46 return; /* all done */
47 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
55 afsremove(struct vcache *adp, struct dcache *tdc,
56 struct vcache *tvc, char *aname, afs_ucred_t *acred,
57 struct vrequest *treqp)
61 struct AFSFetchStatus OutDirStatus;
62 struct AFSVolSync tsync;
63 struct rx_connection *rxconn;
65 if (!AFS_IS_DISCONNECTED) {
67 tc = afs_Conn(&adp->f.fid, treqp, SHARED_LOCK, &rxconn);
69 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
72 RXAFS_RemoveFile(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
73 aname, &OutDirStatus, &tsync);
79 (tc, rxconn, code, &adp->f.fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
83 osi_dnlc_remove(adp, aname, tvc);
87 ReleaseSharedLock(&tdc->lock);
97 ReleaseWriteLock(&adp->lock);
98 code = afs_CheckCode(code, treqp, 21);
102 UpgradeSToWLock(&tdc->lock, 637);
103 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
104 /* we can do it locally */
105 code = afs_dir_Delete(tdc, aname);
107 ZapDCE(tdc); /* surprise error -- invalid value */
112 ReleaseWriteLock(&tdc->lock);
113 afs_PutDCache(tdc); /* drop ref count */
115 ReleaseWriteLock(&adp->lock);
117 /* now, get vnode for unlinked dude, and see if we should force it
118 * from cache. adp is now the deleted files vnode. Note that we
119 * call FindVCache instead of GetVCache since if the file's really
120 * gone, we won't be able to fetch the status info anyway. */
123 afs_MarinerLog("store$Removing", tvc);
124 ObtainWriteLock(&tvc->lock, 141);
125 /* note that callback will be broken on the deleted file if there are
126 * still >0 links left to it, so we'll get the stat right */
127 tvc->f.m.LinkCount--;
128 tvc->f.states &= ~CUnique; /* For the dfs xlator */
129 if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
130 if (!AFS_NFSXLATORREQ(acred))
131 afs_TryToSmush(tvc, acred, 0);
133 ReleaseWriteLock(&tvc->lock);
142 char *name, *sp, *p = ".__afs";
143 afs_int32 rd = afs_random() & 0xffff;
145 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
149 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
156 /* these variables appear to exist for debugging purposes */
157 struct vcache *Tadp1, *Ttvc;
162 /* Note that we don't set CDirty here, this is OK because the unlink
163 * RPC is called synchronously */
165 afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
167 struct vrequest *treq = NULL;
169 struct VenusFid unlinkFid;
172 afs_size_t offset, len;
173 struct afs_fakestat_state fakestate;
176 AFS_STATCNT(afs_remove);
177 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
178 ICL_TYPE_STRING, aname);
181 if ((code = afs_CreateReq(&treq, acred))) {
185 afs_InitFakeStat(&fakestate);
187 code = afs_EvalFakeStat(&adp, &fakestate, treq);
191 /* Check if this is dynroot */
192 if (afs_IsDynroot(adp)) {
193 code = afs_DynrootVOPRemove(adp, acred, aname);
196 if (afs_IsDynrootMount(adp)) {
201 if (strlen(aname) > AFSNAMEMAX) {
206 code = afs_VerifyVCache(adp, treq);
209 code = afs_CheckCode(code, treq, 23);
213 /** If the volume is read-only, return error without making an RPC to the
216 if (adp->f.states & CRO) {
221 /* If we're running disconnected without logging, go no further... */
222 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
227 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1); /* test for error below */
228 ObtainWriteLock(&adp->lock, 142);
230 ObtainSharedLock(&tdc->lock, 638);
233 * Make sure that the data in the cache is current. We may have
234 * received a callback while we were waiting for the write lock.
236 if (!(adp->f.states & CStatd)
237 || (tdc && !afs_IsDCacheFresh(tdc, adp))) {
238 ReleaseWriteLock(&adp->lock);
240 ReleaseSharedLock(&tdc->lock);
246 unlinkFid.Fid.Vnode = 0;
248 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
250 /* This should not be necessary since afs_lookup() has already
255 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
257 unlinkFid.Cell = adp->f.fid.Cell;
258 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
259 if (unlinkFid.Fid.Unique == 0) {
261 afs_LookupVCache(&unlinkFid, treq, adp,
264 ObtainReadLock(&afs_xvcache);
265 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
266 ReleaseReadLock(&afs_xvcache);
271 if (AFS_IS_DISCON_RW) {
272 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
273 /* Make shadow copy of parent dir. */
274 afs_MakeShadowDir(adp, tdc);
277 /* Can't hold a dcache lock whilst we're getting a vcache one */
279 ReleaseSharedLock(&tdc->lock);
281 /* XXX - We're holding adp->lock still, and we've got no
282 * guarantee about whether the ordering matches the lock hierarchy */
283 ObtainWriteLock(&tvc->lock, 713);
285 /* If we were locally created, then we don't need to do very
286 * much beyond ensuring that we don't exist anymore */
287 if (tvc->f.ddirty_flags & VDisconCreate) {
288 afs_DisconRemoveDirty(tvc);
290 /* Add removed file vcache to dirty list. */
291 afs_DisconAddDirty(tvc, VDisconRemove, 1);
293 adp->f.m.LinkCount--;
294 ReleaseWriteLock(&tvc->lock);
296 ObtainSharedLock(&tdc->lock, 714);
299 if (tvc && osi_Active(tvc)) {
300 /* about to delete whole file, prefetch it first */
301 ReleaseWriteLock(&adp->lock);
303 ReleaseSharedLock(&tdc->lock);
304 ObtainWriteLock(&tvc->lock, 143);
305 FetchWholeEnchilada(tvc, treq);
306 ReleaseWriteLock(&tvc->lock);
307 ObtainWriteLock(&adp->lock, 144);
308 /* Technically I don't think we need this back, but let's hold it
309 anyway; The "got" reference should actually be sufficient. */
311 ObtainSharedLock(&tdc->lock, 640);
314 osi_dnlc_remove(adp, aname, tvc);
317 #ifndef AFS_DARWIN80_ENV
318 Tadpr = VREFCOUNT(adp);
323 #ifndef AFS_DARWIN80_ENV
325 Ttvcr = VREFCOUNT(tvc);
328 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
329 && !(tvc->f.states & CUnlinked)) {
331 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
332 && !(tvc->f.states & CUnlinked)) {
334 char *unlname = afs_newname();
336 ReleaseWriteLock(&adp->lock);
338 ReleaseSharedLock(&tdc->lock);
339 code = afsrename(adp, aname, adp, unlname, acred, treq);
342 void *oldmvid = NULL;
343 if (tvc->mvid.silly_name)
344 oldmvid = tvc->mvid.silly_name;
345 tvc->mvid.silly_name = unlname;
347 osi_FreeSmallSpace(oldmvid);
353 tvc->f.states |= CUnlinked;
354 /* if rename succeeded, remove should not */
355 ObtainWriteLock(&tvc->lock, 715);
356 if (tvc->f.ddirty_flags & VDisconRemove) {
357 tvc->f.ddirty_flags &= ~VDisconRemove;
359 ReleaseWriteLock(&tvc->lock);
361 osi_FreeSmallSpace(unlname);
367 code = afsremove(adp, tdc, tvc, aname, acred, treq);
370 afs_PutFakeStat(&fakestate);
371 #if !defined(AFS_DARWIN80_ENV) && !defined(UKERNEL)
372 /* we can't track by thread, it's not exported in the KPI; only do
374 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
377 afs_DestroyReq(treq);
382 /* afs_remunlink -- This tries to delete the file at the server after it has
383 * been renamed when unlinked locally but now has been finally released.
385 * CAUTION -- may be called with avc unheld. */
388 afs_remunlink(struct vcache *avc, int doit)
393 struct VenusFid dirFid;
397 if (NBObtainWriteLock(&avc->lock, 423))
399 #if defined(AFS_DARWIN80_ENV)
400 if (vnode_get(AFSTOV(avc))) {
401 ReleaseWriteLock(&avc->lock);
406 if (avc->mvid.silly_name && (doit || (avc->f.states & CUnlinkedDel))) {
407 struct vrequest *treq = NULL;
409 if ((code = afs_CreateReq(&treq, avc->uncred))) {
410 ReleaseWriteLock(&avc->lock);
412 /* Must bump the refCount because GetVCache may block.
413 * Also clear mvid so no other thread comes here if we block.
415 unlname = avc->mvid.silly_name;
416 avc->mvid.silly_name = NULL;
420 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
423 osi_Assert(osi_vnhold(avc) == 0);
426 /* We'll only try this once. If it fails, just release the vnode.
427 * Clear after doing hold so that NewVCache doesn't find us yet.
429 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
431 ReleaseWriteLock(&avc->lock);
433 dirFid.Cell = avc->f.fid.Cell;
434 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
435 dirFid.Fid.Vnode = avc->f.parent.vnode;
436 dirFid.Fid.Unique = avc->f.parent.unique;
437 adp = afs_GetVCache(&dirFid, treq);
440 tdc = afs_FindDCache(adp, (afs_size_t) 0);
441 ObtainWriteLock(&adp->lock, 159);
443 ObtainSharedLock(&tdc->lock, 639);
445 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
446 code = afsremove(adp, tdc, avc, unlname, cred, treq);
449 /* we failed - and won't be back to try again. */
452 osi_FreeSmallSpace(unlname);
454 afs_DestroyReq(treq);
457 #if defined(AFS_DARWIN80_ENV)
458 vnode_put(AFSTOV(avc));
460 ReleaseWriteLock(&avc->lock);