08473340bbbc58903ddb1231ed92193504f180d6
[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.inode, aname);
158         if (code) {
159             ZapDCE(tdc);        /* surprise error -- invalid value */
160             DZap(&tdc->f.inode);
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
283 #if 0
284     if (adp->mvstat == 2) {
285         afs_PutFakeStat(&fakestate);
286 #ifdef  AFS_OSF_ENV
287         afs_PutVCache(adp);
288         afs_PutVCache(tvc);
289 #endif
290         return EISDIR;
291     }
292 #endif
293
294     if (strlen(aname) > AFSNAMEMAX) {
295         afs_PutFakeStat(&fakestate);
296 #ifdef  AFS_OSF_ENV
297         afs_PutVCache(adp);
298         afs_PutVCache(tvc);
299 #endif
300         return ENAMETOOLONG;
301     }
302   tagain:
303     code = afs_VerifyVCache(adp, &treq);
304 #ifdef  AFS_OSF_ENV
305     tvc = VTOAFS(ndp->ni_vp);   /* should never be null */
306     if (code) {
307         afs_PutVCache(adp);
308         afs_PutVCache(tvc);
309         afs_PutFakeStat(&fakestate);
310         return afs_CheckCode(code, &treq, 22);
311     }
312 #else /* AFS_OSF_ENV */
313     tvc = NULL;
314     if (code) {
315         code = afs_CheckCode(code, &treq, 23);
316         afs_PutFakeStat(&fakestate);
317         return code;
318     }
319 #endif
320
321     /** If the volume is read-only, return error without making an RPC to the
322       * fileserver
323       */
324     if (adp->states & CRO) {
325 #ifdef  AFS_OSF_ENV
326         afs_PutVCache(adp);
327         afs_PutVCache(tvc);
328 #endif
329         code = EROFS;
330         afs_PutFakeStat(&fakestate);
331         return code;
332     }
333
334     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
335     ObtainWriteLock(&adp->lock, 142);
336     if (tdc)
337         ObtainSharedLock(&tdc->lock, 638);
338
339     /*
340      * Make sure that the data in the cache is current. We may have
341      * received a callback while we were waiting for the write lock.
342      */
343     if (!(adp->states & CStatd)
344         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
345         ReleaseWriteLock(&adp->lock);
346         if (tdc) {
347             ReleaseSharedLock(&tdc->lock);
348             afs_PutDCache(tdc);
349         }
350         goto tagain;
351     }
352
353     unlinkFid.Fid.Vnode = 0;
354     if (!tvc) {
355         tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
356     }
357     /* This should not be necessary since afs_lookup() has already
358      * done the work */
359     if (!tvc)
360         if (tdc) {
361             code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
362             if (code == 0) {
363                 afs_int32 cached = 0;
364
365                 unlinkFid.Cell = adp->fid.Cell;
366                 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
367                 if (unlinkFid.Fid.Unique == 0) {
368                     tvc =
369                         afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
370                                          aname);
371                 } else {
372                     ObtainReadLock(&afs_xvcache);
373                     tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
374                     ReleaseReadLock(&afs_xvcache);
375                 }
376             }
377         }
378
379     if (tvc && osi_Active(tvc)) {
380         /* about to delete whole file, prefetch it first */
381         ReleaseWriteLock(&adp->lock);
382         ObtainWriteLock(&tvc->lock, 143);
383 #if     defined(AFS_OSF_ENV)
384         afs_Wire(tvc, &treq);
385 #else /* AFS_OSF_ENV */
386         FetchWholeEnchilada(tvc, &treq);
387 #endif
388         ReleaseWriteLock(&tvc->lock);
389         ObtainWriteLock(&adp->lock, 144);
390     }
391
392     osi_dnlc_remove(adp, aname, tvc);
393     if (tvc)
394         afs_symhint_inval(tvc);
395
396     Tadp1 = adp;
397     Tadpr = VREFCOUNT(adp);
398     Ttvc = tvc;
399     Tnam = aname;
400     Tnam1 = 0;
401     if (tvc)
402         Ttvcr = VREFCOUNT(tvc);
403 #ifdef  AFS_AIX_ENV
404     if (tvc && (VREFCOUNT(tvc) > 2) && tvc->opens > 0
405         && !(tvc->states & CUnlinked)) {
406 #else
407     if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0
408         && !(tvc->states & CUnlinked)) {
409 #endif
410         char *unlname = newname();
411
412         ReleaseWriteLock(&adp->lock);
413         if (tdc)
414             ReleaseSharedLock(&tdc->lock);
415         code = afsrename(adp, aname, adp, unlname, acred, &treq);
416         Tnam1 = unlname;
417         if (!code) {
418             tvc->mvid = (struct VenusFid *)unlname;
419             crhold(acred);
420             if (tvc->uncred) {
421                 crfree(tvc->uncred);
422             }
423             tvc->uncred = acred;
424             tvc->states |= CUnlinked;
425         } else {
426             osi_FreeSmallSpace(unlname);
427         }
428         if (tdc)
429             afs_PutDCache(tdc);
430         afs_PutVCache(tvc);
431     } else {
432         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
433     }
434 #ifdef  AFS_OSF_ENV
435     afs_PutVCache(adp);
436 #endif /* AFS_OSF_ENV */
437     afs_PutFakeStat(&fakestate);
438     return code;
439 }
440
441
442 /* afs_remunlink -- This tries to delete the file at the server after it has
443  *     been renamed when unlinked locally but now has been finally released.
444  *
445  * CAUTION -- may be called with avc unheld. */
446
447 int
448 afs_remunlink(register struct vcache *avc, register int doit)
449 {
450     struct AFS_UCRED *cred;
451     char *unlname;
452     struct vcache *adp;
453     struct vrequest treq;
454     struct VenusFid dirFid;
455     register struct dcache *tdc;
456     afs_int32 code = 0;
457
458     if (NBObtainWriteLock(&avc->lock, 423))
459         return 0;
460
461     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
462         if ((code = afs_InitReq(&treq, avc->uncred))) {
463             ReleaseWriteLock(&avc->lock);
464         } else {
465             /* Must bump the refCount because GetVCache may block.
466              * Also clear mvid so no other thread comes here if we block.
467              */
468             unlname = (char *)avc->mvid;
469             avc->mvid = NULL;
470             cred = avc->uncred;
471             avc->uncred = NULL;
472
473 #ifdef AFS_DARWIN_ENV
474             /* this is called by vrele (via VOP_INACTIVE) when the refcount
475              * is 0. we can't just call VN_HOLD since vref will panic.
476              * we can't just call osi_vnhold because a later AFS_RELE will call
477              * vrele again, which will try to call VOP_INACTIVE again after
478              * vn_locking the vnode. which would be fine except that our vrele
479              * caller also locked the vnode... So instead, we just gimmick the
480              * refcounts and hope nobody else can touch the file now */
481             osi_Assert(VREFCOUNT(avc) == 0);
482             VREFCOUNT_SET(avc, 1);
483 #endif
484             VN_HOLD(AFSTOV(avc));
485
486             /* We'll only try this once. If it fails, just release the vnode.
487              * Clear after doing hold so that NewVCache doesn't find us yet.
488              */
489             avc->states &= ~(CUnlinked | CUnlinkedDel);
490
491             ReleaseWriteLock(&avc->lock);
492
493             dirFid.Cell = avc->fid.Cell;
494             dirFid.Fid.Volume = avc->fid.Fid.Volume;
495             dirFid.Fid.Vnode = avc->parentVnode;
496             dirFid.Fid.Unique = avc->parentUnique;
497             adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
498
499             if (adp) {
500                 tdc = afs_FindDCache(adp, (afs_size_t) 0);
501                 ObtainWriteLock(&adp->lock, 159);
502                 if (tdc)
503                     ObtainSharedLock(&tdc->lock, 639);
504
505                 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
506                 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
507                 afs_PutVCache(adp);
508             } else {
509                 /* we failed - and won't be back to try again. */
510                 afs_PutVCache(avc);
511             }
512             osi_FreeSmallSpace(unlname);
513             crfree(cred);
514 #ifdef AFS_DARWIN_ENV
515             osi_Assert(VREFCOUNT(avc) == 1);
516             VREFCOUNT_SET(avc, 0);
517 #endif
518         }
519     } else {
520         ReleaseWriteLock(&avc->lock);
521     }
522
523     return code;
524 }