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