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