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)
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 if ((code = afs_InitReq(&treq, acred)))
64 afs_InitFakeStat(&fakestate);
66 if (strlen(aname) > AFSNAMEMAX) {
71 if (!afs_ENameOK(aname)) {
78 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
81 code = afs_VerifyVCache(adp, &treq);
85 /** If the volume is read-only, return error without making an RPC to the
88 if (adp->f.states & CRO) {
93 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
94 /*printf("Network is down in afs_mkdir\n");*/
98 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE | AFS_SETGROUP;
99 InStatus.ClientModTime = osi_Time();
100 InStatus.UnixModeBits = attrs->va_mode & 0xffff; /* only care about protection bits */
101 InStatus.Group = (afs_int32) afs_cr_gid(acred);
102 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
103 ObtainWriteLock(&adp->lock, 153);
105 if (!AFS_IS_DISCON_RW) {
107 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK, &rxconn);
109 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
113 RXAFS_MakeDir(rxconn,
114 (struct AFSFid *)&adp->f.fid.Fid,
117 (struct AFSFid *)&newFid.Fid,
124 CallBack.ExpirationTime += now;
125 /* DON'T forget to Set the callback value... */
129 (tc, rxconn, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
134 ObtainWriteLock(&afs_xcbhash, 490);
135 afs_DequeueCallback(adp);
136 adp->f.states &= ~CStatd;
137 ReleaseWriteLock(&afs_xcbhash);
138 osi_dnlc_purgedp(adp);
140 ReleaseWriteLock(&adp->lock);
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.
161 } /* if (!AFS_IS_DISCON_RW) */
163 /* otherwise, we should see if we can make the change to the dir locally */
165 ObtainWriteLock(&tdc->lock, 632);
166 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
167 /* we can do it locally */
168 ObtainWriteLock(&afs_xdcache, 294);
169 code = afs_dir_Create(tdc, aname, &newFid.Fid);
170 ReleaseWriteLock(&afs_xdcache);
172 ZapDCE(tdc); /* surprise error -- use invalid value */
177 ReleaseWriteLock(&tdc->lock);
181 if (AFS_IS_DISCON_RW)
182 /* We will have to settle with the local link count. */
183 adp->f.m.LinkCount++;
185 adp->f.m.LinkCount = OutDirStatus.LinkCount;
186 newFid.Cell = adp->f.fid.Cell;
187 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
188 ReleaseWriteLock(&adp->lock);
189 if (AFS_IS_DISCON_RW) {
190 /* When disconnected, we have to create the full dir here. */
192 /* Generate a new vcache and fill it. */
193 tvc = afs_NewVCache(&newFid, NULL);
201 ObtainWriteLock(&tvc->lock, 738);
202 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VDIR);
203 ReleaseWriteLock(&tvc->lock);
205 /* And now make an empty dir, containing . and .. : */
206 /* Get a new dcache for it first. */
207 new_dc = afs_GetDCache(tvc, (afs_size_t) 0, &treq, &offset, &len, 1);
209 /* printf("afs_mkdir: can't get new dcache for dir.\n"); */
214 ObtainWriteLock(&afs_xdcache, 739);
215 code = afs_dir_MakeDir(new_dc,
216 (afs_int32 *) &newFid.Fid,
217 (afs_int32 *) &adp->f.fid.Fid);
218 ReleaseWriteLock(&afs_xdcache);
219 /* if (code) printf("afs_mkdir: afs_dirMakeDir code = %u\n", code); */
221 afs_PutDCache(new_dc);
223 ObtainWriteLock(&tvc->lock, 731);
224 /* Update length in the vcache. */
225 tvc->f.m.Length = new_dc->f.chunkBytes;
227 afs_DisconAddDirty(tvc, VDisconCreate, 1);
228 ReleaseWriteLock(&tvc->lock);
230 /* now we're done with parent dir, create the real dir's cache entry */
231 tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
237 } /* if (AFS_DISCON_RW) */
242 afs_PutFakeStat(&fakestate);
243 code = afs_CheckCode(code, &treq, 26);
250 /* don't set CDirty in here because RPC is called synchronously */
251 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
252 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp,
255 afs_rmdir(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
258 struct vrequest treq;
260 struct vcache *tvc = NULL;
263 afs_size_t offset, len;
264 struct AFSFetchStatus OutDirStatus;
265 struct AFSVolSync tsync;
266 struct afs_fakestat_state fakestate;
267 struct rx_connection *rxconn;
271 AFS_STATCNT(afs_rmdir);
273 afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
274 ICL_TYPE_STRING, aname);
276 if ((code = afs_InitReq(&treq, acred)))
278 afs_InitFakeStat(&fakestate);
280 if (strlen(aname) > AFSNAMEMAX) {
287 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
291 code = afs_VerifyVCache(adp, &treq);
295 /** If the volume is read-only, return error without making an RPC to the
298 if (adp->f.states & CRO) {
303 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
304 /* Disconnected read only mode. */
309 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1); /* test for error below */
310 ObtainWriteLock(&adp->lock, 154);
312 ObtainSharedLock(&tdc->lock, 633);
313 if (tdc && (adp->f.states & CForeign)) {
314 struct VenusFid unlinkFid;
316 unlinkFid.Fid.Vnode = 0;
317 code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
319 afs_int32 cached = 0;
321 unlinkFid.Cell = adp->f.fid.Cell;
322 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
323 if (unlinkFid.Fid.Unique == 0) {
325 afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
327 ObtainReadLock(&afs_xvcache);
328 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
329 ReleaseReadLock(&afs_xvcache);
334 if (!AFS_IS_DISCON_RW) {
335 /* Not disconnected, can connect to server. */
337 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK, &rxconn);
339 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
342 RXAFS_RemoveDir(rxconn,
343 (struct AFSFid *)&adp->f.fid.Fid,
352 (tc, rxconn, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
357 ReleaseSharedLock(&tdc->lock);
362 ObtainWriteLock(&afs_xcbhash, 491);
363 afs_DequeueCallback(adp);
364 adp->f.states &= ~CStatd;
365 ReleaseWriteLock(&afs_xcbhash);
366 osi_dnlc_purgedp(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);