cb7a3f0af6359ddf77d4a7cc64d2bb996345b921
[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 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->fid.Cell != andp->fid.Cell
71         || aodp->fid.Fid.Volume != andp->fid.Fid.Volume) {
72         code = EXDEV;
73         goto done;
74     }
75     oneDir = 0;
76     code = 0;
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 */
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->states & CRO) || (aodp->states & CRO)) {
101         code = EROFS;
102         goto done;
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);
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->states & CStatd)
136             || !hsame(aodp->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->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->fid.Fid,
180                                         aname1,
181                                         (struct AFSFid *)&andp->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->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->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);
205
206         if (tvc) {
207             if (!(tvc->ddirty_flags & VDisconRename)) {
208                 /* We only care about vnodes that haven't been
209                  * renamed. Those that have been renamed at least once
210                  * already have an asociated shadow dir and the flags set
211                  * for the first old location, which is what interests us
212                  * when reconnecting.
213                  */
214
215                 if (!(aodp->ddirty_flags & VDisconShadowed) &&
216                         !(tvc->ddirty_flags & VDisconCreate)) {
217                     /*
218                      * Make shadow copy of parent dir only.
219                      * Files that are created locally and renamed,
220                      * don't need a shadow dir, so skip this step.
221                      */
222                     if (tdc1)
223                         ReleaseWriteLock(&tdc1->lock);
224                     afs_MakeShadowDir(aodp);
225                     if (tdc1)
226                         ObtainWriteLock(&tdc1->lock, 753);
227                 }
228
229                 if (!tvc->ddirty_flags ||
230                         (tvc->ddirty_flags == VDisconShadowed)) {
231                     /* Add in dirty list.*/
232                     ObtainWriteLock(&afs_DDirtyVCListLock, 751);
233                     AFS_DISCON_ADD_DIRTY(tvc, 1);
234                     ReleaseWriteLock(&afs_DDirtyVCListLock);
235                 }
236
237                 ObtainWriteLock(&tvc->lock, 750);
238                 /* Save old parent dir fid so it will be searchable
239                  * in the shadow dir.
240                  */
241                 tvc->oldVnode = aodp->fid.Fid.Vnode;
242                 tvc->oldUnique = aodp->fid.Fid.Unique;
243                 /* Set discon state flag. */
244                 tvc->ddirty_flags |= VDisconRename;
245                 if (oneDir)
246                     tvc->ddirty_flags |= VDisconRenameSameDir;
247                 ReleaseWriteLock(&tvc->lock);
248             }                   /* if not previously renamed */
249         } else {
250             code = ENOENT;
251         }                       /* if (tvc) */
252 #endif
253     }                           /* if !(AFS_IS_DISCON_RW)*/
254     returnCode = code;          /* remember for later */
255
256     /* Now we try to do things locally.  This is really loathsome code. */
257     unlinkFid.Fid.Vnode = 0;
258     if (code == 0) {
259         /*  In any event, we don't really care if the data (tdc2) is not
260          * in the cache; if it isn't, we won't do the update locally.  */
261         /* see if version numbers increased properly */
262         doLocally = 1;
263         if (!AFS_IS_DISCON_RW) {
264             if (oneDir) {
265                 /* number increases by 1 for whole rename operation */
266                 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1)) {
267                     doLocally = 0;
268                 }
269             } else {
270                 /* two separate dirs, each increasing by 1 */
271                 if (!afs_LocalHero(aodp, tdc1, &OutOldDirStatus, 1))
272                     doLocally = 0;
273                 if (!afs_LocalHero(andp, tdc2, &OutNewDirStatus, 1))
274                     doLocally = 0;
275                 if (!doLocally) {
276                     if (tdc1) {
277                         ZapDCE(tdc1);
278                         DZap(tdc1);
279                     }
280                     if (tdc2) {
281                         ZapDCE(tdc2);
282                         DZap(tdc2);
283                     }
284                 }
285             }
286         }                       /* if (!AFS_IS_DISCON_RW) */
287
288         /* now really do the work */
289         if (doLocally) {
290             /* first lookup the fid of the dude we're moving */
291             code = afs_dir_Lookup(tdc1, aname1, &fileFid.Fid);
292             if (code == 0) {
293                 /* delete the source */
294                 code = afs_dir_Delete(tdc1, aname1);
295             }
296             /* first see if target is there */
297             if (code == 0
298                 && afs_dir_Lookup(tdc2, aname2,
299                                   &unlinkFid.Fid) == 0) {
300                 /* target already exists, and will be unlinked by server */
301                 code = afs_dir_Delete(tdc2, aname2);
302             }
303             if (code == 0) {
304                 ObtainWriteLock(&afs_xdcache, 292);
305                 code = afs_dir_Create(tdc2, aname2, &fileFid.Fid);
306                 ReleaseWriteLock(&afs_xdcache);
307             }
308             if (code != 0) {
309                 ZapDCE(tdc1);
310                 DZap(tdc1);
311                 if (!oneDir) {
312                     ZapDCE(tdc2);
313                     DZap(tdc2);
314                 }
315             }
316         }
317
318         /* update dir link counts */
319         aodp->m.LinkCount = AFS_IS_DISCON_RW ?
320                         (aodp->m.LinkCount - 1):OutOldDirStatus.LinkCount;
321         if (!oneDir)
322             andp->m.LinkCount = AFS_IS_DISCON_RW ?
323                         (andp->m.LinkCount + 1):OutNewDirStatus.LinkCount;
324
325     } else {                    /* operation failed (code != 0) */
326         if (code < 0) {
327             /* if failed, server might have done something anyway, and 
328              * assume that we know about it */
329             ObtainWriteLock(&afs_xcbhash, 498);
330             afs_DequeueCallback(aodp);
331             afs_DequeueCallback(andp);
332             andp->states &= ~CStatd;
333             aodp->states &= ~CStatd;
334             ReleaseWriteLock(&afs_xcbhash);
335             osi_dnlc_purgedp(andp);
336             osi_dnlc_purgedp(aodp);
337         }
338     }
339
340     /* release locks */
341     if (tdc1) {
342         ReleaseWriteLock(&tdc1->lock);
343         afs_PutDCache(tdc1);
344     }
345
346     if ((!oneDir) && tdc2) {
347         ReleaseWriteLock(&tdc2->lock);
348         afs_PutDCache(tdc2);
349     }
350
351     ReleaseWriteLock(&aodp->lock);
352     if (!oneDir)
353         ReleaseWriteLock(&andp->lock);
354     if (returnCode) {
355         code = returnCode;
356         goto done;
357     }
358
359     /* now, some more details.  if unlinkFid.Fid.Vnode then we should decrement
360      * the link count on this file.  Note that if fileFid is a dir, then we don't
361      * have to invalidate its ".." entry, since its DataVersion # should have
362      * changed. However, interface is not good enough to tell us the
363      * *file*'s new DataVersion, so we're stuck.  Our hack: delete mark
364      * the data as having an "unknown" version (effectively discarding the ".."
365      * entry */
366     if (unlinkFid.Fid.Vnode) {
367
368         unlinkFid.Fid.Volume = aodp->fid.Fid.Volume;
369         unlinkFid.Cell = aodp->fid.Cell;
370         tvc = NULL;
371         if (!unlinkFid.Fid.Unique) {
372             tvc = afs_LookupVCache(&unlinkFid, areq, NULL, aodp, aname1);
373         }
374         if (!tvc)               /* lookup failed or wasn't called */
375             tvc = afs_GetVCache(&unlinkFid, areq, NULL, NULL);
376
377         if (tvc) {
378 #ifdef AFS_BOZONLOCK_ENV
379             afs_BozonLock(&tvc->pvnLock, tvc);  /* Since afs_TryToSmush will do a pvn_vptrunc */
380 #endif
381             ObtainWriteLock(&tvc->lock, 151);
382             tvc->m.LinkCount--;
383             tvc->states &= ~CUnique;    /* For the dfs xlator */
384             if (tvc->m.LinkCount == 0 && !osi_Active(tvc)) {
385                 /* if this was last guy (probably) discard from cache.
386                  * We have to be careful to not get rid of the stat
387                  * information, since otherwise operations will start
388                  * failing even if the file was still open (or
389                  * otherwise active), and the server no longer has the
390                  * info.  If the file still has valid links, we'll get
391                  * a break-callback msg from the server, so it doesn't
392                  * matter that we don't discard the status info */
393                 if (!AFS_NFSXLATORREQ(acred))
394                     afs_TryToSmush(tvc, acred, 0);
395             }
396             ReleaseWriteLock(&tvc->lock);
397 #ifdef AFS_BOZONLOCK_ENV
398             afs_BozonUnlock(&tvc->pvnLock, tvc);
399 #endif
400             afs_PutVCache(tvc);
401         }
402     }
403
404     /* now handle ".." invalidation */
405     if (!oneDir) {
406         fileFid.Fid.Volume = aodp->fid.Fid.Volume;
407         fileFid.Cell = aodp->fid.Cell;
408         if (!fileFid.Fid.Unique)
409             tvc = afs_LookupVCache(&fileFid, areq, NULL, andp, aname2);
410         else
411             tvc = afs_GetVCache(&fileFid, areq, NULL, (struct vcache *)0);
412         if (tvc && (vType(tvc) == VDIR)) {
413             ObtainWriteLock(&tvc->lock, 152);
414             tdc1 = afs_FindDCache(tvc, (afs_size_t) 0);
415             if (tdc1) {
416                 if (AFS_IS_DISCON_RW) {
417 #if defined(AFS_DISCON_ENV)
418                     /* If disconnected, we need to fix (not discard) the "..".*/
419                     afs_dir_ChangeFid(tdc1,
420                         "..",
421                         &aodp->fid.Fid.Vnode,
422                         &andp->fid.Fid.Vnode);
423 #endif
424                 } else {
425                     ObtainWriteLock(&tdc1->lock, 648);
426                     ZapDCE(tdc1);       /* mark as unknown */
427                     DZap(tdc1);
428                     ReleaseWriteLock(&tdc1->lock);
429                     afs_PutDCache(tdc1);        /* put it back */
430                 }
431             }
432             osi_dnlc_remove(tvc, "..", 0);
433             ReleaseWriteLock(&tvc->lock);
434             afs_PutVCache(tvc);
435         } else if (AFS_IS_DISCON_RW && tvc && (vType(tvc) == VREG)) {
436             tvc->parentVnode = andp->fid.Fid.Vnode;
437             tvc->parentUnique = andp->fid.Fid.Unique;
438         } else if (tvc) {
439             /* True we shouldn't come here since tvc SHOULD be a dir, but we
440              * 'syntactically' need to unless  we change the 'if' above...
441              */
442             afs_PutVCache(tvc);
443         }
444     }
445     code = returnCode;
446   done:
447     return code;
448 }
449
450 int
451 #if defined(AFS_SGI_ENV)
452 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct pathname *npnp, struct AFS_UCRED *acred)
453 #else
454 afs_rename(OSI_VC_DECL(aodp), char *aname1, struct vcache *andp, char *aname2, struct AFS_UCRED *acred)
455 #endif
456 {
457     register afs_int32 code;
458     struct afs_fakestat_state ofakestate;
459     struct afs_fakestat_state nfakestate;
460     struct vrequest treq;
461     OSI_VC_CONVERT(aodp);
462
463     code = afs_InitReq(&treq, acred);
464     if (code)
465         return code;
466     afs_InitFakeStat(&ofakestate);
467     afs_InitFakeStat(&nfakestate);
468
469     AFS_DISCON_LOCK();
470     
471     code = afs_EvalFakeStat(&aodp, &ofakestate, &treq);
472     if (code)
473         goto done;
474     code = afs_EvalFakeStat(&andp, &nfakestate, &treq);
475     if (code)
476         goto done;
477     code = afsrename(aodp, aname1, andp, aname2, acred, &treq);
478   done:
479     afs_PutFakeStat(&ofakestate);
480     afs_PutFakeStat(&nfakestate);
481
482     AFS_DISCON_UNLOCK();
483     
484     code = afs_CheckCode(code, &treq, 25);
485     return code;
486 }