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