openbsd-mbuf-before-in-20021002
[openafs.git] / src / dir / dir.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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #ifdef KERNEL
16 #if !defined(UKERNEL)
17 #include "../h/types.h"
18 #include "../h/param.h"
19 #ifdef  AFS_AUX_ENV
20 #include "../h/mmu.h"
21 #include "../h/seg.h"
22 #include "../h/sysmacros.h"
23 #include "../h/signal.h"
24 #include "../h/errno.h"
25 #endif
26 #include "../h/time.h"
27 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV)
28 #include "../h/errno.h"
29 #else
30 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
31 #include "../h/kernel.h"
32 #endif
33 #endif
34 #if     defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV)
35 #include "../afs/sysincludes.h"
36 #endif
37 #if defined(AFS_FBSD_ENV)
38 #include "../h/lock.h"
39 #include "../vm/vm.h"
40 #include "../vm/vm_extern.h"
41 #include "../vm/pmap.h"
42 #include "../vm/vm_map.h"
43 #endif /* AFS_FBSD_ENV */
44 #if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN60_ENV)
45 #include "../h/user.h"
46 #endif /* AFS_SGI64_ENV */
47 #include "../h/uio.h"
48 #ifdef  AFS_DEC_ENV
49 #include "../afs/gfs_vfs.h"
50 #include "../afs/gfs_vnode.h"
51 #else
52 #ifdef  AFS_MACH_ENV
53 #ifdef  NeXT
54 #include <sys/vfs.h>
55 #include <sys/vnode.h>
56 #include <ufs/inode.h>
57 #else
58 #include <vfs/vfs.h>
59 #include <vfs/vnode.h>
60 #include <sys/inode.h>
61 #endif /* NeXT */
62 #else /* AFS_MACH_ENV */
63 #ifdef  AFS_OSF_ENV
64 #include <sys/mount.h>
65 #include <sys/vnode.h>
66 #include <ufs/inode.h>
67 #else   /* AFS_OSF_ENV */
68 #ifdef  AFS_SUN5_ENV
69 #else
70 #if !defined(AFS_SGI_ENV)
71 #endif
72 #endif  /* AFS_OSF_ENV */
73 #endif /* AFS_MACH_ENV */
74 #endif
75 #endif
76 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
77 #include "../h/mbuf.h"
78 #endif
79 #ifndef AFS_LINUX20_ENV
80 #include "../netinet/in.h"
81 #endif
82 #else /* !defined(UKERNEL) */
83 #include "../afs/stds.h"
84 #include "../afs/sysincludes.h"
85 #endif /* !defined(UKERNEL) */
86 #include "../afs/afs_osi.h"
87
88 #include "../afs/dir.h"
89
90 #include "../afs/longc_procs.h"
91 #ifdef AFS_LINUX20_ENV
92 #include "../h/string.h"
93 #endif
94
95 /* generic renaming */
96 #define NameBlobs       afs_dir_NameBlobs
97 #define GetBlob         afs_dir_GetBlob
98 #define Create          afs_dir_Create
99 #define Length          afs_dir_Length
100 #define Delete          afs_dir_Delete
101 #define MakeDir         afs_dir_MakeDir
102 #define Lookup          afs_dir_Lookup
103 #define LookupOffset    afs_dir_LookupOffset
104 #define EnumerateDir    afs_dir_EnumerateDir
105 #define IsEmpty         afs_dir_IsEmpty
106 #else /* KERNEL */
107
108 # ifdef HAVE_UNISTD_H
109 #  include <unistd.h>
110 # endif
111 # include <sys/types.h> 
112 # include <errno.h>
113 # include "dir.h"
114 #ifdef AFS_NT40_ENV
115 #include <winsock2.h>
116 #else
117 #include <netinet/in.h>
118 #endif
119
120 #ifdef HAVE_STRING_H
121 #include <string.h>
122 #else
123 #ifdef HAVE_STRINGS_H
124 #include <strings.h>
125 #endif
126 #endif
127 #endif /* KERNEL */
128
129 afs_int32 DErrno;
130 char *DRelease();
131 struct DirEntry *DRead();
132 struct DirEntry *DNew();
133
134 /* Local static prototypes */
135 static struct DirEntry *FindItem (char *dir, char *ename,
136         unsigned short **previtem);
137
138
139 int NameBlobs (char *name)
140 {/* Find out how many entries are required to store a name. */
141     register int i;
142     i = strlen(name)+1;
143     return 1+((i+15)>>5);
144 }
145
146 int Create (char *dir, char *entry, afs_int32 *vfid)
147 {
148     /* Create an entry in a file.  Dir is a file representation, while entry is a string name. */
149     int blobs, firstelt;
150     register int i;
151     register struct DirEntry *ep;
152     unsigned short *pp = NULL;
153     register struct DirHeader *dhp;
154
155     /* check name quality */
156     if (*entry == 0) return EINVAL;
157     /* First check if file already exists. */
158     ep = FindItem(dir,entry,&pp);
159     if (ep) {
160         DRelease(ep, 0);
161         DRelease(pp, 0);
162         return EEXIST;
163     }
164     blobs = NameBlobs(entry);   /* number of entries required */
165     firstelt = FindBlobs(dir,blobs);
166     if (firstelt < 0) return EFBIG;     /* directory is full */
167     /* First, we fill in the directory entry. */
168     ep = GetBlob(dir,firstelt);
169     if (ep == 0) return EIO;
170     ep->flag = FFIRST;
171     ep->fid.vnode = htonl(vfid[1]);
172     ep->fid.vunique = htonl(vfid[2]);
173     strcpy(ep->name,entry);
174     /* Now we just have to thread it on the hash table list. */
175     dhp = (struct DirHeader *) DRead(dir,0);
176     if (!dhp) {
177         DRelease(ep, 1);
178         return EIO;
179     }
180     i = DirHash(entry);
181     ep->next = dhp->hashTable[i];
182     dhp->hashTable[i] = htons(firstelt);
183     DRelease(dhp,1);
184     DRelease(ep,1);
185     return 0;
186 }
187
188 int Length (char *dir)
189 {
190     int i,ctr;
191     struct DirHeader *dhp;
192     dhp = (struct DirHeader *) DRead(dir,0);
193     if (!dhp) return 0;
194     if (dhp->header.pgcount != 0) ctr = ntohs(dhp->header.pgcount);
195     else {
196         /* old style, count the pages */
197         ctr=0;
198         for(i=0;i<MAXPAGES;i++)
199             if (dhp->alloMap[i] != EPP) ctr++;
200     }
201     DRelease(dhp,0);
202     return ctr*AFS_PAGESIZE;
203 }
204
205 int Delete (char *dir, char *entry)
206 {
207     /* Delete an entry from a directory, including update of all free entry descriptors. */
208     int nitems, index;
209     register struct DirEntry *firstitem;
210     unsigned short *previtem;
211     firstitem = FindItem(dir,entry,&previtem);
212     if (firstitem == 0) return ENOENT;
213     *previtem = firstitem->next;
214     DRelease(previtem,1);
215     index = DVOffset(firstitem)/32;
216     nitems = NameBlobs(firstitem->name);
217     DRelease(firstitem,0);
218     FreeBlobs(dir,index,nitems);
219     return 0;
220 }
221
222 int FindBlobs (char *dir, int nblobs)
223 {
224     /* Find a bunch of contiguous entries; at least nblobs in a row. */
225     register int i, j, k;
226     int failed = 0;
227     register struct DirHeader *dhp;
228     struct PageHeader *pp;
229     int pgcount;
230
231     dhp = (struct DirHeader *) DRead(dir,0);    /* read the dir header in first. */
232     if (!dhp) return -1;
233     for(i=0;i<BIGMAXPAGES;i++) {
234         if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
235             /* if page could contain enough entries */
236             /* If there are EPP free entries, then the page is not even allocated. */
237             if (i >= MAXPAGES) {
238                 /* this pages exists past the end of the old-style dir */
239                 pgcount = ntohs(dhp->header.pgcount);
240                 if (pgcount == 0) {
241                     pgcount = MAXPAGES;
242                     dhp->header.pgcount = htons(pgcount);
243                 }
244                 if (i > pgcount - 1) {
245                     /* this page is bigger than last allocated page */
246                     AddPage(dir, i);
247                     dhp->header.pgcount = htons(i+1);
248                 }
249             }
250             else if (dhp->alloMap[i] == EPP) {
251                 /* Add the page to the directory. */
252                 AddPage(dir,i);
253                 dhp->alloMap[i] = EPP-1;
254                 dhp->header.pgcount = htons(i+1);
255             }
256             pp = (struct PageHeader *) DRead(dir,i);  /* read the page in. */
257             if (!pp) {
258                 DRelease(dhp, 1);
259                 break;
260             }
261             for(j=0;j<=EPP-nblobs;j++) {
262                 failed = 0;
263                 for(k=0;k<nblobs;k++)
264                     if ((pp->freebitmap[(j+k)>>3]>>((j+k)&7)) & 1) {
265                         failed = 1;
266                         break;
267                     }
268                 if (!failed) break;
269                 failed = 1;
270             }
271             if (!failed) {
272                 /* Here we have the first index in j.  We update the allocation maps
273                   and free up any resources we've got allocated. */
274                 if (i < MAXPAGES) dhp->alloMap[i] -= nblobs;
275                 DRelease(dhp,1);
276                 for (k=0;k<nblobs;k++)
277                     pp->freebitmap[(j+k)>>3] |= 1<<((j+k)&7);
278                 DRelease(pp,1);
279                 return j+i*EPP;
280             }
281             DRelease(pp, 0);    /* This dir page is unchanged. */
282         }
283     }
284     /* If we make it here, the directory is full. */
285     DRelease(dhp, 1);
286     return -1;
287 }
288
289 void AddPage (char *dir, int pageno)
290 {/* Add a page to a directory. */
291     register int i;
292     register struct PageHeader *pp;
293
294     pp = (struct PageHeader *) DNew(dir,pageno);        /* Get a new buffer labelled dir,pageno */
295     pp->tag = htons(1234);
296     if (pageno > 0) pp->pgcount = 0;
297     pp->freecount = EPP-1;      /* The first dude is already allocated */
298     pp->freebitmap[0] = 0x01;
299     for (i=1;i<EPP/8;i++)       /* It's a constant */
300         pp->freebitmap[i] = 0;
301     DRelease(pp,1);
302 }
303
304 void FreeBlobs(char *dir, register int firstblob, int nblobs)
305 {
306     /* Free a whole bunch of directory entries. */
307     register int i;
308     int page;
309     struct DirHeader *dhp;
310     struct PageHeader *pp;
311     page = firstblob/EPP;
312     firstblob -= EPP*page;      /* convert to page-relative entry */
313     dhp = (struct DirHeader *) DRead(dir,0);
314     if (!dhp) return;
315     if (page < MAXPAGES) dhp->alloMap[page] += nblobs;
316     DRelease(dhp,1);
317     pp = (struct PageHeader *) DRead(dir,page);
318     if (pp) for (i=0;i<nblobs;i++)
319         pp->freebitmap[(firstblob+i)>>3] &= ~(1<<((firstblob+i)&7));
320     DRelease(pp,1);
321     }
322
323 int MakeDir (char *dir, afs_int32 *me, afs_int32 *parent)
324 {
325     /* Format an empty directory properly.  Note that the first 13 entries in a directory header
326       page are allocated, 1 to the page header, 4 to the allocation map and 8 to the hash table. */
327     register int i;
328     register struct DirHeader *dhp;
329     dhp = (struct DirHeader *) DNew(dir,0);
330     dhp->header.pgcount = htons(1);
331     dhp->header.tag = htons(1234);
332     dhp->header.freecount = (EPP-DHE-1);
333     dhp->header.freebitmap[0] = 0xff;
334     dhp->header.freebitmap[1] = 0x1f;
335     for(i=2;i<EPP/8;i++) dhp->header.freebitmap[i] = 0;
336     dhp->alloMap[0]=(EPP-DHE-1);
337     for(i=1;i<MAXPAGES;i++)dhp->alloMap[i] = EPP;
338     for(i=0;i<NHASHENT;i++)dhp->hashTable[i] = 0;
339     DRelease(dhp,1);
340     Create(dir,".",me);
341     Create(dir,"..",parent);    /* Virtue is its own .. */
342     return 0;
343     }
344
345 int Lookup (char *dir, char *entry, register afs_int32 *fid)
346 {
347     /* Look up a file name in directory. */
348     register struct DirEntry *firstitem;
349     unsigned short *previtem;
350
351     firstitem = FindItem(dir,entry,&previtem);
352     if (firstitem == 0) return ENOENT;
353     DRelease(previtem,0);
354     fid[1] = ntohl(firstitem->fid.vnode);
355     fid[2] = ntohl(firstitem->fid.vunique);
356     DRelease(firstitem,0);
357     return 0;
358 }
359
360 int LookupOffset (char *dir, char *entry, register afs_int32 *fid, long *offsetp)
361 {
362       /* Look up a file name in directory. */
363       register struct DirEntry *firstitem;
364       unsigned short *previtem;
365
366       firstitem = FindItem(dir,entry,&previtem);
367       if (firstitem == 0) return ENOENT;
368       DRelease(previtem,0);
369       fid[1] = ntohl(firstitem->fid.vnode);
370       fid[2] = ntohl(firstitem->fid.vunique);
371       if (offsetp)
372           *offsetp = DVOffset(firstitem);
373       DRelease(firstitem,0);
374       return 0;
375 }
376
377 int EnumerateDir (char *dir, int (*hookproc)(), void *hook)
378 {
379     /* Enumerate the contents of a directory. */
380     register int i;
381     int num;
382     register struct DirHeader *dhp;
383     register struct DirEntry *ep;
384
385     dhp = (struct DirHeader *) DRead(dir,0);
386     if (!dhp) return EIO;       /* first page should be there */
387     for(i=0; i<NHASHENT; i++) {
388         /* For each hash chain, enumerate everyone on the list. */
389         num = ntohs(dhp->hashTable[i]);
390         while (num != 0) {
391             /* Walk down the hash table list. */
392             DErrno = 0;
393             ep = GetBlob(dir,num);
394             if (!ep) {
395                 if (DErrno) {
396                     /* we failed, return why */
397                     DRelease(dhp, 0);
398                     return DErrno;
399                 }
400                 break;
401             }
402             num = ntohs(ep->next);
403             (*hookproc) (hook, ep->name, ntohl(ep->fid.vnode), ntohl(ep->fid.vunique));
404             DRelease(ep,0);
405         }
406     }
407     DRelease(dhp,0);
408     return 0;
409 }
410
411 int IsEmpty (char *dir)
412 {
413     /* Enumerate the contents of a directory. */
414     register int i;
415     int num;
416     register struct DirHeader *dhp;
417     register struct DirEntry *ep;
418     dhp = (struct DirHeader *) DRead(dir,0);
419     if (!dhp) return 0;
420     for(i=0;i<NHASHENT;i++) {
421         /* For each hash chain, enumerate everyone on the list. */
422         num = ntohs(dhp->hashTable[i]);
423         while (num != 0) {
424             /* Walk down the hash table list. */
425             ep = GetBlob(dir,num);
426             if (!ep) break;
427             if (strcmp(ep->name,"..") && strcmp(ep->name,".")) {
428                 DRelease(ep, 0);
429                 DRelease(dhp, 0);
430                 return 1;
431             }
432             num = ntohs(ep->next);
433             DRelease(ep,0);
434         }
435     }
436     DRelease(dhp,0);
437     return 0;
438 }
439
440 struct DirEntry *GetBlob (char *dir, afs_int32 blobno)
441 {
442     /* Return a pointer to an entry, given its number. */
443     struct DirEntry *ep;
444     ep=DRead(dir,blobno>>LEPP);
445     if (!ep) return 0;
446     return (struct DirEntry *) (((long)ep)+32*(blobno&(EPP-1)));
447 }
448
449 int DirHash (register char *string)
450 {
451     /* Hash a string to a number between 0 and NHASHENT. */
452     register unsigned char tc;
453     register int hval;
454     register int tval;
455     hval = 0;
456     while((tc=(*string++))) {
457         hval *= 173;
458         hval  += tc;
459     }
460     tval = hval & (NHASHENT-1);
461     if (tval == 0) return tval;
462     else if (hval < 0) tval = NHASHENT-tval;
463     return tval;
464 }
465
466 static struct DirEntry *FindItem (char *dir, char *ename,
467         unsigned short **previtem)
468 {
469     /* Find a directory entry, given its name.  This entry returns a pointer to a locked buffer, and a pointer to a locked buffer (in previtem) referencing the found item (to aid the delete code).  If no entry is found, however, no items are left locked, and a null pointer is returned instead. */
470     register int i;
471     register struct DirHeader *dhp;
472     register unsigned short *lp;
473     register struct DirEntry *tp;
474     i = DirHash(ename);
475     dhp = (struct DirHeader *) DRead(dir,0);
476     if (!dhp) return 0;
477     if (dhp->hashTable[i] == 0) {
478         /* no such entry */
479         DRelease(dhp,0);
480         return 0;
481     }
482     tp = GetBlob(dir,(u_short)ntohs(dhp->hashTable[i]));
483     if (!tp) {
484         DRelease(dhp, 0);
485         return 0;
486     }
487     lp = &(dhp->hashTable[i]);
488     while(1) {
489         /* Look at each hash conflict entry. */
490         if (!strcmp(ename,tp->name)) {
491             /* Found our entry. */
492             *previtem = lp;
493             return tp;
494         }
495         DRelease(lp,0);
496         lp = &(tp->next);
497         if (tp->next == 0) {
498             /* The end of the line */
499             DRelease(lp,0);     /* Release all locks. */
500             return 0;
501         }
502         tp = GetBlob(dir,(u_short)ntohs(tp->next));
503         if (!tp) {
504             DRelease(lp, 0);
505             return 0;
506         }
507     }
508 }