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