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"
21 #include "afs/sysincludes.h" /* Standard vendor system headers */
22 #include "afsincludes.h" /* Afs-based standard headers */
23 #include "afs/afs_stats.h" /* statistics */
24 #include "afs/afs_cbqueue.h"
25 #include "afs/nfsclient.h"
26 #include "afs/afs_osidnlc.h"
28 extern afs_rwlock_t afs_xcbhash;
30 /* Note that we don't set CDirty here, this is OK because the rename
31 * RPC is called synchronously. */
34 afsrename(struct vcache *aodp, char *aname1, struct vcache *andp,
35 char *aname2, afs_ucred_t *acred, struct vrequest *areq)
40 int oneDir, doLocally;
41 afs_size_t offset, len;
42 struct VenusFid unlinkFid, fileFid;
44 struct dcache *tdc1, *tdc2;
45 struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
46 struct AFSVolSync tsync;
47 struct rx_connection *rxconn;
49 AFS_STATCNT(afs_rename);
50 afs_Trace4(afs_iclSetp, CM_TRACE_RENAME, ICL_TYPE_POINTER, aodp,
51 ICL_TYPE_STRING, aname1, ICL_TYPE_POINTER, andp,
52 ICL_TYPE_STRING, aname2);
54 if (strlen(aname1) > AFSNAMEMAX || strlen(aname2) > AFSNAMEMAX) {
59 /* verify the latest versions of the stat cache entries */
61 code = afs_VerifyVCache(aodp, areq);
64 code = afs_VerifyVCache(andp, areq);
68 /* lock in appropriate order, after some checks */
69 if (aodp->f.fid.Cell != andp->f.fid.Cell
70 || aodp->f.fid.Fid.Volume != andp->f.fid.Fid.Volume) {
76 if (andp->f.fid.Fid.Vnode == aodp->f.fid.Fid.Vnode) {
77 if (!strcmp(aname1, aname2)) {
78 /* Same directory and same name; this is a noop and just return success
79 * to save cycles and follow posix standards */
85 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
90 ObtainWriteLock(&andp->lock, 147);
91 tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
95 ObtainWriteLock(&tdc1->lock, 643);
98 oneDir = 1; /* only one dude locked */
99 } else if ((andp->f.states & CRO) || (aodp->f.states & CRO)) {
102 } else if (andp->f.fid.Fid.Vnode < aodp->f.fid.Fid.Vnode) {
103 ObtainWriteLock(&andp->lock, 148); /* lock smaller one first */
104 ObtainWriteLock(&aodp->lock, 149);
105 tdc2 = afs_FindDCache(andp, (afs_size_t) 0);
107 ObtainWriteLock(&tdc2->lock, 644);
108 tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
110 ObtainWriteLock(&tdc1->lock, 645);
114 ObtainWriteLock(&aodp->lock, 150); /* lock smaller one first */
115 ObtainWriteLock(&andp->lock, 557);
116 tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
118 ObtainWriteLock(&tdc1->lock, 646);
121 tdc2 = afs_FindDCache(andp, (afs_size_t) 0);
123 ObtainWriteLock(&tdc2->lock, 647);
126 osi_dnlc_remove(aodp, aname1, 0);
127 osi_dnlc_remove(andp, aname2, 0);
130 * Make sure that the data in the cache is current. We may have
131 * received a callback while we were waiting for the write lock.
134 if (!(aodp->f.states & CStatd)
135 || !hsame(aodp->f.m.DataVersion, tdc1->f.versionNo)) {
137 ReleaseWriteLock(&aodp->lock);
140 ReleaseWriteLock(&tdc2->lock);
143 ReleaseWriteLock(&andp->lock);
145 ReleaseWriteLock(&tdc1->lock);
152 code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
155 ReleaseWriteLock(&tdc1->lock);
158 ReleaseWriteLock(&aodp->lock);
161 ReleaseWriteLock(&tdc2->lock);
164 ReleaseWriteLock(&andp->lock);
169 if (!AFS_IS_DISCON_RW) {
172 tc = afs_Conn(&aodp->f.fid, areq, SHARED_LOCK, &rxconn);
174 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
178 (struct AFSFid *)&aodp->f.fid.Fid,
180 (struct AFSFid *)&andp->f.fid.Fid,
191 (tc, rxconn, code, &andp->f.fid, areq, AFS_STATS_FS_RPCIDX_RENAME,
197 /* Seek moved file vcache. */
198 fileFid.Cell = aodp->f.fid.Cell;
199 fileFid.Fid.Volume = aodp->f.fid.Fid.Volume;
200 ObtainSharedLock(&afs_xvcache, 754);
201 tvc = afs_FindVCache(&fileFid, 0 , 1);
202 ReleaseSharedLock(&afs_xvcache);
205 /* XXX - We're locking this vcache whilst holding dcaches. Ooops */
206 ObtainWriteLock(&tvc->lock, 750);
207 if (!(tvc->f.ddirty_flags & (VDisconRename|VDisconCreate))) {
208 /* If the vnode was created locally, then we don't care
209 * about recording the rename - we'll do it automatically
210 * on replay. If we've already renamed, we've already stored
211 * the required information about where we came from.
214 if (!aodp->f.shadow.vnode) {
215 /* Make shadow copy of parent dir only. */
216 afs_MakeShadowDir(aodp, tdc1);
219 /* Save old parent dir fid so it will be searchable
222 tvc->f.oldParent.vnode = aodp->f.fid.Fid.Vnode;
223 tvc->f.oldParent.unique = aodp->f.fid.Fid.Unique;
225 afs_DisconAddDirty(tvc,
227 | (oneDir ? VDisconRenameSameDir:0),
231 ReleaseWriteLock(&tvc->lock);
236 } /* if !(AFS_IS_DISCON_RW)*/
237 returnCode = code; /* remember for later */
239 /* Now we try to do things locally. This is really loathsome code. */
240 unlinkFid.Fid.Vnode = 0;
242 /* In any event, we don't really care if the data (tdc2) is not
243 * in the cache; if it isn't, we won't do the update locally. */
244 /* see if version numbers increased properly */
246 if (!AFS_IS_DISCON_RW) {
248 /* number increases by 1 for whole rename operation */
249 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) {
253 /* two separate dirs, each increasing by 1 */
254 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1))
256 if (!afs_LocalHero(andp, tdc2, &OutNewDirStatus, 1))
269 } /* if (!AFS_IS_DISCON_RW) */
271 /* now really do the work */
273 /* first lookup the fid of the dude we're moving */
274 code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
276 /* delete the source */
277 code = afs_dir_Delete(tdc1, aname1);
279 /* first see if target is there */
281 && afs_dir_Lookup(tdc2, aname2,
282 &unlinkFid.Fid) == 0) {
283 /* target already exists, and will be unlinked by server */
284 code = afs_dir_Delete(tdc2, aname2);
287 ObtainWriteLock(&afs_xdcache, 292);
288 code = afs_dir_Create(tdc2, aname2, &fileFid.Fid);
289 ReleaseWriteLock(&afs_xdcache);
302 /* update dir link counts */
303 if (AFS_IS_DISCON_RW) {
305 aodp->f.m.LinkCount--;
306 andp->f.m.LinkCount++;
308 /* If we're in the same directory, link count doesn't change */
310 aodp->f.m.LinkCount = OutOldDirStatus.LinkCount;
312 andp->f.m.LinkCount = OutNewDirStatus.LinkCount;
315 } else { /* operation failed (code != 0) */
317 /* if failed, server might have done something anyway, and
318 * assume that we know about it */
319 ObtainWriteLock(&afs_xcbhash, 498);
320 afs_DequeueCallback(aodp);
321 afs_DequeueCallback(andp);
322 andp->f.states &= ~CStatd;
323 aodp->f.states &= ~CStatd;
324 ReleaseWriteLock(&afs_xcbhash);
325 osi_dnlc_purgedp(andp);
326 osi_dnlc_purgedp(aodp);
332 ReleaseWriteLock(&tdc1->lock);
336 if ((!oneDir) && tdc2) {
337 ReleaseWriteLock(&tdc2->lock);
341 ReleaseWriteLock(&aodp->lock);
344 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->f.fid.Fid.Volume;
362 unlinkFid.Cell = aodp->f.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);
375 tvc->f.m.LinkCount--;
376 tvc->f.states &= ~CUnique; /* For the dfs xlator */
377 if (tvc->f.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->f.fid.Fid.Volume;
400 fileFid.Cell = aodp->f.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 disconnected, we need to fix (not discard) the "..".*/
411 afs_dir_ChangeFid(tdc1,
413 &aodp->f.fid.Fid.Vnode,
414 &andp->f.fid.Fid.Vnode);
416 ObtainWriteLock(&tdc1->lock, 648);
417 ZapDCE(tdc1); /* mark as unknown */
419 ReleaseWriteLock(&tdc1->lock);
420 afs_PutDCache(tdc1); /* put it back */
423 osi_dnlc_remove(tvc, "..", 0);
424 ReleaseWriteLock(&tvc->lock);
426 } else if (AFS_IS_DISCON_RW && tvc && (vType(tvc) == VREG)) {
427 /* XXX - Should tvc not get locked here? */
428 tvc->f.parent.vnode = andp->f.fid.Fid.Vnode;
429 tvc->f.parent.unique = andp->f.fid.Fid.Unique;
431 /* True we shouldn't come here since tvc SHOULD be a dir, but we
432 * 'syntactically' need to unless we change the 'if' above...
443 #if defined(AFS_SGI_ENV)
444 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct pathname *npnp, afs_ucred_t *acred)
446 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, afs_ucred_t *acred)
450 struct afs_fakestat_state ofakestate;
451 struct afs_fakestat_state nfakestate;
452 struct vrequest treq;
453 OSI_VC_CONVERT(aodp);
455 code = afs_InitReq(&treq, acred);
458 afs_InitFakeStat(&ofakestate);
459 afs_InitFakeStat(&nfakestate);
463 code = afs_EvalFakeStat(&aodp, &ofakestate, &treq);
466 code = afs_EvalFakeStat(&andp, &nfakestate, &treq);
469 code = afsrename(aodp, aname1, andp, aname2, acred, &treq);
471 afs_PutFakeStat(&ofakestate);
472 afs_PutFakeStat(&nfakestate);
476 code = afs_CheckCode(code, &treq, 25);