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