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;
32 extern afs_rwlock_t afs_xcbhash;
34 /* don't set CDirty in here because RPC is called synchronously */
37 afs_mkdir(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
38 struct vcache **avcp, afs_ucred_t *acred)
40 struct vrequest *treq = NULL;
43 struct rx_connection *rxconn;
44 struct VenusFid newFid;
46 struct dcache *new_dc;
47 afs_size_t offset, len;
49 struct AFSStoreStatus InStatus;
50 struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
51 struct AFSCallBack CallBack;
52 struct AFSVolSync tsync;
54 struct afs_fakestat_state fakestate;
58 AFS_STATCNT(afs_mkdir);
59 afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
60 ICL_TYPE_STRING, aname);
62 OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
63 OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
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 ObtainWriteLock(&afs_xcbhash, 490);
138 afs_DequeueCallback(adp);
139 adp->f.states &= ~CStatd;
140 ReleaseWriteLock(&afs_xcbhash);
141 osi_dnlc_purgedp(adp);
143 ReleaseWriteLock(&adp->lock);
152 /* We have the dir entry now, we can use it while disconnected. */
153 if (adp->mvid.target_root == NULL) {
154 /* If not mount point, generate a new fid. */
155 newFid.Cell = adp->f.fid.Cell;
156 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
157 afs_GenFakeFid(&newFid, VDIR, 1);
159 /* XXX: If mount point???*/
161 /* Operations with the actual dir's cache entry are further
162 * 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 /* When disconnected, we have to create the full dir here. */
195 /* Generate a new vcache and fill it. */
196 tvc = afs_NewVCache(&newFid, NULL);
204 ObtainWriteLock(&tvc->lock, 738);
205 afs_GenDisconStatus(adp, tvc, &newFid, attrs, treq, VDIR);
206 ReleaseWriteLock(&tvc->lock);
208 /* And now make an empty dir, containing . and .. : */
209 /* Get a new dcache for it first. */
210 new_dc = afs_GetDCache(tvc, (afs_size_t) 0, treq, &offset, &len, 1);
212 /* printf("afs_mkdir: can't get new dcache for dir.\n"); */
217 ObtainWriteLock(&afs_xdcache, 739);
218 code = afs_dir_MakeDir(new_dc,
219 (afs_int32 *) &newFid.Fid,
220 (afs_int32 *) &adp->f.fid.Fid);
221 ReleaseWriteLock(&afs_xdcache);
222 /* if (code) printf("afs_mkdir: afs_dirMakeDir code = %u\n", code); */
224 afs_PutDCache(new_dc);
226 ObtainWriteLock(&tvc->lock, 731);
227 /* Update length in the vcache. */
228 tvc->f.m.Length = new_dc->f.chunkBytes;
230 afs_DisconAddDirty(tvc, VDisconCreate, 1);
231 ReleaseWriteLock(&tvc->lock);
233 /* now we're done with parent dir, create the real dir's cache entry */
234 tvc = afs_GetVCache(&newFid, treq, NULL, NULL);
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 afs_int32 cached = 0;
327 unlinkFid.Cell = adp->f.fid.Cell;
328 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
329 if (unlinkFid.Fid.Unique == 0) {
331 afs_LookupVCache(&unlinkFid, treq, &cached, adp, aname);
333 ObtainReadLock(&afs_xvcache);
334 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
335 ReleaseReadLock(&afs_xvcache);
340 if (!AFS_IS_DISCON_RW) {
341 /* Not disconnected, can connect to server. */
343 tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
345 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
348 RXAFS_RemoveDir(rxconn,
349 (struct AFSFid *)&adp->f.fid.Fid,
358 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
363 ReleaseSharedLock(&tdc->lock);
368 ObtainWriteLock(&afs_xcbhash, 491);
369 afs_DequeueCallback(adp);
370 adp->f.states &= ~CStatd;
371 ReleaseWriteLock(&afs_xcbhash);
372 osi_dnlc_purgedp(adp);
374 ReleaseWriteLock(&adp->lock);
378 /* here if rpc worked; update the in-core link count */
379 adp->f.m.LinkCount = OutDirStatus.LinkCount;
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 } /* if (!AFS_IS_DISCON_RW) */
437 UpgradeSToWLock(&tdc->lock, 634);
438 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
439 /* we can do it locally */
440 code = afs_dir_Delete(tdc, aname);
442 ZapDCE(tdc); /* surprise error -- invalid value */
447 ReleaseWriteLock(&tdc->lock);
448 afs_PutDCache(tdc); /* drop ref count */
452 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
454 osi_dnlc_remove(adp, aname, 0);
457 ObtainWriteLock(&tvc->lock, 155);
458 tvc->f.states &= ~CUnique; /* For the dfs xlator */
459 if (AFS_IS_DISCON_RW) {
460 if (tvc->f.ddirty_flags & VDisconCreate) {
461 /* If we we were created whilst disconnected, removal doesn't
462 * need to get logged. Just go away gracefully */
463 afs_DisconRemoveDirty(tvc);
465 afs_DisconAddDirty(tvc, VDisconRemove, 1);
468 ReleaseWriteLock(&tvc->lock);
471 ReleaseWriteLock(&adp->lock);
472 /* don't worry about link count since dirs can not be hardlinked */
477 afs_PutFakeStat(&fakestate);
478 code = afs_CheckCode(code, treq, 27);
479 afs_DestroyReq(treq);