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