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);
283 if (adp->mvstat == 1) {
291 if (strlen(aname) > AFSNAMEMAX) {
292 afs_PutFakeStat(&fakestate);
300 code = afs_VerifyVCache(adp, &treq);
302 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
306 afs_PutFakeStat(&fakestate);
307 return afs_CheckCode(code, &treq, 22);
309 #else /* AFS_OSF_ENV */
312 code = afs_CheckCode(code, &treq, 23);
313 afs_PutFakeStat(&fakestate);
318 /** If the volume is read-only, return error without making an RPC to the
321 if (adp->states & CRO) {
327 afs_PutFakeStat(&fakestate);
331 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
332 ObtainWriteLock(&adp->lock, 142);
334 ObtainSharedLock(&tdc->lock, 638);
337 * Make sure that the data in the cache is current. We may have
338 * received a callback while we were waiting for the write lock.
340 if (!(adp->states & CStatd)
341 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
342 ReleaseWriteLock(&adp->lock);
344 ReleaseSharedLock(&tdc->lock);
350 unlinkFid.Fid.Vnode = 0;
352 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
354 /* This should not be necessary since afs_lookup() has already
358 code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
360 afs_int32 cached = 0;
362 unlinkFid.Cell = adp->fid.Cell;
363 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
364 if (unlinkFid.Fid.Unique == 0) {
366 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
369 ObtainReadLock(&afs_xvcache);
370 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
371 ReleaseReadLock(&afs_xvcache);
376 if (tvc && osi_Active(tvc)) {
377 /* about to delete whole file, prefetch it first */
378 ReleaseWriteLock(&adp->lock);
379 ObtainWriteLock(&tvc->lock, 143);
380 #if defined(AFS_OSF_ENV)
381 afs_Wire(tvc, &treq);
382 #else /* AFS_OSF_ENV */
383 FetchWholeEnchilada(tvc, &treq);
385 ReleaseWriteLock(&tvc->lock);
386 ObtainWriteLock(&adp->lock, 144);
389 osi_dnlc_remove(adp, aname, tvc);
391 afs_symhint_inval(tvc);
394 Tadpr = VREFCOUNT(adp);
399 Ttvcr = VREFCOUNT(tvc);
401 if (tvc && (VREFCOUNT(tvc) > 2) && tvc->opens > 0
402 && !(tvc->states & CUnlinked)) {
404 if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0
405 && !(tvc->states & CUnlinked)) {
407 char *unlname = newname();
409 ReleaseWriteLock(&adp->lock);
411 ReleaseSharedLock(&tdc->lock);
412 code = afsrename(adp, aname, adp, unlname, acred, &treq);
415 tvc->mvid = (struct VenusFid *)unlname;
421 tvc->states |= CUnlinked;
423 osi_FreeSmallSpace(unlname);
429 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
433 #endif /* AFS_OSF_ENV */
434 afs_PutFakeStat(&fakestate);
439 /* afs_remunlink -- This tries to delete the file at the server after it has
440 * been renamed when unlinked locally but now has been finally released.
442 * CAUTION -- may be called with avc unheld. */
445 afs_remunlink(register struct vcache *avc, register int doit)
447 struct AFS_UCRED *cred;
450 struct vrequest treq;
451 struct VenusFid dirFid;
452 register struct dcache *tdc;
455 if (NBObtainWriteLock(&avc->lock, 423))
458 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
459 if ((code = afs_InitReq(&treq, avc->uncred))) {
460 ReleaseWriteLock(&avc->lock);
462 /* Must bump the refCount because GetVCache may block.
463 * Also clear mvid so no other thread comes here if we block.
465 unlname = (char *)avc->mvid;
470 #ifdef AFS_DARWIN_ENV
471 /* this is called by vrele (via VOP_INACTIVE) when the refcount
472 * is 0. we can't just call VN_HOLD since vref will panic.
473 * we can't just call osi_vnhold because a later AFS_RELE will call
474 * vrele again, which will try to call VOP_INACTIVE again after
475 * vn_locking the vnode. which would be fine except that our vrele
476 * caller also locked the vnode... So instead, we just gimmick the
477 * refcounts and hope nobody else can touch the file now */
478 osi_Assert(VREFCOUNT(avc) == 0);
479 VREFCOUNT_SET(avc, 1);
481 VN_HOLD(AFSTOV(avc));
483 /* We'll only try this once. If it fails, just release the vnode.
484 * Clear after doing hold so that NewVCache doesn't find us yet.
486 avc->states &= ~(CUnlinked | CUnlinkedDel);
488 ReleaseWriteLock(&avc->lock);
490 dirFid.Cell = avc->fid.Cell;
491 dirFid.Fid.Volume = avc->fid.Fid.Volume;
492 dirFid.Fid.Vnode = avc->parentVnode;
493 dirFid.Fid.Unique = avc->parentUnique;
494 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
497 tdc = afs_FindDCache(adp, (afs_size_t) 0);
498 ObtainWriteLock(&adp->lock, 159);
500 ObtainSharedLock(&tdc->lock, 639);
502 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
503 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
506 /* we failed - and won't be back to try again. */
509 osi_FreeSmallSpace(unlname);
511 #ifdef AFS_DARWIN_ENV
512 osi_Assert(VREFCOUNT(avc) == 1);
513 VREFCOUNT_SET(avc, 0);
517 ReleaseWriteLock(&avc->lock);