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;
112 if (!AFS_IS_DISCONNECTED) {
114 tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
116 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
119 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->fid.Fid,
120 aname, &OutDirStatus, &tsync);
126 (tc, code, &adp->fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
130 osi_dnlc_remove(adp, aname, tvc);
134 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_IS_DISCON_RW || 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);
190 /* Don't decrease refcount for this vcache if disconnected, we will
191 * need it during replay.
193 if (!AFS_IS_DISCON_RW)
202 char *name, *sp, *p = ".__afs";
203 afs_int32 rd = afs_random() & 0xffff;
205 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
209 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
216 /* these variables appear to exist for debugging purposes */
217 struct vcache *Tadp1, *Ttvc;
222 /* Note that we don't set CDirty here, this is OK because the unlink
223 * RPC is called synchronously */
225 afs_remove(OSI_VC_ARG(adp), aname, acred)
228 struct AFS_UCRED *acred;
230 struct vrequest treq;
231 register struct dcache *tdc;
232 struct VenusFid unlinkFid;
233 register afs_int32 code;
234 register struct vcache *tvc;
235 afs_size_t offset, len;
236 struct afs_fakestat_state fakestate;
239 AFS_STATCNT(afs_remove);
240 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
241 ICL_TYPE_STRING, aname);
244 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
247 if ((code = afs_InitReq(&treq, acred))) {
254 afs_InitFakeStat(&fakestate);
255 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
257 afs_PutFakeStat(&fakestate);
264 /* Check if this is dynroot */
265 if (afs_IsDynroot(adp)) {
266 code = afs_DynrootVOPRemove(adp, acred, aname);
267 afs_PutFakeStat(&fakestate);
273 if (afs_IsDynrootMount(adp)) {
277 if (strlen(aname) > AFSNAMEMAX) {
278 afs_PutFakeStat(&fakestate);
285 code = afs_VerifyVCache(adp, &treq);
287 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
290 afs_PutFakeStat(&fakestate);
291 return afs_CheckCode(code, &treq, 22);
293 #else /* AFS_OSF_ENV */
296 code = afs_CheckCode(code, &treq, 23);
297 afs_PutFakeStat(&fakestate);
302 /** If the volume is read-only, return error without making an RPC to the
305 if (adp->states & CRO) {
310 afs_PutFakeStat(&fakestate);
314 /* If we're running disconnected without logging, go no further... */
315 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
320 afs_PutFakeStat(&fakestate);
324 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
325 ObtainWriteLock(&adp->lock, 142);
326 #if defined(AFS_DISCON_ENV)
327 if (AFS_IS_DISCON_RW && !(adp->ddirty_flags & VDisconShadowed))
328 /* Make shadow copy of parent dir. */
329 afs_MakeShadowDir(adp);
332 ObtainSharedLock(&tdc->lock, 638);
335 * Make sure that the data in the cache is current. We may have
336 * received a callback while we were waiting for the write lock.
338 if (!(adp->states & CStatd)
339 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
340 ReleaseWriteLock(&adp->lock);
342 ReleaseSharedLock(&tdc->lock);
348 unlinkFid.Fid.Vnode = 0;
350 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
352 /* This should not be necessary since afs_lookup() has already
357 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
359 afs_int32 cached = 0;
361 unlinkFid.Cell = adp->fid.Cell;
362 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
363 if (unlinkFid.Fid.Unique == 0) {
365 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
368 ObtainReadLock(&afs_xvcache);
369 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
370 ReleaseReadLock(&afs_xvcache);
375 #if defined(AFS_DISCON_ENV)
376 if (AFS_IS_DISCON_RW) {
377 /* Add removed file vcache to dirty list. */
379 if (!tvc->ddirty_flags ||
380 (tvc->ddirty_flags == VDisconShadowed)) {
381 /* Add to list only if fresh. */
382 ObtainWriteLock(&afs_DDirtyVCListLock, 725);
383 AFS_DISCON_ADD_DIRTY(tvc);
384 ReleaseWriteLock(&afs_DDirtyVCListLock);
387 ObtainWriteLock(&tvc->lock, 726);
388 tvc->ddirty_flags |= VDisconRemove;
389 ReleaseWriteLock(&tvc->lock);
391 //ObtainWriteLock(&adp->lock, 751);
393 //ReleaseWriteLock(&adp->lock);
397 if (tvc && osi_Active(tvc)) {
398 /* about to delete whole file, prefetch it first */
399 ReleaseWriteLock(&adp->lock);
401 ReleaseSharedLock(&tdc->lock);
402 ObtainWriteLock(&tvc->lock, 143);
403 #if defined(AFS_OSF_ENV)
404 afs_Wire(tvc, &treq);
405 #else /* AFS_OSF_ENV */
406 FetchWholeEnchilada(tvc, &treq);
408 ReleaseWriteLock(&tvc->lock);
409 ObtainWriteLock(&adp->lock, 144);
410 /* Technically I don't think we need this back, but let's hold it
411 anyway; The "got" reference should actually be sufficient. */
413 ObtainSharedLock(&tdc->lock, 640);
416 osi_dnlc_remove(adp, aname, tvc);
419 #ifndef AFS_DARWIN80_ENV
420 Tadpr = VREFCOUNT(adp);
425 #ifndef AFS_DARWIN80_ENV
427 Ttvcr = VREFCOUNT(tvc);
430 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
431 && !(tvc->states & CUnlinked)) {
433 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
434 && !(tvc->states & CUnlinked)) {
436 char *unlname = afs_newname();
438 ReleaseWriteLock(&adp->lock);
440 ReleaseSharedLock(&tdc->lock);
441 code = afsrename(adp, aname, adp, unlname, acred, &treq);
444 struct VenusFid *oldmvid = NULL;
447 tvc->mvid = (struct VenusFid *)unlname;
449 osi_FreeSmallSpace(oldmvid);
455 tvc->states |= CUnlinked;
457 osi_FreeSmallSpace(unlname);
461 /* Don't decrease refcount for this vcache if disconnected, we will
462 * need it during replay.
464 if (!AFS_IS_DISCON_RW)
467 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
469 afs_PutFakeStat(&fakestate);
470 #ifndef AFS_DARWIN80_ENV
471 /* we can't track by thread, it's not exported in the KPI; only do
473 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
479 /* afs_remunlink -- This tries to delete the file at the server after it has
480 * been renamed when unlinked locally but now has been finally released.
482 * CAUTION -- may be called with avc unheld. */
485 afs_remunlink(register struct vcache *avc, register int doit)
487 struct AFS_UCRED *cred;
490 struct vrequest treq;
491 struct VenusFid dirFid;
492 register struct dcache *tdc;
495 if (NBObtainWriteLock(&avc->lock, 423))
497 #if defined(AFS_DARWIN80_ENV)
498 if (vnode_get(AFSTOV(avc))) {
499 ReleaseWriteLock(&avc->lock);
504 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
505 if ((code = afs_InitReq(&treq, avc->uncred))) {
506 ReleaseWriteLock(&avc->lock);
508 /* Must bump the refCount because GetVCache may block.
509 * Also clear mvid so no other thread comes here if we block.
511 unlname = (char *)avc->mvid;
516 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
519 VN_HOLD(AFSTOV(avc));
522 /* We'll only try this once. If it fails, just release the vnode.
523 * Clear after doing hold so that NewVCache doesn't find us yet.
525 avc->states &= ~(CUnlinked | CUnlinkedDel);
527 ReleaseWriteLock(&avc->lock);
529 dirFid.Cell = avc->fid.Cell;
530 dirFid.Fid.Volume = avc->fid.Fid.Volume;
531 dirFid.Fid.Vnode = avc->parentVnode;
532 dirFid.Fid.Unique = avc->parentUnique;
533 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
536 tdc = afs_FindDCache(adp, (afs_size_t) 0);
537 ObtainWriteLock(&adp->lock, 159);
539 ObtainSharedLock(&tdc->lock, 639);
541 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
542 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
545 /* we failed - and won't be back to try again. */
548 osi_FreeSmallSpace(unlname);
552 #if defined(AFS_DARWIN80_ENV)
553 vnode_put(AFSTOV(avc));
555 ReleaseWriteLock(&avc->lock);