58fe67626f1619a29d77e24e4847c0d210ed1bde
[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->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->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->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->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->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->fid.Cell;
155             newFid.Fid.Volume = adp->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->m.LinkCount++;
187     else
188         adp->m.LinkCount = OutDirStatus.LinkCount;
189     newFid.Cell = adp->fid.Cell;
190     newFid.Fid.Volume = adp->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->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         afs_DisconAddDirty(tvc, VDisconCreate, 1);
230         /* Update length in the vcache. */
231         tvc->m.Length = new_dc->f.chunkBytes;
232         ReleaseWriteLock(&tvc->lock);
233 #endif                          /* #ifdef AFS_DISCON_ENV */
234     } else {
235         /* now we're done with parent dir, create the real dir's cache entry */
236         tvc = afs_GetVCache(&newFid, &treq, NULL, NULL);
237         if (tvc) {
238             code = 0;
239             *avcp = tvc;
240         } else
241             code = ENOENT;
242     }                           /* if (AFS_DISCON_RW) */
243
244   done:
245     AFS_DISCON_UNLOCK();
246   done3:
247     afs_PutFakeStat(&fakestate);
248     code = afs_CheckCode(code, &treq, 26);
249   done2:
250     return code;
251 }
252
253
254 int
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, 
258           struct AFS_UCRED *acred)
259 #else
260 afs_rmdir(OSI_VC_DECL(adp), char *aname, struct AFS_UCRED *acred)
261 #endif
262 {
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;
272     XSTATS_DECLS;
273     OSI_VC_CONVERT(adp);
274
275     AFS_STATCNT(afs_rmdir);
276
277     afs_Trace2(afs_iclSetp, CM_TRACE_RMDIR, ICL_TYPE_POINTER, adp,
278                ICL_TYPE_STRING, aname);
279
280     if ((code = afs_InitReq(&treq, acred)))
281         goto done2;
282     afs_InitFakeStat(&fakestate);
283
284     if (strlen(aname) > AFSNAMEMAX) {
285         code = ENAMETOOLONG;
286         goto done;
287     }
288
289     AFS_DISCON_LOCK();
290
291     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
292     if (code)
293         goto done;
294
295     code = afs_VerifyVCache(adp, &treq);
296     if (code)
297         goto done;
298
299     /** If the volume is read-only, return error without making an RPC to the
300       * fileserver
301       */
302     if (adp->states & CRO) {
303         code = EROFS;
304         goto done;
305     }
306
307    if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
308         /* Disconnected read only mode. */
309         code = ENETDOWN;
310         goto done;
311     }
312
313     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
314     ObtainWriteLock(&adp->lock, 154);
315     if (tdc)
316         ObtainSharedLock(&tdc->lock, 633);
317     if (tdc && (adp->states & CForeign)) {
318         struct VenusFid unlinkFid;
319
320         unlinkFid.Fid.Vnode = 0;
321         code = afs_dir_Lookup(tdc, aname, &unlinkFid.Fid);
322         if (code == 0) {
323             afs_int32 cached = 0;
324
325             unlinkFid.Cell = adp->fid.Cell;
326             unlinkFid.Fid.Volume = adp->fid.Fid.Volume;
327             if (unlinkFid.Fid.Unique == 0) {
328                 tvc =
329                     afs_LookupVCache(&unlinkFid, &treq, &cached, adp, aname);
330             } else {
331                 ObtainReadLock(&afs_xvcache);
332                 tvc = afs_FindVCache(&unlinkFid, 0, 1 /* do xstats */ );
333                 ReleaseReadLock(&afs_xvcache);
334             }
335         }
336     }
337
338     if (!AFS_IS_DISCON_RW) {
339         /* Not disconnected, can connect to server. */
340         do {
341             tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
342             if (tc) {
343                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_REMOVEDIR);
344                 RX_AFS_GUNLOCK();
345                 code =
346                     RXAFS_RemoveDir(tc->id,
347                                 (struct AFSFid *)&adp->fid.Fid,
348                                 aname,
349                                 &OutDirStatus,
350                                 &tsync);
351                 RX_AFS_GLOCK();
352                 XSTATS_END_TIME;
353             } else
354                 code = -1;
355         } while (afs_Analyze
356                  (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_REMOVEDIR,
357                  SHARED_LOCK, NULL));
358
359         if (code) {
360             if (tdc) {
361                 ReleaseSharedLock(&tdc->lock);
362                 afs_PutDCache(tdc);
363             }
364
365             if (code < 0) {
366                 ObtainWriteLock(&afs_xcbhash, 491);
367                 afs_DequeueCallback(adp);
368                 adp->states &= ~CStatd;
369                 ReleaseWriteLock(&afs_xcbhash);
370                 osi_dnlc_purgedp(adp);
371             }
372             ReleaseWriteLock(&adp->lock);
373             goto done;
374         }
375
376         /* here if rpc worked; update the in-core link count */
377         adp->m.LinkCount = OutDirStatus.LinkCount;
378
379     } else {
380 #if defined(AFS_DISCON_ENV)
381         /* Disconnected. */
382
383         if (!tdc) {
384             ReleaseWriteLock(&adp->lock);
385             printf("afs_rmdir: No local dcache!\n");
386             code = ENETDOWN;
387             goto done;
388         }
389         
390         if (!tvc) {
391             /* Find the vcache. */
392             struct VenusFid tfid;
393
394             tfid.Cell = adp->fid.Cell;
395             tfid.Fid.Volume = adp->fid.Fid.Volume;
396             code = afs_dir_Lookup(tdc, aname, &tfid.Fid);
397
398             ObtainSharedLock(&afs_xvcache, 764);
399             tvc = afs_FindVCache(&tfid, 0, 1 /* do xstats */ );
400             ReleaseSharedLock(&afs_xvcache);
401             
402             if (!tvc) {
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);
407                 code = ENETDOWN;
408                 goto done;
409             }
410         }
411
412         if (tvc->m.LinkCount > 2) {
413             /* This dir contains more than . and .., thus it can't be
414              * deleted.
415              */
416             ReleaseSharedLock(&tdc->lock);
417             afs_PutDCache(tdc);
418             afs_PutVCache(tvc);
419             ReleaseWriteLock(&adp->lock);
420             code = ENOTEMPTY;
421             goto done;
422         }
423
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)
427          */
428         if (!adp->shVnode && !(adp->ddirty_flags & VDisconCreate)) {
429             afs_MakeShadowDir(adp, tdc);
430         }
431
432         adp->m.LinkCount--;
433 #endif                          /* #ifdef AFS_DISCON_ENV */
434     }                           /* if (!AFS_IS_DISCON_RW) */
435
436     if (tdc)
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);
441         if (code) {
442             ZapDCE(tdc);        /* surprise error -- invalid value */
443             DZap(tdc);
444         }
445     }
446     if (tdc) {
447         ReleaseWriteLock(&tdc->lock);
448         afs_PutDCache(tdc);     /* drop ref count */
449     }
450
451     if (tvc)
452         osi_dnlc_purgedp(tvc);  /* get rid of any entries for this directory */
453     else
454         osi_dnlc_remove(adp, aname, 0);
455
456     if (tvc) {
457         ObtainWriteLock(&tvc->lock, 155);
458         tvc->states &= ~CUnique;        /* For the dfs xlator */
459 #if AFS_DISCON_ENV
460         if (AFS_IS_DISCON_RW) {
461             if (tvc->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);
465             } else {
466                 afs_DisconAddDirty(tvc, VDisconRemove, 1);
467             }
468         }
469 #endif
470         ReleaseWriteLock(&tvc->lock);
471         afs_PutVCache(tvc);
472     }
473     ReleaseWriteLock(&adp->lock);
474     /* don't worry about link count since dirs can not be hardlinked */
475     code = 0;
476
477   done:
478     AFS_DISCON_UNLOCK();
479   done3:
480     afs_PutFakeStat(&fakestate);
481     code = afs_CheckCode(code, &treq, 27);
482   done2:
483     return code;
484 }