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->f.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->f.m.Length)
64 #else /* AFS_OSF_ENV */
65 if (pos >= avc->f.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->f.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->f.states & CWired) {
90 if (osi_Active(avc)) {
93 avc->f.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 afs_conn *tc;
106 struct AFSFetchStatus OutDirStatus;
107 struct AFSVolSync tsync;
109 if (!AFS_IS_DISCONNECTED) {
111 tc = afs_Conn(&adp->f.fid, treqp, SHARED_LOCK);
113 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
116 RXAFS_RemoveFile(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
117 aname, &OutDirStatus, &tsync);
123 (tc, code, &adp->f.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->f.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 */
177 tvc->f.m.LinkCount--;
178 tvc->f.states &= ~CUnique; /* For the dfs xlator */
179 if (tvc->f.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);
195 char *name, *sp, *p = ".__afs";
196 afs_int32 rd = afs_random() & 0xffff;
198 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
202 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
209 /* these variables appear to exist for debugging purposes */
210 struct vcache *Tadp1, *Ttvc;
215 /* Note that we don't set CDirty here, this is OK because the unlink
216 * RPC is called synchronously */
218 afs_remove(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
220 struct vrequest treq;
221 register struct dcache *tdc;
222 struct VenusFid unlinkFid;
223 register afs_int32 code;
224 register struct vcache *tvc;
225 afs_size_t offset, len;
226 struct afs_fakestat_state fakestate;
229 AFS_STATCNT(afs_remove);
230 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
231 ICL_TYPE_STRING, aname);
234 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
237 if ((code = afs_InitReq(&treq, acred))) {
244 afs_InitFakeStat(&fakestate);
245 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
247 afs_PutFakeStat(&fakestate);
254 /* Check if this is dynroot */
255 if (afs_IsDynroot(adp)) {
256 code = afs_DynrootVOPRemove(adp, acred, aname);
257 afs_PutFakeStat(&fakestate);
263 if (afs_IsDynrootMount(adp)) {
267 if (strlen(aname) > AFSNAMEMAX) {
268 afs_PutFakeStat(&fakestate);
275 code = afs_VerifyVCache(adp, &treq);
277 tvc = VTOAFS(ndp->ni_vp); /* should never be null */
280 afs_PutFakeStat(&fakestate);
281 return afs_CheckCode(code, &treq, 22);
283 #else /* AFS_OSF_ENV */
286 code = afs_CheckCode(code, &treq, 23);
287 afs_PutFakeStat(&fakestate);
292 /** If the volume is read-only, return error without making an RPC to the
295 if (adp->f.states & CRO) {
300 afs_PutFakeStat(&fakestate);
304 /* If we're running disconnected without logging, go no further... */
305 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
310 afs_PutFakeStat(&fakestate);
314 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
315 ObtainWriteLock(&adp->lock, 142);
317 ObtainSharedLock(&tdc->lock, 638);
320 * Make sure that the data in the cache is current. We may have
321 * received a callback while we were waiting for the write lock.
323 if (!(adp->f.states & CStatd)
324 || (tdc && !hsame(adp->f.m.DataVersion, tdc->f.versionNo))) {
325 ReleaseWriteLock(&adp->lock);
327 ReleaseSharedLock(&tdc->lock);
333 unlinkFid.Fid.Vnode = 0;
335 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
337 /* This should not be necessary since afs_lookup() has already
342 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
344 afs_int32 cached = 0;
346 unlinkFid.Cell = adp->f.fid.Cell;
347 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
348 if (unlinkFid.Fid.Unique == 0) {
350 afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
353 ObtainReadLock(&afs_xvcache);
354 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
355 ReleaseReadLock(&afs_xvcache);
360 #if defined(AFS_DISCON_ENV)
361 if (AFS_IS_DISCON_RW) {
362 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
363 /* Make shadow copy of parent dir. */
364 afs_MakeShadowDir(adp, tdc);
367 /* Can't hold a dcache lock whilst we're getting a vcache one */
369 ReleaseSharedLock(&tdc->lock);
371 /* XXX - We're holding adp->lock still, and we've got no
372 * guarantee about whether the ordering matches the lock hierarchy */
373 ObtainWriteLock(&tvc->lock, 713);
375 /* If we were locally created, then we don't need to do very
376 * much beyond ensuring that we don't exist anymore */
377 if (tvc->f.ddirty_flags & VDisconCreate) {
378 afs_DisconRemoveDirty(tvc);
380 /* Add removed file vcache to dirty list. */
381 afs_DisconAddDirty(tvc, VDisconRemove, 1);
383 adp->f.m.LinkCount--;
384 ReleaseWriteLock(&tvc->lock);
386 ObtainSharedLock(&tdc->lock, 714);
390 if (tvc && osi_Active(tvc)) {
391 /* about to delete whole file, prefetch it first */
392 ReleaseWriteLock(&adp->lock);
394 ReleaseSharedLock(&tdc->lock);
395 ObtainWriteLock(&tvc->lock, 143);
396 #if defined(AFS_OSF_ENV)
397 afs_Wire(tvc, &treq);
398 #else /* AFS_OSF_ENV */
399 FetchWholeEnchilada(tvc, &treq);
401 ReleaseWriteLock(&tvc->lock);
402 ObtainWriteLock(&adp->lock, 144);
403 /* Technically I don't think we need this back, but let's hold it
404 anyway; The "got" reference should actually be sufficient. */
406 ObtainSharedLock(&tdc->lock, 640);
409 osi_dnlc_remove(adp, aname, tvc);
412 #ifndef AFS_DARWIN80_ENV
413 Tadpr = VREFCOUNT(adp);
418 #ifndef AFS_DARWIN80_ENV
420 Ttvcr = VREFCOUNT(tvc);
423 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
424 && !(tvc->f.states & CUnlinked)) {
426 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
427 && !(tvc->f.states & CUnlinked)) {
429 char *unlname = afs_newname();
431 ReleaseWriteLock(&adp->lock);
433 ReleaseSharedLock(&tdc->lock);
434 code = afsrename(adp, aname, adp, unlname, acred, &treq);
437 struct VenusFid *oldmvid = NULL;
440 tvc->mvid = (struct VenusFid *)unlname;
442 osi_FreeSmallSpace(oldmvid);
448 tvc->f.states |= CUnlinked;
449 #if defined(AFS_DISCON_ENV)
450 /* if rename succeeded, remove should not */
451 ObtainWriteLock(&tvc->lock, 715);
452 if (tvc->f.ddirty_flags & VDisconRemove) {
453 tvc->f.ddirty_flags &= ~VDisconRemove;
455 ReleaseWriteLock(&tvc->lock);
458 osi_FreeSmallSpace(unlname);
464 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
466 afs_PutFakeStat(&fakestate);
467 #ifndef AFS_DARWIN80_ENV
468 /* we can't track by thread, it's not exported in the KPI; only do
470 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
476 /* afs_remunlink -- This tries to delete the file at the server after it has
477 * been renamed when unlinked locally but now has been finally released.
479 * CAUTION -- may be called with avc unheld. */
482 afs_remunlink(register struct vcache *avc, register int doit)
484 struct AFS_UCRED *cred;
487 struct vrequest treq;
488 struct VenusFid dirFid;
489 register struct dcache *tdc;
492 if (NBObtainWriteLock(&avc->lock, 423))
494 #if defined(AFS_DARWIN80_ENV)
495 if (vnode_get(AFSTOV(avc))) {
496 ReleaseWriteLock(&avc->lock);
501 if (avc->mvid && (doit || (avc->f.states & CUnlinkedDel))) {
502 if ((code = afs_InitReq(&treq, avc->uncred))) {
503 ReleaseWriteLock(&avc->lock);
505 /* Must bump the refCount because GetVCache may block.
506 * Also clear mvid so no other thread comes here if we block.
508 unlname = (char *)avc->mvid;
513 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
516 VN_HOLD(AFSTOV(avc));
519 /* We'll only try this once. If it fails, just release the vnode.
520 * Clear after doing hold so that NewVCache doesn't find us yet.
522 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
524 ReleaseWriteLock(&avc->lock);
526 dirFid.Cell = avc->f.fid.Cell;
527 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
528 dirFid.Fid.Vnode = avc->f.parent.vnode;
529 dirFid.Fid.Unique = avc->f.parent.unique;
530 adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
533 tdc = afs_FindDCache(adp, (afs_size_t) 0);
534 ObtainWriteLock(&adp->lock, 159);
536 ObtainSharedLock(&tdc->lock, 639);
538 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
539 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
542 /* we failed - and won't be back to try again. */
545 osi_FreeSmallSpace(unlname);
549 #if defined(AFS_DARWIN80_ENV)
550 vnode_put(AFSTOV(avc));
552 ReleaseWriteLock(&avc->lock);