more-64bit-file-size-support-20011031
[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 <afsconfig.h>
23 #include "../afs/param.h"
24
25 RCSID("$Header$");
26
27 #include "../afs/sysincludes.h" /* Standard vendor system headers */
28 #include "../afs/afsincludes.h" /* Afs-based standard headers */
29 #include "../afs/afs_stats.h" /* statistics */
30 #include "../afs/afs_cbqueue.h"
31 #include "../afs/nfsclient.h"
32 #include "../afs/afs_osidnlc.h"
33
34
35 extern afs_rwlock_t afs_xvcache;
36 extern afs_rwlock_t afs_xcbhash;
37
38
39 #ifdef  AFS_OSF_ENV
40 /*
41  *  Wire down file in cache: prefetch all data, and turn on CWired flag
42  *  so that callbacks/callback expirations are (temporarily) ignored
43  *  and cache file(s) are kept in cache. File will be unwired when
44  *  afs_inactive is called (ie no one has VN_HOLD on vnode), or when
45  *  afs_IsWired notices that the file is no longer Active.
46  */
47 afs_Wire(avc, areq)
48 #else   /* AFS_OSF_ENV */
49 static void FetchWholeEnchilada(avc, areq)
50 #endif
51 struct vrequest *areq;
52 register struct vcache *avc; {
53     register afs_int32 nextChunk;
54     register struct dcache *tdc;
55     afs_size_t pos, offset, len;
56
57     AFS_STATCNT(FetchWholeEnchilada);
58     if ((avc->states & CStatd) == 0) 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->m.Length) break;        /* all done */
63 #else   /* AFS_OSF_ENV */
64         if (pos >= avc->m.Length) return;       /* all done */
65 #endif
66         tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
67         if (!tdc) 
68 #if     defined(AFS_OSF_ENV)
69             break;
70 #else   /* AFS_OSF_ENV */
71             return;
72 #endif
73         afs_PutDCache(tdc);
74     }
75 #if defined(AFS_OSF_ENV)
76     avc->states |= CWired;
77 #endif  /* AFS_OSF_ENV */
78 }
79
80 #if     defined(AFS_OSF_ENV)
81 /*
82  *  Tests whether file is wired down, after unwiring the file if it
83  *  is found to be inactive (ie not open and not being paged from).
84  */
85 afs_IsWired(avc)
86     register struct vcache *avc; {
87     if (avc->states & CWired) {
88         if (osi_Active(avc)) {
89             return 1;
90         }
91         avc->states &= ~CWired;
92     }
93     return 0;
94 }
95 #endif  /* AFS_OSF_ENV */
96
97 afsremove(adp, tdc, tvc, aname, acred, treqp)
98     register struct vcache *adp;
99     register struct dcache *tdc;
100     register struct vcache *tvc;
101     char *aname;
102     struct vrequest *treqp;
103     struct AFS_UCRED *acred; {
104     register afs_int32 code;
105     register struct conn *tc;
106     struct AFSFetchStatus OutDirStatus;
107     struct AFSVolSync tsync;
108     XSTATS_DECLS
109
110     do {
111         tc = afs_Conn(&adp->fid, treqp, SHARED_LOCK);
112         if (tc) {
113           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
114 #ifdef RX_ENABLE_LOCKS
115             AFS_GUNLOCK();
116 #endif /* RX_ENABLE_LOCKS */
117             code = RXAFS_RemoveFile(tc->id, (struct AFSFid *) &adp->fid.Fid,
118                                     aname, &OutDirStatus, &tsync);
119 #ifdef RX_ENABLE_LOCKS
120             AFS_GLOCK();
121 #endif /* RX_ENABLE_LOCKS */
122           XSTATS_END_TIME;
123       }
124         else code = -1;
125     } while
126       (afs_Analyze(tc, code, &adp->fid, treqp,
127                    AFS_STATS_FS_RPCIDX_REMOVEFILE, SHARED_LOCK, (struct cell *)0));
128
129     osi_dnlc_remove (adp, aname, tvc);
130     if (tvc) afs_symhint_inval(tvc);   /* XXX: don't really need to be so extreme */
131
132     if (code) {
133         if (tdc) afs_PutDCache(tdc);
134         if (tvc) afs_PutVCache(tvc, WRITE_LOCK);
135
136         if (code < 0) {
137           ObtainWriteLock(&afs_xcbhash, 497);
138           afs_DequeueCallback(adp);
139           adp->states &= ~CStatd;
140           ReleaseWriteLock(&afs_xcbhash);
141           osi_dnlc_purgedp(adp);
142         }
143         ReleaseWriteLock(&adp->lock);
144         code = afs_CheckCode(code, treqp, 21);
145         return code;
146     }
147     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
148         /* we can do it locally */
149         code = afs_dir_Delete(&tdc->f.inode, aname);
150         if (code) {
151             ZapDCE(tdc);        /* surprise error -- invalid value */
152             DZap(&tdc->f.inode);
153         }
154     }
155     if (tdc) {
156         afs_PutDCache(tdc);     /* drop ref count */
157     }
158     ReleaseWriteLock(&adp->lock);
159
160     /* now, get vnode for unlinked dude, and see if we should force it
161      * from cache.  adp is now the deleted files vnode.  Note that we
162      * call FindVCache instead of GetVCache since if the file's really
163      * gone, we won't be able to fetch the status info anyway.  */
164     if (tvc) {
165 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
166         afs_BozonLock(&tvc->pvnLock, tvc);      
167         /* Since afs_TryToSmush will do a pvn_vptrunc */
168 #endif
169         ObtainWriteLock(&tvc->lock,141);
170         /* note that callback will be broken on the deleted file if there are
171          * still >0 links left to it, so we'll get the stat right */
172         tvc->m.LinkCount--;
173         tvc->states &= ~CUnique;                /* For the dfs xlator */
174         if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
175             if (!AFS_NFSXLATORREQ(acred)) afs_TryToSmush(tvc, acred, 0);
176         } 
177         ReleaseWriteLock(&tvc->lock);
178 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
179         afs_BozonUnlock(&tvc->pvnLock, tvc);
180 #endif
181         afs_PutVCache(tvc, WRITE_LOCK);
182     }
183     return (0);
184 }
185
186 static char *newname() {
187     char *name, *sp, *p = ".__afs";
188     afs_int32 rd = afs_random() & 0xffff;
189
190     sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
191     while (*p != '\0') *sp++ = *p++;
192     while (rd) {
193         *sp++= "0123456789ABCDEF"[rd & 0x0f];
194         rd >>= 4;
195     }
196     *sp = '\0';
197     return (name);
198 }
199
200 /* these variables appear to exist for debugging purposes */
201 struct vcache * Tadp1, * Ttvc;
202 int Tadpr, Ttvcr;
203 char *Tnam;
204 char *Tnam1;
205
206 /* Note that we don't set CDirty here, this is OK because the unlink
207  * RPC is called synchronously */
208 #ifdef  AFS_OSF_ENV
209 afs_remove(ndp)
210     struct nameidata *ndp; {
211     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
212     char *aname = ndp->ni_dent.d_name;
213     struct ucred *acred = ndp->ni_cred;
214 #else   /* AFS_OSF_ENV */
215 afs_remove(OSI_VC_ARG(adp), aname, acred)
216     OSI_VC_DECL(adp);
217     char *aname;
218     struct AFS_UCRED *acred; {
219 #endif
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 AFSFetchStatus OutDirStatus;
227     struct AFSVolSync tsync;
228     XSTATS_DECLS
229     OSI_VC_CONVERT(adp)
230
231     AFS_STATCNT(afs_remove);
232     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
233                ICL_TYPE_STRING, aname);
234
235     if (code = afs_InitReq(&treq, acred))
236       return code;
237 tagain:
238     code = afs_VerifyVCache(adp, &treq);
239 #ifdef  AFS_OSF_ENV
240     tvc = (struct vcache *)ndp->ni_vp;  /* should never be null */
241     if (code) {
242         afs_PutVCache(adp, 0);
243         afs_PutVCache(tvc, 0);
244         return afs_CheckCode(code, &treq, 22);
245     }
246 #else   /* AFS_OSF_ENV */
247     tvc = (struct vcache *) 0;
248     if (code) {
249       code = afs_CheckCode(code, &treq, 23);
250       return code;
251     }
252 #endif
253
254     /** If the volume is read-only, return error without making an RPC to the
255       * fileserver
256       */
257     if ( adp->states & CRO ) {
258         code = EROFS;
259         return code;
260     }
261
262     tdc = afs_GetDCache(adp, (afs_size_t) 0,    &treq, &offset, &len, 1);  /* test for error below */
263     ObtainWriteLock(&adp->lock,142);
264
265     /*
266      * Make sure that the data in the cache is current. We may have
267      * received a callback while we were waiting for the write lock.
268      */
269     if (!(adp->states & CStatd)
270         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
271         ReleaseWriteLock(&adp->lock);
272         if (tdc)
273             afs_PutDCache(tdc);
274         goto tagain;
275     }
276
277     unlinkFid.Fid.Vnode = 0;
278     if (!tvc) {
279       tvc = osi_dnlc_lookup (adp, aname, WRITE_LOCK);
280     }
281     /* This should not be necessary since afs_lookup() has already
282      * done the work */
283     if (!tvc)
284       if (tdc) {
285         code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
286         if (code == 0) {        
287             afs_int32 cached=0;
288
289             unlinkFid.Cell = adp->fid.Cell;
290             unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
291             if (unlinkFid.Fid.Unique == 0) {
292                 tvc = afs_LookupVCache(&unlinkFid, &treq, &cached, 
293                                        WRITE_LOCK, adp, aname);
294             } else {
295                 ObtainReadLock(&afs_xvcache);
296                 tvc = afs_FindVCache(&unlinkFid, 1, WRITE_LOCK, 
297                                      0 , DO_STATS );
298                 ReleaseReadLock(&afs_xvcache);
299             }
300         }
301     }
302
303     if (tvc && osi_Active(tvc)) {
304         /* about to delete whole file, prefetch it first */
305         ReleaseWriteLock(&adp->lock);
306         ObtainWriteLock(&tvc->lock,143);
307 #if     defined(AFS_OSF_ENV)
308         afs_Wire(tvc, &treq);
309 #else   /* AFS_OSF_ENV */
310         FetchWholeEnchilada(tvc, &treq);
311 #endif
312         ReleaseWriteLock(&tvc->lock);
313         ObtainWriteLock(&adp->lock,144);
314     }
315
316     osi_dnlc_remove ( adp, aname, tvc);
317     if (tvc) afs_symhint_inval(tvc);
318
319     Tadp1 = adp; Tadpr = VREFCOUNT(adp); Ttvc = tvc; Tnam = aname; Tnam1 = 0;
320     if (tvc) Ttvcr = VREFCOUNT(tvc);
321 #ifdef  AFS_AIX_ENV
322     if (tvc && (VREFCOUNT(tvc) > 2) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
323 #else
324     if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0 && !(tvc->states & CUnlinked)) {
325 #endif
326         char *unlname = newname();
327
328         ReleaseWriteLock(&adp->lock);
329         code = afsrename(adp, aname, adp, unlname, acred);
330         Tnam1 = unlname;
331         if (!code) {
332             tvc->mvid = (struct VenusFid *)unlname;
333             crhold(acred);
334             if (tvc->uncred) {
335                 crfree(tvc->uncred);
336             }
337             tvc->uncred = acred;
338             tvc->states |= CUnlinked;
339         } else {
340             osi_FreeSmallSpace(unlname);            
341         }
342         if ( tdc )
343                 afs_PutDCache(tdc);
344         afs_PutVCache(tvc, WRITE_LOCK); 
345     } else {
346         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
347     }
348 #ifdef  AFS_OSF_ENV
349     afs_PutVCache(adp, WRITE_LOCK);
350 #endif  /* AFS_OSF_ENV */
351     return code;
352 }
353
354
355 /* afs_remunlink -- This tries to delete the file at the server after it has
356  *     been renamed when unlinked locally but now has been finally released.
357  *
358  * CAUTION -- may be called with avc unheld. */
359
360 afs_remunlink(avc, doit)
361     register struct vcache *avc;
362     register int doit;
363 {
364     struct AFS_UCRED *cred;
365     char *unlname;
366     struct vcache *adp;
367     struct vrequest treq;
368     struct VenusFid dirFid;
369     register struct dcache *tdc;
370     afs_int32 code=0;
371
372     if (NBObtainWriteLock(&avc->lock, 423))
373         return 0;
374
375     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
376         if (code = afs_InitReq(&treq, avc->uncred)) {
377             ReleaseWriteLock(&avc->lock);
378         }
379         else {
380             /* Must bump the refCount because GetVCache may block.
381              * Also clear mvid so no other thread comes here if we block.
382              */
383             unlname = (char *)avc->mvid;
384             avc->mvid = NULL;
385             cred = avc->uncred;
386             avc->uncred = NULL;
387
388             VN_HOLD(&avc->v);
389
390             /* We'll only try this once. If it fails, just release the vnode.
391              * Clear after doing hold so that NewVCache doesn't find us yet.
392              */
393             avc->states  &= ~(CUnlinked | CUnlinkedDel);
394
395             ReleaseWriteLock(&avc->lock);
396
397             dirFid.Cell = avc->fid.Cell;
398             dirFid.Fid.Volume = avc->fid.Fid.Volume;
399             dirFid.Fid.Vnode = avc->parentVnode;
400             dirFid.Fid.Unique = avc->parentUnique;
401             adp = afs_GetVCache(&dirFid, &treq, (afs_int32 *)0, 
402                                 (struct vcache *)0, WRITE_LOCK);
403             
404             if (adp) {
405                 tdc = afs_FindDCache(adp, 0);
406                 ObtainWriteLock(&adp->lock, 159);
407
408                 /* afsremove releases the adp lock, and does vn_rele(avc) */
409                 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
410                 afs_PutVCache(adp, WRITE_LOCK);
411             } else {
412                 /* we failed - and won't be back to try again. */
413                 afs_PutVCache(avc, WRITE_LOCK);
414             }
415             osi_FreeSmallSpace(unlname);
416             crfree(cred);
417         }
418     }
419     else {
420         ReleaseWriteLock(&avc->lock);
421     }
422
423     return code;
424 }