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
22 #include "../afs/param.h" /* Should be always first */
23 #include "../afs/sysincludes.h" /* Standard vendor system headers */
24 #include "../afs/afsincludes.h" /* Afs-based standard headers */
25 #include "../afs/afs_stats.h" /* statistics */
26 #include "../afs/afs_cbqueue.h"
27 #include "../afs/nfsclient.h"
28 #include "../afs/afs_osidnlc.h"
31 extern afs_rwlock_t afs_xvcache;
32 extern afs_rwlock_t afs_xcbhash;
37 * Wire down file in cache: prefetch all data, and turn on CWired flag
38 * so that callbacks/callback expirations are (temporarily) ignored
39 * and cache file(s) are kept in cache. File will be unwired when
40 * afs_inactive is called (ie no one has VN_HOLD on vnode), or when
41 * afs_IsWired notices that the file is no longer Active.
44 #else /* AFS_OSF_ENV */
45 static void FetchWholeEnchilada(avc, areq)
47 struct vrequest *areq;
48 register struct vcache *avc; {
49 register afs_int32 nextChunk, pos;
50 register struct dcache *tdc;
51 afs_int32 offset, len;
53 AFS_STATCNT(FetchWholeEnchilada);
54 if ((avc->states & CStatd) == 0) return; /* don't know size */
55 for(nextChunk=0;nextChunk<1024;nextChunk++) { /* sanity check on N chunks */
56 pos = AFS_CHUNKTOBASE(nextChunk);
57 #if defined(AFS_OSF_ENV)
58 if (pos >= avc->m.Length) break; /* all done */
59 #else /* AFS_OSF_ENV */
60 if (pos >= avc->m.Length) return; /* all done */
62 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
64 #if defined(AFS_OSF_ENV)
66 #else /* AFS_OSF_ENV */
71 #if defined(AFS_OSF_ENV)
72 avc->states |= CWired;
73 #endif /* AFS_OSF_ENV */
76 #if defined(AFS_OSF_ENV)
78 * Tests whether file is wired down, after unwiring the file if it
79 * is found to be inactive (ie not open and not being paged from).
82 register struct vcache *avc; {
83 if (avc->states & CWired) {
84 if (osi_Active(avc)) {
87 avc->states &= ~CWired;
91 #endif /* AFS_OSF_ENV */
93 afsremove(adp, tdc, tvc, aname, acred, treqp)
94 register struct vcache *adp;
95 register struct dcache *tdc;
96 register struct vcache *tvc;
98 struct vrequest *treqp;
99 struct AFS_UCRED *acred; {
100 register afs_int32 code;
101 register struct conn *tc;
102 afs_int32 offset, len;
103 struct AFSFetchStatus OutDirStatus;
104 struct AFSVolSync tsync;
108 tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
110 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
111 #ifdef RX_ENABLE_LOCKS
113 #endif /* RX_ENABLE_LOCKS */
114 code = RXAFS_RemoveFile(tc->id, (struct AFSFid *) &adp->fid.Fid,
115 aname, &OutDirStatus, &tsync);
116 #ifdef RX_ENABLE_LOCKS
118 #endif /* RX_ENABLE_LOCKS */
123 (afs_Analyze(tc, code, &adp->fid, treqp,
124 AFS_STATS_FS_RPCIDX_REMOVEFILE, SHARED_LOCK, (struct cell *)0));
126 osi_dnlc_remove (adp, aname, tvc);
127 if (tvc) afs_symhint_inval(tvc); /* XXX: don't really need to be so extreme */
130 if (tdc) afs_PutDCache(tdc);
131 if (tvc) afs_PutVCache(tvc, WRITE_LOCK);
134 ObtainWriteLock(&afs_xcbhash, 497);
135 afs_DequeueCallback(adp);
136 adp->states &= ~CStatd;
137 ReleaseWriteLock(&afs_xcbhash);
138 osi_dnlc_purgedp(adp);
140 ReleaseWriteLock(&adp->lock);
141 code = afs_CheckCode(code, treqp, 21);
144 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
145 /* we can do it locally */
146 code = afs_dir_Delete(&tdc->f.inode, aname);
148 ZapDCE(tdc); /* surprise error -- invalid value */
153 afs_PutDCache(tdc); /* drop ref count */
155 ReleaseWriteLock(&adp->lock);
157 /* now, get vnode for unlinked dude, and see if we should force it
158 * from cache. adp is now the deleted files vnode. Note that we
159 * call FindVCache instead of GetVCache since if the file's really
160 * gone, we won't be able to fetch the status info anyway. */
162 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
163 afs_BozonLock(&tvc->pvnLock, tvc);
164 /* Since afs_TryToSmush will do a pvn_vptrunc */
166 ObtainWriteLock(&tvc->lock,141);
167 /* note that callback will be broken on the deleted file if there are
168 * still >0 links left to it, so we'll get the stat right */
170 tvc->states &= ~CUnique; /* For the dfs xlator */
171 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
172 if (!AFS_NFSXLATORREQ(acred)) afs_TryToSmush(tvc, acred, 0);
174 ReleaseWriteLock(&tvc->lock);
175 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
176 afs_BozonUnlock(&tvc->pvnLock, tvc);
178 afs_PutVCache(tvc, WRITE_LOCK);
183 static char *newname() {
184 char *name, *sp, *p = ".__afs";
185 afs_int32 rd = afs_random() & 0xffff;
187 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
188 while (*p != '\0') *sp++ = *p++;
190 *sp++= "0123456789ABCDEF"[rd & 0x0f];
197 /* these variables appear to exist for debugging purposes */
198 struct vcache * Tadp1, * Ttvc;
203 /* Note that we don't set CDirty here, this is OK because the unlink
204 * RPC is called synchronously */
207 struct nameidata *ndp; {
208 register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
209 char *aname = ndp->ni_dent.d_name;
210 struct ucred *acred = ndp->ni_cred;
211 #else /* AFS_OSF_ENV */
212 afs_remove(OSI_VC_ARG(adp), aname, acred)
215 struct AFS_UCRED *acred; {
217 struct vrequest treq;
218 register struct dcache *tdc;
219 struct VenusFid unlinkFid;
220 register afs_int32 code;
221 register struct vcache *tvc;
222 afs_int32 offset, len;
223 struct AFSFetchStatus OutDirStatus;
224 struct AFSVolSync tsync;
228 AFS_STATCNT(afs_remove);
229 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
230 ICL_TYPE_STRING, aname);
232 if (code = afs_InitReq(&treq, acred))
235 code = afs_VerifyVCache(adp, &treq);
237 tvc = (struct vcache *)ndp->ni_vp; /* should never be null */
239 afs_PutVCache(adp, 0);
240 afs_PutVCache(tvc, 0);
241 return afs_CheckCode(code, &treq, 22);
243 #else /* AFS_OSF_ENV */
244 tvc = (struct vcache *) 0;
246 code = afs_CheckCode(code, &treq, 23);
251 /** If the volume is read-only, return error without making an RPC to the
254 if ( adp->states & CRO ) {
259 tdc = afs_GetDCache(adp, 0, &treq, &offset, &len, 1); /* test for error below */
260 ObtainWriteLock(&adp->lock,142);
263 * Make sure that the data in the cache is current. We may have
264 * received a callback while we were waiting for the write lock.
266 if (!(adp->states & CStatd)
267 || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
268 ReleaseWriteLock(&adp->lock);
274 unlinkFid.Fid.Vnode = 0;
276 tvc = osi_dnlc_lookup (adp, aname, WRITE_LOCK);
278 /* This should not be necessary since afs_lookup() has already
282 code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
286 unlinkFid.Cell = adp->fid.Cell;
287 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
288 if (unlinkFid.Fid.Unique == 0) {
289 tvc = afs_LookupVCache(&unlinkFid, &treq, &cached,
290 WRITE_LOCK, adp, aname);
292 ObtainReadLock(&afs_xvcache);
293 tvc = afs_FindVCache(&unlinkFid, 1, WRITE_LOCK,
295 ReleaseReadLock(&afs_xvcache);
300 if (tvc && osi_Active(tvc)) {
301 /* about to delete whole file, prefetch it first */
302 ReleaseWriteLock(&adp->lock);
303 ObtainWriteLock(&tvc->lock,143);
304 #if defined(AFS_OSF_ENV)
305 afs_Wire(tvc, &treq);
306 #else /* AFS_OSF_ENV */
307 FetchWholeEnchilada(tvc, &treq);
309 ReleaseWriteLock(&tvc->lock);
310 ObtainWriteLock(&adp->lock,144);
313 osi_dnlc_remove ( adp, aname, tvc);
314 if (tvc) afs_symhint_inval(tvc);
316 Tadp1 = adp; Tadpr = adp->vrefCount; Ttvc = tvc; Tnam = aname; Tnam1 = 0;
317 if (tvc) Ttvcr = tvc->vrefCount;
319 if (tvc && (tvc->vrefCount > 2) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
321 if (tvc && (tvc->vrefCount > 1) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
323 char *unlname = newname();
325 ReleaseWriteLock(&adp->lock);
326 code = afsrename(adp, aname, adp, unlname, acred);
329 tvc->mvid = (struct VenusFid *)unlname;
335 tvc->states |= CUnlinked;
337 osi_FreeSmallSpace(unlname);
341 afs_PutVCache(tvc, WRITE_LOCK);
343 code = afsremove(adp, tdc, tvc, aname, acred, &treq);
346 afs_PutVCache(adp, WRITE_LOCK);
347 #endif /* AFS_OSF_ENV */
352 /* afs_remunlink -- This tries to delete the file at the server after it has
353 * been renamed when unlinked locally but now has been finally released.
355 * CAUTION -- may be called with avc unheld. */
357 afs_remunlink(avc, doit)
358 register struct vcache *avc;
361 struct AFS_UCRED *cred;
364 struct vrequest treq;
365 struct VenusFid dirFid;
366 register struct dcache *tdc;
367 afs_int32 offset, len, code=0;
369 if (NBObtainWriteLock(&avc->lock, 423))
372 if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
373 if (code = afs_InitReq(&treq, avc->uncred)) {
374 ReleaseWriteLock(&avc->lock);
377 /* Must bump the refCount because GetVCache may block.
378 * Also clear mvid so no other thread comes here if we block.
380 unlname = (char *)avc->mvid;
387 /* We'll only try this once. If it fails, just release the vnode.
388 * Clear after doing hold so that NewVCache doesn't find us yet.
390 avc->states &= ~(CUnlinked | CUnlinkedDel);
392 ReleaseWriteLock(&avc->lock);
394 dirFid.Cell = avc->fid.Cell;
395 dirFid.Fid.Volume = avc->fid.Fid.Volume;
396 dirFid.Fid.Vnode = avc->parentVnode;
397 dirFid.Fid.Unique = avc->parentUnique;
398 adp = afs_GetVCache(&dirFid, &treq, (afs_int32 *)0,
399 (struct vcache *)0, WRITE_LOCK);
402 tdc = afs_FindDCache(adp, 0);
403 ObtainWriteLock(&adp->lock, 159);
405 /* afsremove releases the adp lock, and does vn_rele(avc) */
406 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
407 afs_PutVCache(adp, WRITE_LOCK);
409 /* we failed - and won't be back to try again. */
410 afs_PutVCache(avc, WRITE_LOCK);
412 osi_FreeSmallSpace(unlname);
417 ReleaseWriteLock(&avc->lock);