remove-vnop-denies-unlink-on-mtpt-20040325
[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 (adp->mvstat == 1) {
284 #ifdef  AFS_OSF_ENV
285         afs_PutVCache(adp);
286         afs_PutVCache(tvc);
287 #endif
288         return EISDIR;
289     }
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->f.inode, 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     if (tvc && (VREFCOUNT(tvc) > 1) && tvc->opens > 0
405         && !(tvc->states & CUnlinked)) {
406 #endif
407         char *unlname = newname();
408
409         ReleaseWriteLock(&adp->lock);
410         if (tdc)
411             ReleaseSharedLock(&tdc->lock);
412         code = afsrename(adp, aname, adp, unlname, acred, &treq);
413         Tnam1 = unlname;
414         if (!code) {
415             tvc->mvid = (struct VenusFid *)unlname;
416             crhold(acred);
417             if (tvc->uncred) {
418                 crfree(tvc->uncred);
419             }
420             tvc->uncred = acred;
421             tvc->states |= CUnlinked;
422         } else {
423             osi_FreeSmallSpace(unlname);
424         }
425         if (tdc)
426             afs_PutDCache(tdc);
427         afs_PutVCache(tvc);
428     } else {
429         code = afsremove(adp, tdc, tvc, aname, acred, &treq);
430     }
431 #ifdef  AFS_OSF_ENV
432     afs_PutVCache(adp);
433 #endif /* AFS_OSF_ENV */
434     afs_PutFakeStat(&fakestate);
435     return code;
436 }
437
438
439 /* afs_remunlink -- This tries to delete the file at the server after it has
440  *     been renamed when unlinked locally but now has been finally released.
441  *
442  * CAUTION -- may be called with avc unheld. */
443
444 int
445 afs_remunlink(register struct vcache *avc, register int doit)
446 {
447     struct AFS_UCRED *cred;
448     char *unlname;
449     struct vcache *adp;
450     struct vrequest treq;
451     struct VenusFid dirFid;
452     register struct dcache *tdc;
453     afs_int32 code = 0;
454
455     if (NBObtainWriteLock(&avc->lock, 423))
456         return 0;
457
458     if (avc->mvid && (doit || (avc->states & CUnlinkedDel))) {
459         if ((code = afs_InitReq(&treq, avc->uncred))) {
460             ReleaseWriteLock(&avc->lock);
461         } else {
462             /* Must bump the refCount because GetVCache may block.
463              * Also clear mvid so no other thread comes here if we block.
464              */
465             unlname = (char *)avc->mvid;
466             avc->mvid = NULL;
467             cred = avc->uncred;
468             avc->uncred = NULL;
469
470 #ifdef AFS_DARWIN_ENV
471             /* this is called by vrele (via VOP_INACTIVE) when the refcount
472              * is 0. we can't just call VN_HOLD since vref will panic.
473              * we can't just call osi_vnhold because a later AFS_RELE will call
474              * vrele again, which will try to call VOP_INACTIVE again after
475              * vn_locking the vnode. which would be fine except that our vrele
476              * caller also locked the vnode... So instead, we just gimmick the
477              * refcounts and hope nobody else can touch the file now */
478             osi_Assert(VREFCOUNT(avc) == 0);
479             VREFCOUNT_SET(avc, 1);
480 #endif
481             VN_HOLD(AFSTOV(avc));
482
483             /* We'll only try this once. If it fails, just release the vnode.
484              * Clear after doing hold so that NewVCache doesn't find us yet.
485              */
486             avc->states &= ~(CUnlinked | CUnlinkedDel);
487
488             ReleaseWriteLock(&avc->lock);
489
490             dirFid.Cell = avc->fid.Cell;
491             dirFid.Fid.Volume = avc->fid.Fid.Volume;
492             dirFid.Fid.Vnode = avc->parentVnode;
493             dirFid.Fid.Unique = avc->parentUnique;
494             adp = afs_GetVCache(&dirFid, &treq, NULL, NULL);
495
496             if (adp) {
497                 tdc = afs_FindDCache(adp, (afs_size_t) 0);
498                 ObtainWriteLock(&adp->lock, 159);
499                 if (tdc)
500                     ObtainSharedLock(&tdc->lock, 639);
501
502                 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
503                 code = afsremove(adp, tdc, avc, unlname, cred, &treq);
504                 afs_PutVCache(adp);
505             } else {
506                 /* we failed - and won't be back to try again. */
507                 afs_PutVCache(avc);
508             }
509             osi_FreeSmallSpace(unlname);
510             crfree(cred);
511 #ifdef AFS_DARWIN_ENV
512             osi_Assert(VREFCOUNT(avc) == 1);
513             VREFCOUNT_SET(avc, 0);
514 #endif
515         }
516     } else {
517         ReleaseWriteLock(&avc->lock);
518     }
519
520     return code;
521 }