pull-prototypes-to-head-20020821
[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 = VTOAFS(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     struct afs_fakestat_state fakestate;
66     XSTATS_DECLS
67     OSI_VC_CONVERT(adp)
68
69     AFS_STATCNT(afs_mkdir);
70     afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
71                ICL_TYPE_STRING, aname);
72
73     if ((code = afs_InitReq(&treq, acred))) 
74         goto done2;
75     afs_InitFakeStat(&fakestate);
76
77     if (strlen(aname) > AFSNAMEMAX) {
78         code = ENAMETOOLONG;
79         goto done;
80     }
81
82     if (!afs_ENameOK(aname)) {
83         code = EINVAL;
84         goto done;
85     }
86     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
87     if (code) goto done;
88     code = afs_VerifyVCache(adp, &treq);
89     if (code) goto done;
90
91     /** If the volume is read-only, return error without making an RPC to the
92       * fileserver
93       */
94     if ( adp->states & CRO ) {
95         code = EROFS;
96         goto done;
97     }
98
99     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
100     InStatus.ClientModTime = osi_Time();
101     InStatus.UnixModeBits = attrs->va_mode & 0xffff;   /* only care about protection bits */
102     InStatus.Group = (afs_int32)acred->cr_gid;
103     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
104     ObtainWriteLock(&adp->lock,153);
105     do {
106         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
107         if (tc) {
108           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
109           now = osi_Time();
110           RX_AFS_GUNLOCK();
111           code = RXAFS_MakeDir(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
112                               &InStatus, (struct AFSFid *) &newFid.Fid,
113                               &OutFidStatus, &OutDirStatus, &CallBack, &tsync);
114           RX_AFS_GLOCK();
115           XSTATS_END_TIME;
116             CallBack.ExpirationTime += now;
117             /* DON'T forget to Set the callback value... */
118         }
119         else code = -1;
120     } while
121       (afs_Analyze(tc, code, &adp->fid, &treq,
122                    AFS_STATS_FS_RPCIDX_MAKEDIR, SHARED_LOCK, NULL));
123
124     if (code) {
125         if (code < 0) {
126           ObtainWriteLock(&afs_xcbhash, 490);
127           afs_DequeueCallback(adp);
128           adp->states &= ~CStatd;
129           ReleaseWriteLock(&afs_xcbhash);
130           osi_dnlc_purgedp(adp);
131         }
132         ReleaseWriteLock(&adp->lock);
133         if (tdc) afs_PutDCache(tdc);
134         goto done;
135     }
136     /* otherwise, we should see if we can make the change to the dir locally */
137     if (tdc) ObtainWriteLock(&tdc->lock, 632);
138     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
139         /* we can do it locally */
140         code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
141         if (code) {
142             ZapDCE(tdc);        /* surprise error -- use invalid value */
143             DZap(&tdc->f.inode);
144         }
145     }
146     if (tdc) {
147         ReleaseWriteLock(&tdc->lock);
148         afs_PutDCache(tdc);
149     }
150     adp->m.LinkCount = OutDirStatus.LinkCount;
151     newFid.Cell = adp->fid.Cell;
152     newFid.Fid.Volume = adp->fid.Fid.Volume;
153     ReleaseWriteLock(&adp->lock);
154     /* now we're done with parent dir, create the real dir's cache entry */
155     tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
156     if (tvc) {
157         code = 0;
158         *avcp = tvc;
159     }
160     else code = ENOENT;
161 done:
162     afs_PutFakeStat(&fakestate);
163     code = afs_CheckCode(code, &treq, 26);
164 done2:
165 #ifdef  AFS_OSF_ENV
166     AFS_RELE(ndp->ni_dvp);
167 #endif  /* AFS_OSF_ENV */
168     return code;
169 }
170
171
172 #ifdef  AFS_OSF_ENV
173 afs_rmdir(ndp)
174     struct nameidata *ndp; {
175     register struct vcache *adp = VTOAFS(ndp->ni_dvp);
176     char *aname = ndp->ni_dent.d_name;
177     struct ucred *acred = ndp->ni_cred;
178 #else   /* AFS_OSF_ENV */
179 /* don't set CDirty in here because RPC is called synchronously */
180 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
181 afs_rmdir(OSI_VC_ARG(adp), aname, cdirp, acred)
182     struct vnode *cdirp;
183 #else
184 afs_rmdir(adp, aname, acred)
185 #endif
186     OSI_VC_DECL(adp);
187     char *aname;
188     struct AFS_UCRED *acred; {
189 #endif
190     struct vrequest treq;
191     register struct dcache *tdc;
192     register struct vcache *tvc = NULL;
193     register afs_int32 code;
194     register struct conn *tc;
195     afs_size_t offset, len;
196     struct AFSFetchStatus OutDirStatus;
197     struct AFSVolSync tsync;
198     struct afs_fakestat_state fakestate;
199     XSTATS_DECLS
200     OSI_VC_CONVERT(adp)
201
202     AFS_STATCNT(afs_rmdir);
203
204     afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp, 
205                ICL_TYPE_STRING, aname);
206
207     if ((code = afs_InitReq(&treq, acred)))
208         goto done2;
209     afs_InitFakeStat(&fakestate);
210
211     if (strlen(aname) > AFSNAMEMAX) {
212         code = ENAMETOOLONG;
213         goto done;
214     }
215
216     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
217     if (code)
218         goto done;
219
220     code = afs_VerifyVCache(adp, &treq);
221     if (code) goto done;
222
223     /** If the volume is read-only, return error without making an RPC to the
224       * fileserver
225       */
226     if ( adp->states & CRO ) {
227         code = EROFS;
228         goto done;
229     }
230
231     tdc = afs_GetDCache(adp, (afs_size_t) 0,    &treq, &offset, &len, 1);       /* test for error below */
232     ObtainWriteLock(&adp->lock,154);
233     if (tdc) ObtainSharedLock(&tdc->lock, 633);
234     if (tdc && (adp->states & CForeign)) {
235         struct VenusFid unlinkFid;
236
237         unlinkFid.Fid.Vnode = 0;
238         code = afs_dir_Lookup(&tdc->f.inode, aname, &unlinkFid.Fid);
239         if (code == 0) {        
240             afs_int32 cached=0;
241
242             unlinkFid.Cell = adp->fid.Cell;
243             unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
244             if (unlinkFid.Fid.Unique == 0) {
245                 tvc = afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
246             } else {
247                 ObtainReadLock(&afs_xvcache);
248                 tvc = afs_FindVCache(&unlinkFid, 0, 1/* do xstats */);
249                 ReleaseReadLock(&afs_xvcache);
250             }
251         }
252     }
253
254     do {
255         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
256         if (tc) {
257           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
258           RX_AFS_GUNLOCK();
259             code = RXAFS_RemoveDir(tc->id, (struct AFSFid *) &adp->fid.Fid,
260                                    aname, &OutDirStatus, &tsync);
261           RX_AFS_GLOCK();
262           XSTATS_END_TIME;
263         }
264         else code = -1;
265     } while
266       (afs_Analyze(tc, code, &adp->fid, &treq,
267                    AFS_STATS_FS_RPCIDX_REMOVEDIR, SHARED_LOCK, NULL));
268
269     if (code) {
270         if (tdc) {
271             ReleaseSharedLock(&tdc->lock);
272             afs_PutDCache(tdc);
273         }
274         if (code < 0) {
275           ObtainWriteLock(&afs_xcbhash, 491);
276           afs_DequeueCallback(adp);
277           adp->states &= ~CStatd;
278           ReleaseWriteLock(&afs_xcbhash);
279           osi_dnlc_purgedp(adp);
280         }
281         ReleaseWriteLock(&adp->lock);
282         goto done;
283     }
284     /* here if rpc worked; update the in-core link count */
285     adp->m.LinkCount = OutDirStatus.LinkCount;
286     if (tdc) UpgradeSToWLock(&tdc->lock, 634);
287     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
288         /* we can do it locally */
289         code = afs_dir_Delete(&tdc->f.inode, aname);
290         if (code) { 
291             ZapDCE(tdc);        /* surprise error -- invalid value */
292             DZap(&tdc->f.inode);
293         }
294     }
295     if (tdc) {
296         ReleaseWriteLock(&tdc->lock);
297         afs_PutDCache(tdc);     /* drop ref count */
298     }
299
300
301     if (tvc) {
302         osi_dnlc_purgedp (tvc);  /* get rid of any entries for this directory */
303         afs_symhint_inval(tvc);
304     } else
305         osi_dnlc_remove (adp, aname, 0);
306
307     if (tvc) {
308         ObtainWriteLock(&tvc->lock,155);
309         tvc->states &= ~CUnique;                /* For the dfs xlator */
310         ReleaseWriteLock(&tvc->lock);
311         afs_PutVCache(tvc);
312     }
313     ReleaseWriteLock(&adp->lock);
314     /* don't worry about link count since dirs can not be hardlinked */
315     code = 0;
316
317 done:
318     afs_PutFakeStat(&fakestate);
319     code = afs_CheckCode(code, &treq, 27); 
320 done2:
321 #ifdef  AFS_OSF_ENV
322     afs_PutVCache(adp);
323     afs_PutVCache(ndp->ni_vp);
324 #endif  /* AFS_OSF_ENV */
325     return code;
326 }