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 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->states & CRO) {
94 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW)
95 /*printf("Network is down in afs_mkdir\n");*/
97 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
98 InStatus.ClientModTime = osi_Time();
99 InStatus.UnixModeBits = attrs->va_mode & 0xffff; /* only care about protection bits */
100 InStatus.Group = (afs_int32) acred->cr_gid;
101 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
102 ObtainWriteLock(&adp->lock, 153);
104 if (!AFS_IS_DISCON_RW) {
107 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
109 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
113 RXAFS_MakeDir(tc->id,
114 (struct AFSFid *)&adp->fid.Fid,
117 (struct AFSFid *)&newFid.Fid,
124 CallBack.ExpirationTime += now;
125 /* DON'T forget to Set the callback value... */
129 (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
134 ObtainWriteLock(&afs_xcbhash, 490);
135 afs_DequeueCallback(adp);
136 adp->states &= ~CStatd;
137 ReleaseWriteLock(&afs_xcbhash);
138 osi_dnlc_purgedp(adp);
140 ReleaseWriteLock(&adp->lock);
147 #if defined(AFS_DISCON_ENV)
150 /* We have the dir entry now, we can use it while disconnected. */
151 if (adp->mvid == NULL) {
152 /* If not mount point, generate a new fid. */
153 newFid.Cell = adp->fid.Cell;
154 newFid.Fid.Volume = adp->fid.Fid.Volume;
155 afs_GenFakeFid(&newFid, VDIR);
157 /* XXX: If mount point???*/
159 /* Operations with the actual dir's cache entry are further
160 * down, where the dir entry gets created.
163 } /* if (!AFS_IS_DISCON_RW) */
165 /* otherwise, we should see if we can make the change to the dir locally */
167 ObtainWriteLock(&tdc->lock, 632);
168 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
169 /* we can do it locally */
170 ObtainWriteLock(&afs_xdcache, 294);
171 code = afs_dir_Create(tdc, aname, &newFid.Fid);
172 ReleaseWriteLock(&afs_xdcache);
174 ZapDCE(tdc); /* surprise error -- use invalid value */
179 ReleaseWriteLock(&tdc->lock);
183 if (AFS_IS_DISCON_RW)
184 /* We will have to settle with the local link count. */
187 adp->m.LinkCount = OutDirStatus.LinkCount;
188 newFid.Cell = adp->fid.Cell;
189 newFid.Fid.Volume = adp->fid.Fid.Volume;
190 ReleaseWriteLock(&adp->lock);
191 if (AFS_IS_DISCON_RW) {
192 #if defined(AFS_DISCON_ENV)
193 /* When disconnected, we have to create the full dir here. */
195 /* Generate a new vcache and fill it. */
196 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->fid.Fid);
222 ReleaseWriteLock(&afs_xdcache);
224 printf("afs_mkdir: afs_dirMakeDir code = %u\n", code);
226 /* Add to dirty list. */
227 if (!tvc->ddirty_flags ||
228 (tvc->ddirty_flags == VDisconShadowed)) {
230 /* Put it in the list only if it's fresh. */
231 ObtainWriteLock(&afs_DDirtyVCListLock, 730);
232 AFS_DISCON_ADD_DIRTY(tvc);
233 ReleaseWriteLock(&afs_DDirtyVCListLock);
236 ObtainWriteLock(&tvc->lock, 731);
237 /* Update length in the vcache. */
238 tvc->m.Length = new_dc->f.chunkBytes;
239 /* Set create flag. */
240 tvc->ddirty_flags |= VDisconCreate;
241 ReleaseWriteLock(&tvc->lock);
242 #endif /* #ifdef AFS_DISCON_ENV */
244 /* now we're done with parent dir, create the real dir's cache entry */
245 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
251 } /* if (AFS_DISCON_RW) */
256 afs_PutFakeStat(&fakestate);
257 code = afs_CheckCode(code, &treq, 26);
264 /* don't set CDirty in here because RPC is called synchronously */
265 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
266 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp,
267 struct AFS_UCRED *acred)
269 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
272 struct vrequest treq;
273 register struct dcache *tdc;
274 register struct vcache *tvc = NULL;
275 register afs_int32 code;
276 register struct conn *tc;
277 afs_size_t offset, len;
278 struct AFSFetchStatus OutDirStatus;
279 struct AFSVolSync tsync;
280 struct afs_fakestat_state fakestate;
284 AFS_STATCNT(afs_rmdir);
286 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
287 ICL_TYPE_STRING, aname);
289 if ((code = afs_InitReq(&treq, acred)))
291 afs_InitFakeStat(&fakestate);
293 if (strlen(aname) > AFSNAMEMAX) {
300 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
304 code = afs_VerifyVCache(adp, &treq);
308 /** If the volume is read-only, return error without making an RPC to the
311 if (adp->states & CRO) {
316 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
317 /* Disconnected read only mode. */
322 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
323 ObtainWriteLock(&adp->lock, 154);
325 ObtainSharedLock(&tdc->lock, 633);
326 if (tdc && (adp->states & CForeign)) {
327 struct VenusFid unlinkFid;
329 unlinkFid.Fid.Vnode = 0;
330 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
332 afs_int32 cached = 0;
334 unlinkFid.Cell = adp->fid.Cell;
335 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
336 if (unlinkFid.Fid.Unique == 0) {
338 afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
340 ObtainReadLock(&afs_xvcache);
341 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
342 ReleaseReadLock(&afs_xvcache);
347 if (!AFS_IS_DISCON_RW) {
348 /* Not disconnected, can connect to server. */
350 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
352 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
355 RXAFS_RemoveDir(tc->id,
356 (struct AFSFid *)&adp->fid.Fid,
365 (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
370 ReleaseSharedLock(&tdc->lock);
375 ObtainWriteLock(&afs_xcbhash, 491);
376 afs_DequeueCallback(adp);
377 adp->states &= ~CStatd;
378 ReleaseWriteLock(&afs_xcbhash);
379 osi_dnlc_purgedp(adp);
381 ReleaseWriteLock(&adp->lock);
385 /* here if rpc worked; update the in-core link count */
386 adp->m.LinkCount = OutDirStatus.LinkCount;
389 #if defined(AFS_DISCON_ENV)
393 /* Find the vcache. */
394 struct VenusFid tfid;
396 tfid.Cell = adp->fid.Cell;
397 tfid.Fid.Volume = adp->fid.Fid.Volume;
398 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
400 ObtainSharedLock(&afs_xvcache, 764);
401 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
402 ReleaseSharedLock(&afs_xvcache);
405 printf("afs_rmdir: Can't find dir's vcache!\n");
406 ReleaseSharedLock(&tdc->lock);
407 afs_PutDCache(tdc); /* drop ref count */
408 ReleaseWriteLock(&adp->lock);
413 if (tvc->m.LinkCount > 2) {
414 /* This dir contains more than . and .., thus it can't be
417 ReleaseSharedLock(&tdc->lock);
422 /* Make a shadow copy of the parent dir (if not done already).
423 * There's no need to make a shadow copy of the deleted directory
424 * because a dir must be empty in order to be rmdir'ed.
425 * If the deleted dir has no shadow, it means that it was empty.
427 if (!(adp->ddirty_flags & VDisconShadowed)) {
428 /* If tdc available, then it is locked.
429 * afs_MakeShadowDir unlocks it.
432 ReleaseSharedLock(&tdc->lock);
433 afs_MakeShadowDir(adp);
435 ObtainSharedLock(&tdc->lock, 732);
438 if (!tvc->ddirty_flags ||
439 (tvc->ddirty_flags == VDisconShadowed)) {
440 /* Put it in the list only if it's fresh or has only been shadowed. */
441 ObtainWriteLock(&afs_DDirtyVCListLock, 728);
442 AFS_DISCON_ADD_DIRTY(tvc);
443 ReleaseWriteLock(&afs_DDirtyVCListLock);
446 /* Now add the vcache to the dirty list. */
447 ObtainWriteLock(&tvc->lock, 727);
448 tvc->ddirty_flags |= VDisconRemove;
449 ReleaseWriteLock(&tvc->lock);
452 #endif /* #ifdef AFS_DISCON_ENV */
453 } /* if (!AFS_IS_DISCON_RW) */
456 UpgradeSToWLock(&tdc->lock, 634);
457 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
458 /* we can do it locally */
459 code = afs_dir_Delete(tdc, aname);
461 ZapDCE(tdc); /* surprise error -- invalid value */
466 ReleaseWriteLock(&tdc->lock);
467 afs_PutDCache(tdc); /* drop ref count */
472 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
474 osi_dnlc_remove(adp, aname, 0);
477 ObtainWriteLock(&tvc->lock, 155);
478 tvc->states &= ~CUnique; /* For the dfs xlator */
479 ReleaseWriteLock(&tvc->lock);
480 /* If disconnected, keep this vcache around for resync. */
481 if (!AFS_IS_DISCON_RW)
484 ReleaseWriteLock(&adp->lock);
485 /* don't worry about link count since dirs can not be hardlinked */
491 afs_PutFakeStat(&fakestate);
492 code = afs_CheckCode(code, &treq, 27);