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"
24 #include "afs/sysincludes.h" /* Standard vendor system headers */
25 #include "afsincludes.h" /* Afs-based standard headers */
26 #include "afs/afs_stats.h" /* statistics */
27 #include "afs/afs_cbqueue.h"
28 #include "afs/nfsclient.h"
29 #include "afs/afs_osidnlc.h"
31 extern afs_rwlock_t afs_xvcache;
33 /* don't set CDirty in here because RPC is called synchronously */
36 afs_mkdir(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
37 struct vcache **avcp, afs_ucred_t *acred)
39 struct vrequest *treq = NULL;
42 struct rx_connection *rxconn;
43 struct VenusFid newFid;
45 struct dcache *new_dc;
46 afs_size_t offset, len;
48 struct AFSStoreStatus InStatus;
49 struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
50 struct AFSCallBack CallBack;
51 struct AFSVolSync tsync;
53 struct afs_fakestat_state fakestate;
57 AFS_STATCNT(afs_mkdir);
58 afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
59 ICL_TYPE_STRING, aname);
61 OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
62 OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
63 memset(&InStatus, 0, sizeof(InStatus));
65 if ((code = afs_CreateReq(&treq, acred)))
67 afs_InitFakeStat(&fakestate);
69 if (strlen(aname) > AFSNAMEMAX) {
74 if (!afs_ENameOK(aname)) {
81 code = afs_EvalFakeStat(&adp, &fakestate, treq);
84 code = afs_VerifyVCache(adp, treq);
88 /** If the volume is read-only, return error without making an RPC to the
91 if (adp->f.states & CRO) {
96 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
97 /*printf("Network is down in afs_mkdir\n");*/
101 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
102 InStatus.ClientModTime = osi_Time();
103 InStatus.UnixModeBits = attrs->va_mode & 0xffff; /* only care about protection bits */
104 InStatus.Group = (afs_int32) afs_cr_gid(acred);
105 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);
106 ObtainWriteLock(&adp->lock, 153);
108 if (!AFS_IS_DISCON_RW) {
110 tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
112 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
116 RXAFS_MakeDir(rxconn,
117 (struct AFSFid *)&adp->f.fid.Fid,
120 (struct AFSFid *)&newFid.Fid,
127 CallBack.ExpirationTime += now;
128 /* DON'T forget to Set the callback value... */
132 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
137 afs_StaleVCache(adp);
139 ReleaseWriteLock(&adp->lock);
148 /* We have the dir entry now, we can use it while disconnected. */
149 if (adp->mvid.target_root == NULL) {
150 /* If not mount point, generate a new fid. */
151 newFid.Cell = adp->f.fid.Cell;
152 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
153 afs_GenFakeFid(&newFid, VDIR, 1);
155 /* XXX: If mount point???*/
157 /* Operations with the actual dir's cache entry are further
158 * down, where the dir entry gets created.
160 } /* if (!AFS_IS_DISCON_RW) */
162 /* otherwise, we should see if we can make the change to the dir locally */
164 ObtainWriteLock(&tdc->lock, 632);
165 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
166 /* we can do it locally */
167 ObtainWriteLock(&afs_xdcache, 294);
168 code = afs_dir_Create(tdc, aname, &newFid.Fid);
169 ReleaseWriteLock(&afs_xdcache);
171 ZapDCE(tdc); /* surprise error -- use invalid value */
176 ReleaseWriteLock(&tdc->lock);
180 if (AFS_IS_DISCON_RW)
181 /* We will have to settle with the local link count. */
182 adp->f.m.LinkCount++;
184 adp->f.m.LinkCount = OutDirStatus->LinkCount;
185 newFid.Cell = adp->f.fid.Cell;
186 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
187 ReleaseWriteLock(&adp->lock);
188 if (AFS_IS_DISCON_RW) {
189 /* When disconnected, we have to create the full dir here. */
191 /* Generate a new vcache and fill it. */
192 tvc = afs_NewVCache(&newFid, NULL);
200 ObtainWriteLock(&tvc->lock, 738);
201 afs_GenDisconStatus(adp, tvc, &newFid, attrs, treq, VDIR);
202 ReleaseWriteLock(&tvc->lock);
204 /* And now make an empty dir, containing . and .. : */
205 /* Get a new dcache for it first. */
206 new_dc = afs_GetDCache(tvc, (afs_size_t) 0, treq, &offset, &len, 1);
208 /* printf("afs_mkdir: can't get new dcache for dir.\n"); */
213 ObtainWriteLock(&afs_xdcache, 739);
214 code = afs_dir_MakeDir(new_dc,
215 (afs_int32 *) &newFid.Fid,
216 (afs_int32 *) &adp->f.fid.Fid);
217 ReleaseWriteLock(&afs_xdcache);
218 /* if (code) printf("afs_mkdir: afs_dirMakeDir code = %u\n", code); */
220 afs_PutDCache(new_dc);
222 ObtainWriteLock(&tvc->lock, 731);
223 /* Update length in the vcache. */
224 tvc->f.m.Length = new_dc->f.chunkBytes;
226 afs_DisconAddDirty(tvc, VDisconCreate, 1);
227 ReleaseWriteLock(&tvc->lock);
229 /* now we're done with parent dir, create the real dir's cache entry */
230 tvc = afs_GetVCache(&newFid, treq);
236 /* For some reason, we cannot fetch the vcache for our
237 * newly-created dir. */
240 } /* if (AFS_DISCON_RW) */
245 afs_PutFakeStat(&fakestate);
246 code = afs_CheckCode(code, treq, 26);
247 afs_DestroyReq(treq);
249 osi_FreeSmallSpace(OutFidStatus);
250 osi_FreeSmallSpace(OutDirStatus);
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,
261 afs_rmdir(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
264 struct vrequest *treq = NULL;
266 struct vcache *tvc = NULL;
269 afs_size_t offset, len;
270 struct AFSFetchStatus OutDirStatus;
271 struct AFSVolSync tsync;
272 struct afs_fakestat_state fakestate;
273 struct rx_connection *rxconn;
277 AFS_STATCNT(afs_rmdir);
279 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
280 ICL_TYPE_STRING, aname);
282 if ((code = afs_CreateReq(&treq, acred)))
284 afs_InitFakeStat(&fakestate);
286 if (strlen(aname) > AFSNAMEMAX) {
293 code = afs_EvalFakeStat(&adp, &fakestate, treq);
297 code = afs_VerifyVCache(adp, treq);
301 /** If the volume is read-only, return error without making an RPC to the
304 if (adp->f.states & CRO) {
309 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
310 /* Disconnected read only mode. */
315 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1); /* test for error below */
316 ObtainWriteLock(&adp->lock, 154);
318 ObtainSharedLock(&tdc->lock, 633);
319 if (tdc && (adp->f.states & CForeign)) {
320 struct VenusFid unlinkFid;
322 unlinkFid.Fid.Vnode = 0;
323 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
325 unlinkFid.Cell = adp->f.fid.Cell;
326 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
327 if (unlinkFid.Fid.Unique == 0) {
329 afs_LookupVCache(&unlinkFid, treq, adp, aname);
331 ObtainReadLock(&afs_xvcache);
332 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
333 ReleaseReadLock(&afs_xvcache);
338 if (!AFS_IS_DISCON_RW) {
339 /* Not disconnected, can connect to server. */
341 tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
343 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
346 RXAFS_RemoveDir(rxconn,
347 (struct AFSFid *)&adp->f.fid.Fid,
356 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
361 ReleaseSharedLock(&tdc->lock);
366 afs_StaleVCache(adp);
368 ReleaseWriteLock(&adp->lock);
372 /* here if rpc worked; update the in-core link count */
373 adp->f.m.LinkCount = OutDirStatus.LinkCount;
379 ReleaseWriteLock(&adp->lock);
380 /* printf("afs_rmdir: No local dcache!\n"); */
386 /* Find the vcache. */
387 struct VenusFid tfid;
389 tfid.Cell = adp->f.fid.Cell;
390 tfid.Fid.Volume = adp->f.fid.Fid.Volume;
391 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
393 ObtainSharedLock(&afs_xvcache, 764);
394 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
395 ReleaseSharedLock(&afs_xvcache);
398 /* printf("afs_rmdir: Can't find dir's vcache!\n"); */
399 ReleaseSharedLock(&tdc->lock);
400 afs_PutDCache(tdc); /* drop ref count */
401 ReleaseWriteLock(&adp->lock);
407 if (tvc->f.m.LinkCount > 2) {
408 /* This dir contains more than . and .., thus it can't be
411 ReleaseSharedLock(&tdc->lock);
414 ReleaseWriteLock(&adp->lock);
419 /* Make a shadow copy of the parent dir (if not done already).
420 * If we were created locally, then we don't need to have a shadow
421 * directory (as there's no server state to remember)
423 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
424 afs_MakeShadowDir(adp, tdc);
427 adp->f.m.LinkCount--;
428 } /* if (!AFS_IS_DISCON_RW) */
431 UpgradeSToWLock(&tdc->lock, 634);
432 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
433 /* we can do it locally */
434 code = afs_dir_Delete(tdc, aname);
436 ZapDCE(tdc); /* surprise error -- invalid value */
441 ReleaseWriteLock(&tdc->lock);
442 afs_PutDCache(tdc); /* drop ref count */
446 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
448 osi_dnlc_remove(adp, aname, 0);
451 ObtainWriteLock(&tvc->lock, 155);
452 tvc->f.states &= ~CUnique; /* For the dfs xlator */
453 if (AFS_IS_DISCON_RW) {
454 if (tvc->f.ddirty_flags & VDisconCreate) {
455 /* If we we were created whilst disconnected, removal doesn't
456 * need to get logged. Just go away gracefully */
457 afs_DisconRemoveDirty(tvc);
459 afs_DisconAddDirty(tvc, VDisconRemove, 1);
462 ReleaseWriteLock(&tvc->lock);
465 ReleaseWriteLock(&adp->lock);
466 /* don't worry about link count since dirs can not be hardlinked */
471 afs_PutFakeStat(&fakestate);
472 code = afs_CheckCode(code, treq, 27);
473 afs_DestroyReq(treq);