discon-ifdef-cleanup-20090318
[openafs.git] / src / afs / VNOPS / afs_vnop_dirops.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * afs_vnop_dirops.c - make and remove directories
12  *
13  * Implements:
14  *
15  * afs_mkdir
16  * afs_rmdir
17  *
18  */
19
20 #include <afsconfig.h>
21 #include "afs/param.h"
22
23 RCSID
24     ("$Header$");
25
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"
32
33 extern afs_rwlock_t afs_xvcache;
34 extern afs_rwlock_t afs_xcbhash;
35
36 /* don't set CDirty in here because RPC is called synchronously */
37
38 int
39 afs_mkdir(OSI_VC_DECL(adp), char *aname, struct vattr *attrs, 
40      register struct vcache **avcp, struct AFS_UCRED *acred)
41 {
42     struct vrequest treq;
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;
54     afs_int32 now;
55     struct afs_fakestat_state fakestate;
56     XSTATS_DECLS;
57     OSI_VC_CONVERT(adp);
58
59     AFS_STATCNT(afs_mkdir);
60     afs_Trace2(afs_iclSetp, CM_TRACE_MKDIR, ICL_TYPE_POINTER, adp,
61                ICL_TYPE_STRING, aname);
62
63     if ((code = afs_InitReq(&treq, acred)))
64         goto done2;
65     afs_InitFakeStat(&fakestate);
66
67     if (strlen(aname) > AFSNAMEMAX) {
68         code = ENAMETOOLONG;
69         goto done3;
70     }
71
72     if (!afs_ENameOK(aname)) {
73         code = EINVAL;
74         goto done3;
75     }
76     
77     AFS_DISCON_LOCK();
78
79     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
80     if (code)
81         goto done;
82     code = afs_VerifyVCache(adp, &treq);
83     if (code)
84         goto done;
85
86     /** If the volume is read-only, return error without making an RPC to the
87       * fileserver
88       */
89     if (adp->f.states & CRO) {
90         code = EROFS;
91         goto done;
92     }
93    
94     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
95         /*printf("Network is down in afs_mkdir\n");*/
96         code = ENETDOWN;
97         goto done;
98     }
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) acred->cr_gid;
103     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
104     ObtainWriteLock(&adp->lock, 153);
105
106     if (!AFS_IS_DISCON_RW) {
107         do {
108             tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
109             if (tc) {
110                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_MAKEDIR);
111                 now = osi_Time();
112                 RX_AFS_GUNLOCK();
113                 code =
114                     RXAFS_MakeDir(tc->id,
115                                 (struct AFSFid *)&adp->f.fid.Fid,
116                                 aname,
117                                 &InStatus,
118                                 (struct AFSFid *)&newFid.Fid,
119                                 &OutFidStatus,
120                                 &OutDirStatus,
121                                 &CallBack,
122                                 &tsync);
123                 RX_AFS_GLOCK();
124                 XSTATS_END_TIME;
125                 CallBack.ExpirationTime += now;
126                 /* DON'T forget to Set the callback value... */
127             } else
128                 code = -1;
129         } while (afs_Analyze
130                     (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_MAKEDIR,
131                      SHARED_LOCK, NULL));
132
133         if (code) {
134             if (code < 0) {
135                 ObtainWriteLock(&afs_xcbhash, 490);
136                 afs_DequeueCallback(adp);
137                 adp->f.states &= ~CStatd;
138                 ReleaseWriteLock(&afs_xcbhash);
139                 osi_dnlc_purgedp(adp);
140             }
141             ReleaseWriteLock(&adp->lock);
142             if (tdc)
143                 afs_PutDCache(tdc);
144             goto done;
145         }
146
147     } else {
148 #if defined(AFS_DISCON_ENV)
149         /* Disconnected. */
150
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);
157         }
158         /* XXX: If mount point???*/
159
160         /* Operations with the actual dir's cache entry are further
161          * down, where the dir entry gets created.
162          */
163 #endif
164     }                   /* if (!AFS_IS_DISCON_RW) */
165
166     /* otherwise, we should see if we can make the change to the dir locally */
167     if (tdc)
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);
174         if (code) {
175             ZapDCE(tdc);        /* surprise error -- use invalid value */
176             DZap(tdc);
177         }
178     }
179     if (tdc) {
180         ReleaseWriteLock(&tdc->lock);
181         afs_PutDCache(tdc);
182     }
183
184     if (AFS_IS_DISCON_RW)
185         /* We will have to settle with the local link count. */
186         adp->f.m.LinkCount++;
187     else
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. */
195
196         /* Generate a new vcache and fill it. */
197         tvc = afs_NewVCache(&newFid, NULL);
198         if (tvc) {
199             *avcp = tvc;
200         } else {
201             code = ENOENT;
202             goto done;
203         }
204
205         ObtainWriteLock(&tvc->lock, 738);
206         afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VDIR);
207         ReleaseWriteLock(&tvc->lock);
208
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);
212         if (!new_dc) {
213             printf("afs_mkdir: can't get new dcache for dir.\n");
214             code = ENOENT;
215             goto done;
216         }
217
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)
224             printf("afs_mkdir: afs_dirMakeDir code = %u\n", code);
225
226         afs_PutDCache(new_dc);
227
228         ObtainWriteLock(&tvc->lock, 731);
229         /* Update length in the vcache. */
230         tvc->f.m.Length = new_dc->f.chunkBytes;
231
232         afs_DisconAddDirty(tvc, VDisconCreate, 1);
233         ReleaseWriteLock(&tvc->lock);
234 #endif                          /* #ifdef AFS_DISCON_ENV */
235     } else {
236         /* now we're done with parent dir, create the real dir's cache entry */
237         tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
238         if (tvc) {
239             code = 0;
240             *avcp = tvc;
241         } else
242             code = ENOENT;
243     }                           /* if (AFS_DISCON_RW) */
244
245   done:
246     AFS_DISCON_UNLOCK();
247   done3:
248     afs_PutFakeStat(&fakestate);
249     code = afs_CheckCode(code, &treq, 26);
250   done2:
251     return code;
252 }
253
254
255 int
256 /* don't set CDirty in here because RPC is called synchronously */
257 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
258 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct vnode *cdirp, 
259           struct AFS_UCRED *acred)
260 #else
261 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
262 #endif
263 {
264     struct vrequest treq;
265     register struct dcache *tdc;
266     register struct vcache *tvc = NULL;
267     register afs_int32 code;
268     register struct afs_conn *tc;
269     afs_size_t offset, len;
270     struct AFSFetchStatus OutDirStatus;
271     struct AFSVolSync tsync;
272     struct afs_fakestat_state fakestate;
273     XSTATS_DECLS;
274     OSI_VC_CONVERT(adp);
275
276     AFS_STATCNT(afs_rmdir);
277
278     afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
279                ICL_TYPE_STRING, aname);
280
281     if ((code = afs_InitReq(&treq, acred)))
282         goto done2;
283     afs_InitFakeStat(&fakestate);
284
285     if (strlen(aname) > AFSNAMEMAX) {
286         code = ENAMETOOLONG;
287         goto done;
288     }
289
290     AFS_DISCON_LOCK();
291
292     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
293     if (code)
294         goto done;
295
296     code = afs_VerifyVCache(adp, &treq);
297     if (code)
298         goto done;
299
300     /** If the volume is read-only, return error without making an RPC to the
301       * fileserver
302       */
303     if (adp->f.states & CRO) {
304         code = EROFS;
305         goto done;
306     }
307
308    if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
309         /* Disconnected read only mode. */
310         code = ENETDOWN;
311         goto done;
312     }
313
314     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
315     ObtainWriteLock(&adp->lock, 154);
316     if (tdc)
317         ObtainSharedLock(&tdc->lock, 633);
318     if (tdc && (adp->f.states & CForeign)) {
319         struct VenusFid unlinkFid;
320
321         unlinkFid.Fid.Vnode = 0;
322         code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
323         if (code == 0) {
324             afs_int32 cached = 0;
325
326             unlinkFid.Cell = adp->f.fid.Cell;
327             unlinkFid.Fid.Volume = adp->f.fid.Fid.Volume;
328             if (unlinkFid.Fid.Unique == 0) {
329                 tvc =
330                     afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
331             } else {
332                 ObtainReadLock(&afs_xvcache);
333                 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
334                 ReleaseReadLock(&afs_xvcache);
335             }
336         }
337     }
338
339     if (!AFS_IS_DISCON_RW) {
340         /* Not disconnected, can connect to server. */
341         do {
342             tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
343             if (tc) {
344                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
345                 RX_AFS_GUNLOCK();
346                 code =
347                     RXAFS_RemoveDir(tc->id,
348                                 (struct AFSFid *)&adp->f.fid.Fid,
349                                 aname,
350                                 &OutDirStatus,
351                                 &tsync);
352                 RX_AFS_GLOCK();
353                 XSTATS_END_TIME;
354             } else
355                 code = -1;
356         } while (afs_Analyze
357                  (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
358                  SHARED_LOCK, NULL));
359
360         if (code) {
361             if (tdc) {
362                 ReleaseSharedLock(&tdc->lock);
363                 afs_PutDCache(tdc);
364             }
365
366             if (code < 0) {
367                 ObtainWriteLock(&afs_xcbhash, 491);
368                 afs_DequeueCallback(adp);
369                 adp->f.states &= ~CStatd;
370                 ReleaseWriteLock(&afs_xcbhash);
371                 osi_dnlc_purgedp(adp);
372             }
373             ReleaseWriteLock(&adp->lock);
374             goto done;
375         }
376
377         /* here if rpc worked; update the in-core link count */
378         adp->f.m.LinkCount = OutDirStatus.LinkCount;
379
380     } else {
381 #if defined(AFS_DISCON_ENV)
382         /* Disconnected. */
383
384         if (!tdc) {
385             ReleaseWriteLock(&adp->lock);
386             printf("afs_rmdir: No local dcache!\n");
387             code = ENETDOWN;
388             goto done;
389         }
390         
391         if (!tvc) {
392             /* Find the vcache. */
393             struct VenusFid tfid;
394
395             tfid.Cell = adp->f.fid.Cell;
396             tfid.Fid.Volume = adp->f.fid.Fid.Volume;
397             code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
398
399             ObtainSharedLock(&afs_xvcache, 764);
400             tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
401             ReleaseSharedLock(&afs_xvcache);
402             
403             if (!tvc) {
404                 printf("afs_rmdir: Can't find dir's vcache!\n");
405                 ReleaseSharedLock(&tdc->lock);
406                 afs_PutDCache(tdc);     /* drop ref count */
407                 ReleaseWriteLock(&adp->lock);
408                 code = ENETDOWN;
409                 goto done;
410             }
411         }
412
413         if (tvc->f.m.LinkCount > 2) {
414             /* This dir contains more than . and .., thus it can't be
415              * deleted.
416              */
417             ReleaseSharedLock(&tdc->lock);
418             afs_PutDCache(tdc);
419             afs_PutVCache(tvc);
420             ReleaseWriteLock(&adp->lock);
421             code = ENOTEMPTY;
422             goto done;
423         }
424
425         /* Make a shadow copy of the parent dir (if not done already).
426          * If we were created locally, then we don't need to have a shadow
427          * directory (as there's no server state to remember)
428          */
429         if (!adp->f.shadow.vnode && !(adp->f.ddirty_flags & VDisconCreate)) {
430             afs_MakeShadowDir(adp, tdc);
431         }
432
433         adp->f.m.LinkCount--;
434 #endif                          /* #ifdef AFS_DISCON_ENV */
435     }                           /* if (!AFS_IS_DISCON_RW) */
436
437     if (tdc)
438         UpgradeSToWLock(&tdc->lock, 634);
439     if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
440         /* we can do it locally */
441         code = afs_dir_Delete(tdc, aname);
442         if (code) {
443             ZapDCE(tdc);        /* surprise error -- invalid value */
444             DZap(tdc);
445         }
446     }
447     if (tdc) {
448         ReleaseWriteLock(&tdc->lock);
449         afs_PutDCache(tdc);     /* drop ref count */
450     }
451
452     if (tvc)
453         osi_dnlc_purgedp(tvc);  /* get rid of any entries for this directory */
454     else
455         osi_dnlc_remove(adp, aname, 0);
456
457     if (tvc) {
458         ObtainWriteLock(&tvc->lock, 155);
459         tvc->f.states &= ~CUnique;      /* For the dfs xlator */
460 #if defined(AFS_DISCON_ENV)
461         if (AFS_IS_DISCON_RW) {
462             if (tvc->f.ddirty_flags & VDisconCreate) {
463                 /* If we we were created whilst disconnected, removal doesn't
464                  * need to get logged. Just go away gracefully */
465                 afs_DisconRemoveDirty(tvc);
466             } else {
467                 afs_DisconAddDirty(tvc, VDisconRemove, 1);
468             }
469         }
470 #endif
471         ReleaseWriteLock(&tvc->lock);
472         afs_PutVCache(tvc);
473     }
474     ReleaseWriteLock(&adp->lock);
475     /* don't worry about link count since dirs can not be hardlinked */
476     code = 0;
477
478   done:
479     AFS_DISCON_UNLOCK();
480   done3:
481     afs_PutFakeStat(&fakestate);
482     code = afs_CheckCode(code, &treq, 27);
483   done2:
484     return code;
485 }