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