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
17 #include <afsconfig.h>
18 #include "afs/param.h"
23 #include "afs/sysincludes.h" /* Standard vendor system headers */
24 #include "afsincludes.h" /* Afs-based standard headers */
25 #include "afs/afs_stats.h" /* statistics */
26 #include "afs/afs_cbqueue.h"
27 #include "afs/nfsclient.h"
28 #include "afs/afs_osidnlc.h"
30 extern afs_rwlock_t afs_xcbhash;
32 /* Note that we don't set CDirty here, this is OK because the rename
33 * RPC is called synchronously. */
36 afsrename(struct vcache *aodp, char *aname1, struct vcache *andp,
37 char *aname2, struct AFS_UCRED *acred, struct vrequest *areq)
39 register struct afs_conn *tc;
40 register afs_int32 code = 0;
42 int oneDir, doLocally;
43 afs_size_t offset, len;
44 struct VenusFid unlinkFid, fileFid;
46 struct dcache *tdc1, *tdc2;
47 struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
48 struct AFSVolSync tsync;
50 AFS_STATCNT(afs_rename);
51 afs_Trace4(afs_iclSetp, CM_TRACE_RENAME, ICL_TYPE_POINTER, aodp,
52 ICL_TYPE_STRING, aname1, ICL_TYPE_POINTER, andp,
53 ICL_TYPE_STRING, aname2);
55 if (strlen(aname1) > AFSNAMEMAX || strlen(aname2) > AFSNAMEMAX) {
60 /* verify the latest versions of the stat cache entries */
62 code = afs_VerifyVCache(aodp, areq);
65 code = afs_VerifyVCache(andp, areq);
69 /* lock in appropriate order, after some checks */
70 if (aodp->fid.Cell != andp->fid.Cell
71 || aodp->fid.Fid.Volume != andp->fid.Fid.Volume) {
77 if (andp->fid.Fid.Vnode == aodp->fid.Fid.Vnode) {
78 if (!strcmp(aname1, aname2)) {
79 /* Same directory and same name; this is a noop and just return success
80 * to save cycles and follow posix standards */
86 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
91 ObtainWriteLock(&andp->lock, 147);
92 tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
96 ObtainWriteLock(&tdc1->lock, 643);
99 oneDir = 1; /* only one dude locked */
100 } else if ((andp->states & CRO) || (aodp->states & CRO)) {
103 } else if (andp->fid.Fid.Vnode < aodp->fid.Fid.Vnode) {
104 ObtainWriteLock(&andp->lock, 148); /* lock smaller one first */
105 ObtainWriteLock(&aodp->lock, 149);
106 tdc2 = afs_FindDCache(andp, (afs_size_t) 0);
108 ObtainWriteLock(&tdc2->lock, 644);
109 tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
111 ObtainWriteLock(&tdc1->lock, 645);
115 ObtainWriteLock(&aodp->lock, 150); /* lock smaller one first */
116 ObtainWriteLock(&andp->lock, 557);
117 tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
119 ObtainWriteLock(&tdc1->lock, 646);
122 tdc2 = afs_FindDCache(andp, (afs_size_t) 0);
124 ObtainWriteLock(&tdc2->lock, 647);
127 osi_dnlc_remove(aodp, aname1, 0);
128 osi_dnlc_remove(andp, aname2, 0);
131 * Make sure that the data in the cache is current. We may have
132 * received a callback while we were waiting for the write lock.
135 if (!(aodp->states & CStatd)
136 || !hsame(aodp->m.DataVersion, tdc1->f.versionNo)) {
138 ReleaseWriteLock(&aodp->lock);
141 ReleaseWriteLock(&tdc2->lock);
144 ReleaseWriteLock(&andp->lock);
146 ReleaseWriteLock(&tdc1->lock);
153 code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
156 ReleaseWriteLock(&tdc1->lock);
159 ReleaseWriteLock(&aodp->lock);
162 ReleaseWriteLock(&tdc2->lock);
165 ReleaseWriteLock(&andp->lock);
170 if (!AFS_IS_DISCON_RW) {
173 tc = afs_Conn(&aodp->fid, areq, SHARED_LOCK);
175 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
179 (struct AFSFid *)&aodp->fid.Fid,
181 (struct AFSFid *)&andp->fid.Fid,
192 (tc, code, &andp->fid, areq, AFS_STATS_FS_RPCIDX_RENAME,
196 #if defined(AFS_DISCON_ENV)
199 /* Seek moved file vcache. */
200 fileFid.Cell = aodp->fid.Cell;
201 fileFid.Fid.Volume = aodp->fid.Fid.Volume;
202 ObtainSharedLock(&afs_xvcache, 754);
203 tvc = afs_FindVCache(&fileFid, 0 , 1);
204 ReleaseSharedLock(&afs_xvcache);
207 /* XXX - We're locking this vcache whilst holding dcaches. Ooops */
208 ObtainWriteLock(&tvc->lock, 750);
209 if (!(tvc->ddirty_flags & (VDisconRename|VDisconCreate))) {
210 /* If the vnode was created locally, then we don't care
211 * about recording the rename - we'll do it automatically
212 * on replay. If we've already renamed, we've already stored
213 * the required information about where we came from.
216 if (!aodp->shVnode) {
217 /* Make shadow copy of parent dir only. */
218 afs_MakeShadowDir(aodp, tdc1);
221 afs_DisconAddDirty(tvc,
223 | (oneDir ? VDisconRenameSameDir:0),
226 /* Save old parent dir fid so it will be searchable
229 tvc->oldVnode = aodp->fid.Fid.Vnode;
230 tvc->oldUnique = aodp->fid.Fid.Unique;
233 ReleaseWriteLock(&tvc->lock);
239 } /* if !(AFS_IS_DISCON_RW)*/
240 returnCode = code; /* remember for later */
242 /* Now we try to do things locally. This is really loathsome code. */
243 unlinkFid.Fid.Vnode = 0;
245 /* In any event, we don't really care if the data (tdc2) is not
246 * in the cache; if it isn't, we won't do the update locally. */
247 /* see if version numbers increased properly */
249 if (!AFS_IS_DISCON_RW) {
251 /* number increases by 1 for whole rename operation */
252 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) {
256 /* two separate dirs, each increasing by 1 */
257 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1))
259 if (!afs_LocalHero(andp, tdc2, &OutNewDirStatus, 1))
272 } /* if (!AFS_IS_DISCON_RW) */
274 /* now really do the work */
276 /* first lookup the fid of the dude we're moving */
277 code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
279 /* delete the source */
280 code = afs_dir_Delete(tdc1, aname1);
282 /* first see if target is there */
284 && afs_dir_Lookup(tdc2, aname2,
285 &unlinkFid.Fid) == 0) {
286 /* target already exists, and will be unlinked by server */
287 code = afs_dir_Delete(tdc2, aname2);
290 ObtainWriteLock(&afs_xdcache, 292);
291 code = afs_dir_Create(tdc2, aname2, &fileFid.Fid);
292 ReleaseWriteLock(&afs_xdcache);
305 /* update dir link counts */
306 if (AFS_IS_DISCON_RW) {
311 /* If we're in the same directory, link count doesn't change */
313 aodp->m.LinkCount = OutOldDirStatus.LinkCount;
315 andp->m.LinkCount = OutNewDirStatus.LinkCount;
318 } else { /* operation failed (code != 0) */
320 /* if failed, server might have done something anyway, and
321 * assume that we know about it */
322 ObtainWriteLock(&afs_xcbhash, 498);
323 afs_DequeueCallback(aodp);
324 afs_DequeueCallback(andp);
325 andp->states &= ~CStatd;
326 aodp->states &= ~CStatd;
327 ReleaseWriteLock(&afs_xcbhash);
328 osi_dnlc_purgedp(andp);
329 osi_dnlc_purgedp(aodp);
335 ReleaseWriteLock(&tdc1->lock);
339 if ((!oneDir) && tdc2) {
340 ReleaseWriteLock(&tdc2->lock);
344 ReleaseWriteLock(&aodp->lock);
346 ReleaseWriteLock(&andp->lock);
352 /* now, some more details. if unlinkFid.Fid.Vnode then we should decrement
353 * the link count on this file. Note that if fileFid is a dir, then we don't
354 * have to invalidate its ".." entry, since its DataVersion # should have
355 * changed. However, interface is not good enough to tell us the
356 * *file*'s new DataVersion, so we're stuck. Our hack: delete mark
357 * the data as having an "unknown" version (effectively discarding the ".."
359 if (unlinkFid.Fid.Vnode) {
361 unlinkFid.Fid.Volume = aodp->fid.Fid.Volume;
362 unlinkFid.Cell = aodp->fid.Cell;
364 if (!unlinkFid.Fid.Unique) {
365 tvc = afs_LookupVCache(&unlinkFid, areq, NULL, aodp, aname1);
367 if (!tvc) /* lookup failed or wasn't called */
368 tvc = afs_GetVCache(&unlinkFid, areq, NULL, NULL);
371 #ifdef AFS_BOZONLOCK_ENV
372 afs_BozonLock(&tvc->pvnLock, tvc); /* Since afs_TryToSmush will do a pvn_vptrunc */
374 ObtainWriteLock(&tvc->lock, 151);
376 tvc->states &= ~CUnique; /* For the dfs xlator */
377 if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
378 /* if this was last guy (probably) discard from cache.
379 * We have to be careful to not get rid of the stat
380 * information, since otherwise operations will start
381 * failing even if the file was still open (or
382 * otherwise active), and the server no longer has the
383 * info. If the file still has valid links, we'll get
384 * a break-callback msg from the server, so it doesn't
385 * matter that we don't discard the status info */
386 if (!AFS_NFSXLATORREQ(acred))
387 afs_TryToSmush(tvc, acred, 0);
389 ReleaseWriteLock(&tvc->lock);
390 #ifdef AFS_BOZONLOCK_ENV
391 afs_BozonUnlock(&tvc->pvnLock, tvc);
397 /* now handle ".." invalidation */
399 fileFid.Fid.Volume = aodp->fid.Fid.Volume;
400 fileFid.Cell = aodp->fid.Cell;
401 if (!fileFid.Fid.Unique)
402 tvc = afs_LookupVCache(&fileFid, areq, NULL, andp, aname2);
404 tvc = afs_GetVCache(&fileFid, areq, NULL, (struct vcache *)0);
405 if (tvc && (vType(tvc) == VDIR)) {
406 ObtainWriteLock(&tvc->lock, 152);
407 tdc1 = afs_FindDCache(tvc, (afs_size_t) 0);
409 if (AFS_IS_DISCON_RW) {
410 #if defined(AFS_DISCON_ENV)
411 /* If disconnected, we need to fix (not discard) the "..".*/
412 afs_dir_ChangeFid(tdc1,
414 &aodp->fid.Fid.Vnode,
415 &andp->fid.Fid.Vnode);
418 ObtainWriteLock(&tdc1->lock, 648);
419 ZapDCE(tdc1); /* mark as unknown */
421 ReleaseWriteLock(&tdc1->lock);
422 afs_PutDCache(tdc1); /* put it back */
425 osi_dnlc_remove(tvc, "..", 0);
426 ReleaseWriteLock(&tvc->lock);
428 } else if (AFS_IS_DISCON_RW && tvc && (vType(tvc) == VREG)) {
429 tvc->parentVnode = andp->fid.Fid.Vnode;
430 tvc->parentUnique = andp->fid.Fid.Unique;
432 /* True we shouldn't come here since tvc SHOULD be a dir, but we
433 * 'syntactically' need to unless we change the 'if' above...
444 #if defined(AFS_SGI_ENV)
445 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct pathname *npnp, struct AFS_UCRED *acred)
447 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct AFS_UCRED *acred)
450 register afs_int32 code;
451 struct afs_fakestat_state ofakestate;
452 struct afs_fakestat_state nfakestate;
453 struct vrequest treq;
454 OSI_VC_CONVERT(aodp);
456 code = afs_InitReq(&treq, acred);
459 afs_InitFakeStat(&ofakestate);
460 afs_InitFakeStat(&nfakestate);
464 code = afs_EvalFakeStat(&aodp, &ofakestate, &treq);
467 code = afs_EvalFakeStat(&andp, &nfakestate, &treq);
470 code = afsrename(aodp, aname1, andp, aname2, acred, &treq);
472 afs_PutFakeStat(&ofakestate);
473 afs_PutFakeStat(&nfakestate);
477 code = afs_CheckCode(code, &treq, 25);