/*! * Changes a file's parent fid references. */ static int fix_children_fids_hook(void *hdata, char *aname, afs_int32 vnode, afs_int32 unique) { struct VenusFid tfid; struct VenusFid *afid = (struct VenusFid *) hdata; struct vcache *tvc; struct dcache *tdc = NULL; if ((aname[0] == '.') && !aname[1]) return 0; if ((aname[0] == '.') && (aname[1] == '.') && !aname[2]) return 0; tfid.Cell = afid->Cell; tfid.Fid.Volume = afid->Fid.Volume; tfid.Fid.Vnode = vnode; tfid.Fid.Unique = unique; if (!(vnode % 2)) { /* vnode's parity indicates that it's a file. */ /* Get the vcache. */ ObtainSharedLock(&afs_xvcache, 759); tvc = afs_FindVCache(&tfid, 0, 1); ReleaseSharedLock(&afs_xvcache); /* Change the fields. */ if (tvc) { tvc->f.parent.vnode = afid->Fid.Vnode; tvc->f.parent.unique = afid->Fid.Unique; afs_PutVCache(tvc); } } else { /* It's a dir. Fix this dir's .. entry to contain the new fid. */ /* Seek the dir's dcache. */ tdc = afs_FindDCacheByFid(&tfid); if (tdc) { /* Change the .. entry fid. */ afs_dir_ChangeFid(tdc, "..", NULL, &afid->Fid.Vnode); afs_PutDCache(tdc); } } /* if (!(vnode % 2))*/ return 0; }
int afsrename(struct vcache *aodp, char *aname1, struct vcache *andp, char *aname2, struct AFS_UCRED *acred, struct vrequest *areq) { register struct afs_conn *tc; register afs_int32 code = 0; afs_int32 returnCode; int oneDir, doLocally; afs_size_t offset, len; struct VenusFid unlinkFid, fileFid; struct vcache *tvc; struct dcache *tdc1, *tdc2; struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus; struct AFSVolSync tsync; XSTATS_DECLS; AFS_STATCNT(afs_rename); afs_Trace4(afs_iclSetp, CM_TRACE_RENAME, ICL_TYPE_POINTER, aodp, ICL_TYPE_STRING, aname1, ICL_TYPE_POINTER, andp, ICL_TYPE_STRING, aname2); if (strlen(aname1) > AFSNAMEMAX || strlen(aname2) > AFSNAMEMAX) { code = ENAMETOOLONG; goto done; } /* verify the latest versions of the stat cache entries */ tagain: code = afs_VerifyVCache(aodp, areq); if (code) goto done; code = afs_VerifyVCache(andp, areq); if (code) goto done; /* lock in appropriate order, after some checks */ if (aodp->f.fid.Cell != andp->f.fid.Cell || aodp->f.fid.Fid.Volume != andp->f.fid.Fid.Volume) { code = EXDEV; goto done; } oneDir = 0; code = 0; if (andp->f.fid.Fid.Vnode == aodp->f.fid.Fid.Vnode) { if (!strcmp(aname1, aname2)) { /* Same directory and same name; this is a noop and just return success * to save cycles and follow posix standards */ code = 0; goto done; } if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) { code = ENETDOWN; goto done; } ObtainWriteLock(&andp->lock, 147); tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0); if (!tdc1) { code = ENOENT; } else { ObtainWriteLock(&tdc1->lock, 643); } tdc2 = tdc1; oneDir = 1; /* only one dude locked */ } else if ((andp->f.states & CRO) || (aodp->f.states & CRO)) { code = EROFS; goto done; } else if (andp->f.fid.Fid.Vnode < aodp->f.fid.Fid.Vnode) { ObtainWriteLock(&andp->lock, 148); /* lock smaller one first */ ObtainWriteLock(&aodp->lock, 149); tdc2 = afs_FindDCache(andp, (afs_size_t) 0); if (tdc2) ObtainWriteLock(&tdc2->lock, 644); tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0); if (tdc1) ObtainWriteLock(&tdc1->lock, 645); else code = ENOENT; } else { ObtainWriteLock(&aodp->lock, 150); /* lock smaller one first */ ObtainWriteLock(&andp->lock, 557); tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0); if (tdc1) ObtainWriteLock(&tdc1->lock, 646); else code = ENOENT; tdc2 = afs_FindDCache(andp, (afs_size_t) 0); if (tdc2) ObtainWriteLock(&tdc2->lock, 647); } osi_dnlc_remove(aodp, aname1, 0); osi_dnlc_remove(andp, aname2, 0); /* * Make sure that the data in the cache is current. We may have * received a callback while we were waiting for the write lock. */ if (tdc1) { if (!(aodp->f.states & CStatd) || !hsame(aodp->f.m.DataVersion, tdc1->f.versionNo)) { ReleaseWriteLock(&aodp->lock); if (!oneDir) { if (tdc2) { ReleaseWriteLock(&tdc2->lock); afs_PutDCache(tdc2); } ReleaseWriteLock(&andp->lock); } ReleaseWriteLock(&tdc1->lock); afs_PutDCache(tdc1); goto tagain; } } if (code == 0) code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid); if (code) { if (tdc1) { ReleaseWriteLock(&tdc1->lock); afs_PutDCache(tdc1); } ReleaseWriteLock(&aodp->lock); if (!oneDir) { if (tdc2) { ReleaseWriteLock(&tdc2->lock); afs_PutDCache(tdc2); } ReleaseWriteLock(&andp->lock); } goto done; } if (!AFS_IS_DISCON_RW) { /* Connected. */ do { tc = afs_Conn(&aodp->f.fid, areq, SHARED_LOCK); if (tc) { XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME); RX_AFS_GUNLOCK(); code = RXAFS_Rename(tc->id, (struct AFSFid *)&aodp->f.fid.Fid, aname1, (struct AFSFid *)&andp->f.fid.Fid, aname2, &OutOldDirStatus, &OutNewDirStatus, &tsync); RX_AFS_GLOCK(); XSTATS_END_TIME; } else code = -1; } while (afs_Analyze (tc, code, &andp->f.fid, areq, AFS_STATS_FS_RPCIDX_RENAME, SHARED_LOCK, NULL)); } else { #if defined(AFS_DISCON_ENV) /* Disconnected. */ /* Seek moved file vcache. */ fileFid.Cell = aodp->f.fid.Cell; fileFid.Fid.Volume = aodp->f.fid.Fid.Volume; ObtainSharedLock(&afs_xvcache, 754); tvc = afs_FindVCache(&fileFid, 0 , 1); ReleaseSharedLock(&afs_xvcache); if (tvc) { /* XXX - We're locking this vcache whilst holding dcaches. Ooops */ ObtainWriteLock(&tvc->lock, 750); if (!(tvc->f.ddirty_flags & (VDisconRename|VDisconCreate))) { /* If the vnode was created locally, then we don't care * about recording the rename - we'll do it automatically * on replay. If we've already renamed, we've already stored * the required information about where we came from. */ if (!aodp->f.shadow.vnode) { /* Make shadow copy of parent dir only. */ afs_MakeShadowDir(aodp, tdc1); } /* Save old parent dir fid so it will be searchable * in the shadow dir. */ tvc->f.oldParent.vnode = aodp->f.fid.Fid.Vnode; tvc->f.oldParent.unique = aodp->f.fid.Fid.Unique; afs_DisconAddDirty(tvc, VDisconRename | (oneDir ? VDisconRenameSameDir:0), 1); } ReleaseWriteLock(&tvc->lock); afs_PutVCache(tvc); } else { code = ENOENT; } /* if (tvc) */ #endif } /* if !(AFS_IS_DISCON_RW)*/ returnCode = code; /* remember for later */ /* Now we try to do things locally. This is really loathsome code. */ unlinkFid.Fid.Vnode = 0; if (code == 0) { /* In any event, we don't really care if the data (tdc2) is not * in the cache; if it isn't, we won't do the update locally. */ /* see if version numbers increased properly */ doLocally = 1; if (!AFS_IS_DISCON_RW) { if (oneDir) { /* number increases by 1 for whole rename operation */ if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) { doLocally = 0; } } else { /* two separate dirs, each increasing by 1 */ if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) doLocally = 0; if (!afs_LocalHero(andp, tdc2, &OutNewDirStatus, 1)) doLocally = 0; if (!doLocally) { if (tdc1) { ZapDCE(tdc1); DZap(tdc1); } if (tdc2) { ZapDCE(tdc2); DZap(tdc2); } } } } /* if (!AFS_IS_DISCON_RW) */ /* now really do the work */ if (doLocally) { /* first lookup the fid of the dude we're moving */ code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid); if (code == 0) { /* delete the source */ code = afs_dir_Delete(tdc1, aname1); } /* first see if target is there */ if (code == 0 && afs_dir_Lookup(tdc2, aname2, &unlinkFid.Fid) == 0) { /* target already exists, and will be unlinked by server */ code = afs_dir_Delete(tdc2, aname2); } if (code == 0) { ObtainWriteLock(&afs_xdcache, 292); code = afs_dir_Create(tdc2, aname2, &fileFid.Fid); ReleaseWriteLock(&afs_xdcache); } if (code != 0) { ZapDCE(tdc1); DZap(tdc1); if (!oneDir) { ZapDCE(tdc2); DZap(tdc2); } } } /* update dir link counts */ if (AFS_IS_DISCON_RW) { if (!oneDir) { aodp->f.m.LinkCount--; andp->f.m.LinkCount++; } /* If we're in the same directory, link count doesn't change */ } else { aodp->f.m.LinkCount = OutOldDirStatus.LinkCount; if (!oneDir) andp->f.m.LinkCount = OutNewDirStatus.LinkCount; } } else { /* operation failed (code != 0) */ if (code < 0) { /* if failed, server might have done something anyway, and * assume that we know about it */ ObtainWriteLock(&afs_xcbhash, 498); afs_DequeueCallback(aodp); afs_DequeueCallback(andp); andp->f.states &= ~CStatd; aodp->f.states &= ~CStatd; ReleaseWriteLock(&afs_xcbhash); osi_dnlc_purgedp(andp); osi_dnlc_purgedp(aodp); } } /* release locks */ if (tdc1) { ReleaseWriteLock(&tdc1->lock); afs_PutDCache(tdc1); } if ((!oneDir) && tdc2) { ReleaseWriteLock(&tdc2->lock); afs_PutDCache(tdc2); } ReleaseWriteLock(&aodp->lock); if (!oneDir) { ReleaseWriteLock(&andp->lock); } if (returnCode) { code = returnCode; goto done; } /* now, some more details. if unlinkFid.Fid.Vnode then we should decrement * the link count on this file. Note that if fileFid is a dir, then we don't * have to invalidate its ".." entry, since its DataVersion # should have * changed. However, interface is not good enough to tell us the * *file*'s new DataVersion, so we're stuck. Our hack: delete mark * the data as having an "unknown" version (effectively discarding the ".." * entry */ if (unlinkFid.Fid.Vnode) { unlinkFid.Fid.Volume = aodp->f.fid.Fid.Volume; unlinkFid.Cell = aodp->f.fid.Cell; tvc = NULL; if (!unlinkFid.Fid.Unique) { tvc = afs_LookupVCache(&unlinkFid, areq, NULL, aodp, aname1); } if (!tvc) /* lookup failed or wasn't called */ tvc = afs_GetVCache(&unlinkFid, areq, NULL, NULL); if (tvc) { #ifdef AFS_BOZONLOCK_ENV afs_BozonLock(&tvc->pvnLock, tvc); /* Since afs_TryToSmush will do a pvn_vptrunc */ #endif ObtainWriteLock(&tvc->lock, 151); tvc->f.m.LinkCount--; tvc->f.states &= ~CUnique; /* For the dfs xlator */ if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) { /* if this was last guy (probably) discard from cache. * We have to be careful to not get rid of the stat * information, since otherwise operations will start * failing even if the file was still open (or * otherwise active), and the server no longer has the * info. If the file still has valid links, we'll get * a break-callback msg from the server, so it doesn't * matter that we don't discard the status info */ if (!AFS_NFSXLATORREQ(acred)) afs_TryToSmush(tvc, acred, 0); } ReleaseWriteLock(&tvc->lock); #ifdef AFS_BOZONLOCK_ENV afs_BozonUnlock(&tvc->pvnLock, tvc); #endif afs_PutVCache(tvc); } } /* now handle ".." invalidation */ if (!oneDir) { fileFid.Fid.Volume = aodp->f.fid.Fid.Volume; fileFid.Cell = aodp->f.fid.Cell; if (!fileFid.Fid.Unique) tvc = afs_LookupVCache(&fileFid, areq, NULL, andp, aname2); else tvc = afs_GetVCache(&fileFid, areq, NULL, (struct vcache *)0); if (tvc && (vType(tvc) == VDIR)) { ObtainWriteLock(&tvc->lock, 152); tdc1 = afs_FindDCache(tvc, (afs_size_t) 0); if (tdc1) { if (AFS_IS_DISCON_RW) { #if defined(AFS_DISCON_ENV) /* If disconnected, we need to fix (not discard) the "..".*/ afs_dir_ChangeFid(tdc1, "..", &aodp->f.fid.Fid.Vnode, &andp->f.fid.Fid.Vnode); #endif } else { ObtainWriteLock(&tdc1->lock, 648); ZapDCE(tdc1); /* mark as unknown */ DZap(tdc1); ReleaseWriteLock(&tdc1->lock); afs_PutDCache(tdc1); /* put it back */ } } osi_dnlc_remove(tvc, "..", 0); ReleaseWriteLock(&tvc->lock); afs_PutVCache(tvc); } else if (AFS_IS_DISCON_RW && tvc && (vType(tvc) == VREG)) { /* XXX - Should tvc not get locked here? */ tvc->f.parent.vnode = andp->f.fid.Fid.Vnode; tvc->f.parent.unique = andp->f.fid.Fid.Unique; } else if (tvc) { /* True we shouldn't come here since tvc SHOULD be a dir, but we * 'syntactically' need to unless we change the 'if' above... */ afs_PutVCache(tvc); } } code = returnCode; done: return code; }
/*! * Handles all the reconnection details: * - Get all the details about the vnode: name, fid, and parent dir fid. * - Send data to server. * - Handle errors. * - Reorder vhash and dcaches in their hashes, using the newly acquired fid. */ int afs_ProcessOpCreate(struct vcache *avc, struct vrequest *areq, afs_ucred_t *acred) { char *tname = NULL, *ttargetName = NULL; struct AFSStoreStatus InStatus; struct AFSFetchStatus OutFidStatus, OutDirStatus; struct VenusFid pdir_fid, newFid; struct AFSCallBack CallBack; struct AFSVolSync tsync; struct vcache *tdp = NULL, *tvc = NULL; struct dcache *tdc = NULL; struct afs_conn *tc; struct rx_connection *rxconn; afs_int32 hash, new_hash, index; afs_size_t tlen; int code, op = 0; XSTATS_DECLS; tname = afs_osi_Alloc(AFSNAMEMAX); if (!tname) return ENOMEM; code = afs_GetParentVCache(avc, 0, &pdir_fid, tname, &tdp); if (code) goto end; /* This data may also be in linkData, but then we have to deal with * the joy of terminating NULLs and . and file modes. So just get * it from the dcache where it won't have been fiddled with. */ if (vType(avc) == VLNK) { afs_size_t offset; struct dcache *tdc; struct osi_file *tfile; tdc = afs_GetDCache(avc, 0, areq, &offset, &tlen, 0); if (!tdc) { code = ENOENT; goto end; } if (tlen > 1024) { afs_PutDCache(tdc); code = EFAULT; goto end; } tlen++; /* space for NULL */ ttargetName = afs_osi_Alloc(tlen); if (!ttargetName) { afs_PutDCache(tdc); return ENOMEM; } ObtainReadLock(&tdc->lock); tfile = afs_CFileOpen(&tdc->f.inode); code = afs_CFileRead(tfile, 0, ttargetName, tlen); ttargetName[tlen-1] = '\0'; afs_CFileClose(tfile); ReleaseReadLock(&tdc->lock); afs_PutDCache(tdc); } /* Set status. */ InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP; InStatus.ClientModTime = avc->f.m.Date; InStatus.Owner = avc->f.m.Owner; InStatus.Group = (afs_int32) afs_cr_gid(acred); /* Only care about protection bits. */ InStatus.UnixModeBits = avc->f.m.Mode & 0xffff; do { tc = afs_Conn(&tdp->f.fid, areq, SHARED_LOCK, &rxconn); if (tc) { switch (vType(avc)) { case VREG: /* Make file on server. */ op = AFS_STATS_FS_RPCIDX_CREATEFILE; XSTATS_START_TIME(op); RX_AFS_GUNLOCK(); code = RXAFS_CreateFile(tc->id, (struct AFSFid *)&tdp->f.fid.Fid, tname, &InStatus, (struct AFSFid *) &newFid.Fid, &OutFidStatus, &OutDirStatus, &CallBack, &tsync); RX_AFS_GLOCK(); XSTATS_END_TIME; break; case VDIR: /* Make dir on server. */ op = AFS_STATS_FS_RPCIDX_MAKEDIR; XSTATS_START_TIME(op); RX_AFS_GUNLOCK(); code = RXAFS_MakeDir(rxconn, (struct AFSFid *) &tdp->f.fid.Fid, tname, &InStatus, (struct AFSFid *) &newFid.Fid, &OutFidStatus, &OutDirStatus, &CallBack, &tsync); RX_AFS_GLOCK(); XSTATS_END_TIME; break; case VLNK: /* Make symlink on server. */ op = AFS_STATS_FS_RPCIDX_SYMLINK; XSTATS_START_TIME(op); RX_AFS_GUNLOCK(); code = RXAFS_Symlink(rxconn, (struct AFSFid *) &tdp->f.fid.Fid, tname, ttargetName, &InStatus, (struct AFSFid *) &newFid.Fid, &OutFidStatus, &OutDirStatus, &tsync); RX_AFS_GLOCK(); XSTATS_END_TIME; break; default: op = AFS_STATS_FS_RPCIDX_CREATEFILE; code = 1; break; } } else code = -1; } while (afs_Analyze(tc, rxconn, code, &tdp->f.fid, areq, op, SHARED_LOCK, NULL)); /* TODO: Handle errors. */ if (code) { /* printf("afs_ProcessOpCreate: error while creating vnode on server, code=%d .\n", code); */ goto end; } /* The rpc doesn't set the cell number. */ newFid.Cell = avc->f.fid.Cell; /* * Change the fid in the dir entry. */ /* Seek the dir's dcache. */ tdc = afs_FindDCacheByFid(&tdp->f.fid); if (tdc) { /* And now change the fid in the parent dir entry. */ afs_dir_ChangeFid(tdc, tname, &avc->f.fid.Fid.Vnode, &newFid.Fid.Vnode); afs_PutDCache(tdc); } if (vType(avc) == VDIR) { /* Change fid in the dir for the "." entry. ".." has alredy been * handled by afs_FixChildrenFids when processing the parent dir. */ tdc = afs_FindDCacheByFid(&avc->f.fid); if (tdc) { afs_dir_ChangeFid(tdc, ".", &avc->f.fid.Fid.Vnode, &newFid.Fid.Vnode); if (avc->f.m.LinkCount >= 2) /* For non empty dirs, fix children's parentVnode and * parentUnique reference. */ afs_FixChildrenFids(&avc->f.fid, &newFid); afs_PutDCache(tdc); } } /* Recompute hash chain positions for vnode and dcaches. * Then change to the new FID. */ /* The vcache goes first. */ ObtainWriteLock(&afs_xvcache, 735); /* Old fid hash. */ hash = VCHash(&avc->f.fid); /* New fid hash. */ new_hash = VCHash(&newFid); /* Remove hash from old position. */ /* XXX: not checking array element contents. It shouldn't be empty. * If it oopses, then something else might be wrong. */ if (afs_vhashT[hash] == avc) { /* First in hash chain (might be the only one). */ afs_vhashT[hash] = avc->hnext; } else { /* More elements in hash chain. */ for (tvc = afs_vhashT[hash]; tvc; tvc = tvc->hnext) { if (tvc->hnext == avc) { tvc->hnext = avc->hnext; break; } } } /* if (!afs_vhashT[i]->hnext) */ QRemove(&avc->vhashq); /* Insert hash in new position. */ avc->hnext = afs_vhashT[new_hash]; afs_vhashT[new_hash] = avc; QAdd(&afs_vhashTV[VCHashV(&newFid)], &avc->vhashq); ReleaseWriteLock(&afs_xvcache); /* Do the same thing for all dcaches. */ hash = DVHash(&avc->f.fid); ObtainWriteLock(&afs_xdcache, 743); for (index = afs_dvhashTbl[hash]; index != NULLIDX; index = hash) { hash = afs_dvnextTbl[index]; tdc = afs_GetValidDSlot(index); ReleaseReadLock(&tdc->tlock); if (afs_indexUnique[index] == avc->f.fid.Fid.Unique) { if (!FidCmp(&tdc->f.fid, &avc->f.fid)) { /* Safer but slower. */ afs_HashOutDCache(tdc, 0); /* Put dcache in new positions in the dchash and dvhash. */ new_hash = DCHash(&newFid, tdc->f.chunk); afs_dcnextTbl[tdc->index] = afs_dchashTbl[new_hash]; afs_dchashTbl[new_hash] = tdc->index; new_hash = DVHash(&newFid); afs_dvnextTbl[tdc->index] = afs_dvhashTbl[new_hash]; afs_dvhashTbl[new_hash] = tdc->index; afs_indexUnique[tdc->index] = newFid.Fid.Unique; memcpy(&tdc->f.fid, &newFid, sizeof(struct VenusFid)); } /* if fid match */ } /* if uniquifier match */ if (tdc) afs_PutDCache(tdc); } /* for all dcaches in this hash bucket */ ReleaseWriteLock(&afs_xdcache); /* Now we can set the new fid. */ memcpy(&avc->f.fid, &newFid, sizeof(struct VenusFid)); end: if (tdp) afs_PutVCache(tdp); afs_osi_Free(tname, AFSNAMEMAX); if (ttargetName) afs_osi_Free(ttargetName, tlen); return code; }