6f8238a2bf5d884b29f89bd0687e6fd34ee3ab1e
[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 = 0;
108     register struct conn *tc;
109     struct AFSFetchStatus OutDirStatus;
110     struct AFSVolSync tsync;
111     XSTATS_DECLS;
112     
113     if (!AFS_IS_DISCONNECTED) {
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
131     osi_dnlc_remove(adp, aname, tvc);
132
133     if (code) {
134         if (tdc) {
135             ReleaseSharedLock(&tdc->lock);
136             afs_PutDCache(tdc);
137         }
138         if (tvc)
139             afs_PutVCache(tvc);
140
141         if (code < 0) {
142             ObtainWriteLock(&afs_xcbhash, 497);
143             afs_DequeueCallback(adp);
144             adp->states &= ~CStatd;
145             ReleaseWriteLock(&afs_xcbhash);
146             osi_dnlc_purgedp(adp);
147         }
148         ReleaseWriteLock(&adp->lock);
149         code = afs_CheckCode(code, treqp, 21);
150         return code;
151     }
152     if (tdc)
153         UpgradeSToWLock(&tdc->lock, 637);
154     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
155         /* we can do it locally */
156         code = afs_dir_Delete(tdc, aname);
157         if (code) {
158             ZapDCE(tdc);        /* surprise error -- invalid value */
159             DZap(tdc);
160         }
161     }
162     if (tdc) {
163         ReleaseWriteLock(&tdc->lock);
164         afs_PutDCache(tdc);     /* drop ref count */
165     }
166     ReleaseWriteLock(&adp->lock);
167
168     /* now, get vnode for unlinked dude, and see if we should force it
169      * from cache.  adp is now the deleted files vnode.  Note that we
170      * call FindVCache instead of GetVCache since if the file's really
171      * gone, we won't be able to fetch the status info anyway.  */
172     if (tvc) {
173 #ifdef AFS_BOZONLOCK_ENV
174         afs_BozonLock(&tvc->pvnLock, tvc);
175         /* Since afs_TryToSmush will do a pvn_vptrunc */
176 #endif
177         ObtainWriteLock(&tvc->lock, 141);
178         /* note that callback will be broken on the deleted file if there are
179          * still >0 links left to it, so we'll get the stat right */
180         tvc->m.LinkCount--;
181         tvc->states &= ~CUnique;        /* For the dfs xlator */
182         if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
183             if (!AFS_NFSXLATORREQ(acred))
184                 afs_TryToSmush(tvc, acred, 0);
185         }
186         ReleaseWriteLock(&tvc->lock);
187 #ifdef AFS_BOZONLOCK_ENV
188         afs_BozonUnlock(&tvc->pvnLock, tvc);
189 #endif
190         afs_PutVCache(tvc);
191     }
192     return (0);
193 }
194
195 char *
196 afs_newname(void)
197 {
198     char *name, *sp, *p = ".__afs";
199     afs_int32 rd = afs_random() & 0xffff;
200
201     sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
202     while (*p != '\0')
203         *sp++ = *p++;
204     while (rd) {
205         *sp++ = "0123456789ABCDEF"[rd & 0x0f];
206         rd >>= 4;
207     }
208     *sp = '\0';
209     return (name);
210 }
211
212 /* these variables appear to exist for debugging purposes */
213 struct vcache *Tadp1, *Ttvc;
214 int Tadpr, Ttvcr;
215 char *Tnam;
216 char *Tnam1;
217
218 /* Note that we don't set CDirty here, this is OK because the unlink
219  * RPC is called synchronously */
220 int
221 afs_remove(OSI_VC_ARG(adp), aname, acred)
222      OSI_VC_DECL(adp);
223      char *aname;
224      struct AFS_UCRED *acred;
225 {
226     struct vrequest treq;
227     register struct dcache *tdc;
228     struct VenusFid unlinkFid;
229     register afs_int32 code;
230     register struct vcache *tvc;
231     afs_size_t offset, len;
232     struct afs_fakestat_state fakestate;
233     OSI_VC_CONVERT(adp);
234
235     AFS_STATCNT(afs_remove);
236     afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
237                ICL_TYPE_STRING, aname);
238
239 #ifdef  AFS_OSF_ENV
240     tvc = (struct vcache *)ndp->ni_vp;  /* should never be null */
241 #endif
242
243     if ((code = afs_InitReq(&treq, acred))) {
244 #ifdef  AFS_OSF_ENV
245         afs_PutVCache(tvc);
246 #endif
247         return code;
248     }
249
250     afs_InitFakeStat(&fakestate);
251     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
252     if (code) {
253         afs_PutFakeStat(&fakestate);
254 #ifdef  AFS_OSF_ENV
255         afs_PutVCache(tvc);
256 #endif
257         return code;
258     }
259
260     /* Check if this is dynroot */
261     if (afs_IsDynroot(adp)) {
262         code = afs_DynrootVOPRemove(adp, acred, aname);
263         afs_PutFakeStat(&fakestate);
264 #ifdef  AFS_OSF_ENV
265         afs_PutVCache(tvc);
266 #endif
267         return code;
268     }
269     if (afs_IsDynrootMount(adp)) {
270         return ENOENT;
271     }
272
273     if (strlen(aname) > AFSNAMEMAX) {
274         afs_PutFakeStat(&fakestate);
275 #ifdef  AFS_OSF_ENV
276         afs_PutVCache(tvc);
277 #endif
278         return ENAMETOOLONG;
279     }
280   tagain:
281     code = afs_VerifyVCache(adp, &treq);
282 #ifdef  AFS_OSF_ENV
283     tvc = VTOAFS(ndp->ni_vp);   /* should never be null */
284     if (code) {
285         afs_PutVCache(tvc);
286         afs_PutFakeStat(&fakestate);
287         return afs_CheckCode(code, &treq, 22);
288     }
289 #else /* AFS_OSF_ENV */
290     tvc = NULL;
291     if (code) {
292         code = afs_CheckCode(code, &treq, 23);
293         afs_PutFakeStat(&fakestate);
294         return code;
295     }
296 #endif
297
298     /** If the volume is read-only, return error without making an RPC to the
299       * fileserver
300       */
301     if (adp->states & CRO) {
302 #ifdef  AFS_OSF_ENV
303         afs_PutVCache(tvc);
304 #endif
305         code = EROFS;
306         afs_PutFakeStat(&fakestate);
307         return code;
308     }
309
310     /* If we're running disconnected without logging, go no further... */
311     if (AFS_IS_DISCONNECTED && !AFS_IS_LOGGING) {
312 #ifdef  AFS_OSF_ENV
313         afs_PutVCache(tvc);
314 #endif
315         code = ENETDOWN;
316         afs_PutFakeStat(&fakestate);
317         return code;
318     }
319     
320     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
321     ObtainWriteLock(&adp->lock, 142);
322     if (tdc)
323         ObtainSharedLock(&tdc->lock, 638);
324
325     /*
326      * Make sure that the data in the cache is current. We may have
327      * received a callback while we were waiting for the write lock.
328      */
329     if (!(adp->states & CStatd)
330         || (tdc && !hsame(adp->m.DataVersion, tdc->f.versionNo))) {
331         ReleaseWriteLock(&adp->lock);
332         if (tdc) {
333             ReleaseSharedLock(&tdc->lock);
334             afs_PutDCache(tdc);
335         }
336         goto tagain;
337     }
338
339     unlinkFid.Fid.Vnode = 0;
340     if (!tvc) {
341         tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
342     }
343     /* This should not be necessary since afs_lookup() has already
344      * done the work */
345     if (!tvc)
346         if (tdc) {
347             code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
348             if (code == 0) {
349                 afs_int32 cached = 0;
350
351                 unlinkFid.Cell = adp->fid.Cell;
352                 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
353                 if (unlinkFid.Fid.Unique == 0) {
354                     tvc =
355                         afs_LookupVCache(&unlinkFid, &treq, &cached, adp,
356                                          aname);
357                 } else {
358                     ObtainReadLock(&afs_xvcache);
359                     tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
360                     ReleaseReadLock(&afs_xvcache);
361                 }
362             }
363         }
364
365     if (tvc && osi_Active(tvc)) {
366         /* about to delete whole file, prefetch it first */
367         ReleaseWriteLock(&adp->lock);
368         if (tdc)
369             ReleaseSharedLock(&tdc->lock);
370         ObtainWriteLock(&tvc->lock, 143);
371 #if     defined(AFS_OSF_ENV)
372         afs_Wire(tvc, &treq);
373 #else /* AFS_OSF_ENV */
374         FetchWholeEnchilada(tvc, &treq);
375 #endif
376         ReleaseWriteLock(&tvc->lock);
377         ObtainWriteLock(&adp->lock, 144);
378         /* Technically I don't think we need this back, but let's hold it 
379            anyway; The "got" reference should actually be sufficient. */
380         if (tdc) 
381             ObtainSharedLock(&tdc->lock, 640);
382     }
383
384     osi_dnlc_remove(adp, aname, tvc);
385
386     Tadp1 = adp;
387 #ifndef AFS_DARWIN80_ENV
388     Tadpr = VREFCOUNT(adp);
389 #endif
390     Ttvc = tvc;
391     Tnam = aname;
392     Tnam1 = 0;
393 #ifndef AFS_DARWIN80_ENV
394     if (tvc)
395         Ttvcr = VREFCOUNT(tvc);
396 #endif
397 #ifdef  AFS_AIX_ENV
398     if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
399         && !(tvc->states & CUnlinked)) {
400 #else
401     if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
402         && !(tvc->states & CUnlinked)) {
403 #endif
404         char *unlname = afs_newname();
405
406         ReleaseWriteLock(&adp->lock);
407         if (tdc)
408             ReleaseSharedLock(&tdc->lock);
409         code = afsrename(adp, aname, adp, unlname, acred, &treq);
410         Tnam1 = unlname;
411         if (!code) {
412             struct VenusFid *oldmvid = NULL;
413             if (tvc->mvid) 
414                 oldmvid = tvc->mvid;
415             tvc->mvid = (struct VenusFid *)unlname;
416             if (oldmvid)
417                 osi_FreeSmallSpace(oldmvid);
418             crhold(acred);
419             if (tvc->uncred) {
420                 crfree(tvc->uncred);
421             }
422             tvc->uncred = acred;
423             tvc->states |= CUnlinked;
424         } else {
425             osi_FreeSmallSpace(unlname);
426         }
427         if (tdc)
428             afs_PutDCache(tdc);
429         afs_PutVCache(tvc);
430     } else {
431         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
432     }
433     afs_PutFakeStat(&fakestate);
434 #ifndef AFS_DARWIN80_ENV
435     /* we can't track by thread, it's not exported in the KPI; only do
436        this on !macos */
437     osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
438 #endif
439     return code;
440 }
441
442
443 /* afs_remunlink -- This tries to delete the file at the server after it has
444  *     been renamed when unlinked locally but now has been finally released.
445  *
446  * CAUTION -- may be called with avc unheld. */
447
448 int
449 afs_remunlink(register struct vcache *avc, register int doit)
450 {
451     struct AFS_UCRED *cred;
452     char *unlname;
453     struct vcache *adp;
454     struct vrequest treq;
455     struct VenusFid dirFid;
456     register struct dcache *tdc;
457     afs_int32 code = 0;
458
459     if (NBObtainWriteLock(&avc->lock, 423))
460         return 0;
461 #if defined(AFS_DARWIN80_ENV)
462     if (vnode_get(AFSTOV(avc))) {
463         ReleaseWriteLock(&avc->lock);
464         return 0;
465     }
466 #endif
467
468     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
469         if ((code = afs_InitReq(&treq, avc->uncred))) {
470             ReleaseWriteLock(&avc->lock);
471         } else {
472             /* Must bump the refCount because GetVCache may block.
473              * Also clear mvid so no other thread comes here if we block.
474              */
475             unlname = (char *)avc->mvid;
476             avc->mvid = NULL;
477             cred = avc->uncred;
478             avc->uncred = NULL;
479
480 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
481             VREF(AFSTOV(avc));
482 #else
483             VN_HOLD(AFSTOV(avc));
484 #endif
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         }
515     } else {
516 #if defined(AFS_DARWIN80_ENV)
517         vnode_put(AFSTOV(avc));
518 #endif
519         ReleaseWriteLock(&avc->lock);
520     }
521
522     return code;
523 }