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