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(register struct vcache *avc, struct vrequest *areq)
52 register afs_int32 nextChunk;
53 register struct dcache *tdc;
54 afs_size_t pos, offset, len;
56 AFS_STATCNT(FetchWholeEnchilada);
57 if ((avc->states & CStatd) == 0)
58 return; /* don't know size */
59 for (nextChunk = 0; nextChunk < 1024; nextChunk++) { /* sanity check on N chunks */
60 pos = AFS_CHUNKTOBASE(nextChunk);
61 #if defined(AFS_OSF_ENV)
62 if (pos >= avc->m.Length)
64 #else /* AFS_OSF_ENV */
65 if (pos >= avc->m.Length)
66 return; /* all done */
68 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
70 #if defined(AFS_OSF_ENV)
72 #else /* AFS_OSF_ENV */
77 #if defined(AFS_OSF_ENV)
78 avc->states |= CWired;
79 #endif /* AFS_OSF_ENV */
82 #if defined(AFS_OSF_ENV)
84 * Tests whether file is wired down, after unwiring the file if it
85 * is found to be inactive (ie not open and not being paged from).
87 afs_IsWired(register struct vcache *avc)
89 if (avc->states & CWired) {
90 if (osi_Active(avc)) {
93 avc->states &= ~CWired;
97 #endif /* AFS_OSF_ENV */
100 afsremove(register struct vcache *adp, register struct dcache *tdc,
101 register struct vcache *tvc, char *aname, struct AFS_UCRED *acred,
102 struct vrequest *treqp)
104 register afs_int32 code = 0;
105 register struct conn *tc;
106 struct AFSFetchStatus OutDirStatus;
107 struct AFSVolSync tsync;
109 if (!AFS_IS_DISCONNECTED) {
111 tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
113 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
116 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
117 aname, &OutDirStatus, &tsync);
123 (tc, code, &adp->fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
127 osi_dnlc_remove(adp, aname, tvc);
131 ReleaseSharedLock(&tdc->lock);
139 ObtainWriteLock(&afs_xcbhash, 497);
140 afs_DequeueCallback(adp);
141 adp->states &= ~CStatd;
142 ReleaseWriteLock(&afs_xcbhash);
143 osi_dnlc_purgedp(adp);
145 ReleaseWriteLock(&adp->lock);
146 code = afs_CheckCode(code, treqp, 21);
150 UpgradeSToWLock(&tdc->lock, 637);
151 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
152 /* we can do it locally */
153 code = afs_dir_Delete(tdc, aname);
155 ZapDCE(tdc); /* surprise error -- invalid value */
160 ReleaseWriteLock(&tdc->lock);
161 afs_PutDCache(tdc); /* drop ref count */
163 ReleaseWriteLock(&adp->lock);
165 /* now, get vnode for unlinked dude, and see if we should force it
166 * from cache. adp is now the deleted files vnode. Note that we
167 * call FindVCache instead of GetVCache since if the file's really
168 * gone, we won't be able to fetch the status info anyway. */
170 #ifdef AFS_BOZONLOCK_ENV
171 afs_BozonLock(&tvc->pvnLock, tvc);
172 /* Since afs_TryToSmush will do a pvn_vptrunc */
174 ObtainWriteLock(&tvc->lock, 141);
175 /* note that callback will be broken on the deleted file if there are
176 * still >0 links left to it, so we'll get the stat right */
178 tvc->states &= ~CUnique; /* For the dfs xlator */
179 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
180 if (!AFS_NFSXLATORREQ(acred))
181 afs_TryToSmush(tvc, acred, 0);
183 ReleaseWriteLock(&tvc->lock);
184 #ifdef AFS_BOZONLOCK_ENV
185 afs_BozonUnlock(&tvc->pvnLock, tvc);
187 /* Don't decrease refcount for this vcache if disconnected, we will
188 * need it during replay.
190 if (!AFS_IS_DISCON_RW)
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 */
222 afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
224 struct vrequest treq;
225 register struct dcache *tdc;
226 struct VenusFid unlinkFid;
227 register afs_int32 code;
228 register struct vcache *tvc;
229 afs_size_t offset, len;
230 struct afs_fakestat_state fakestate;
233 AFS_STATCNT(afs_remove);
234 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
235 ICL_TYPE_STRING, aname);
238 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
241 if ((code = afs_InitReq(&treq, acred))) {
248 afs_InitFakeStat(&fakestate);
249 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
251 afs_PutFakeStat(&fakestate);
258 /* Check if this is dynroot */
259 if (afs_IsDynroot(adp)) {
260 code = afs_DynrootVOPRemove(adp, acred, aname);
261 afs_PutFakeStat(&fakestate);
267 if (afs_IsDynrootMount(adp)) {
271 if (strlen(aname) > AFSNAMEMAX) {
272 afs_PutFakeStat(&fakestate);
279 code = afs_VerifyVCache(adp, &treq);
281 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
284 afs_PutFakeStat(&fakestate);
285 return afs_CheckCode(code, &treq, 22);
287 #else /* AFS_OSF_ENV */
290 code = afs_CheckCode(code, &treq, 23);
291 afs_PutFakeStat(&fakestate);
296 /** If the volume is read-only, return error without making an RPC to the
299 if (adp->states & CRO) {
304 afs_PutFakeStat(&fakestate);
308 /* If we're running disconnected without logging, go no further... */
309 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
314 afs_PutFakeStat(&fakestate);
318 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
319 ObtainWriteLock(&adp->lock, 142);
320 #if defined(AFS_DISCON_ENV)
321 if (AFS_IS_DISCON_RW && !(adp->ddirty_flags & VDisconShadowed))
322 /* Make shadow copy of parent dir. */
323 afs_MakeShadowDir(adp);
326 ObtainSharedLock(&tdc->lock, 638);
329 * Make sure that the data in the cache is current. We may have
330 * received a callback while we were waiting for the write lock.
332 if (!(adp->states & CStatd)
333 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
334 ReleaseWriteLock(&adp->lock);
336 ReleaseSharedLock(&tdc->lock);
342 unlinkFid.Fid.Vnode = 0;
344 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
346 /* This should not be necessary since afs_lookup() has already
351 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
353 afs_int32 cached = 0;
355 unlinkFid.Cell = adp->fid.Cell;
356 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
357 if (unlinkFid.Fid.Unique == 0) {
359 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
362 ObtainReadLock(&afs_xvcache);
363 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
364 ReleaseReadLock(&afs_xvcache);
369 #if defined(AFS_DISCON_ENV)
370 if (AFS_IS_DISCON_RW) {
371 /* Add removed file vcache to dirty list. */
373 if (!tvc->ddirty_flags ||
374 (tvc->ddirty_flags == VDisconShadowed)) {
375 /* Add to list only if fresh. */
376 ObtainWriteLock(&afs_DDirtyVCListLock, 725);
377 AFS_DISCON_ADD_DIRTY(tvc);
378 ReleaseWriteLock(&afs_DDirtyVCListLock);
381 ObtainWriteLock(&tvc->lock, 726);
382 tvc->ddirty_flags |= VDisconRemove;
383 ReleaseWriteLock(&tvc->lock);
385 //ObtainWriteLock(&adp->lock, 751);
387 //ReleaseWriteLock(&adp->lock);
391 if (tvc && osi_Active(tvc)) {
392 /* about to delete whole file, prefetch it first */
393 ReleaseWriteLock(&adp->lock);
395 ReleaseSharedLock(&tdc->lock);
396 ObtainWriteLock(&tvc->lock, 143);
397 #if defined(AFS_OSF_ENV)
398 afs_Wire(tvc, &treq);
399 #else /* AFS_OSF_ENV */
400 FetchWholeEnchilada(tvc, &treq);
402 ReleaseWriteLock(&tvc->lock);
403 ObtainWriteLock(&adp->lock, 144);
404 /* Technically I don't think we need this back, but let's hold it
405 anyway; The "got" reference should actually be sufficient. */
407 ObtainSharedLock(&tdc->lock, 640);
410 osi_dnlc_remove(adp, aname, tvc);
413 #ifndef AFS_DARWIN80_ENV
414 Tadpr = VREFCOUNT(adp);
419 #ifndef AFS_DARWIN80_ENV
421 Ttvcr = VREFCOUNT(tvc);
424 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
425 && !(tvc->states & CUnlinked)) {
427 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
428 && !(tvc->states & CUnlinked)) {
430 char *unlname = afs_newname();
432 ReleaseWriteLock(&adp->lock);
434 ReleaseSharedLock(&tdc->lock);
435 code = afsrename(adp, aname, adp, unlname, acred, &treq);
438 struct VenusFid *oldmvid = NULL;
441 tvc->mvid = (struct VenusFid *)unlname;
443 osi_FreeSmallSpace(oldmvid);
449 tvc->states |= CUnlinked;
451 osi_FreeSmallSpace(unlname);
455 /* Don't decrease refcount for this vcache if disconnected, we will
456 * need it during replay.
458 if (!AFS_IS_DISCON_RW)
461 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
463 afs_PutFakeStat(&fakestate);
464 #ifndef AFS_DARWIN80_ENV
465 /* we can't track by thread, it's not exported in the KPI; only do
467 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
473 /* afs_remunlink -- This tries to delete the file at the server after it has
474 * been renamed when unlinked locally but now has been finally released.
476 * CAUTION -- may be called with avc unheld. */
479 afs_remunlink(register struct vcache *avc, register int doit)
481 struct AFS_UCRED *cred;
484 struct vrequest treq;
485 struct VenusFid dirFid;
486 register struct dcache *tdc;
489 if (NBObtainWriteLock(&avc->lock, 423))
491 #if defined(AFS_DARWIN80_ENV)
492 if (vnode_get(AFSTOV(avc))) {
493 ReleaseWriteLock(&avc->lock);
498 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
499 if ((code = afs_InitReq(&treq, avc->uncred))) {
500 ReleaseWriteLock(&avc->lock);
502 /* Must bump the refCount because GetVCache may block.
503 * Also clear mvid so no other thread comes here if we block.
505 unlname = (char *)avc->mvid;
510 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
513 VN_HOLD(AFSTOV(avc));
516 /* We'll only try this once. If it fails, just release the vnode.
517 * Clear after doing hold so that NewVCache doesn't find us yet.
519 avc->states &= ~(CUnlinked | CUnlinkedDel);
521 ReleaseWriteLock(&avc->lock);
523 dirFid.Cell = avc->fid.Cell;
524 dirFid.Fid.Volume = avc->fid.Fid.Volume;
525 dirFid.Fid.Vnode = avc->parentVnode;
526 dirFid.Fid.Unique = avc->parentUnique;
527 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
530 tdc = afs_FindDCache(adp, (afs_size_t) 0);
531 ObtainWriteLock(&adp->lock, 159);
533 ObtainSharedLock(&tdc->lock, 639);
535 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
536 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
539 /* we failed - and won't be back to try again. */
542 osi_FreeSmallSpace(unlname);
546 #if defined(AFS_DARWIN80_ENV)
547 vnode_put(AFSTOV(avc));
549 ReleaseWriteLock(&avc->lock);