2 * Copyright 2000, International Business Machines Corporation and others.
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
11 * afs_vnop_dirops.c - make and remove directories
20 #include <afsconfig.h>
21 #include "afs/param.h"
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"
33 extern afs_rwlock_t afs_xvcache;
34 extern afs_rwlock_t afs_xcbhash;
36 /* don't set CDirty in here because RPC is called synchronously */
39 afs_mkdir(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
40 register struct vcache **avcp, struct AFS_UCRED *acred)
43 register afs_int32 code;
44 register struct afs_conn *tc;
45 struct VenusFid newFid;
46 register struct dcache *tdc;
47 struct dcache *new_dc;
48 afs_size_t offset, len;
49 register struct vcache *tvc;
50 struct AFSStoreStatus InStatus;
51 struct AFSFetchStatus OutFidStatus, OutDirStatus;
52 struct AFSCallBack CallBack;
53 struct AFSVolSync tsync;
55 struct afs_fakestat_state fakestate;
59 AFS_STATCNT(afs_mkdir);
60 afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
61 ICL_TYPE_STRING, aname);
63 if ((code = afs_InitReq(&treq, acred)))
65 afs_InitFakeStat(&fakestate);
67 if (strlen(aname) > AFSNAMEMAX) {
72 if (!afs_ENameOK(aname)) {
79 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
82 code = afs_VerifyVCache(adp, &treq);
86 /** If the volume is read-only, return error without making an RPC to the
89 if (adp->f.states & CRO) {
94 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
95 /*printf("Network is down in afs_mkdir\n");*/
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);
106 if (!AFS_IS_DISCON_RW) {
108 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
110 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
114 RXAFS_MakeDir(tc->id,
115 (struct AFSFid *)&adp->f.fid.Fid,
118 (struct AFSFid *)&newFid.Fid,
125 CallBack.ExpirationTime += now;
126 /* DON'T forget to Set the callback value... */
130 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
135 ObtainWriteLock(&afs_xcbhash, 490);
136 afs_DequeueCallback(adp);
137 adp->f.states &= ~CStatd;
138 ReleaseWriteLock(&afs_xcbhash);
139 osi_dnlc_purgedp(adp);
141 ReleaseWriteLock(&adp->lock);
148 #if defined(AFS_DISCON_ENV)
151 /* We have the dir entry now, we can use it while disconnected. */
152 if (adp->mvid == NULL) {
153 /* If not mount point, generate a new fid. */
154 newFid.Cell = adp->f.fid.Cell;
155 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
156 afs_GenFakeFid(&newFid, VDIR, 1);
158 /* XXX: If mount point???*/
160 /* Operations with the actual dir's cache entry are further
161 * down, where the dir entry gets created.
164 } /* if (!AFS_IS_DISCON_RW) */
166 /* otherwise, we should see if we can make the change to the dir locally */
168 ObtainWriteLock(&tdc->lock, 632);
169 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
170 /* we can do it locally */
171 ObtainWriteLock(&afs_xdcache, 294);
172 code = afs_dir_Create(tdc, aname, &newFid.Fid);
173 ReleaseWriteLock(&afs_xdcache);
175 ZapDCE(tdc); /* surprise error -- use invalid value */
180 ReleaseWriteLock(&tdc->lock);
184 if (AFS_IS_DISCON_RW)
185 /* We will have to settle with the local link count. */
186 adp->f.m.LinkCount++;
188 adp->f.m.LinkCount = OutDirStatus.LinkCount;
189 newFid.Cell = adp->f.fid.Cell;
190 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
191 ReleaseWriteLock(&adp->lock);
192 if (AFS_IS_DISCON_RW) {
193 #if defined(AFS_DISCON_ENV)
194 /* When disconnected, we have to create the full dir here. */
196 /* Generate a new vcache and fill it. */
197 tvc = afs_NewVCache(&newFid, NULL);
205 ObtainWriteLock(&tvc->lock, 738);
206 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VDIR);
207 ReleaseWriteLock(&tvc->lock);
209 /* And now make an empty dir, containing . and .. : */
210 /* Get a new dcache for it first. */
211 new_dc = afs_GetDCache(tvc, (afs_size_t) 0, &treq, &offset, &len, 1);
213 printf("afs_mkdir: can't get new dcache for dir.\n");
218 ObtainWriteLock(&afs_xdcache, 739);
219 code = afs_dir_MakeDir(new_dc,
220 (afs_int32 *) &newFid.Fid,
221 (afs_int32 *) &adp->f.fid.Fid);
222 ReleaseWriteLock(&afs_xdcache);
224 printf("afs_mkdir: afs_dirMakeDir code = %u\n", code);
226 afs_PutDCache(new_dc);
228 ObtainWriteLock(&tvc->lock, 731);
229 /* Update length in the vcache. */
230 tvc->f.m.Length = new_dc->f.chunkBytes;
232 afs_DisconAddDirty(tvc, VDisconCreate, 1);
233 ReleaseWriteLock(&tvc->lock);
234 #endif /* #ifdef AFS_DISCON_ENV */
236 /* now we're done with parent dir, create the real dir's cache entry */
237 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
243 } /* if (AFS_DISCON_RW) */
248 afs_PutFakeStat(&fakestate);
249 code = afs_CheckCode(code, &treq, 26);
256 /* don't set CDirty in here because RPC is called synchronously */
257 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
258 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp,
259 struct AFS_UCRED *acred)
261 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
264 struct vrequest treq;
265 register struct dcache *tdc;
266 register struct vcache *tvc = NULL;
267 register afs_int32 code;
268 register struct afs_conn *tc;
269 afs_size_t offset, len;
270 struct AFSFetchStatus OutDirStatus;
271 struct AFSVolSync tsync;
272 struct afs_fakestat_state fakestate;
276 AFS_STATCNT(afs_rmdir);
278 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
279 ICL_TYPE_STRING, aname);
281 if ((code = afs_InitReq(&treq, acred)))
283 afs_InitFakeStat(&fakestate);
285 if (strlen(aname) > AFSNAMEMAX) {
292 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
296 code = afs_VerifyVCache(adp, &treq);
300 /** If the volume is read-only, return error without making an RPC to the
303 if (adp->f.states & CRO) {
308 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
309 /* Disconnected read only mode. */
314 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
315 ObtainWriteLock(&adp->lock, 154);
317 ObtainSharedLock(&tdc->lock, 633);
318 if (tdc && (adp->f.states & CForeign)) {
319 struct VenusFid unlinkFid;
321 unlinkFid.Fid.Vnode = 0;
322 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
324 afs_int32 cached = 0;
326 unlinkFid.Cell = adp->f.fid.Cell;
327 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
328 if (unlinkFid.Fid.Unique == 0) {
330 afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
332 ObtainReadLock(&afs_xvcache);
333 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
334 ReleaseReadLock(&afs_xvcache);
339 if (!AFS_IS_DISCON_RW) {
340 /* Not disconnected, can connect to server. */
342 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
344 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
347 RXAFS_RemoveDir(tc->id,
348 (struct AFSFid *)&adp->f.fid.Fid,
357 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
362 ReleaseSharedLock(&tdc->lock);
367 ObtainWriteLock(&afs_xcbhash, 491);
368 afs_DequeueCallback(adp);
369 adp->f.states &= ~CStatd;
370 ReleaseWriteLock(&afs_xcbhash);
371 osi_dnlc_purgedp(adp);
373 ReleaseWriteLock(&adp->lock);
377 /* here if rpc worked; update the in-core link count */
378 adp->f.m.LinkCount = OutDirStatus.LinkCount;
381 #if defined(AFS_DISCON_ENV)
385 ReleaseWriteLock(&adp->lock);
386 printf("afs_rmdir: No local dcache!\n");
392 /* Find the vcache. */
393 struct VenusFid tfid;
395 tfid.Cell = adp->f.fid.Cell;
396 tfid.Fid.Volume = adp->f.fid.Fid.Volume;
397 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
399 ObtainSharedLock(&afs_xvcache, 764);
400 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
401 ReleaseSharedLock(&afs_xvcache);
404 printf("afs_rmdir: Can't find dir's vcache!\n");
405 ReleaseSharedLock(&tdc->lock);
406 afs_PutDCache(tdc); /* drop ref count */
407 ReleaseWriteLock(&adp->lock);
413 if (tvc->f.m.LinkCount > 2) {
414 /* This dir contains more than . and .., thus it can't be
417 ReleaseSharedLock(&tdc->lock);
420 ReleaseWriteLock(&adp->lock);
425 /* Make a shadow copy of the parent dir (if not done already).
426 * If we were created locally, then we don't need to have a shadow
427 * directory (as there's no server state to remember)
429 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
430 afs_MakeShadowDir(adp, tdc);
433 adp->f.m.LinkCount--;
434 #endif /* #ifdef AFS_DISCON_ENV */
435 } /* if (!AFS_IS_DISCON_RW) */
438 UpgradeSToWLock(&tdc->lock, 634);
439 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
440 /* we can do it locally */
441 code = afs_dir_Delete(tdc, aname);
443 ZapDCE(tdc); /* surprise error -- invalid value */
448 ReleaseWriteLock(&tdc->lock);
449 afs_PutDCache(tdc); /* drop ref count */
453 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
455 osi_dnlc_remove(adp, aname, 0);
458 ObtainWriteLock(&tvc->lock, 155);
459 tvc->f.states &= ~CUnique; /* For the dfs xlator */
460 #if defined(AFS_DISCON_ENV)
461 if (AFS_IS_DISCON_RW) {
462 if (tvc->f.ddirty_flags & VDisconCreate) {
463 /* If we we were created whilst disconnected, removal doesn't
464 * need to get logged. Just go away gracefully */
465 afs_DisconRemoveDirty(tvc);
467 afs_DisconAddDirty(tvc, VDisconRemove, 1);
471 ReleaseWriteLock(&tvc->lock);
474 ReleaseWriteLock(&adp->lock);
475 /* don't worry about link count since dirs can not be hardlinked */
481 afs_PutFakeStat(&fakestate);
482 code = afs_CheckCode(code, &treq, 27);