openbsd-20021011
[openafs.git] / src / afs / VNOPS / afs_vnop_rename.c
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  * afsrename
13  * afs_rename
14  *
15  */
16
17 #include <afsconfig.h>
18 #include "../afs/param.h"
19
20 RCSID("$Header$");
21
22 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include "../afs/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 extern afs_rwlock_t afs_xcbhash;
30
31 /* Note that we don't set CDirty here, this is OK because the rename
32  * RPC is called synchronously. */
33
34 afsrename(aodp, aname1, andp, aname2, acred, areq)
35     struct vcache *aodp, *andp;
36     char *aname1, *aname2;
37     struct AFS_UCRED *acred;
38     struct vrequest *areq;
39 {
40     register struct conn *tc;
41     register afs_int32 code;
42     afs_int32 returnCode;
43     int oneDir, doLocally;
44     afs_size_t offset, len;
45     struct VenusFid unlinkFid, fileFid;
46     struct vcache *tvc;
47     struct dcache *tdc1, *tdc2;
48     struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
49     struct AFSVolSync tsync;
50     XSTATS_DECLS
51
52     AFS_STATCNT(afs_rename);
53     afs_Trace4(afs_iclSetp, CM_TRACE_RENAME, ICL_TYPE_POINTER, aodp, 
54                ICL_TYPE_STRING, aname1, ICL_TYPE_POINTER, andp,
55                ICL_TYPE_STRING, aname2);
56
57     if (strlen(aname1) > AFSNAMEMAX || strlen(aname2) > AFSNAMEMAX) {
58         code = ENAMETOOLONG;
59         goto done;
60     }
61
62     /* verify the latest versions of the stat cache entries */
63 tagain:
64     code = afs_VerifyVCache(aodp, areq);
65     if (code) goto done;
66     code = afs_VerifyVCache(andp, areq);
67     if (code) goto done;
68     
69     /* lock in appropriate order, after some checks */
70     if (aodp->fid.Cell != andp->fid.Cell || aodp->fid.Fid.Volume != andp->fid.Fid.Volume) {
71         code = EXDEV;
72         goto done;
73     }
74     oneDir = 0;
75     code = 0;
76     if (andp->fid.Fid.Vnode == aodp->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 */
80
81             code = 0;
82             goto done;
83         }
84         ObtainWriteLock(&andp->lock,147);
85         tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
86         if (!tdc1) {
87             code = ENOENT;
88         } else {
89             ObtainWriteLock(&tdc1->lock, 643);
90         }
91         tdc2 = tdc1;
92         oneDir = 1;         /* only one dude locked */
93     }
94     else if ((andp->states & CRO) || (aodp->states & CRO)) {
95        code = EROFS;
96        goto done;
97      }
98     else if (andp->fid.Fid.Vnode < aodp->fid.Fid.Vnode) {
99         ObtainWriteLock(&andp->lock,148);       /* lock smaller one first */
100         ObtainWriteLock(&aodp->lock,149);
101         tdc2 = afs_FindDCache(andp, (afs_size_t)0);
102         if (tdc2) ObtainWriteLock(&tdc2->lock, 644);
103         tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
104         if (tdc1)
105             ObtainWriteLock(&tdc1->lock, 645);
106         else
107             code = ENOENT;
108     }
109     else {
110         ObtainWriteLock(&aodp->lock,150);       /* lock smaller one first */
111         ObtainWriteLock(&andp->lock,557);
112         tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
113         if (tdc1)
114             ObtainWriteLock(&tdc1->lock, 646);
115         else
116             code = ENOENT;
117         tdc2 = afs_FindDCache(andp, (afs_size_t)0);
118         if (tdc2) ObtainWriteLock(&tdc2->lock, 647);
119     }
120
121     osi_dnlc_remove (aodp, aname1, 0);
122     osi_dnlc_remove (andp, aname2, 0);
123     afs_symhint_inval(aodp); 
124     afs_symhint_inval(andp);
125
126     /*
127      * Make sure that the data in the cache is current. We may have
128      * received a callback while we were waiting for the write lock.
129      */
130     if (tdc1) {
131         if (!(aodp->states & CStatd)
132             || !hsame(aodp->m.DataVersion, tdc1->f.versionNo)) {
133
134             ReleaseWriteLock(&aodp->lock);
135             if (!oneDir) {
136                 if (tdc2) {
137                     ReleaseWriteLock(&tdc2->lock);
138                     afs_PutDCache(tdc2);
139                 }
140                 ReleaseWriteLock(&andp->lock);
141             }
142             ReleaseWriteLock(&tdc1->lock);
143             afs_PutDCache(tdc1);
144             goto tagain;
145         }
146     }
147
148     if (code == 0)
149         code = afs_dir_Lookup(&tdc1->f.inode, aname1, &fileFid.Fid);
150     if (code) {
151         if (tdc1) {
152             ReleaseWriteLock(&tdc1->lock);
153             afs_PutDCache(tdc1);
154         }
155         ReleaseWriteLock(&aodp->lock);
156         if (!oneDir) {
157             if (tdc2) {
158                 ReleaseWriteLock(&tdc2->lock);
159                 afs_PutDCache(tdc2);
160             }
161             ReleaseWriteLock(&andp->lock);
162         }
163         goto done;
164     }
165
166     /* locks are now set, proceed to do the real work */
167     do {
168         tc = afs_Conn(&aodp->fid, areq, SHARED_LOCK);
169         if (tc) {
170           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
171           RX_AFS_GUNLOCK();
172           code = RXAFS_Rename(tc->id, (struct AFSFid *) &aodp->fid.Fid, aname1,
173                                 (struct AFSFid *) &andp->fid.Fid, aname2,
174                                 &OutOldDirStatus, &OutNewDirStatus, &tsync);
175           RX_AFS_GLOCK();
176           XSTATS_END_TIME;
177         } else code = -1;
178
179     } while
180         (afs_Analyze(tc, code, &andp->fid, areq,
181                      AFS_STATS_FS_RPCIDX_RENAME, SHARED_LOCK, NULL));
182
183     returnCode = code;      /* remember for later */
184     
185     /* Now we try to do things locally.  This is really loathsome code. */
186     unlinkFid.Fid.Vnode = 0;
187     if (code == 0) {
188         /*  In any event, we don't really care if the data (tdc2) is not
189             in the cache; if it isn't, we won't do the update locally.  */
190         /* see if version numbers increased properly */
191         doLocally = 1;
192         if (oneDir) {
193             /* number increases by 1 for whole rename operation */
194             if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) {
195                 doLocally = 0;
196             }
197         }
198         else {
199             /* two separate dirs, each increasing by 1 */
200             if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1))
201                 doLocally = 0;
202             if (!afs_LocalHero(andp, tdc2, &OutNewDirStatus, 1))
203                 doLocally = 0;
204             if (!doLocally) {
205                 if (tdc1) {
206                     ZapDCE(tdc1);
207                     DZap(&tdc1->f.inode);
208                 }
209                 if (tdc2) {
210                     ZapDCE(tdc2);
211                     DZap(&tdc2->f.inode);
212                 }
213             }
214         }
215         /* now really do the work */
216         if (doLocally) {
217             /* first lookup the fid of the dude we're moving */
218             code = afs_dir_Lookup(&tdc1->f.inode, aname1, &fileFid.Fid);
219             if (code == 0) {
220                 /* delete the source */
221                 code = afs_dir_Delete(&tdc1->f.inode, aname1);
222             }
223             /* first see if target is there */
224             if (code == 0 &&
225                 afs_dir_Lookup(&tdc2->f.inode, aname2, &unlinkFid.Fid) == 0) {
226                 /* target already exists, and will be unlinked by server */
227                 code = afs_dir_Delete(&tdc2->f.inode, aname2);
228             }
229             if (code == 0) {
230                 code = afs_dir_Create(&tdc2->f.inode, aname2, &fileFid.Fid);
231             }
232             if (code != 0) {
233                 ZapDCE(tdc1);
234                 DZap(&tdc1->f.inode);
235                 if (!oneDir) {
236                     ZapDCE(tdc2);
237                     DZap(&tdc2->f.inode);
238                 }
239             }
240         }
241
242         /* update dir link counts */
243         aodp->m.LinkCount = OutOldDirStatus.LinkCount;
244         if (!oneDir)
245             andp->m.LinkCount = OutNewDirStatus.LinkCount;
246
247     }
248     else {      /* operation failed (code != 0) */
249         if (code < 0) { 
250             /* if failed, server might have done something anyway, and 
251              * assume that we know about it */
252           ObtainWriteLock(&afs_xcbhash, 498);
253           afs_DequeueCallback(aodp);
254           afs_DequeueCallback(andp);
255           andp->states &= ~CStatd;
256           aodp->states &= ~CStatd;
257           ReleaseWriteLock(&afs_xcbhash);
258           osi_dnlc_purgedp(andp);
259           osi_dnlc_purgedp(aodp);
260         }
261     }
262
263     /* release locks */
264     if (tdc1) {
265         ReleaseWriteLock(&tdc1->lock);
266         afs_PutDCache(tdc1);
267     }
268
269     if ((!oneDir) && tdc2) {
270         ReleaseWriteLock(&tdc2->lock);
271         afs_PutDCache(tdc2);
272     }
273
274     ReleaseWriteLock(&aodp->lock);
275     if (!oneDir) ReleaseWriteLock(&andp->lock);
276     
277     if (returnCode) {
278         code = returnCode;
279         goto done;
280     }
281
282     /* now, some more details.  if unlinkFid.Fid.Vnode then we should decrement
283         the link count on this file.  Note that if fileFid is a dir, then we don't
284         have to invalidate its ".." entry, since its DataVersion # should have
285         changed. However, interface is not good enough to tell us the
286         *file*'s new DataVersion, so we're stuck.  Our hack: delete mark
287         the data as having an "unknown" version (effectively discarding the ".."
288         entry */
289     if (unlinkFid.Fid.Vnode) {
290         unlinkFid.Fid.Volume = aodp->fid.Fid.Volume;
291         unlinkFid.Cell = aodp->fid.Cell;
292         tvc = NULL;
293         if (!unlinkFid.Fid.Unique) {
294             tvc = afs_LookupVCache(&unlinkFid, areq, NULL, aodp, aname1);
295         }
296         if (!tvc) /* lookup failed or wasn't called */
297            tvc = afs_GetVCache(&unlinkFid, areq, NULL, NULL);
298
299         if (tvc) {
300 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
301             afs_BozonLock(&tvc->pvnLock, tvc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
302 #endif
303             ObtainWriteLock(&tvc->lock,151);
304             tvc->m.LinkCount--;
305             tvc->states &= ~CUnique;            /* For the dfs xlator */
306             if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
307                 /* if this was last guy (probably) discard from cache.
308                  * We have to be careful to not get rid of the stat
309                  * information, since otherwise operations will start
310                  * failing even if the file was still open (or
311                  * otherwise active), and the server no longer has the
312                  * info.  If the file still has valid links, we'll get
313                  * a break-callback msg from the server, so it doesn't
314                  * matter that we don't discard the status info */
315                 if (!AFS_NFSXLATORREQ(acred)) afs_TryToSmush(tvc, acred, 0);
316             }
317             ReleaseWriteLock(&tvc->lock);
318 #if     defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)  || defined(AFS_SUN5_ENV)
319             afs_BozonUnlock(&tvc->pvnLock, tvc);
320 #endif
321             afs_PutVCache(tvc);
322         }
323     }
324
325     /* now handle ".." invalidation */
326     if (!oneDir) {
327         fileFid.Fid.Volume = aodp->fid.Fid.Volume;
328         fileFid.Cell = aodp->fid.Cell;
329         if (!fileFid.Fid.Unique)
330             tvc = afs_LookupVCache(&fileFid, areq, NULL, andp, aname2);
331         else
332             tvc = afs_GetVCache(&fileFid, areq, NULL, (struct vcache*)0);
333         if (tvc && (vType(tvc) == VDIR)) {
334             ObtainWriteLock(&tvc->lock,152);
335             tdc1 = afs_FindDCache(tvc, (afs_size_t)0);
336             if (tdc1) {
337                 ObtainWriteLock(&tdc1->lock, 648);
338                 ZapDCE(tdc1);   /* mark as unknown */
339                 DZap(&tdc1->f.inode);
340                 ReleaseWriteLock(&tdc1->lock);
341                 afs_PutDCache(tdc1);    /* put it back */
342             }
343             osi_dnlc_remove(tvc, "..", 0);
344             ReleaseWriteLock(&tvc->lock);
345             afs_PutVCache(tvc);
346         } else if (tvc) {
347             /* True we shouldn't come here since tvc SHOULD be a dir, but we
348              * 'syntactically' need to unless  we change the 'if' above...
349              */
350             afs_PutVCache(tvc);
351         }
352     }
353     code = returnCode;
354 done:
355     return code;
356 }
357
358 #ifdef  AFS_OSF_ENV
359 afs_rename(fndp, tndp)
360     struct nameidata *fndp, *tndp; {
361     struct vcache *aodp = VTOAFS(fndp->ni_dvp);
362     char *aname1 = fndp->ni_dent.d_name;
363     struct vcache *andp = VTOAFS(tndp->ni_dvp);
364     char *aname2 = tndp->ni_dent.d_name;
365     struct ucred *acred = tndp->ni_cred;
366 #else   /* AFS_OSF_ENV */
367 #if defined(AFS_SGI_ENV)
368 afs_rename(OSI_VC_ARG(aodp), aname1, andp, aname2, npnp, acred)
369     struct pathname *npnp;
370 #else
371 afs_rename(OSI_VC_ARG(aodp), aname1, andp, aname2, acred)
372 #endif
373     OSI_VC_DECL(aodp);
374     struct vcache *andp;
375     char *aname1, *aname2;
376     struct AFS_UCRED *acred; {
377 #endif
378     register afs_int32 code;
379     struct afs_fakestat_state ofakestate;
380     struct afs_fakestat_state nfakestate;
381     struct vrequest treq;
382     OSI_VC_CONVERT(aodp)
383
384     code = afs_InitReq(&treq, acred);
385     if (code) return code;
386     afs_InitFakeStat(&ofakestate);
387     afs_InitFakeStat(&nfakestate);
388     code = afs_EvalFakeStat(&aodp, &ofakestate, &treq);
389     if (code) goto done;
390     code = afs_EvalFakeStat(&andp, &nfakestate, &treq);
391     if (code) goto done;
392     code = afsrename(aodp, aname1, andp, aname2, acred, &treq);
393 done:
394     afs_PutFakeStat(&ofakestate);
395     afs_PutFakeStat(&nfakestate);
396 #ifdef  AFS_OSF_ENV
397     AFS_RELE(tndp->ni_dvp);
398     if (tndp->ni_vp != NULL) {
399         AFS_RELE(tndp->ni_vp);
400     }
401     AFS_RELE(fndp->ni_dvp);
402     AFS_RELE(fndp->ni_vp);
403 #endif  /* AFS_OSF_ENV */
404     code = afs_CheckCode(code, &treq, 25);
405     return code;
406 }