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
22 #include <afsconfig.h>
23 #include "afs/param.h"
28 #include "afs/sysincludes.h" /* Standard vendor system headers */
29 #include "afsincludes.h" /* Afs-based standard headers */
30 #include "afs/afs_stats.h" /* statistics */
31 #include "afs/afs_cbqueue.h"
32 #include "afs/nfsclient.h"
33 #include "afs/afs_osidnlc.h"
36 extern afs_rwlock_t afs_xvcache;
37 extern afs_rwlock_t afs_xcbhash;
42 * Wire down file in cache: prefetch all data, and turn on CWired flag
43 * so that callbacks/callback expirations are (temporarily) ignored
44 * and cache file(s) are kept in cache. File will be unwired when
45 * afs_inactive is called (ie no one has VN_HOLD on vnode), or when
46 * afs_IsWired notices that the file is no longer Active.
49 #else /* AFS_OSF_ENV */
51 FetchWholeEnchilada(avc, areq)
53 struct vrequest *areq;
54 register struct vcache *avc;
56 register afs_int32 nextChunk;
57 register struct dcache *tdc;
58 afs_size_t pos, offset, len;
60 AFS_STATCNT(FetchWholeEnchilada);
61 if ((avc->states & CStatd) == 0)
62 return; /* don't know size */
63 for (nextChunk = 0; nextChunk < 1024; nextChunk++) { /* sanity check on N chunks */
64 pos = AFS_CHUNKTOBASE(nextChunk);
65 #if defined(AFS_OSF_ENV)
66 if (pos >= avc->m.Length)
68 #else /* AFS_OSF_ENV */
69 if (pos >= avc->m.Length)
70 return; /* all done */
72 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
74 #if defined(AFS_OSF_ENV)
76 #else /* AFS_OSF_ENV */
81 #if defined(AFS_OSF_ENV)
82 avc->states |= CWired;
83 #endif /* AFS_OSF_ENV */
86 #if defined(AFS_OSF_ENV)
88 * Tests whether file is wired down, after unwiring the file if it
89 * is found to be inactive (ie not open and not being paged from).
92 register struct vcache *avc;
94 if (avc->states & CWired) {
95 if (osi_Active(avc)) {
98 avc->states &= ~CWired;
102 #endif /* AFS_OSF_ENV */
105 afsremove(register struct vcache *adp, register struct dcache *tdc,
106 register struct vcache *tvc, char *aname, struct AFS_UCRED *acred,
107 struct vrequest *treqp)
109 register afs_int32 code;
110 register struct conn *tc;
111 struct AFSFetchStatus OutDirStatus;
112 struct AFSVolSync tsync;
115 tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
117 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
120 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
121 aname, &OutDirStatus, &tsync);
127 (tc, code, &adp->fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
130 osi_dnlc_remove(adp, aname, tvc);
132 afs_symhint_inval(tvc); /* XXX: don't really need to be so extreme */
136 ReleaseSharedLock(&tdc->lock);
143 ObtainWriteLock(&afs_xcbhash, 497);
144 afs_DequeueCallback(adp);
145 adp->states &= ~CStatd;
146 ReleaseWriteLock(&afs_xcbhash);
147 osi_dnlc_purgedp(adp);
149 ReleaseWriteLock(&adp->lock);
150 code = afs_CheckCode(code, treqp, 21);
154 UpgradeSToWLock(&tdc->lock, 637);
155 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
156 /* we can do it locally */
157 code = afs_dir_Delete(&tdc->f.inode, aname);
159 ZapDCE(tdc); /* surprise error -- invalid value */
164 ReleaseWriteLock(&tdc->lock);
165 afs_PutDCache(tdc); /* drop ref count */
167 ReleaseWriteLock(&adp->lock);
169 /* now, get vnode for unlinked dude, and see if we should force it
170 * from cache. adp is now the deleted files vnode. Note that we
171 * call FindVCache instead of GetVCache since if the file's really
172 * gone, we won't be able to fetch the status info anyway. */
174 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
175 afs_BozonLock(&tvc->pvnLock, tvc);
176 /* Since afs_TryToSmush will do a pvn_vptrunc */
178 ObtainWriteLock(&tvc->lock, 141);
179 /* note that callback will be broken on the deleted file if there are
180 * still >0 links left to it, so we'll get the stat right */
182 tvc->states &= ~CUnique; /* For the dfs xlator */
183 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
184 if (!AFS_NFSXLATORREQ(acred))
185 afs_TryToSmush(tvc, acred, 0);
187 ReleaseWriteLock(&tvc->lock);
188 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
189 afs_BozonUnlock(&tvc->pvnLock, tvc);
199 char *name, *sp, *p = ".__afs";
200 afs_int32 rd = afs_random() & 0xffff;
202 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
206 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
213 /* these variables appear to exist for debugging purposes */
214 struct vcache *Tadp1, *Ttvc;
219 /* Note that we don't set CDirty here, this is OK because the unlink
220 * RPC is called synchronously */
224 struct nameidata *ndp;
226 register struct vcache *adp = VTOAFS(ndp->ni_dvp);
227 char *aname = ndp->ni_dent.d_name;
228 struct ucred *acred = ndp->ni_cred;
229 #else /* AFS_OSF_ENV */
230 afs_remove(OSI_VC_ARG(adp), aname, acred)
233 struct AFS_UCRED *acred;
236 struct vrequest treq;
237 register struct dcache *tdc;
238 struct VenusFid unlinkFid;
239 register afs_int32 code;
240 register struct vcache *tvc;
241 afs_size_t offset, len;
242 struct afs_fakestat_state fakestate;
245 AFS_STATCNT(afs_remove);
246 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
247 ICL_TYPE_STRING, aname);
250 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
253 if ((code = afs_InitReq(&treq, acred))) {
261 afs_InitFakeStat(&fakestate);
262 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
264 afs_PutFakeStat(&fakestate);
272 /* Check if this is dynroot */
273 if (afs_IsDynroot(adp)) {
274 code = afs_DynrootVOPRemove(adp, acred, aname);
275 afs_PutFakeStat(&fakestate);
284 if (adp->mvstat == 2) {
285 afs_PutFakeStat(&fakestate);
294 if (strlen(aname) > AFSNAMEMAX) {
295 afs_PutFakeStat(&fakestate);
303 code = afs_VerifyVCache(adp, &treq);
305 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
309 afs_PutFakeStat(&fakestate);
310 return afs_CheckCode(code, &treq, 22);
312 #else /* AFS_OSF_ENV */
315 code = afs_CheckCode(code, &treq, 23);
316 afs_PutFakeStat(&fakestate);
321 /** If the volume is read-only, return error without making an RPC to the
324 if (adp->states & CRO) {
330 afs_PutFakeStat(&fakestate);
334 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
335 ObtainWriteLock(&adp->lock, 142);
337 ObtainSharedLock(&tdc->lock, 638);
340 * Make sure that the data in the cache is current. We may have
341 * received a callback while we were waiting for the write lock.
343 if (!(adp->states & CStatd)
344 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
345 ReleaseWriteLock(&adp->lock);
347 ReleaseSharedLock(&tdc->lock);
353 unlinkFid.Fid.Vnode = 0;
355 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
357 /* This should not be necessary since afs_lookup() has already
361 code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
363 afs_int32 cached = 0;
365 unlinkFid.Cell = adp->fid.Cell;
366 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
367 if (unlinkFid.Fid.Unique == 0) {
369 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
372 ObtainReadLock(&afs_xvcache);
373 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
374 ReleaseReadLock(&afs_xvcache);
379 if (tvc && osi_Active(tvc)) {
380 /* about to delete whole file, prefetch it first */
381 ReleaseWriteLock(&adp->lock);
382 ObtainWriteLock(&tvc->lock, 143);
383 #if defined(AFS_OSF_ENV)
384 afs_Wire(tvc, &treq);
385 #else /* AFS_OSF_ENV */
386 FetchWholeEnchilada(tvc, &treq);
388 ReleaseWriteLock(&tvc->lock);
389 ObtainWriteLock(&adp->lock, 144);
392 osi_dnlc_remove(adp, aname, tvc);
394 afs_symhint_inval(tvc);
397 Tadpr = VREFCOUNT(adp);
402 Ttvcr = VREFCOUNT(tvc);
404 if (tvc && (VREFCOUNT(tvc) > 2) && tvc->opens > 0
405 && !(tvc->states & CUnlinked))
407 #ifdef AFS_DARWIN14_ENV
408 if (tvc && (VREFCOUNT(tvc) > 1 + DARWIN_REFBASE) && tvc->opens > 0 && !(tvc->states & CUnlinked))
410 if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0
411 && !(tvc->states & CUnlinked))
415 char *unlname = newname();
417 ReleaseWriteLock(&adp->lock);
419 ReleaseSharedLock(&tdc->lock);
420 code = afsrename(adp, aname, adp, unlname, acred, &treq);
423 tvc->mvid = (struct VenusFid *)unlname;
429 tvc->states |= CUnlinked;
431 osi_FreeSmallSpace(unlname);
437 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
441 #endif /* AFS_OSF_ENV */
442 afs_PutFakeStat(&fakestate);
447 /* afs_remunlink -- This tries to delete the file at the server after it has
448 * been renamed when unlinked locally but now has been finally released.
450 * CAUTION -- may be called with avc unheld. */
453 afs_remunlink(register struct vcache *avc, register int doit)
455 struct AFS_UCRED *cred;
458 struct vrequest treq;
459 struct VenusFid dirFid;
460 register struct dcache *tdc;
464 if (NBObtainWriteLock(&avc->lock, 423))
467 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
468 if ((code = afs_InitReq(&treq, avc->uncred))) {
469 ReleaseWriteLock(&avc->lock);
471 /* Must bump the refCount because GetVCache may block.
472 * Also clear mvid so no other thread comes here if we block.
474 unlname = (char *)avc->mvid;
479 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN14_ENV)
480 /* this is called by vrele (via VOP_INACTIVE) when the refcount
481 * is 0. we can't just call VN_HOLD since vref will panic.
482 * we can't just call osi_vnhold because a later AFS_RELE will call
483 * vrele again, which will try to call VOP_INACTIVE again after
484 * vn_locking the vnode. which would be fine except that our vrele
485 * caller also locked the vnode... So instead, we just gimmick the
486 * refcounts and hope nobody else can touch the file now */
487 osi_Assert(VREFCOUNT(avc) == 0);
488 VREFCOUNT_SET(avc, 1);
490 VN_HOLD(AFSTOV(avc));
492 /* We'll only try this once. If it fails, just release the vnode.
493 * Clear after doing hold so that NewVCache doesn't find us yet.
495 avc->states &= ~(CUnlinked | CUnlinkedDel);
497 #ifdef AFS_DARWIN14_ENV
498 if (VREFCOUNT(avc) < 4) {
499 oldref = 4 - VREFCOUNT(avc);
500 VREFCOUNT_SET(avc, 4);
503 ReleaseWriteLock(&avc->lock);
505 dirFid.Cell = avc->fid.Cell;
506 dirFid.Fid.Volume = avc->fid.Fid.Volume;
507 dirFid.Fid.Vnode = avc->parentVnode;
508 dirFid.Fid.Unique = avc->parentUnique;
509 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
512 tdc = afs_FindDCache(adp, (afs_size_t) 0);
513 ObtainWriteLock(&adp->lock, 159);
515 ObtainSharedLock(&tdc->lock, 639);
517 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
518 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
521 /* we failed - and won't be back to try again. */
524 osi_FreeSmallSpace(unlname);
526 #ifdef AFS_DARWIN_ENV
527 #ifndef AFS_DARWIN14_ENV
528 osi_Assert(VREFCOUNT(avc) == 1);
529 VREFCOUNT_SET(avc, 0);
532 int newref = VREFCOUNT(avc) - oldref;
533 VREFCOUNT_SET(avc, newref);
539 ReleaseWriteLock(&avc->lock);