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->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) {
106 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
108 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
112 RXAFS_MakeDir(tc->id,
113 (struct AFSFid *)&adp->fid.Fid,
116 (struct AFSFid *)&newFid.Fid,
123 CallBack.ExpirationTime += now;
124 /* DON'T forget to Set the callback value... */
128 (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
133 ObtainWriteLock(&afs_xcbhash, 490);
134 afs_DequeueCallback(adp);
135 adp->states &= ~CStatd;
136 ReleaseWriteLock(&afs_xcbhash);
137 osi_dnlc_purgedp(adp);
139 ReleaseWriteLock(&adp->lock);
146 #if defined(AFS_DISCON_ENV)
149 /* We have the dir entry now, we can use it while disconnected. */
150 if (adp->mvid == NULL) {
151 /* If not mount point, generate a new fid. */
152 newFid.Cell = adp->fid.Cell;
153 newFid.Fid.Volume = adp->fid.Fid.Volume;
154 afs_GenFakeFid(&newFid, VDIR);
156 /* XXX: If mount point???*/
158 /* Operations with the actual dir's cache entry are further
159 * down, where the dir entry gets created.
162 } /* if (!AFS_IS_DISCON_RW) */
164 /* otherwise, we should see if we can make the change to the dir locally */
166 ObtainWriteLock(&tdc->lock, 632);
167 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
168 /* we can do it locally */
169 ObtainWriteLock(&afs_xdcache, 294);
170 code = afs_dir_Create(tdc, aname, &newFid.Fid);
171 ReleaseWriteLock(&afs_xdcache);
173 ZapDCE(tdc); /* surprise error -- use invalid value */
178 ReleaseWriteLock(&tdc->lock);
182 if (AFS_IS_DISCON_RW)
183 /* We will have to settle with the local link count. */
186 adp->m.LinkCount = OutDirStatus.LinkCount;
187 newFid.Cell = adp->fid.Cell;
188 newFid.Fid.Volume = adp->fid.Fid.Volume;
189 ReleaseWriteLock(&adp->lock);
190 if (AFS_IS_DISCON_RW) {
191 #if defined(AFS_DISCON_ENV)
192 /* When disconnected, we have to create the full dir here. */
194 /* Generate a new vcache and fill it. */
195 tvc = afs_NewVCache(&newFid, NULL);
203 ObtainWriteLock(&tvc->lock, 738);
204 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VDIR);
205 ReleaseWriteLock(&tvc->lock);
207 /* And now make an empty dir, containing . and .. : */
208 /* Get a new dcache for it first. */
209 new_dc = afs_GetDCache(tvc, (afs_size_t) 0, &treq, &offset, &len, 1);
211 printf("afs_mkdir: can't get new dcache for dir.\n");
216 ObtainWriteLock(&afs_xdcache, 739);
217 code = afs_dir_MakeDir(new_dc,
218 (afs_int32 *) &newFid.Fid,
219 (afs_int32) &adp->fid.Fid);
220 ReleaseWriteLock(&afs_xdcache);
222 printf("afs_mkdir: afs_dirMakeDir code = %u\n", code);
224 afs_PutDCache(new_dc);
226 ObtainWriteLock(&tvc->lock, 731);
227 afs_DisconAddDirty(tvc, VDisconCreate, 1);
228 /* Update length in the vcache. */
229 tvc->m.Length = new_dc->f.chunkBytes;
230 ReleaseWriteLock(&tvc->lock);
231 #endif /* #ifdef AFS_DISCON_ENV */
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);
253 /* don't set CDirty in here because RPC is called synchronously */
254 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
255 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp,
256 struct AFS_UCRED *acred)
258 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
261 struct vrequest treq;
262 register struct dcache *tdc;
263 register struct vcache *tvc = NULL;
264 register afs_int32 code;
265 register struct afs_conn *tc;
266 afs_size_t offset, len;
267 struct AFSFetchStatus OutDirStatus;
268 struct AFSVolSync tsync;
269 struct afs_fakestat_state fakestate;
273 AFS_STATCNT(afs_rmdir);
275 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
276 ICL_TYPE_STRING, aname);
278 if ((code = afs_InitReq(&treq, acred)))
280 afs_InitFakeStat(&fakestate);
282 if (strlen(aname) > AFSNAMEMAX) {
289 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
293 code = afs_VerifyVCache(adp, &treq);
297 /** If the volume is read-only, return error without making an RPC to the
300 if (adp->states & CRO) {
305 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
306 /* Disconnected read only mode. */
311 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
312 ObtainWriteLock(&adp->lock, 154);
314 ObtainSharedLock(&tdc->lock, 633);
315 if (tdc && (adp->states & CForeign)) {
316 struct VenusFid unlinkFid;
318 unlinkFid.Fid.Vnode = 0;
319 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
321 afs_int32 cached = 0;
323 unlinkFid.Cell = adp->fid.Cell;
324 unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
325 if (unlinkFid.Fid.Unique == 0) {
327 afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
329 ObtainReadLock(&afs_xvcache);
330 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
331 ReleaseReadLock(&afs_xvcache);
336 if (!AFS_IS_DISCON_RW) {
337 /* Not disconnected, can connect to server. */
339 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
341 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
344 RXAFS_RemoveDir(tc->id,
345 (struct AFSFid *)&adp->fid.Fid,
354 (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
359 ReleaseSharedLock(&tdc->lock);
364 ObtainWriteLock(&afs_xcbhash, 491);
365 afs_DequeueCallback(adp);
366 adp->states &= ~CStatd;
367 ReleaseWriteLock(&afs_xcbhash);
368 osi_dnlc_purgedp(adp);
370 ReleaseWriteLock(&adp->lock);
374 /* here if rpc worked; update the in-core link count */
375 adp->m.LinkCount = OutDirStatus.LinkCount;
378 #if defined(AFS_DISCON_ENV)
382 ReleaseWriteLock(&adp->lock);
383 printf("afs_rmdir: No local dcache!\n");
389 /* Find the vcache. */
390 struct VenusFid tfid;
392 tfid.Cell = adp->fid.Cell;
393 tfid.Fid.Volume = adp->fid.Fid.Volume;
394 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
396 ObtainSharedLock(&afs_xvcache, 764);
397 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
398 ReleaseSharedLock(&afs_xvcache);
401 printf("afs_rmdir: Can't find dir's vcache!\n");
402 ReleaseSharedLock(&tdc->lock);
403 afs_PutDCache(tdc); /* drop ref count */
404 ReleaseWriteLock(&adp->lock);
410 if (tvc->m.LinkCount > 2) {
411 /* This dir contains more than . and .., thus it can't be
414 ReleaseSharedLock(&tdc->lock);
417 ReleaseWriteLock(&adp->lock);
422 /* Make a shadow copy of the parent dir (if not done already).
423 * If we were created locally, then we don't need to have a shadow
424 * directory (as there's no server state to remember)
426 if (!adp->shVnode && !(adp->ddirty_flags & VDisconCreate)) {
427 afs_MakeShadowDir(adp, tdc);
431 #endif /* #ifdef AFS_DISCON_ENV */
432 } /* if (!AFS_IS_DISCON_RW) */
435 UpgradeSToWLock(&tdc->lock, 634);
436 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
437 /* we can do it locally */
438 code = afs_dir_Delete(tdc, aname);
440 ZapDCE(tdc); /* surprise error -- invalid value */
445 ReleaseWriteLock(&tdc->lock);
446 afs_PutDCache(tdc); /* drop ref count */
450 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
452 osi_dnlc_remove(adp, aname, 0);
455 ObtainWriteLock(&tvc->lock, 155);
456 tvc->states &= ~CUnique; /* For the dfs xlator */
458 if (AFS_IS_DISCON_RW) {
459 if (tvc->ddirty_flags & VDisconCreate) {
460 /* If we we were created whilst disconnected, removal doesn't
461 * need to get logged. Just go away gracefully */
462 afs_DisconRemoveDirty(tvc);
464 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 */
478 afs_PutFakeStat(&fakestate);
479 code = afs_CheckCode(code, &treq, 27);