1 /* Copyright (C) 1995, 1989, 1998 Transarc Corporation - All rights reserved */
3 * (C) COPYRIGHT IBM CORPORATION 1987, 1988
4 * LICENSED MATERIALS - PROPERTY OF IBM
20 #include "../afs/param.h" /* Should be always first */
21 #include "../afs/sysincludes.h" /* Standard vendor system headers */
22 #include "../afs/afsincludes.h" /* Afs-based standard headers */
23 #include "../afs/afs_stats.h" /* statistics */
24 #include "../afs/afs_cbqueue.h"
25 #include "../afs/nfsclient.h"
26 #include "../afs/afs_osidnlc.h"
29 extern afs_rwlock_t afs_xvcache;
30 extern afs_rwlock_t afs_xcbhash;
35 * Wire down file in cache: prefetch all data, and turn on CWired flag
36 * so that callbacks/callback expirations are (temporarily) ignored
37 * and cache file(s) are kept in cache. File will be unwired when
38 * afs_inactive is called (ie no one has VN_HOLD on vnode), or when
39 * afs_IsWired notices that the file is no longer Active.
42 #else /* AFS_OSF_ENV */
43 static void FetchWholeEnchilada(avc, areq)
45 struct vrequest *areq;
46 register struct vcache *avc; {
47 register afs_int32 nextChunk, pos;
48 register struct dcache *tdc;
49 afs_int32 offset, len;
51 AFS_STATCNT(FetchWholeEnchilada);
52 if ((avc->states & CStatd) == 0) return; /* don't know size */
53 for(nextChunk=0;nextChunk<1024;nextChunk++) { /* sanity check on N chunks */
54 pos = AFS_CHUNKTOBASE(nextChunk);
55 #if defined(AFS_OSF_ENV)
56 if (pos >= avc->m.Length) break; /* all done */
57 #else /* AFS_OSF_ENV */
58 if (pos >= avc->m.Length) return; /* all done */
60 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
62 #if defined(AFS_OSF_ENV)
64 #else /* AFS_OSF_ENV */
69 #if defined(AFS_OSF_ENV)
70 avc->states |= CWired;
71 #endif /* AFS_OSF_ENV */
74 #if defined(AFS_OSF_ENV)
76 * Tests whether file is wired down, after unwiring the file if it
77 * is found to be inactive (ie not open and not being paged from).
80 register struct vcache *avc; {
81 if (avc->states & CWired) {
82 if (osi_Active(avc)) {
85 avc->states &= ~CWired;
89 #endif /* AFS_OSF_ENV */
91 afsremove(adp, tdc, tvc, aname, acred, treqp)
92 register struct vcache *adp;
93 register struct dcache *tdc;
94 register struct vcache *tvc;
96 struct vrequest *treqp;
97 struct AFS_UCRED *acred; {
98 register afs_int32 code;
99 register struct conn *tc;
100 afs_int32 offset, len;
101 struct AFSFetchStatus OutDirStatus;
102 struct AFSVolSync tsync;
106 tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
108 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
109 #ifdef RX_ENABLE_LOCKS
111 #endif /* RX_ENABLE_LOCKS */
112 code = RXAFS_RemoveFile(tc->id, (struct AFSFid *) &adp->fid.Fid,
113 aname, &OutDirStatus, &tsync);
114 #ifdef RX_ENABLE_LOCKS
116 #endif /* RX_ENABLE_LOCKS */
121 (afs_Analyze(tc, code, &adp->fid, treqp,
122 AFS_STATS_FS_RPCIDX_REMOVEFILE, SHARED_LOCK, (struct cell *)0));
124 osi_dnlc_remove (adp, aname, tvc);
125 if (tvc) afs_symhint_inval(tvc); /* XXX: don't really need to be so extreme */
128 if (tdc) afs_PutDCache(tdc);
129 if (tvc) afs_PutVCache(tvc, WRITE_LOCK);
132 ObtainWriteLock(&afs_xcbhash, 497);
133 afs_DequeueCallback(adp);
134 adp->states &= ~CStatd;
135 ReleaseWriteLock(&afs_xcbhash);
136 osi_dnlc_purgedp(adp);
138 ReleaseWriteLock(&adp->lock);
139 code = afs_CheckCode(code, treqp, 21);
142 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
143 /* we can do it locally */
144 code = afs_dir_Delete(&tdc->f.inode, aname);
146 ZapDCE(tdc); /* surprise error -- invalid value */
151 afs_PutDCache(tdc); /* drop ref count */
153 ReleaseWriteLock(&adp->lock);
155 /* now, get vnode for unlinked dude, and see if we should force it
156 * from cache. adp is now the deleted files vnode. Note that we
157 * call FindVCache instead of GetVCache since if the file's really
158 * gone, we won't be able to fetch the status info anyway. */
160 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
161 afs_BozonLock(&tvc->pvnLock, tvc);
162 /* Since afs_TryToSmush will do a pvn_vptrunc */
164 ObtainWriteLock(&tvc->lock,141);
165 /* note that callback will be broken on the deleted file if there are
166 * still >0 links left to it, so we'll get the stat right */
168 tvc->states &= ~CUnique; /* For the dfs xlator */
169 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
170 if (!AFS_NFSXLATORREQ(acred)) afs_TryToSmush(tvc, acred, 0);
172 ReleaseWriteLock(&tvc->lock);
173 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
174 afs_BozonUnlock(&tvc->pvnLock, tvc);
176 afs_PutVCache(tvc, WRITE_LOCK);
181 static char *newname() {
182 char *name, *sp, *p = ".__afs";
183 afs_int32 rd = afs_random() & 0xffff;
185 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
186 while (*p != '\0') *sp++ = *p++;
188 *sp++= "0123456789ABCDEF"[rd & 0x0f];
195 /* these variables appear to exist for debugging purposes */
196 struct vcache * Tadp1, * Ttvc;
201 /* Note that we don't set CDirty here, this is OK because the unlink
202 * RPC is called synchronously */
205 struct nameidata *ndp; {
206 register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
207 char *aname = ndp->ni_dent.d_name;
208 struct ucred *acred = ndp->ni_cred;
209 #else /* AFS_OSF_ENV */
210 afs_remove(OSI_VC_ARG(adp), aname, acred)
213 struct AFS_UCRED *acred; {
215 struct vrequest treq;
216 register struct dcache *tdc;
217 struct VenusFid unlinkFid;
218 register afs_int32 code;
219 register struct vcache *tvc;
220 afs_int32 offset, len;
221 struct AFSFetchStatus OutDirStatus;
222 struct AFSVolSync tsync;
226 AFS_STATCNT(afs_remove);
227 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
228 ICL_TYPE_STRING, aname);
230 if (code = afs_InitReq(&treq, acred))
233 code = afs_VerifyVCache(adp, &treq);
235 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
237 afs_PutVCache(adp, 0);
238 afs_PutVCache(tvc, 0);
239 return afs_CheckCode(code, &treq, 22);
241 #else /* AFS_OSF_ENV */
242 tvc = (struct vcache *) 0;
244 code = afs_CheckCode(code, &treq, 23);
249 /** If the volume is read-only, return error without making an RPC to the
252 if ( adp->states & CRO ) {
257 tdc = afs_GetDCache(adp, 0, &treq, &offset, &len, 1); /* test for error below */
258 ObtainWriteLock(&adp->lock,142);
261 * Make sure that the data in the cache is current. We may have
262 * received a callback while we were waiting for the write lock.
264 if (!(adp->states & CStatd)
265 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
266 ReleaseWriteLock(&adp->lock);
272 unlinkFid.Fid.Vnode = 0;
274 tvc = osi_dnlc_lookup (adp, aname, WRITE_LOCK);
276 /* This should not be necessary since afs_lookup() has already
280 code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
284 unlinkFid.Cell = adp->fid.Cell;
285 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
286 if (unlinkFid.Fid.Unique == 0) {
287 tvc = afs_LookupVCache(&unlinkFid, &treq, &cached,
288 WRITE_LOCK, adp, aname);
290 ObtainReadLock(&afs_xvcache);
291 tvc = afs_FindVCache(&unlinkFid, 1, WRITE_LOCK,
293 ReleaseReadLock(&afs_xvcache);
298 if (tvc && osi_Active(tvc)) {
299 /* about to delete whole file, prefetch it first */
300 ReleaseWriteLock(&adp->lock);
301 ObtainWriteLock(&tvc->lock,143);
302 #if defined(AFS_OSF_ENV)
303 afs_Wire(tvc, &treq);
304 #else /* AFS_OSF_ENV */
305 FetchWholeEnchilada(tvc, &treq);
307 ReleaseWriteLock(&tvc->lock);
308 ObtainWriteLock(&adp->lock,144);
311 osi_dnlc_remove ( adp, aname, tvc);
312 if (tvc) afs_symhint_inval(tvc);
314 Tadp1 = adp; Tadpr = adp->vrefCount; Ttvc = tvc; Tnam = aname; Tnam1 = 0;
315 if (tvc) Ttvcr = tvc->vrefCount;
317 if (tvc && (tvc->vrefCount > 2) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
319 if (tvc && (tvc->vrefCount > 1) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
321 char *unlname = newname();
323 ReleaseWriteLock(&adp->lock);
324 code = afsrename(adp, aname, adp, unlname, acred);
327 tvc->mvid = (struct VenusFid *)unlname;
333 tvc->states |= CUnlinked;
335 osi_FreeSmallSpace(unlname);
339 afs_PutVCache(tvc, WRITE_LOCK);
341 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
344 afs_PutVCache(adp, WRITE_LOCK);
345 #endif /* AFS_OSF_ENV */
350 /* afs_remunlink -- This tries to delete the file at the server after it has
351 * been renamed when unlinked locally but now has been finally released.
353 * CAUTION -- may be called with avc unheld. */
355 afs_remunlink(avc, doit)
356 register struct vcache *avc;
359 struct AFS_UCRED *cred;
362 struct vrequest treq;
363 struct VenusFid dirFid;
364 register struct dcache *tdc;
365 afs_int32 offset, len, code=0;
367 if (NBObtainWriteLock(&avc->lock, 423))
370 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
371 if (code = afs_InitReq(&treq, avc->uncred)) {
372 ReleaseWriteLock(&avc->lock);
375 /* Must bump the refCount because GetVCache may block.
376 * Also clear mvid so no other thread comes here if we block.
378 unlname = (char *)avc->mvid;
385 /* We'll only try this once. If it fails, just release the vnode.
386 * Clear after doing hold so that NewVCache doesn't find us yet.
388 avc->states &= ~(CUnlinked | CUnlinkedDel);
390 ReleaseWriteLock(&avc->lock);
392 dirFid.Cell = avc->fid.Cell;
393 dirFid.Fid.Volume = avc->fid.Fid.Volume;
394 dirFid.Fid.Vnode = avc->parentVnode;
395 dirFid.Fid.Unique = avc->parentUnique;
396 adp = afs_GetVCache(&dirFid, &treq, (afs_int32 *)0,
397 (struct vcache *)0, WRITE_LOCK);
400 tdc = afs_FindDCache(adp, 0);
401 ObtainWriteLock(&adp->lock, 159);
403 /* afsremove releases the adp lock, and does vn_rele(avc) */
404 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
405 afs_PutVCache(adp, WRITE_LOCK);
407 /* we failed - and won't be back to try again. */
408 afs_PutVCache(avc, WRITE_LOCK);
410 osi_FreeSmallSpace(unlname);
415 ReleaseWriteLock(&avc->lock);