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