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