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