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 register struct vcache **avcp, struct AFS_UCRED *acred)
41 register afs_int32 code;
42 register struct afs_conn *tc;
43 struct VenusFid newFid;
44 register struct dcache *tdc;
45 struct dcache *new_dc;
46 afs_size_t offset, len;
47 register struct vcache *tvc;
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 if ((code = afs_InitReq(&treq, acred)))
63 afs_InitFakeStat(&fakestate);
65 if (strlen(aname) > AFSNAMEMAX) {
70 if (!afs_ENameOK(aname)) {
77 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
80 code = afs_VerifyVCache(adp, &treq);
84 /** If the volume is read-only, return error without making an RPC to the
87 if (adp->f.states & CRO) {
92 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
93 /*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->f.fid, &treq, SHARED_LOCK);
108 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
112 RXAFS_MakeDir(tc->id,
113 (struct AFSFid *)&adp->f.fid.Fid,
116 (struct AFSFid *)&newFid.Fid,
123 CallBack.ExpirationTime += now;
124 /* DON'T forget to Set the callback value... */
128 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
133 ObtainWriteLock(&afs_xcbhash, 490);
134 afs_DequeueCallback(adp);
135 adp->f.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->f.fid.Cell;
153 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
154 afs_GenFakeFid(&newFid, VDIR, 1);
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. */
184 adp->f.m.LinkCount++;
186 adp->f.m.LinkCount = OutDirStatus.LinkCount;
187 newFid.Cell = adp->f.fid.Cell;
188 newFid.Fid.Volume = adp->f.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->f.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 /* Update length in the vcache. */
228 tvc->f.m.Length = new_dc->f.chunkBytes;
230 afs_DisconAddDirty(tvc, VDisconCreate, 1);
231 ReleaseWriteLock(&tvc->lock);
232 #endif /* #ifdef AFS_DISCON_ENV */
234 /* now we're done with parent dir, create the real dir's cache entry */
235 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
241 } /* if (AFS_DISCON_RW) */
246 afs_PutFakeStat(&fakestate);
247 code = afs_CheckCode(code, &treq, 26);
254 /* don't set CDirty in here because RPC is called synchronously */
255 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
256 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp,
257 struct AFS_UCRED *acred)
259 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
262 struct vrequest treq;
263 register struct dcache *tdc;
264 register struct vcache *tvc = NULL;
265 register afs_int32 code;
266 register struct afs_conn *tc;
267 afs_size_t offset, len;
268 struct AFSFetchStatus OutDirStatus;
269 struct AFSVolSync tsync;
270 struct afs_fakestat_state fakestate;
274 AFS_STATCNT(afs_rmdir);
276 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
277 ICL_TYPE_STRING, aname);
279 if ((code = afs_InitReq(&treq, acred)))
281 afs_InitFakeStat(&fakestate);
283 if (strlen(aname) > AFSNAMEMAX) {
290 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
294 code = afs_VerifyVCache(adp, &treq);
298 /** If the volume is read-only, return error without making an RPC to the
301 if (adp->f.states & CRO) {
306 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
307 /* Disconnected read only mode. */
312 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
313 ObtainWriteLock(&adp->lock, 154);
315 ObtainSharedLock(&tdc->lock, 633);
316 if (tdc && (adp->f.states & CForeign)) {
317 struct VenusFid unlinkFid;
319 unlinkFid.Fid.Vnode = 0;
320 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
322 afs_int32 cached = 0;
324 unlinkFid.Cell = adp->f.fid.Cell;
325 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
326 if (unlinkFid.Fid.Unique == 0) {
328 afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
330 ObtainReadLock(&afs_xvcache);
331 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
332 ReleaseReadLock(&afs_xvcache);
337 if (!AFS_IS_DISCON_RW) {
338 /* Not disconnected, can connect to server. */
340 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
342 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
345 RXAFS_RemoveDir(tc->id,
346 (struct AFSFid *)&adp->f.fid.Fid,
355 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
360 ReleaseSharedLock(&tdc->lock);
365 ObtainWriteLock(&afs_xcbhash, 491);
366 afs_DequeueCallback(adp);
367 adp->f.states &= ~CStatd;
368 ReleaseWriteLock(&afs_xcbhash);
369 osi_dnlc_purgedp(adp);
371 ReleaseWriteLock(&adp->lock);
375 /* here if rpc worked; update the in-core link count */
376 adp->f.m.LinkCount = OutDirStatus.LinkCount;
379 #if defined(AFS_DISCON_ENV)
383 ReleaseWriteLock(&adp->lock);
384 printf("afs_rmdir: No local dcache!\n");
390 /* Find the vcache. */
391 struct VenusFid tfid;
393 tfid.Cell = adp->f.fid.Cell;
394 tfid.Fid.Volume = adp->f.fid.Fid.Volume;
395 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
397 ObtainSharedLock(&afs_xvcache, 764);
398 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
399 ReleaseSharedLock(&afs_xvcache);
402 printf("afs_rmdir: Can't find dir's vcache!\n");
403 ReleaseSharedLock(&tdc->lock);
404 afs_PutDCache(tdc); /* drop ref count */
405 ReleaseWriteLock(&adp->lock);
411 if (tvc->f.m.LinkCount > 2) {
412 /* This dir contains more than . and .., thus it can't be
415 ReleaseSharedLock(&tdc->lock);
418 ReleaseWriteLock(&adp->lock);
423 /* Make a shadow copy of the parent dir (if not done already).
424 * If we were created locally, then we don't need to have a shadow
425 * directory (as there's no server state to remember)
427 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
428 afs_MakeShadowDir(adp, tdc);
431 adp->f.m.LinkCount--;
432 #endif /* #ifdef AFS_DISCON_ENV */
433 } /* if (!AFS_IS_DISCON_RW) */
436 UpgradeSToWLock(&tdc->lock, 634);
437 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
438 /* we can do it locally */
439 code = afs_dir_Delete(tdc, aname);
441 ZapDCE(tdc); /* surprise error -- invalid value */
446 ReleaseWriteLock(&tdc->lock);
447 afs_PutDCache(tdc); /* drop ref count */
451 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
453 osi_dnlc_remove(adp, aname, 0);
456 ObtainWriteLock(&tvc->lock, 155);
457 tvc->f.states &= ~CUnique; /* For the dfs xlator */
458 #if defined(AFS_DISCON_ENV)
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);
469 ReleaseWriteLock(&tvc->lock);
472 ReleaseWriteLock(&adp->lock);
473 /* don't worry about link count since dirs can not be hardlinked */
479 afs_PutFakeStat(&fakestate);
480 code = afs_CheckCode(code, &treq, 27);