tru64-avoid-leaking-vnodes-when-failure-20020320
[openafs.git] / src / afs / VNOPS / afs_vnop_dirops.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  * afs_vnop_dirops.c - make and remove directories
12  *
13  * Implements:
14  *
15  * afs_mkdir
16  * afs_rmdir
17  *
18  */
19
20 #include <afsconfig.h>
21 #include "../afs/param.h"
22
23 RCSID("$Header$");
24
25 #include "../afs/sysincludes.h" /* Standard vendor system headers */
26 #include "../afs/afsincludes.h" /* Afs-based standard headers */
27 #include "../afs/afs_stats.h" /* statistics */
28 #include "../afs/afs_cbqueue.h"
29 #include "../afs/nfsclient.h"
30 #include "../afs/afs_osidnlc.h"
31
32 extern afs_rwlock_t afs_xvcache;
33 extern afs_rwlock_t afs_xcbhash;
34
35 /* don't set CDirty in here because RPC is called synchronously */
36
37 #ifdef  AFS_OSF_ENV
38 afs_mkdir(ndp, attrs)
39     struct nameidata *ndp;
40     struct vattr *attrs; {
41     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
42     char *aname = ndp->ni_dent.d_name;
43     register struct vcache **avcp = (struct vcache **)&(ndp->ni_vp);
44     struct ucred *acred = ndp->ni_cred;
45 #else   /* AFS_OSF_ENV */
46 afs_mkdir(OSI_VC_ARG(adp), aname, attrs, avcp, acred)
47     OSI_VC_DECL(adp);
48     register struct vcache **avcp;
49     char *aname;
50     struct vattr *attrs;
51     struct AFS_UCRED *acred; {
52 #endif
53     struct vrequest treq;
54     register afs_int32 code;
55     register struct conn *tc;
56     struct VenusFid newFid;
57     register struct dcache *tdc;
58     afs_size_t offset, len;
59     register struct vcache *tvc;
60     struct AFSStoreStatus InStatus;
61     struct AFSFetchStatus OutFidStatus, OutDirStatus;
62     struct AFSCallBack CallBack;
63     struct AFSVolSync tsync;
64     afs_int32 now;
65     XSTATS_DECLS
66     OSI_VC_CONVERT(adp)
67
68     AFS_STATCNT(afs_mkdir);
69     afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
70                ICL_TYPE_STRING, aname);
71
72     if (code = afs_InitReq(&treq, acred)) 
73         goto done2;
74
75     if (!afs_ENameOK(aname)) {
76         code = EINVAL;
77         goto done;
78     }
79     code = afs_VerifyVCache(adp, &treq);
80     if (code) goto done;
81
82     /** If the volume is read-only, return error without making an RPC to the
83       * fileserver
84       */
85     if ( adp->states & CRO ) {
86         code = EROFS;
87         goto done;
88     }
89
90     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
91     InStatus.ClientModTime = osi_Time();
92     InStatus.UnixModeBits = attrs->va_mode & 0xffff;   /* only care about protection bits */
93     InStatus.Group = (afs_int32)acred->cr_gid;
94     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
95     ObtainWriteLock(&adp->lock,153);
96     do {
97         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
98         if (tc) {
99           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
100           now = osi_Time();
101 #ifdef RX_ENABLE_LOCKS
102           AFS_GUNLOCK();
103 #endif /* RX_ENABLE_LOCKS */
104           code = RXAFS_MakeDir(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
105                               &InStatus, (struct AFSFid *) &newFid.Fid,
106                               &OutFidStatus, &OutDirStatus, &CallBack, &tsync);
107 #ifdef RX_ENABLE_LOCKS
108           AFS_GLOCK();
109 #endif /* RX_ENABLE_LOCKS */
110           XSTATS_END_TIME;
111             CallBack.ExpirationTime += now;
112             /* DON'T forget to Set the callback value... */
113         }
114         else code = -1;
115     } while
116       (afs_Analyze(tc, code, &adp->fid, &treq,
117                    AFS_STATS_FS_RPCIDX_MAKEDIR, SHARED_LOCK, (struct cell *)0));
118
119     if (code) {
120         if (code < 0) {
121           ObtainWriteLock(&afs_xcbhash, 490);
122           afs_DequeueCallback(adp);
123           adp->states &= ~CStatd;
124           ReleaseWriteLock(&afs_xcbhash);
125           osi_dnlc_purgedp(adp);
126         }
127         ReleaseWriteLock(&adp->lock);
128         if (tdc) afs_PutDCache(tdc);
129         goto done;
130     }
131     /* otherwise, we should see if we can make the change to the dir locally */
132     if (tdc) ObtainWriteLock(&tdc->lock, 632);
133     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
134         /* we can do it locally */
135         code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
136         if (code) {
137             ZapDCE(tdc);        /* surprise error -- use invalid value */
138             DZap(&tdc->f.inode);
139         }
140     }
141     if (tdc) {
142         ReleaseWriteLock(&tdc->lock);
143         afs_PutDCache(tdc);
144     }
145     adp->m.LinkCount = OutDirStatus.LinkCount;
146     newFid.Cell = adp->fid.Cell;
147     newFid.Fid.Volume = adp->fid.Fid.Volume;
148     ReleaseWriteLock(&adp->lock);
149     /* now we're done with parent dir, create the real dir's cache entry */
150     tvc = afs_GetVCache(&newFid, &treq, (afs_int32 *)0, (struct vcache*)0, 0);
151     if (tvc) {
152         code = 0;
153         *avcp = tvc;
154     }
155     else code = ENOENT;
156 done:
157     code = afs_CheckCode(code, &treq, 26);
158 done2:
159 #ifdef  AFS_OSF_ENV
160     AFS_RELE(ndp->ni_dvp);
161 #endif  /* AFS_OSF_ENV */
162     return code;
163 }
164
165
166 #ifdef  AFS_OSF_ENV
167 afs_rmdir(ndp)
168     struct nameidata *ndp; {
169     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
170     char *aname = ndp->ni_dent.d_name;
171     struct ucred *acred = ndp->ni_cred;
172 #else   /* AFS_OSF_ENV */
173 /* don't set CDirty in here because RPC is called synchronously */
174 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
175 afs_rmdir(OSI_VC_ARG(adp), aname, cdirp, acred)
176     struct vnode *cdirp;
177 #else
178 afs_rmdir(adp, aname, acred)
179 #endif
180     OSI_VC_DECL(adp);
181     char *aname;
182     struct AFS_UCRED *acred; {
183 #endif
184     struct vrequest treq;
185     register struct dcache *tdc;
186     register struct vcache *tvc = (struct vcache *)0;
187     register afs_int32 code;
188     register struct conn *tc;
189     afs_size_t offset, len;
190     struct AFSFetchStatus OutDirStatus;
191     struct AFSVolSync tsync;
192     XSTATS_DECLS
193     OSI_VC_CONVERT(adp)
194
195     AFS_STATCNT(afs_rmdir);
196
197     afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp, 
198                ICL_TYPE_STRING, aname);
199
200     if (code = afs_InitReq(&treq, acred)) 
201         goto done2;
202
203     code = afs_VerifyVCache(adp, &treq);
204     if (code) goto done;
205
206     /** If the volume is read-only, return error without making an RPC to the
207       * fileserver
208       */
209     if ( adp->states & CRO ) {
210         code = EROFS;
211         goto done;
212     }
213
214     tdc = afs_GetDCache(adp, (afs_size_t) 0,    &treq, &offset, &len, 1);       /* test for error below */
215     ObtainWriteLock(&adp->lock,154);
216     if (tdc) ObtainSharedLock(&tdc->lock, 633);
217     if (tdc && (adp->states & CForeign)) {
218         struct VenusFid unlinkFid;
219
220         unlinkFid.Fid.Vnode = 0;
221         code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
222         if (code == 0) {        
223             afs_int32 cached=0;
224
225             unlinkFid.Cell = adp->fid.Cell;
226             unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
227             if (unlinkFid.Fid.Unique == 0) {
228                 tvc = afs_LookupVCache(&unlinkFid, &treq, &cached, 
229                                        WRITE_LOCK, adp, aname);
230             } else {
231                 ObtainReadLock(&afs_xvcache);
232                 tvc = afs_FindVCache(&unlinkFid, 1, WRITE_LOCK, 
233                                      0, 1/* do xstats */);
234                 ReleaseReadLock(&afs_xvcache);
235             }
236         }
237     }
238
239     do {
240         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
241         if (tc) {
242           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
243 #ifdef RX_ENABLE_LOCKS
244           AFS_GUNLOCK();
245 #endif /* RX_ENABLE_LOCKS */
246             code = RXAFS_RemoveDir(tc->id, (struct AFSFid *) &adp->fid.Fid,
247                                    aname, &OutDirStatus, &tsync);
248 #ifdef RX_ENABLE_LOCKS
249           AFS_GLOCK();
250 #endif /* RX_ENABLE_LOCKS */
251           XSTATS_END_TIME;
252         }
253         else code = -1;
254     } while
255       (afs_Analyze(tc, code, &adp->fid, &treq,
256                    AFS_STATS_FS_RPCIDX_REMOVEDIR, SHARED_LOCK, (struct cell *)0));
257
258     if (code) {
259         if (tdc) {
260             ReleaseSharedLock(&tdc->lock);
261             afs_PutDCache(tdc);
262         }
263         if (code < 0) {
264           ObtainWriteLock(&afs_xcbhash, 491);
265           afs_DequeueCallback(adp);
266           adp->states &= ~CStatd;
267           ReleaseWriteLock(&afs_xcbhash);
268           osi_dnlc_purgedp(adp);
269         }
270         ReleaseWriteLock(&adp->lock);
271         goto done;
272     }
273     /* here if rpc worked; update the in-core link count */
274     adp->m.LinkCount = OutDirStatus.LinkCount;
275     if (tdc) UpgradeSToWLock(&tdc->lock, 634);
276     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
277         /* we can do it locally */
278         code = afs_dir_Delete(&tdc->f.inode, aname);
279         if (code) { 
280             ZapDCE(tdc);        /* surprise error -- invalid value */
281             DZap(&tdc->f.inode);
282         }
283     }
284     if (tdc) {
285         ReleaseWriteLock(&tdc->lock);
286         afs_PutDCache(tdc);     /* drop ref count */
287     }
288
289
290     if (tvc) {
291         osi_dnlc_purgedp (tvc);  /* get rid of any entries for this directory */
292         afs_symhint_inval(tvc);
293     } else
294         osi_dnlc_remove (adp, aname, 0);
295
296     if (tvc) {
297         ObtainWriteLock(&tvc->lock,155);
298         tvc->states &= ~CUnique;                /* For the dfs xlator */
299         ReleaseWriteLock(&tvc->lock);
300         afs_PutVCache(tvc, WRITE_LOCK);
301     }
302     ReleaseWriteLock(&adp->lock);
303     /* don't worry about link count since dirs can not be hardlinked */
304     code = 0;
305
306 done:
307     code = afs_CheckCode(code, &treq, 27); 
308 done2:
309 #ifdef  AFS_OSF_ENV
310     afs_PutVCache(adp, 0);
311     afs_PutVCache(ndp->ni_vp, 0);
312 #endif  /* AFS_OSF_ENV */
313     return code;
314 }