1bd063f681e1644c3b582021ed020562dde19b71
[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
21     ("$Header$");
22
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"
29
30 extern afs_rwlock_t afs_xcbhash;
31
32 /* Note that we don't set CDirty here, this is OK because the rename
33  * RPC is called synchronously. */
34
35 int
36 afsrename(struct vcache *aodp, char *aname1, struct vcache *andp,
37           char *aname2, struct AFS_UCRED *acred, struct vrequest *areq)
38 {
39     register struct afs_conn *tc;
40     register afs_int32 code = 0;
41     afs_int32 returnCode;
42     int oneDir, doLocally;
43     afs_size_t offset, len;
44     struct VenusFid unlinkFid, fileFid;
45     struct vcache *tvc;
46     struct dcache *tdc1, *tdc2;
47     struct AFSFetchStatus OutOldDirStatus, OutNewDirStatus;
48     struct AFSVolSync tsync;
49     XSTATS_DECLS;
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);
54
55     if (strlen(aname1) > AFSNAMEMAX || strlen(aname2) > AFSNAMEMAX) {
56         code = ENAMETOOLONG;
57         goto done;
58     }
59
60     /* verify the latest versions of the stat cache entries */
61   tagain:
62     code = afs_VerifyVCache(aodp, areq);
63     if (code)
64         goto done;
65     code = afs_VerifyVCache(andp, areq);
66     if (code)
67         goto done;
68
69     /* lock in appropriate order, after some checks */
70     if (aodp->f.fid.Cell != andp->f.fid.Cell
71         || aodp->f.fid.Fid.Volume != andp->f.fid.Fid.Volume) {
72         code = EXDEV;
73         goto done;
74     }
75     oneDir = 0;
76     code = 0;
77     if (andp->f.fid.Fid.Vnode == aodp->f.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 */
81
82             code = 0;
83             goto done;
84         }
85         
86         if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
87             code = ENETDOWN;
88             goto done;
89         }
90         
91         ObtainWriteLock(&andp->lock, 147);
92         tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
93         if (!tdc1) {
94             code = ENOENT;
95         } else {
96             ObtainWriteLock(&tdc1->lock, 643);
97         }
98         tdc2 = tdc1;
99         oneDir = 1;             /* only one dude locked */
100     } else if ((andp->f.states & CRO) || (aodp->f.states & CRO)) {
101         code = EROFS;
102         goto done;
103     } else if (andp->f.fid.Fid.Vnode < aodp->f.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);
107         if (tdc2)
108             ObtainWriteLock(&tdc2->lock, 644);
109         tdc1 = afs_GetDCache(aodp, (afs_size_t) 0, areq, &offset, &len, 0);
110         if (tdc1)
111             ObtainWriteLock(&tdc1->lock, 645);
112         else
113             code = ENOENT;
114     } else {
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);
118         if (tdc1)
119             ObtainWriteLock(&tdc1->lock, 646);
120         else
121             code = ENOENT;
122         tdc2 = afs_FindDCache(andp, (afs_size_t) 0);
123         if (tdc2)
124             ObtainWriteLock(&tdc2->lock, 647);
125     }
126
127     osi_dnlc_remove(aodp, aname1, 0);
128     osi_dnlc_remove(andp, aname2, 0);
129
130     /*
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.
133      */
134     if (tdc1) {
135         if (!(aodp->f.states & CStatd)
136             || !hsame(aodp->f.m.DataVersion, tdc1->f.versionNo)) {
137
138             ReleaseWriteLock(&aodp->lock);
139             if (!oneDir) {
140                 if (tdc2) {
141                     ReleaseWriteLock(&tdc2->lock);
142                     afs_PutDCache(tdc2);
143                 }
144                 ReleaseWriteLock(&andp->lock);
145             }
146             ReleaseWriteLock(&tdc1->lock);
147             afs_PutDCache(tdc1);
148             goto tagain;
149         }
150     }
151
152     if (code == 0)
153         code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
154     if (code) {
155         if (tdc1) {
156             ReleaseWriteLock(&tdc1->lock);
157             afs_PutDCache(tdc1);
158         }
159         ReleaseWriteLock(&aodp->lock);
160         if (!oneDir) {
161             if (tdc2) {
162                 ReleaseWriteLock(&tdc2->lock);
163                 afs_PutDCache(tdc2);
164             }
165             ReleaseWriteLock(&andp->lock);
166         }
167         goto done;
168     }
169
170     if (!AFS_IS_DISCON_RW) {
171         /* Connected. */
172         do {
173             tc = afs_Conn(&aodp->f.fid, areq, SHARED_LOCK);
174             if (tc) {
175                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RENAME);
176                 RX_AFS_GUNLOCK();
177                 code =
178                     RXAFS_Rename(tc->id,
179                                         (struct AFSFid *)&aodp->f.fid.Fid,
180                                         aname1,
181                                         (struct AFSFid *)&andp->f.fid.Fid,
182                                         aname2,
183                                         &OutOldDirStatus,
184                                         &OutNewDirStatus,
185                                         &tsync);
186                 RX_AFS_GLOCK();
187                 XSTATS_END_TIME;
188             } else
189                 code = -1;
190
191         } while (afs_Analyze
192              (tc, code, &andp->f.fid, areq, AFS_STATS_FS_RPCIDX_RENAME,
193               SHARED_LOCK, NULL));
194
195     } else {
196 #if defined(AFS_DISCON_ENV)
197         /* Disconnected. */
198
199         /* Seek moved file vcache. */
200         fileFid.Cell = aodp->f.fid.Cell;
201         fileFid.Fid.Volume = aodp->f.fid.Fid.Volume;
202         ObtainSharedLock(&afs_xvcache, 754);
203         tvc = afs_FindVCache(&fileFid, 0 , 1);
204         ReleaseSharedLock(&afs_xvcache);
205
206         if (tvc) {
207             /* XXX - We're locking this vcache whilst holding dcaches. Ooops */
208             ObtainWriteLock(&tvc->lock, 750);
209             if (!(tvc->f.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.
214                  */
215                 
216                 if (!aodp->f.shadow.vnode) {
217                     /* Make shadow copy of parent dir only. */
218                     afs_MakeShadowDir(aodp, tdc1);
219                 }
220
221                 /* Save old parent dir fid so it will be searchable
222                  * in the shadow dir.
223                  */
224                 tvc->f.oldParent.vnode = aodp->f.fid.Fid.Vnode;
225                 tvc->f.oldParent.unique = aodp->f.fid.Fid.Unique;
226
227                 afs_DisconAddDirty(tvc, 
228                                    VDisconRename 
229                                      | (oneDir ? VDisconRenameSameDir:0), 
230                                    1);
231             }
232
233             ReleaseWriteLock(&tvc->lock);
234             afs_PutVCache(tvc);
235         } else {
236             code = ENOENT;
237         }                       /* if (tvc) */
238 #endif
239     }                           /* if !(AFS_IS_DISCON_RW)*/
240     returnCode = code;          /* remember for later */
241
242     /* Now we try to do things locally.  This is really loathsome code. */
243     unlinkFid.Fid.Vnode = 0;
244     if (code == 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 */
248         doLocally = 1;
249         if (!AFS_IS_DISCON_RW) {
250             if (oneDir) {
251                 /* number increases by 1 for whole rename operation */
252                 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) {
253                     doLocally = 0;
254                 }
255             } else {
256                 /* two separate dirs, each increasing by 1 */
257                 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1))
258                     doLocally = 0;
259                 if (!afs_LocalHero(andp, tdc2, &OutNewDirStatus, 1))
260                     doLocally = 0;
261                 if (!doLocally) {
262                     if (tdc1) {
263                         ZapDCE(tdc1);
264                         DZap(tdc1);
265                     }
266                     if (tdc2) {
267                         ZapDCE(tdc2);
268                         DZap(tdc2);
269                     }
270                 }
271             }
272         }                       /* if (!AFS_IS_DISCON_RW) */
273
274         /* now really do the work */
275         if (doLocally) {
276             /* first lookup the fid of the dude we're moving */
277             code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
278             if (code == 0) {
279                 /* delete the source */
280                 code = afs_dir_Delete(tdc1, aname1);
281             }
282             /* first see if target is there */
283             if (code == 0
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);
288             }
289             if (code == 0) {
290                 ObtainWriteLock(&afs_xdcache, 292);
291                 code = afs_dir_Create(tdc2, aname2, &fileFid.Fid);
292                 ReleaseWriteLock(&afs_xdcache);
293             }
294             if (code != 0) {
295                 ZapDCE(tdc1);
296                 DZap(tdc1);
297                 if (!oneDir) {
298                     ZapDCE(tdc2);
299                     DZap(tdc2);
300                 }
301             }
302         }
303
304
305         /* update dir link counts */
306         if (AFS_IS_DISCON_RW) {
307             if (!oneDir) {
308                 aodp->f.m.LinkCount--;
309                 andp->f.m.LinkCount++;
310             }
311             /* If we're in the same directory, link count doesn't change */
312         } else {
313             aodp->f.m.LinkCount = OutOldDirStatus.LinkCount;
314             if (!oneDir)
315                 andp->f.m.LinkCount = OutNewDirStatus.LinkCount;
316         }
317
318     } else {                    /* operation failed (code != 0) */
319         if (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->f.states &= ~CStatd;
326             aodp->f.states &= ~CStatd;
327             ReleaseWriteLock(&afs_xcbhash);
328             osi_dnlc_purgedp(andp);
329             osi_dnlc_purgedp(aodp);
330         }
331     }
332
333     /* release locks */
334     if (tdc1) {
335         ReleaseWriteLock(&tdc1->lock);
336         afs_PutDCache(tdc1);
337     }
338
339     if ((!oneDir) && tdc2) {
340         ReleaseWriteLock(&tdc2->lock);
341         afs_PutDCache(tdc2);
342     }
343
344     ReleaseWriteLock(&aodp->lock);
345
346     if (!oneDir) {
347         ReleaseWriteLock(&andp->lock);
348     }
349
350     if (returnCode) {
351         code = returnCode;
352         goto done;
353     }
354
355     /* now, some more details.  if unlinkFid.Fid.Vnode then we should decrement
356      * the link count on this file.  Note that if fileFid is a dir, then we don't
357      * have to invalidate its ".." entry, since its DataVersion # should have
358      * changed. However, interface is not good enough to tell us the
359      * *file*'s new DataVersion, so we're stuck.  Our hack: delete mark
360      * the data as having an "unknown" version (effectively discarding the ".."
361      * entry */
362     if (unlinkFid.Fid.Vnode) {
363
364         unlinkFid.Fid.Volume = aodp->f.fid.Fid.Volume;
365         unlinkFid.Cell = aodp->f.fid.Cell;
366         tvc = NULL;
367         if (!unlinkFid.Fid.Unique) {
368             tvc = afs_LookupVCache(&unlinkFid, areq, NULL, aodp, aname1);
369         }
370         if (!tvc)               /* lookup failed or wasn't called */
371             tvc = afs_GetVCache(&unlinkFid, areq, NULL, NULL);
372
373         if (tvc) {
374 #ifdef AFS_BOZONLOCK_ENV
375             afs_BozonLock(&tvc->pvnLock, tvc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
376 #endif
377             ObtainWriteLock(&tvc->lock, 151);
378             tvc->f.m.LinkCount--;
379             tvc->f.states &= ~CUnique;  /* For the dfs xlator */
380             if (tvc->f.m.LinkCount == 0 && !osi_Active(tvc)) {
381                 /* if this was last guy (probably) discard from cache.
382                  * We have to be careful to not get rid of the stat
383                  * information, since otherwise operations will start
384                  * failing even if the file was still open (or
385                  * otherwise active), and the server no longer has the
386                  * info.  If the file still has valid links, we'll get
387                  * a break-callback msg from the server, so it doesn't
388                  * matter that we don't discard the status info */
389                 if (!AFS_NFSXLATORREQ(acred))
390                     afs_TryToSmush(tvc, acred, 0);
391             }
392             ReleaseWriteLock(&tvc->lock);
393 #ifdef AFS_BOZONLOCK_ENV
394             afs_BozonUnlock(&tvc->pvnLock, tvc);
395 #endif
396             afs_PutVCache(tvc);
397         }
398     }
399
400     /* now handle ".." invalidation */
401     if (!oneDir) {
402         fileFid.Fid.Volume = aodp->f.fid.Fid.Volume;
403         fileFid.Cell = aodp->f.fid.Cell;
404         if (!fileFid.Fid.Unique)
405             tvc = afs_LookupVCache(&fileFid, areq, NULL, andp, aname2);
406         else
407             tvc = afs_GetVCache(&fileFid, areq, NULL, (struct vcache *)0);
408         if (tvc && (vType(tvc) == VDIR)) {
409             ObtainWriteLock(&tvc->lock, 152);
410             tdc1 = afs_FindDCache(tvc, (afs_size_t) 0);
411             if (tdc1) {
412                 if (AFS_IS_DISCON_RW) {
413 #if defined(AFS_DISCON_ENV)
414                     /* If disconnected, we need to fix (not discard) the "..".*/
415                     afs_dir_ChangeFid(tdc1,
416                         "..",
417                         &aodp->f.fid.Fid.Vnode,
418                         &andp->f.fid.Fid.Vnode);
419 #endif
420                 } else {
421                     ObtainWriteLock(&tdc1->lock, 648);
422                     ZapDCE(tdc1);       /* mark as unknown */
423                     DZap(tdc1);
424                     ReleaseWriteLock(&tdc1->lock);
425                     afs_PutDCache(tdc1);        /* put it back */
426                 }
427             }
428             osi_dnlc_remove(tvc, "..", 0);
429             ReleaseWriteLock(&tvc->lock);
430             afs_PutVCache(tvc);
431         } else if (AFS_IS_DISCON_RW && tvc && (vType(tvc) == VREG)) {
432             /* XXX - Should tvc not get locked here? */
433             tvc->f.parent.vnode = andp->f.fid.Fid.Vnode;
434             tvc->f.parent.unique = andp->f.fid.Fid.Unique;
435         } else if (tvc) {
436             /* True we shouldn't come here since tvc SHOULD be a dir, but we
437              * 'syntactically' need to unless  we change the 'if' above...
438              */
439             afs_PutVCache(tvc);
440         }
441     }
442     code = returnCode;
443   done:
444     return code;
445 }
446
447 int
448 #if defined(AFS_SGI_ENV)
449 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct pathname *npnp, struct AFS_UCRED *acred)
450 #else
451 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct AFS_UCRED *acred)
452 #endif
453 {
454     register afs_int32 code;
455     struct afs_fakestat_state ofakestate;
456     struct afs_fakestat_state nfakestate;
457     struct vrequest treq;
458     OSI_VC_CONVERT(aodp);
459
460     code = afs_InitReq(&treq, acred);
461     if (code)
462         return code;
463     afs_InitFakeStat(&ofakestate);
464     afs_InitFakeStat(&nfakestate);
465
466     AFS_DISCON_LOCK();
467     
468     code = afs_EvalFakeStat(&aodp, &ofakestate, &treq);
469     if (code)
470         goto done;
471     code = afs_EvalFakeStat(&andp, &nfakestate, &treq);
472     if (code)
473         goto done;
474     code = afsrename(aodp, aname1, andp, aname2, acred, &treq);
475   done:
476     afs_PutFakeStat(&ofakestate);
477     afs_PutFakeStat(&nfakestate);
478
479     AFS_DISCON_UNLOCK();
480     
481     code = afs_CheckCode(code, &treq, 25);
482     return code;
483 }