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
20 #include <afsconfig.h>
21 #include "afs/param.h"
26 #include "afs/sysincludes.h" /* Standard vendor system headers */
27 #include "afsincludes.h" /* Afs-based standard headers */
28 #include "afs/afs_stats.h" /* statistics */
29 #include "afs/afs_cbqueue.h"
30 #include "afs/nfsclient.h"
31 #include "afs/afs_osidnlc.h"
34 extern afs_rwlock_t afs_xvcache;
35 extern afs_rwlock_t afs_xcbhash;
40 * Wire down file in cache: prefetch all data, and turn on CWired flag
41 * so that callbacks/callback expirations are (temporarily) ignored
42 * and cache file(s) are kept in cache. File will be unwired when
43 * afs_inactive is called (ie no one has VN_HOLD on vnode), or when
44 * afs_IsWired notices that the file is no longer Active.
47 #else /* AFS_OSF_ENV */
49 FetchWholeEnchilada(avc, areq)
51 struct vrequest *areq;
52 register struct vcache *avc;
54 register afs_int32 nextChunk;
55 register struct dcache *tdc;
56 afs_size_t pos, offset, len;
58 AFS_STATCNT(FetchWholeEnchilada);
59 if ((avc->states & CStatd) == 0)
60 return; /* don't know size */
61 for (nextChunk = 0; nextChunk < 1024; nextChunk++) { /* sanity check on N chunks */
62 pos = AFS_CHUNKTOBASE(nextChunk);
63 #if defined(AFS_OSF_ENV)
64 if (pos >= avc->m.Length)
66 #else /* AFS_OSF_ENV */
67 if (pos >= avc->m.Length)
68 return; /* all done */
70 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
72 #if defined(AFS_OSF_ENV)
74 #else /* AFS_OSF_ENV */
79 #if defined(AFS_OSF_ENV)
80 avc->states |= CWired;
81 #endif /* AFS_OSF_ENV */
84 #if defined(AFS_OSF_ENV)
86 * Tests whether file is wired down, after unwiring the file if it
87 * is found to be inactive (ie not open and not being paged from).
90 register struct vcache *avc;
92 if (avc->states & CWired) {
93 if (osi_Active(avc)) {
96 avc->states &= ~CWired;
100 #endif /* AFS_OSF_ENV */
103 afsremove(register struct vcache *adp, register struct dcache *tdc,
104 register struct vcache *tvc, char *aname, struct AFS_UCRED *acred,
105 struct vrequest *treqp)
107 register afs_int32 code;
108 register struct conn *tc;
109 struct AFSFetchStatus OutDirStatus;
110 struct AFSVolSync tsync;
113 tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
115 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
118 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
119 aname, &OutDirStatus, &tsync);
125 (tc, code, &adp->fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
128 osi_dnlc_remove(adp, aname, tvc);
130 afs_symhint_inval(tvc); /* XXX: don't really need to be so extreme */
134 ReleaseSharedLock(&tdc->lock);
141 ObtainWriteLock(&afs_xcbhash, 497);
142 afs_DequeueCallback(adp);
143 adp->states &= ~CStatd;
144 ReleaseWriteLock(&afs_xcbhash);
145 osi_dnlc_purgedp(adp);
147 ReleaseWriteLock(&adp->lock);
148 code = afs_CheckCode(code, treqp, 21);
152 UpgradeSToWLock(&tdc->lock, 637);
153 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
154 /* we can do it locally */
155 code = afs_dir_Delete(tdc, aname);
157 ZapDCE(tdc); /* surprise error -- invalid value */
162 ReleaseWriteLock(&tdc->lock);
163 afs_PutDCache(tdc); /* drop ref count */
165 ReleaseWriteLock(&adp->lock);
167 /* now, get vnode for unlinked dude, and see if we should force it
168 * from cache. adp is now the deleted files vnode. Note that we
169 * call FindVCache instead of GetVCache since if the file's really
170 * gone, we won't be able to fetch the status info anyway. */
172 #ifdef AFS_BOZONLOCK_ENV
173 afs_BozonLock(&tvc->pvnLock, tvc);
174 /* Since afs_TryToSmush will do a pvn_vptrunc */
176 ObtainWriteLock(&tvc->lock, 141);
177 /* note that callback will be broken on the deleted file if there are
178 * still >0 links left to it, so we'll get the stat right */
180 tvc->states &= ~CUnique; /* For the dfs xlator */
181 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
182 if (!AFS_NFSXLATORREQ(acred))
183 afs_TryToSmush(tvc, acred, 0);
185 ReleaseWriteLock(&tvc->lock);
186 #ifdef AFS_BOZONLOCK_ENV
187 afs_BozonUnlock(&tvc->pvnLock, tvc);
197 char *name, *sp, *p = ".__afs";
198 afs_int32 rd = afs_random() & 0xffff;
200 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
204 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
211 /* these variables appear to exist for debugging purposes */
212 struct vcache *Tadp1, *Ttvc;
217 /* Note that we don't set CDirty here, this is OK because the unlink
218 * RPC is called synchronously */
220 afs_remove(OSI_VC_ARG(adp), aname, acred)
223 struct AFS_UCRED *acred;
225 struct vrequest treq;
226 register struct dcache *tdc;
227 struct VenusFid unlinkFid;
228 register afs_int32 code;
229 register struct vcache *tvc;
230 afs_size_t offset, len;
231 struct afs_fakestat_state fakestate;
234 AFS_STATCNT(afs_remove);
235 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
236 ICL_TYPE_STRING, aname);
239 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
242 if ((code = afs_InitReq(&treq, acred))) {
249 afs_InitFakeStat(&fakestate);
250 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
252 afs_PutFakeStat(&fakestate);
259 /* Check if this is dynroot */
260 if (afs_IsDynroot(adp)) {
261 code = afs_DynrootVOPRemove(adp, acred, aname);
262 afs_PutFakeStat(&fakestate);
269 if (strlen(aname) > AFSNAMEMAX) {
270 afs_PutFakeStat(&fakestate);
277 code = afs_VerifyVCache(adp, &treq);
279 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
282 afs_PutFakeStat(&fakestate);
283 return afs_CheckCode(code, &treq, 22);
285 #else /* AFS_OSF_ENV */
288 code = afs_CheckCode(code, &treq, 23);
289 afs_PutFakeStat(&fakestate);
294 /** If the volume is read-only, return error without making an RPC to the
297 if (adp->states & CRO) {
302 afs_PutFakeStat(&fakestate);
306 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
307 ObtainWriteLock(&adp->lock, 142);
309 ObtainSharedLock(&tdc->lock, 638);
312 * Make sure that the data in the cache is current. We may have
313 * received a callback while we were waiting for the write lock.
315 if (!(adp->states & CStatd)
316 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
317 ReleaseWriteLock(&adp->lock);
319 ReleaseSharedLock(&tdc->lock);
325 unlinkFid.Fid.Vnode = 0;
327 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
329 /* This should not be necessary since afs_lookup() has already
333 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
335 afs_int32 cached = 0;
337 unlinkFid.Cell = adp->fid.Cell;
338 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
339 if (unlinkFid.Fid.Unique == 0) {
341 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
344 ObtainReadLock(&afs_xvcache);
345 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
346 ReleaseReadLock(&afs_xvcache);
351 if (tvc && osi_Active(tvc)) {
352 /* about to delete whole file, prefetch it first */
353 ReleaseWriteLock(&adp->lock);
354 ObtainWriteLock(&tvc->lock, 143);
355 #if defined(AFS_OSF_ENV)
356 afs_Wire(tvc, &treq);
357 #else /* AFS_OSF_ENV */
358 FetchWholeEnchilada(tvc, &treq);
360 ReleaseWriteLock(&tvc->lock);
361 ObtainWriteLock(&adp->lock, 144);
364 osi_dnlc_remove(adp, aname, tvc);
366 afs_symhint_inval(tvc);
369 Tadpr = VREFCOUNT(adp);
374 Ttvcr = VREFCOUNT(tvc);
376 if (tvc && (VREFCOUNT(tvc) > 2) && tvc->opens > 0
377 && !(tvc->states & CUnlinked))
379 if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0
380 && !(tvc->states & CUnlinked))
383 char *unlname = afs_newname();
385 ReleaseWriteLock(&adp->lock);
387 ReleaseSharedLock(&tdc->lock);
388 code = afsrename(adp, aname, adp, unlname, acred, &treq);
391 tvc->mvid = (struct VenusFid *)unlname;
397 tvc->states |= CUnlinked;
399 osi_FreeSmallSpace(unlname);
405 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
407 afs_PutFakeStat(&fakestate);
412 /* afs_remunlink -- This tries to delete the file at the server after it has
413 * been renamed when unlinked locally but now has been finally released.
415 * CAUTION -- may be called with avc unheld. */
418 afs_remunlink(register struct vcache *avc, register int doit)
420 struct AFS_UCRED *cred;
423 struct vrequest treq;
424 struct VenusFid dirFid;
425 register struct dcache *tdc;
428 if (NBObtainWriteLock(&avc->lock, 423))
431 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
432 if ((code = afs_InitReq(&treq, avc->uncred))) {
433 ReleaseWriteLock(&avc->lock);
435 /* Must bump the refCount because GetVCache may block.
436 * Also clear mvid so no other thread comes here if we block.
438 unlname = (char *)avc->mvid;
443 #ifdef AFS_DARWIN_ENV
446 VN_HOLD(AFSTOV(avc));
449 /* We'll only try this once. If it fails, just release the vnode.
450 * Clear after doing hold so that NewVCache doesn't find us yet.
452 avc->states &= ~(CUnlinked | CUnlinkedDel);
454 ReleaseWriteLock(&avc->lock);
456 dirFid.Cell = avc->fid.Cell;
457 dirFid.Fid.Volume = avc->fid.Fid.Volume;
458 dirFid.Fid.Vnode = avc->parentVnode;
459 dirFid.Fid.Unique = avc->parentUnique;
460 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
463 tdc = afs_FindDCache(adp, (afs_size_t) 0);
464 ObtainWriteLock(&adp->lock, 159);
466 ObtainSharedLock(&tdc->lock, 639);
468 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
469 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
472 /* we failed - and won't be back to try again. */
475 osi_FreeSmallSpace(unlname);
479 ReleaseWriteLock(&avc->lock);