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 /* don't set CDirty in here because RPC is called synchronously */
34 afs_mkdir(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
35 struct vcache **avcp, afs_ucred_t *acred)
37 struct vrequest *treq = NULL;
40 struct rx_connection *rxconn;
41 struct VenusFid newFid;
43 struct dcache *new_dc;
44 afs_size_t offset, len;
46 struct AFSStoreStatus InStatus;
47 struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
48 struct AFSCallBack CallBack;
49 struct AFSVolSync tsync;
51 struct afs_fakestat_state fakestate;
55 AFS_STATCNT(afs_mkdir);
56 afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
57 ICL_TYPE_STRING, aname);
59 OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
60 OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
61 memset(&InStatus, 0, sizeof(InStatus));
63 if ((code = afs_CreateReq(&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, &rxconn);
110 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
114 RXAFS_MakeDir(rxconn,
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, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
135 afs_StaleVCache(adp);
137 ReleaseWriteLock(&adp->lock);
146 /* We have the dir entry now, we can use it while disconnected. */
147 if (adp->mvid.target_root == NULL) {
148 /* If not mount point, generate a new fid. */
149 newFid.Cell = adp->f.fid.Cell;
150 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
151 afs_GenFakeFid(&newFid, VDIR, 1);
153 /* XXX: If mount point???*/
155 /* Operations with the actual dir's cache entry are further
156 * down, where the dir entry gets created.
158 } /* if (!AFS_IS_DISCON_RW) */
160 /* otherwise, we should see if we can make the change to the dir locally */
162 ObtainWriteLock(&tdc->lock, 632);
163 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
164 /* we can do it locally */
165 ObtainWriteLock(&afs_xdcache, 294);
166 code = afs_dir_Create(tdc, aname, &newFid.Fid);
167 ReleaseWriteLock(&afs_xdcache);
169 ZapDCE(tdc); /* surprise error -- use invalid value */
174 ReleaseWriteLock(&tdc->lock);
178 if (AFS_IS_DISCON_RW)
179 /* We will have to settle with the local link count. */
180 adp->f.m.LinkCount++;
182 adp->f.m.LinkCount = OutDirStatus->LinkCount;
183 newFid.Cell = adp->f.fid.Cell;
184 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
185 ReleaseWriteLock(&adp->lock);
186 if (AFS_IS_DISCON_RW) {
187 /* When disconnected, we have to create the full dir here. */
189 /* Generate a new vcache and fill it. */
190 tvc = afs_NewVCache(&newFid, NULL);
198 ObtainWriteLock(&tvc->lock, 738);
199 afs_GenDisconStatus(adp, tvc, &newFid, attrs, treq, VDIR);
200 ReleaseWriteLock(&tvc->lock);
202 /* And now make an empty dir, containing . and .. : */
203 /* Get a new dcache for it first. */
204 new_dc = afs_GetDCache(tvc, (afs_size_t) 0, treq, &offset, &len, 1);
206 /* printf("afs_mkdir: can't get new dcache for dir.\n"); */
211 ObtainWriteLock(&afs_xdcache, 739);
212 code = afs_dir_MakeDir(new_dc,
213 (afs_int32 *) &newFid.Fid,
214 (afs_int32 *) &adp->f.fid.Fid);
215 ReleaseWriteLock(&afs_xdcache);
216 /* if (code) printf("afs_mkdir: afs_dirMakeDir code = %u\n", code); */
218 afs_PutDCache(new_dc);
220 ObtainWriteLock(&tvc->lock, 731);
221 /* Update length in the vcache. */
222 tvc->f.m.Length = new_dc->f.chunkBytes;
224 afs_DisconAddDirty(tvc, VDisconCreate, 1);
225 ReleaseWriteLock(&tvc->lock);
227 /* now we're done with parent dir, create the real dir's cache entry */
228 tvc = afs_GetVCache(&newFid, treq);
234 /* For some reason, we cannot fetch the vcache for our
235 * newly-created dir. */
238 } /* if (AFS_DISCON_RW) */
243 afs_PutFakeStat(&fakestate);
244 code = afs_CheckCode(code, treq, 26);
245 afs_DestroyReq(treq);
247 osi_FreeSmallSpace(OutFidStatus);
248 osi_FreeSmallSpace(OutDirStatus);
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,
259 afs_rmdir(OSI_VC_DECL(adp), char *aname, afs_ucred_t *acred)
262 struct vrequest *treq = NULL;
264 struct vcache *tvc = NULL;
267 afs_size_t offset, len;
268 struct AFSFetchStatus OutDirStatus;
269 struct AFSVolSync tsync;
270 struct afs_fakestat_state fakestate;
271 struct rx_connection *rxconn;
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_CreateReq(&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 unlinkFid.Cell = adp->f.fid.Cell;
324 unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
325 if (unlinkFid.Fid.Unique == 0) {
327 afs_LookupVCache(&unlinkFid, treq, 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->f.fid, treq, SHARED_LOCK, &rxconn);
341 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
344 RXAFS_RemoveDir(rxconn,
345 (struct AFSFid *)&adp->f.fid.Fid,
354 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
359 ReleaseSharedLock(&tdc->lock);
364 afs_StaleVCache(adp);
366 ReleaseWriteLock(&adp->lock);
370 /* here if rpc worked; update the in-core link count */
371 adp->f.m.LinkCount = OutDirStatus.LinkCount;
377 ReleaseWriteLock(&adp->lock);
378 /* printf("afs_rmdir: No local dcache!\n"); */
384 /* Find the vcache. */
385 struct VenusFid tfid;
387 tfid.Cell = adp->f.fid.Cell;
388 tfid.Fid.Volume = adp->f.fid.Fid.Volume;
389 code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
391 ObtainSharedLock(&afs_xvcache, 764);
392 tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
393 ReleaseSharedLock(&afs_xvcache);
396 /* printf("afs_rmdir: Can't find dir's vcache!\n"); */
397 ReleaseSharedLock(&tdc->lock);
398 afs_PutDCache(tdc); /* drop ref count */
399 ReleaseWriteLock(&adp->lock);
405 if (tvc->f.m.LinkCount > 2) {
406 /* This dir contains more than . and .., thus it can't be
409 ReleaseSharedLock(&tdc->lock);
412 ReleaseWriteLock(&adp->lock);
417 /* Make a shadow copy of the parent dir (if not done already).
418 * If we were created locally, then we don't need to have a shadow
419 * directory (as there's no server state to remember)
421 if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
422 afs_MakeShadowDir(adp, tdc);
425 adp->f.m.LinkCount--;
426 } /* if (!AFS_IS_DISCON_RW) */
429 UpgradeSToWLock(&tdc->lock, 634);
430 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
431 /* we can do it locally */
432 code = afs_dir_Delete(tdc, aname);
434 ZapDCE(tdc); /* surprise error -- invalid value */
439 ReleaseWriteLock(&tdc->lock);
440 afs_PutDCache(tdc); /* drop ref count */
444 osi_dnlc_purgedp(tvc); /* get rid of any entries for this directory */
446 osi_dnlc_remove(adp, aname, 0);
449 ObtainWriteLock(&tvc->lock, 155);
450 tvc->f.states &= ~CUnique; /* For the dfs xlator */
451 if (AFS_IS_DISCON_RW) {
452 if (tvc->f.ddirty_flags & VDisconCreate) {
453 /* If we we were created whilst disconnected, removal doesn't
454 * need to get logged. Just go away gracefully */
455 afs_DisconRemoveDirty(tvc);
457 afs_DisconAddDirty(tvc, VDisconRemove, 1);
460 ReleaseWriteLock(&tvc->lock);
463 ReleaseWriteLock(&adp->lock);
464 /* don't worry about link count since dirs can not be hardlinked */
469 afs_PutFakeStat(&fakestate);
470 code = afs_CheckCode(code, treq, 27);
471 afs_DestroyReq(treq);