2 * Copyright 2000, International Business Machines Corporation and others.
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
18 #include <afsconfig.h>
19 #include "afs/param.h"
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"
31 FetchWholeEnchilada(struct vcache *avc, struct vrequest *areq)
35 afs_size_t pos, offset, len;
37 AFS_STATCNT(FetchWholeEnchilada);
38 if ((avc->f.states & CStatd) == 0)
39 return; /* don't know size */
40 for (nextChunk = 0; nextChunk < 1024; nextChunk++) { /* sanity check on N chunks */
41 pos = AFS_CHUNKTOBASE(nextChunk);
42 if (pos >= avc->f.m.Length)
43 return; /* all done */
44 tdc = afs_GetDCache(avc, pos, areq, &offset, &len, 0);
52 afsremove(struct vcache *adp, struct dcache *tdc,
53 struct vcache *tvc, char *aname, afs_ucred_t *acred,
54 struct vrequest *treqp)
58 struct AFSFetchStatus OutDirStatus;
59 struct AFSVolSync tsync;
60 struct rx_connection *rxconn;
62 if (!AFS_IS_DISCONNECTED) {
64 tc = afs_Conn(&adp->f.fid, treqp, SHARED_LOCK, &rxconn);
66 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEFILE);
69 RXAFS_RemoveFile(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
70 aname, &OutDirStatus, &tsync);
76 (tc, rxconn, code, &adp->f.fid, treqp, AFS_STATS_FS_RPCIDX_REMOVEFILE,
80 osi_dnlc_remove(adp, aname, tvc);
84 ReleaseSharedLock(&tdc->lock);
94 ReleaseWriteLock(&adp->lock);
95 code = afs_CheckCode(code, treqp, 21);
99 UpgradeSToWLock(&tdc->lock, 637);
100 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
101 /* we can do it locally */
102 code = afs_dir_Delete(tdc, aname);
104 ZapDCE(tdc); /* surprise error -- invalid value */
109 ReleaseWriteLock(&tdc->lock);
110 afs_PutDCache(tdc); /* drop ref count */
112 ReleaseWriteLock(&adp->lock);
114 /* now, get vnode for unlinked dude, and see if we should force it
115 * from cache. adp is now the deleted files vnode. Note that we
116 * call FindVCache instead of GetVCache since if the file's really
117 * gone, we won't be able to fetch the status info anyway. */
120 afs_MarinerLog("store$Removing", tvc);
121 ObtainWriteLock(&tvc->lock, 141);
122 /* note that callback will be broken on the deleted file if there are
123 * still >0 links left to it, so we'll get the stat right */
124 tvc->f.m.LinkCount--;
125 tvc->f.states &= ~CUnique; /* For the dfs xlator */
126 if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
127 if (!AFS_NFSXLATORREQ(acred))
128 afs_TryToSmush(tvc, acred, 0);
130 ReleaseWriteLock(&tvc->lock);
139 char *name, *sp, *p = ".__afs";
140 afs_int32 rd = afs_random() & 0xffff;
142 sp = name = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
146 *sp++ = "0123456789ABCDEF"[rd & 0x0f];
153 /* these variables appear to exist for debugging purposes */
154 struct vcache *Tadp1, *Ttvc;
159 /* Note that we don't set CDirty here, this is OK because the unlink
160 * RPC is called synchronously */
162 afs_remove(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
164 struct vrequest *treq = NULL;
166 struct VenusFid unlinkFid;
169 afs_size_t offset, len;
170 struct afs_fakestat_state fakestate;
173 AFS_STATCNT(afs_remove);
174 afs_Trace2(afs_iclSetp, CM_TRACE_REMOVE, ICL_TYPE_POINTER, adp,
175 ICL_TYPE_STRING, aname);
178 if ((code = afs_CreateReq(&treq, acred))) {
182 afs_InitFakeStat(&fakestate);
184 code = afs_EvalFakeStat(&adp, &fakestate, treq);
188 /* Check if this is dynroot */
189 if (afs_IsDynroot(adp)) {
190 code = afs_DynrootVOPRemove(adp, acred, aname);
193 if (afs_IsDynrootMount(adp)) {
198 if (strlen(aname) > AFSNAMEMAX) {
203 code = afs_VerifyVCache(adp, treq);
206 code = afs_CheckCode(code, treq, 23);
210 /** If the volume is read-only, return error without making an RPC to the
213 if (adp->f.states & CRO) {
218 /* If we're running disconnected without logging, go no further... */
219 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
224 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1); /* test for error below */
225 ObtainWriteLock(&adp->lock, 142);
227 ObtainSharedLock(&tdc->lock, 638);
230 * Make sure that the data in the cache is current. We may have
231 * received a callback while we were waiting for the write lock.
233 if (!(adp->f.states & CStatd)
234 || (tdc && !afs_IsDCacheFresh(tdc, adp))) {
235 ReleaseWriteLock(&adp->lock);
237 ReleaseSharedLock(&tdc->lock);
243 unlinkFid.Fid.Vnode = 0;
245 tvc = osi_dnlc_lookup(adp, aname, WRITE_LOCK);
247 /* This should not be necessary since afs_lookup() has already
252 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
254 unlinkFid.Cell = adp->f.fid.Cell;
255 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
256 if (unlinkFid.Fid.Unique == 0) {
258 afs_LookupVCache(&unlinkFid, treq, adp,
261 ObtainReadLock(&afs_xvcache);
262 tvc = afs_FindVCache(&unlinkFid, 0, DO_STATS);
263 ReleaseReadLock(&afs_xvcache);
268 if (AFS_IS_DISCON_RW) {
269 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
270 /* Make shadow copy of parent dir. */
271 afs_MakeShadowDir(adp, tdc);
274 /* Can't hold a dcache lock whilst we're getting a vcache one */
276 ReleaseSharedLock(&tdc->lock);
278 /* XXX - We're holding adp->lock still, and we've got no
279 * guarantee about whether the ordering matches the lock hierarchy */
280 ObtainWriteLock(&tvc->lock, 713);
282 /* If we were locally created, then we don't need to do very
283 * much beyond ensuring that we don't exist anymore */
284 if (tvc->f.ddirty_flags & VDisconCreate) {
285 afs_DisconRemoveDirty(tvc);
287 /* Add removed file vcache to dirty list. */
288 afs_DisconAddDirty(tvc, VDisconRemove, 1);
290 adp->f.m.LinkCount--;
291 ReleaseWriteLock(&tvc->lock);
293 ObtainSharedLock(&tdc->lock, 714);
296 if (tvc && osi_Active(tvc)) {
297 /* about to delete whole file, prefetch it first */
298 ReleaseWriteLock(&adp->lock);
300 ReleaseSharedLock(&tdc->lock);
301 ObtainWriteLock(&tvc->lock, 143);
302 FetchWholeEnchilada(tvc, treq);
303 ReleaseWriteLock(&tvc->lock);
304 ObtainWriteLock(&adp->lock, 144);
305 /* Technically I don't think we need this back, but let's hold it
306 anyway; The "got" reference should actually be sufficient. */
308 ObtainSharedLock(&tdc->lock, 640);
311 osi_dnlc_remove(adp, aname, tvc);
314 #ifndef AFS_DARWIN80_ENV
315 Tadpr = VREFCOUNT(adp);
320 #ifndef AFS_DARWIN80_ENV
322 Ttvcr = VREFCOUNT(tvc);
325 if (tvc && VREFCOUNT_GT(tvc, 2) && tvc->opens > 0
326 && !(tvc->f.states & CUnlinked)) {
328 if (tvc && VREFCOUNT_GT(tvc, 1) && tvc->opens > 0
329 && !(tvc->f.states & CUnlinked)) {
331 char *unlname = afs_newname();
333 ReleaseWriteLock(&adp->lock);
335 ReleaseSharedLock(&tdc->lock);
336 code = afsrename(adp, aname, adp, unlname, acred, treq);
339 void *oldmvid = NULL;
340 if (tvc->mvid.silly_name)
341 oldmvid = tvc->mvid.silly_name;
342 tvc->mvid.silly_name = unlname;
344 osi_FreeSmallSpace(oldmvid);
350 tvc->f.states |= CUnlinked;
351 /* if rename succeeded, remove should not */
352 ObtainWriteLock(&tvc->lock, 715);
353 if (tvc->f.ddirty_flags & VDisconRemove) {
354 tvc->f.ddirty_flags &= ~VDisconRemove;
356 ReleaseWriteLock(&tvc->lock);
358 osi_FreeSmallSpace(unlname);
364 code = afsremove(adp, tdc, tvc, aname, acred, treq);
367 afs_PutFakeStat(&fakestate);
368 #if !defined(AFS_DARWIN80_ENV) && !defined(UKERNEL)
369 /* we can't track by thread, it's not exported in the KPI; only do
371 osi_Assert(!WriteLocked(&adp->lock) || (adp->lock.pid_writer != MyPidxx));
374 afs_DestroyReq(treq);
379 /* afs_remunlink -- This tries to delete the file at the server after it has
380 * been renamed when unlinked locally but now has been finally released.
382 * CAUTION -- may be called with avc unheld. */
385 afs_remunlink(struct vcache *avc, int doit)
390 struct VenusFid dirFid;
394 if (NBObtainWriteLock(&avc->lock, 423))
396 #if defined(AFS_DARWIN80_ENV)
397 if (vnode_get(AFSTOV(avc))) {
398 ReleaseWriteLock(&avc->lock);
403 if (avc->mvid.silly_name && (doit || (avc->f.states & CUnlinkedDel))) {
404 struct vrequest *treq = NULL;
406 if ((code = afs_CreateReq(&treq, avc->uncred))) {
407 ReleaseWriteLock(&avc->lock);
409 /* Must bump the refCount because GetVCache may block.
410 * Also clear mvid so no other thread comes here if we block.
412 unlname = avc->mvid.silly_name;
413 avc->mvid.silly_name = NULL;
417 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
420 osi_Assert(osi_vnhold(avc) == 0);
423 /* We'll only try this once. If it fails, just release the vnode.
424 * Clear after doing hold so that NewVCache doesn't find us yet.
426 avc->f.states &= ~(CUnlinked | CUnlinkedDel);
428 ReleaseWriteLock(&avc->lock);
430 dirFid.Cell = avc->f.fid.Cell;
431 dirFid.Fid.Volume = avc->f.fid.Fid.Volume;
432 dirFid.Fid.Vnode = avc->f.parent.vnode;
433 dirFid.Fid.Unique = avc->f.parent.unique;
434 adp = afs_GetVCache(&dirFid, treq);
437 tdc = afs_FindDCache(adp, (afs_size_t) 0);
438 ObtainWriteLock(&adp->lock, 159);
440 ObtainSharedLock(&tdc->lock, 639);
442 /* afsremove releases the adp & tdc locks, and does vn_rele(avc) */
443 code = afsremove(adp, tdc, avc, unlname, cred, treq);
446 /* we failed - and won't be back to try again. */
449 osi_FreeSmallSpace(unlname);
451 afs_DestroyReq(treq);
454 #if defined(AFS_DARWIN80_ENV)
455 vnode_put(AFSTOV(avc));
457 ReleaseWriteLock(&avc->lock);