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