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, afs_ucred_t *acred)
41 register afs_int32 code;
42 register struct afs_conn *tc;
43 struct VenusFid newFid;
44 register struct dcache *tdc;
46 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->f.states & CRO) {
94 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
95 /*printf("Network is down in afs_mkdir\n");*/
99 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
100 InStatus.ClientModTime = osi_Time();
101 InStatus.UnixModeBits = attrs->va_mode & 0xffff; /* only care about protection bits */
102 InStatus.Group = (afs_int32) afs_cr_gid(acred);
103 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
104 ObtainWriteLock(&adp->lock, 153);
106 if (!AFS_IS_DISCON_RW) {
108 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
110 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
114 RXAFS_MakeDir(tc->id,
115 (struct AFSFid *)&adp->f.fid.Fid,
118 (struct AFSFid *)&newFid.Fid,
125 CallBack.ExpirationTime += now;
126 /* DON'T forget to Set the callback value... */
130 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
135 ObtainWriteLock(&afs_xcbhash, 490);
136 afs_DequeueCallback(adp);
137 adp->f.states &= ~CStatd;
138 ReleaseWriteLock(&afs_xcbhash);
139 osi_dnlc_purgedp(adp);
141 ReleaseWriteLock(&adp->lock);
148 #if defined(AFS_DISCON_ENV)
151 /* We have the dir entry now, we can use it while disconnected. */
152 if (adp->mvid == NULL) {
153 /* If not mount point, generate a new fid. */
154 newFid.Cell = adp->f.fid.Cell;
155 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
156 afs_GenFakeFid(&newFid, VDIR, 1);
158 /* XXX: If mount point???*/
160 /* Operations with the actual dir's cache entry are further
161 * 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 #if defined(AFS_DISCON_ENV)
194 /* When disconnected, we have to create the full dir here. */
196 /* Generate a new vcache and fill it. */
197 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->f.fid.Fid);
222 ReleaseWriteLock(&afs_xdcache);
223 /* if (code) printf("afs_mkdir: afs_dirMakeDir code = %u\n", code); */
225 afs_PutDCache(new_dc);
227 ObtainWriteLock(&tvc->lock, 731);
228 /* Update length in the vcache. */
229 tvc->f.m.Length = new_dc->f.chunkBytes;
231 afs_DisconAddDirty(tvc, VDisconCreate, 1);
232 ReleaseWriteLock(&tvc->lock);
233 #endif /* #ifdef AFS_DISCON_ENV */
235 /* now we're done with parent dir, create the real dir's cache entry */
236 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
242 } /* if (AFS_DISCON_RW) */
247 afs_PutFakeStat(&fakestate);
248 code = afs_CheckCode(code, &treq, 26);
255 /* don't set CDirty in here because RPC is called synchronously */
256 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
257 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp,
260 afs_rmdir(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
263 struct vrequest treq;
264 register struct dcache *tdc;
265 register struct vcache *tvc = NULL;
266 register afs_int32 code;
267 register struct afs_conn *tc;
268 afs_size_t offset, len;
269 struct AFSFetchStatus OutDirStatus;
270 struct AFSVolSync tsync;
271 struct afs_fakestat_state fakestate;
275 AFS_STATCNT(afs_rmdir);
277 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
278 ICL_TYPE_STRING, aname);
280 if ((code = afs_InitReq(&treq, acred)))
282 afs_InitFakeStat(&fakestate);
284 if (strlen(aname) > AFSNAMEMAX) {
291 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
295 code = afs_VerifyVCache(adp, &treq);
299 /** If the volume is read-only, return error without making an RPC to the
302 if (adp->f.states & CRO) {
307 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
308 /* Disconnected read only mode. */
313 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
314 ObtainWriteLock(&adp->lock, 154);
316 ObtainSharedLock(&tdc->lock, 633);
317 if (tdc && (adp->f.states & CForeign)) {
318 struct VenusFid unlinkFid;
320 unlinkFid.Fid.Vnode = 0;
321 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
323 afs_int32 cached = 0;
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, &cached, 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);
343 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
346 RXAFS_RemoveDir(tc->id,
347 (struct AFSFid *)&adp->f.fid.Fid,
356 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
361 ReleaseSharedLock(&tdc->lock);
366 ObtainWriteLock(&afs_xcbhash, 491);
367 afs_DequeueCallback(adp);
368 adp->f.states &= ~CStatd;
369 ReleaseWriteLock(&afs_xcbhash);
370 osi_dnlc_purgedp(adp);
372 ReleaseWriteLock(&adp->lock);
376 /* here if rpc worked; update the in-core link count */
377 adp->f.m.LinkCount = OutDirStatus.LinkCount;
380 #if defined(AFS_DISCON_ENV)
384 ReleaseWriteLock(&adp->lock);
385 /* printf("afs_rmdir: No local dcache!\n"); */
391 /* Find the vcache. */
392 struct VenusFid tfid;
394 tfid.Cell = adp->f.fid.Cell;
395 tfid.Fid.Volume = adp->f.fid.Fid.Volume;
396 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
398 ObtainSharedLock(&afs_xvcache, 764);
399 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
400 ReleaseSharedLock(&afs_xvcache);
403 /* printf("afs_rmdir: Can't find dir's vcache!\n"); */
404 ReleaseSharedLock(&tdc->lock);
405 afs_PutDCache(tdc); /* drop ref count */
406 ReleaseWriteLock(&adp->lock);
412 if (tvc->f.m.LinkCount > 2) {
413 /* This dir contains more than . and .., thus it can't be
416 ReleaseSharedLock(&tdc->lock);
419 ReleaseWriteLock(&adp->lock);
424 /* Make a shadow copy of the parent dir (if not done already).
425 * If we were created locally, then we don't need to have a shadow
426 * directory (as there's no server state to remember)
428 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
429 afs_MakeShadowDir(adp, tdc);
432 adp->f.m.LinkCount--;
433 #endif /* #ifdef AFS_DISCON_ENV */
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 defined(AFS_DISCON_ENV)
460 if (AFS_IS_DISCON_RW) {
461 if (tvc->f.ddirty_flags & VDisconCreate) {
462 /* If we we were created whilst disconnected, removal doesn't
463 * need to get logged. Just go away gracefully */
464 afs_DisconRemoveDirty(tvc);
466 afs_DisconAddDirty(tvc, VDisconRemove, 1);
470 ReleaseWriteLock(&tvc->lock);
473 ReleaseWriteLock(&adp->lock);
474 /* don't worry about link count since dirs can not be hardlinked */
479 afs_PutFakeStat(&fakestate);
480 code = afs_CheckCode(code, &treq, 27);