Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / afs / VNOPS / afs_vnop_remove.c
1 /* Copyright (C) 1995, 1989, 1998 Transarc Corporation - All rights reserved */
2 /*
3  * (C) COPYRIGHT IBM CORPORATION 1987, 1988
4  * LICENSED MATERIALS - PROPERTY OF IBM
5  */
6 /*
7  * afs_vnop_remove.c
8  *
9  * Implements:
10  * afs_Wire (DUX)
11  * FetchWholeEnchilada
12  * afs_IsWired (DUX)
13  * afsremove
14  * afs_remove
15  *
16  * Local:
17  * newname
18  *
19  */
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"
27
28
29 extern afs_rwlock_t afs_xvcache;
30 extern afs_rwlock_t afs_xcbhash;
31
32
33 #ifdef  AFS_OSF_ENV
34 /*
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.
40  */
41 afs_Wire(avc, areq)
42 #else   /* AFS_OSF_ENV */
43 static void FetchWholeEnchilada(avc, areq)
44 #endif
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;
50
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 */
59 #endif
60         tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
61         if (!tdc) 
62 #if     defined(AFS_OSF_ENV)
63             break;
64 #else   /* AFS_OSF_ENV */
65             return;
66 #endif
67         afs_PutDCache(tdc);
68     }
69 #if defined(AFS_OSF_ENV)
70     avc->states |= CWired;
71 #endif  /* AFS_OSF_ENV */
72 }
73
74 #if     defined(AFS_OSF_ENV)
75 /*
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).
78  */
79 afs_IsWired(avc)
80     register struct vcache *avc; {
81     if (avc->states & CWired) {
82         if (osi_Active(avc)) {
83             return 1;
84         }
85         avc->states &= ~CWired;
86     }
87     return 0;
88 }
89 #endif  /* AFS_OSF_ENV */
90
91 afsremove(adp, tdc, tvc, aname, acred, treqp)
92     register struct vcache *adp;
93     register struct dcache *tdc;
94     register struct vcache *tvc;
95     char *aname;
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;
103     XSTATS_DECLS
104
105     do {
106         tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
107         if (tc) {
108           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
109 #ifdef RX_ENABLE_LOCKS
110             AFS_GUNLOCK();
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
115             AFS_GLOCK();
116 #endif /* RX_ENABLE_LOCKS */
117           XSTATS_END_TIME;
118       }
119         else code = -1;
120     } while
121       (afs_Analyze(tc, code, &adp->fid, treqp,
122                    AFS_STATS_FS_RPCIDX_REMOVEFILE, SHARED_LOCK, (struct cell *)0));
123
124     osi_dnlc_remove (adp, aname, tvc);
125     if (tvc) afs_symhint_inval(tvc);   /* XXX: don't really need to be so extreme */
126
127     if (code) {
128         if (tdc) afs_PutDCache(tdc);
129         if (tvc) afs_PutVCache(tvc, WRITE_LOCK);
130
131         if (code < 0) {
132           ObtainWriteLock(&afs_xcbhash, 497);
133           afs_DequeueCallback(adp);
134           adp->states &= ~CStatd;
135           ReleaseWriteLock(&afs_xcbhash);
136           osi_dnlc_purgedp(adp);
137         }
138         ReleaseWriteLock(&adp->lock);
139         code = afs_CheckCode(code, treqp, 21);
140         return code;
141     }
142     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
143         /* we can do it locally */
144         code = afs_dir_Delete(&tdc->f.inode, aname);
145         if (code) {
146             ZapDCE(tdc);        /* surprise error -- invalid value */
147             DZap(&tdc->f.inode);
148         }
149     }
150     if (tdc) {
151         afs_PutDCache(tdc);     /* drop ref count */
152     }
153     ReleaseWriteLock(&adp->lock);
154
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.  */
159     if (tvc) {
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 */
163 #endif
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 */
167         tvc->m.LinkCount--;
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);
171         } 
172         ReleaseWriteLock(&tvc->lock);
173 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
174         afs_BozonUnlock(&tvc->pvnLock, tvc);
175 #endif
176         afs_PutVCache(tvc, WRITE_LOCK);
177     }
178     return (0);
179 }
180
181 static char *newname() {
182     char *name, *sp, *p = ".__afs";
183     afs_int32 rd = afs_random() & 0xffff;
184
185     sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
186     while (*p != '\0') *sp++ = *p++;
187     while (rd) {
188         *sp++= "0123456789ABCDEF"[rd & 0x0f];
189         rd >>= 4;
190     }
191     *sp = '\0';
192     return (name);
193 }
194
195 /* these variables appear to exist for debugging purposes */
196 struct vcache * Tadp1, * Ttvc;
197 int Tadpr, Ttvcr;
198 char *Tnam;
199 char *Tnam1;
200
201 /* Note that we don't set CDirty here, this is OK because the unlink
202  * RPC is called synchronously */
203 #ifdef  AFS_OSF_ENV
204 afs_remove(ndp)
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)
211     OSI_VC_DECL(adp);
212     char *aname;
213     struct AFS_UCRED *acred; {
214 #endif
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;
223     XSTATS_DECLS
224     OSI_VC_CONVERT(adp)
225
226     AFS_STATCNT(afs_remove);
227     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
228                ICL_TYPE_STRING, aname);
229
230     if (code = afs_InitReq(&treq, acred))
231       return code;
232 tagain:
233     code = afs_VerifyVCache(adp, &treq);
234 #ifdef  AFS_OSF_ENV
235     tvc = (struct vcache *)ndp->ni_vp;  /* should never be null */
236     if (code) {
237         afs_PutVCache(adp, 0);
238         afs_PutVCache(tvc, 0);
239         return afs_CheckCode(code, &treq, 22);
240     }
241 #else   /* AFS_OSF_ENV */
242     tvc = (struct vcache *) 0;
243     if (code) {
244       code = afs_CheckCode(code, &treq, 23);
245       return code;
246     }
247 #endif
248
249     /** If the volume is read-only, return error without making an RPC to the
250       * fileserver
251       */
252     if ( adp->states & CRO ) {
253         code = EROFS;
254         return code;
255     }
256
257     tdc = afs_GetDCache(adp, 0, &treq, &offset, &len, 1);  /* test for error below */
258     ObtainWriteLock(&adp->lock,142);
259
260     /*
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.
263      */
264     if (!(adp->states & CStatd)
265         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
266         ReleaseWriteLock(&adp->lock);
267         if (tdc)
268             afs_PutDCache(tdc);
269         goto tagain;
270     }
271
272     unlinkFid.Fid.Vnode = 0;
273     if (!tvc) {
274       tvc = osi_dnlc_lookup (adp, aname, WRITE_LOCK);
275     }
276     /* This should not be necessary since afs_lookup() has already
277      * done the work */
278     if (!tvc)
279       if (tdc) {
280         code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
281         if (code == 0) {        
282             afs_int32 cached=0;
283
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);
289             } else {
290                 ObtainReadLock(&afs_xvcache);
291                 tvc = afs_FindVCache(&unlinkFid, 1, WRITE_LOCK, 
292                                      0 , DO_STATS );
293                 ReleaseReadLock(&afs_xvcache);
294             }
295         }
296     }
297
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);
306 #endif
307         ReleaseWriteLock(&tvc->lock);
308         ObtainWriteLock(&adp->lock,144);
309     }
310
311     osi_dnlc_remove ( adp, aname, tvc);
312     if (tvc) afs_symhint_inval(tvc);
313
314     Tadp1 = adp; Tadpr = adp->vrefCount; Ttvc = tvc; Tnam = aname; Tnam1 = 0;
315     if (tvc) Ttvcr = tvc->vrefCount;
316 #ifdef  AFS_AIX_ENV
317     if (tvc && (tvc->vrefCount > 2) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
318 #else
319     if (tvc && (tvc->vrefCount > 1) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
320 #endif
321         char *unlname = newname();
322
323         ReleaseWriteLock(&adp->lock);
324         code = afsrename(adp, aname, adp, unlname, acred);
325         Tnam1 = unlname;
326         if (!code) {
327             tvc->mvid = (struct VenusFid *)unlname;
328             crhold(acred);
329             if (tvc->uncred) {
330                 crfree(tvc->uncred);
331             }
332             tvc->uncred = acred;
333             tvc->states |= CUnlinked;
334         } else {
335             osi_FreeSmallSpace(unlname);            
336         }
337         if ( tdc )
338                 afs_PutDCache(tdc);
339         afs_PutVCache(tvc, WRITE_LOCK); 
340     } else {
341         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
342     }
343 #ifdef  AFS_OSF_ENV
344     afs_PutVCache(adp, WRITE_LOCK);
345 #endif  /* AFS_OSF_ENV */
346     return code;
347 }
348
349
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.
352  *
353  * CAUTION -- may be called with avc unheld. */
354
355 afs_remunlink(avc, doit)
356     register struct vcache *avc;
357     register int doit;
358 {
359     struct AFS_UCRED *cred;
360     char *unlname;
361     struct vcache *adp;
362     struct vrequest treq;
363     struct VenusFid dirFid;
364     register struct dcache *tdc;
365     afs_int32 offset, len, code=0;
366
367     if (NBObtainWriteLock(&avc->lock, 423))
368         return 0;
369
370     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
371         if (code = afs_InitReq(&treq, avc->uncred)) {
372             ReleaseWriteLock(&avc->lock);
373         }
374         else {
375             /* Must bump the refCount because GetVCache may block.
376              * Also clear mvid so no other thread comes here if we block.
377              */
378             unlname = (char *)avc->mvid;
379             avc->mvid = NULL;
380             cred = avc->uncred;
381             avc->uncred = NULL;
382
383             VN_HOLD(&avc->v);
384
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.
387              */
388             avc->states  &= ~(CUnlinked | CUnlinkedDel);
389
390             ReleaseWriteLock(&avc->lock);
391
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);
398             
399             if (adp) {
400                 tdc = afs_FindDCache(adp, 0);
401                 ObtainWriteLock(&adp->lock, 159);
402
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);
406             } else {
407                 /* we failed - and won't be back to try again. */
408                 afs_PutVCache(avc, WRITE_LOCK);
409             }
410             osi_FreeSmallSpace(unlname);
411             crfree(cred);
412         }
413     }
414     else {
415         ReleaseWriteLock(&avc->lock);
416     }
417
418     return code;
419 }