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 = 0;
108 register struct conn *tc;
109 struct AFSFetchStatus OutDirStatus;
110 struct AFSVolSync tsync;
113 if (!AFS_IS_DISCONNECTED) {
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,
131 osi_dnlc_remove(adp, aname, tvc);
135 ReleaseSharedLock(&tdc->lock);
142 ObtainWriteLock(&afs_xcbhash, 497);
143 afs_DequeueCallback(adp);
144 adp->states &= ~CStatd;
145 ReleaseWriteLock(&afs_xcbhash);
146 osi_dnlc_purgedp(adp);
148 ReleaseWriteLock(&adp->lock);
149 code = afs_CheckCode(code, treqp, 21);
153 UpgradeSToWLock(&tdc->lock, 637);
154 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
155 /* we can do it locally */
156 code = afs_dir_Delete(tdc, aname);
158 ZapDCE(tdc); /* surprise error -- invalid value */
163 ReleaseWriteLock(&tdc->lock);
164 afs_PutDCache(tdc); /* drop ref count */
166 ReleaseWriteLock(&adp->lock);
168 /* now, get vnode for unlinked dude, and see if we should force it
169 * from cache. adp is now the deleted files vnode. Note that we
170 * call FindVCache instead of GetVCache since if the file's really
171 * gone, we won't be able to fetch the status info anyway. */
173 #ifdef AFS_BOZONLOCK_ENV
174 afs_BozonLock(&tvc->pvnLock, tvc);
175 /* Since afs_TryToSmush will do a pvn_vptrunc */
177 ObtainWriteLock(&tvc->lock, 141);
178 /* note that callback will be broken on the deleted file if there are
179 * still >0 links left to it, so we'll get the stat right */
181 tvc->states &= ~CUnique; /* For the dfs xlator */
182 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
183 if (!AFS_NFSXLATORREQ(acred))
184 afs_TryToSmush(tvc, acred, 0);
186 ReleaseWriteLock(&tvc->lock);
187 #ifdef AFS_BOZONLOCK_ENV
188 afs_BozonUnlock(&tvc->pvnLock, tvc);
198 char *name, *sp, *p = ".__afs";
199 afs_int32 rd = afs_random() & 0xffff;
201 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
205 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
212 /* these variables appear to exist for debugging purposes */
213 struct vcache *Tadp1, *Ttvc;
218 /* Note that we don't set CDirty here, this is OK because the unlink
219 * RPC is called synchronously */
221 afs_remove(OSI_VC_ARG(adp), aname, acred)
224 struct AFS_UCRED *acred;
226 struct vrequest treq;
227 register struct dcache *tdc;
228 struct VenusFid unlinkFid;
229 register afs_int32 code;
230 register struct vcache *tvc;
231 afs_size_t offset, len;
232 struct afs_fakestat_state fakestate;
235 AFS_STATCNT(afs_remove);
236 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
237 ICL_TYPE_STRING, aname);
240 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
243 if ((code = afs_InitReq(&treq, acred))) {
250 afs_InitFakeStat(&fakestate);
251 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
253 afs_PutFakeStat(&fakestate);
260 /* Check if this is dynroot */
261 if (afs_IsDynroot(adp)) {
262 code = afs_DynrootVOPRemove(adp, acred, aname);
263 afs_PutFakeStat(&fakestate);
269 if (afs_IsDynrootMount(adp)) {
273 if (strlen(aname) > AFSNAMEMAX) {
274 afs_PutFakeStat(&fakestate);
281 code = afs_VerifyVCache(adp, &treq);
283 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
286 afs_PutFakeStat(&fakestate);
287 return afs_CheckCode(code, &treq, 22);
289 #else /* AFS_OSF_ENV */
292 code = afs_CheckCode(code, &treq, 23);
293 afs_PutFakeStat(&fakestate);
298 /** If the volume is read-only, return error without making an RPC to the
301 if (adp->states & CRO) {
306 afs_PutFakeStat(&fakestate);
310 /* If we're running disconnected without logging, go no further... */
311 if (AFS_IS_DISCONNECTED && !AFS_IS_LOGGING) {
316 afs_PutFakeStat(&fakestate);
320 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
321 ObtainWriteLock(&adp->lock, 142);
323 ObtainSharedLock(&tdc->lock, 638);
326 * Make sure that the data in the cache is current. We may have
327 * received a callback while we were waiting for the write lock.
329 if (!(adp->states & CStatd)
330 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
331 ReleaseWriteLock(&adp->lock);
333 ReleaseSharedLock(&tdc->lock);
339 unlinkFid.Fid.Vnode = 0;
341 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
343 /* This should not be necessary since afs_lookup() has already
347 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
349 afs_int32 cached = 0;
351 unlinkFid.Cell = adp->fid.Cell;
352 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
353 if (unlinkFid.Fid.Unique == 0) {
355 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
358 ObtainReadLock(&afs_xvcache);
359 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
360 ReleaseReadLock(&afs_xvcache);
365 if (tvc && osi_Active(tvc)) {
366 /* about to delete whole file, prefetch it first */
367 ReleaseWriteLock(&adp->lock);
369 ReleaseSharedLock(&tdc->lock);
370 ObtainWriteLock(&tvc->lock, 143);
371 #if defined(AFS_OSF_ENV)
372 afs_Wire(tvc, &treq);
373 #else /* AFS_OSF_ENV */
374 FetchWholeEnchilada(tvc, &treq);
376 ReleaseWriteLock(&tvc->lock);
377 ObtainWriteLock(&adp->lock, 144);
378 /* Technically I don't think we need this back, but let's hold it
379 anyway; The "got" reference should actually be sufficient. */
381 ObtainSharedLock(&tdc->lock, 640);
384 osi_dnlc_remove(adp, aname, tvc);
387 #ifndef AFS_DARWIN80_ENV
388 Tadpr = VREFCOUNT(adp);
393 #ifndef AFS_DARWIN80_ENV
395 Ttvcr = VREFCOUNT(tvc);
398 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
399 && !(tvc->states & CUnlinked)) {
401 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
402 && !(tvc->states & CUnlinked)) {
404 char *unlname = afs_newname();
406 ReleaseWriteLock(&adp->lock);
408 ReleaseSharedLock(&tdc->lock);
409 code = afsrename(adp, aname, adp, unlname, acred, &treq);
412 struct VenusFid *oldmvid = NULL;
415 tvc->mvid = (struct VenusFid *)unlname;
417 osi_FreeSmallSpace(oldmvid);
423 tvc->states |= CUnlinked;
425 osi_FreeSmallSpace(unlname);
431 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
433 afs_PutFakeStat(&fakestate);
434 #ifndef AFS_DARWIN80_ENV
435 /* we can't track by thread, it's not exported in the KPI; only do
437 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
443 /* afs_remunlink -- This tries to delete the file at the server after it has
444 * been renamed when unlinked locally but now has been finally released.
446 * CAUTION -- may be called with avc unheld. */
449 afs_remunlink(register struct vcache *avc, register int doit)
451 struct AFS_UCRED *cred;
454 struct vrequest treq;
455 struct VenusFid dirFid;
456 register struct dcache *tdc;
459 if (NBObtainWriteLock(&avc->lock, 423))
461 #if defined(AFS_DARWIN80_ENV)
462 if (vnode_get(AFSTOV(avc))) {
463 ReleaseWriteLock(&avc->lock);
468 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
469 if ((code = afs_InitReq(&treq, avc->uncred))) {
470 ReleaseWriteLock(&avc->lock);
472 /* Must bump the refCount because GetVCache may block.
473 * Also clear mvid so no other thread comes here if we block.
475 unlname = (char *)avc->mvid;
480 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
483 VN_HOLD(AFSTOV(avc));
486 /* We'll only try this once. If it fails, just release the vnode.
487 * Clear after doing hold so that NewVCache doesn't find us yet.
489 avc->states &= ~(CUnlinked | CUnlinkedDel);
491 ReleaseWriteLock(&avc->lock);
493 dirFid.Cell = avc->fid.Cell;
494 dirFid.Fid.Volume = avc->fid.Fid.Volume;
495 dirFid.Fid.Vnode = avc->parentVnode;
496 dirFid.Fid.Unique = avc->parentUnique;
497 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
500 tdc = afs_FindDCache(adp, (afs_size_t) 0);
501 ObtainWriteLock(&adp->lock, 159);
503 ObtainSharedLock(&tdc->lock, 639);
505 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
506 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
509 /* we failed - and won't be back to try again. */
512 osi_FreeSmallSpace(unlname);
516 #if defined(AFS_DARWIN80_ENV)
517 vnode_put(AFSTOV(avc));
519 ReleaseWriteLock(&avc->lock);