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
10 #include <afsconfig.h>
11 #include <afs/param.h>
14 # if !defined(UKERNEL)
20 # include "h/sysmacros.h"
21 # include "h/signal.h"
25 # if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_LINUX20_ENV)
28 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
29 # include "h/kernel.h"
32 # if defined(AFS_SUN56_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV) || defined(AFS_DARWIN80_ENV)
33 # include "afs/sysincludes.h"
35 # if !defined(AFS_SGI64_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_OBSD48_ENV) && !defined(AFS_NBSD_ENV)
37 # endif /* AFS_SGI64_ENV */
40 # include <sys/mount.h>
41 # include <sys/vnode.h>
42 # include <ufs/inode.h>
44 # if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_HPUX110_ENV)
47 # ifndef AFS_LINUX20_ENV
48 # include "netinet/in.h"
50 # else /* !defined(UKERNEL) */
51 # include "afs/stds.h"
52 # include "afs/sysincludes.h"
53 # endif /* !defined(UKERNEL) */
56 /* These are needed because afs_prototypes.h is not included here */
59 extern int DRead(struct dcache *adc, int page, struct DirBuffer *);
60 extern int DNew(struct dcache *adc, int page, struct DirBuffer *);
62 # include "afs/afs_osi.h"
66 # ifdef AFS_LINUX20_ENV
67 # include "h/string.h"
78 /* Local static prototypes */
79 static int FindBlobs(dir_file_t, int);
80 static void AddPage(dir_file_t, int);
81 static void FreeBlobs(dir_file_t, int, int);
82 static int FindItem(dir_file_t, char *, struct DirBuffer *,
85 /* Find out how many entries are required to store a name. */
87 afs_dir_NameBlobs(char *name)
91 return 1 + ((i + 15) >> 5);
94 /* Create an entry in a file. Dir is a file representation, while entry is
97 afs_dir_Create(dir_file_t dir, char *entry, void *voidfid)
99 afs_int32 *vfid = (afs_int32 *) voidfid;
102 struct DirBuffer entrybuf, prevbuf, headerbuf;
104 struct DirHeader *dhp;
106 /* check name quality */
110 /* First check if file already exists. */
111 if (FindItem(dir, entry, &prevbuf, &entrybuf) == 0) {
112 DRelease(&entrybuf, 0);
113 DRelease(&prevbuf, 0);
114 printf("Exists ...\n");
118 blobs = afs_dir_NameBlobs(entry); /* number of entries required */
119 firstelt = FindBlobs(dir, blobs);
121 return EFBIG; /* directory is full */
123 /* First, we fill in the directory entry. */
124 if (afs_dir_GetBlob(dir, firstelt, &entrybuf) != 0)
126 ep = (struct DirEntry *)entrybuf.data;
129 ep->fid.vnode = htonl(vfid[1]);
130 ep->fid.vunique = htonl(vfid[2]);
131 strcpy(ep->name, entry);
133 /* Now we just have to thread it on the hash table list. */
134 if (DRead(dir, 0, &headerbuf) != 0) {
135 DRelease(&entrybuf, 1);
138 dhp = (struct DirHeader *)headerbuf.data;
140 i = afs_dir_DirHash(entry);
141 ep->next = dhp->hashTable[i];
142 dhp->hashTable[i] = htons(firstelt);
143 DRelease(&headerbuf, 1);
144 DRelease(&entrybuf, 1);
149 afs_dir_Length(dir_file_t dir)
152 struct DirBuffer headerbuf;
153 struct DirHeader *dhp;
155 if (DRead(dir, 0, &headerbuf) != 0)
157 dhp = (struct DirHeader *)headerbuf.data;
159 if (dhp->header.pgcount != 0)
160 ctr = ntohs(dhp->header.pgcount);
162 /* old style, count the pages */
164 for (i = 0; i < MAXPAGES; i++)
165 if (dhp->alloMap[i] != EPP)
168 DRelease(&headerbuf, 0);
169 return ctr * AFS_PAGESIZE;
172 /* Delete an entry from a directory, including update of all free entry
175 afs_dir_Delete(dir_file_t dir, char *entry)
179 struct DirBuffer entrybuf, prevbuf;
180 struct DirEntry *firstitem;
181 unsigned short *previtem;
183 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
186 firstitem = (struct DirEntry *)entrybuf.data;
187 previtem = (unsigned short *)prevbuf.data;
189 *previtem = firstitem->next;
190 DRelease(&prevbuf, 1);
191 index = DVOffset(&entrybuf) / 32;
192 nitems = afs_dir_NameBlobs(firstitem->name);
193 DRelease(&entrybuf, 0);
194 FreeBlobs(dir, index, nitems);
198 /* Find a bunch of contiguous entries; at least nblobs in a row. */
200 FindBlobs(dir_file_t dir, int nblobs)
204 struct DirBuffer headerbuf, pagebuf;
205 struct DirHeader *dhp;
206 struct PageHeader *pp;
209 /* read the dir header in first. */
210 if (DRead(dir, 0, &headerbuf) != 0)
212 dhp = (struct DirHeader *)headerbuf.data;
214 for (i = 0; i < BIGMAXPAGES; i++) {
215 if (i >= MAXPAGES || dhp->alloMap[i] >= nblobs) {
216 /* if page could contain enough entries */
217 /* If there are EPP free entries, then the page is not even allocated. */
219 /* this pages exists past the end of the old-style dir */
220 pgcount = ntohs(dhp->header.pgcount);
223 dhp->header.pgcount = htons(pgcount);
225 if (i > pgcount - 1) {
226 /* this page is bigger than last allocated page */
228 dhp->header.pgcount = htons(i + 1);
230 } else if (dhp->alloMap[i] == EPP) {
231 /* Add the page to the directory. */
233 dhp->alloMap[i] = EPP - 1;
234 dhp->header.pgcount = htons(i + 1);
237 /* read the page in. */
238 if (DRead(dir, i, &pagebuf) != 0) {
239 DRelease(&headerbuf, 1);
242 pp = (struct PageHeader *)pagebuf.data;
243 for (j = 0; j <= EPP - nblobs; j++) {
245 for (k = 0; k < nblobs; k++)
246 if ((pp->freebitmap[(j + k) >> 3] >> ((j + k) & 7)) & 1) {
255 /* Here we have the first index in j. We update the allocation maps
256 * and free up any resources we've got allocated. */
258 dhp->alloMap[i] -= nblobs;
259 DRelease(&headerbuf, 1);
260 for (k = 0; k < nblobs; k++)
261 pp->freebitmap[(j + k) >> 3] |= 1 << ((j + k) & 7);
262 DRelease(&pagebuf, 1);
265 DRelease(&pagebuf, 0); /* This dir page is unchanged. */
268 /* If we make it here, the directory is full. */
269 DRelease(&headerbuf, 1);
274 AddPage(dir_file_t dir, int pageno)
275 { /* Add a page to a directory. */
277 struct PageHeader *pp;
278 struct DirBuffer pagebuf;
280 /* Get a new buffer labelled dir,pageno */
281 DNew(dir, pageno, &pagebuf);
282 pp = (struct PageHeader *)pagebuf.data;
284 pp->tag = htons(1234);
287 pp->freecount = EPP - 1; /* The first dude is already allocated */
288 pp->freebitmap[0] = 0x01;
289 for (i = 1; i < EPP / 8; i++) /* It's a constant */
290 pp->freebitmap[i] = 0;
291 DRelease(&pagebuf, 1);
294 /* Free a whole bunch of directory entries. */
297 FreeBlobs(dir_file_t dir, int firstblob, int nblobs)
301 struct DirBuffer headerbuf, pagehdbuf;
302 struct DirHeader *dhp;
303 struct PageHeader *pp;
304 page = firstblob / EPP;
305 firstblob -= EPP * page; /* convert to page-relative entry */
307 if (DRead(dir, 0, &headerbuf) != 0)
309 dhp = (struct DirHeader *)headerbuf.data;
312 dhp->alloMap[page] += nblobs;
314 DRelease(&headerbuf, 1);
316 if (DRead(dir, page, &pagehdbuf) != 0)
318 pp = (struct PageHeader *)pagehdbuf.data;
320 for (i = 0; i < nblobs; i++)
321 pp->freebitmap[(firstblob + i) >> 3] &= ~(1 << ((firstblob + i) & 7));
323 DRelease(&pagehdbuf, 1);
327 * Format an empty directory properly. Note that the first 13 entries in a
328 * directory header page are allocated, 1 to the page header, 4 to the
329 * allocation map and 8 to the hash table.
332 afs_dir_MakeDir(dir_file_t dir, afs_int32 * me, afs_int32 * parent)
335 struct DirBuffer buffer;
336 struct DirHeader *dhp;
338 DNew(dir, 0, &buffer);
339 dhp = (struct DirHeader *)buffer.data;
341 dhp->header.pgcount = htons(1);
342 dhp->header.tag = htons(1234);
343 dhp->header.freecount = (EPP - DHE - 1);
344 dhp->header.freebitmap[0] = 0xff;
345 dhp->header.freebitmap[1] = 0x1f;
346 for (i = 2; i < EPP / 8; i++)
347 dhp->header.freebitmap[i] = 0;
348 dhp->alloMap[0] = (EPP - DHE - 1);
349 for (i = 1; i < MAXPAGES; i++)
350 dhp->alloMap[i] = EPP;
351 for (i = 0; i < NHASHENT; i++)
352 dhp->hashTable[i] = 0;
353 DRelease(&buffer, 1);
354 afs_dir_Create(dir, ".", me);
355 afs_dir_Create(dir, "..", parent); /* Virtue is its own .. */
359 /* Look up a file name in directory. */
362 afs_dir_Lookup(dir_file_t dir, char *entry, void *voidfid)
364 afs_int32 *fid = (afs_int32 *) voidfid;
365 struct DirBuffer firstbuf, prevbuf;
366 struct DirEntry *firstitem;
368 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
370 DRelease(&prevbuf, 0);
371 firstitem = (struct DirEntry *)firstbuf.data;
373 fid[1] = ntohl(firstitem->fid.vnode);
374 fid[2] = ntohl(firstitem->fid.vunique);
375 DRelease(&firstbuf, 0);
379 /* Look up a file name in directory. */
382 afs_dir_LookupOffset(dir_file_t dir, char *entry, void *voidfid,
385 afs_int32 *fid = (afs_int32 *) voidfid;
386 struct DirBuffer firstbuf, prevbuf;
387 struct DirEntry *firstitem;
389 if (FindItem(dir, entry, &prevbuf, &firstbuf) != 0)
391 DRelease(&prevbuf, 0);
392 firstitem = (struct DirEntry *)firstbuf.data;
394 fid[1] = ntohl(firstitem->fid.vnode);
395 fid[2] = ntohl(firstitem->fid.vunique);
397 *offsetp = DVOffset(&firstbuf);
398 DRelease(&firstbuf, 0);
403 * Enumerate the contents of a directory. Break when hook function
408 afs_dir_EnumerateDir(dir_file_t dir, int (*proc) (void *, char *name,
415 struct DirBuffer headerbuf, entrybuf;
416 struct DirHeader *dhp;
420 if (DRead(dir, 0, &headerbuf) != 0)
422 dhp = (struct DirHeader *)headerbuf.data;
424 for (i = 0; i < NHASHENT; i++) {
425 /* For each hash chain, enumerate everyone on the list. */
426 num = ntohs(dhp->hashTable[i]);
428 /* Walk down the hash table list. */
430 if (afs_dir_GetBlob(dir, num, &entrybuf) != 0) {
432 /* we failed, return why */
433 DRelease(&headerbuf, 0);
438 ep = (struct DirEntry *)entrybuf.data;
440 num = ntohs(ep->next);
441 code = (*proc) (hook, ep->name, ntohl(ep->fid.vnode),
442 ntohl(ep->fid.vunique));
443 DRelease(&entrybuf, 0);
448 DRelease(&headerbuf, 0);
453 afs_dir_IsEmpty(dir_file_t dir)
455 /* Enumerate the contents of a directory. */
458 struct DirBuffer headerbuf, entrybuf;
459 struct DirHeader *dhp;
462 if (DRead(dir, 0, &headerbuf) != 0)
464 dhp = (struct DirHeader *)headerbuf.data;
466 for (i = 0; i < NHASHENT; i++) {
467 /* For each hash chain, enumerate everyone on the list. */
468 num = ntohs(dhp->hashTable[i]);
470 /* Walk down the hash table list. */
471 if (afs_dir_GetBlob(dir, num, &entrybuf) != 0);
473 ep = (struct DirEntry *)entrybuf.data;
474 if (strcmp(ep->name, "..") && strcmp(ep->name, ".")) {
475 DRelease(&entrybuf, 0);
476 DRelease(&headerbuf, 0);
479 num = ntohs(ep->next);
480 DRelease(&entrybuf, 0);
483 DRelease(&headerbuf, 0);
488 afs_dir_GetBlob(dir_file_t dir, afs_int32 blobno, struct DirBuffer *buffer)
492 memset(buffer, 0, sizeof(struct DirBuffer));
494 code = DRead(dir, blobno >> LEPP, buffer);
498 buffer->data = (void *)(((long)buffer->data) + 32 * (blobno & (EPP - 1)));
504 afs_dir_DirHash(char *string)
506 /* Hash a string to a number between 0 and NHASHENT. */
511 while ((tc = (*string++))) {
515 tval = hval & (NHASHENT - 1);
518 else if (hval >= 1<<31)
519 tval = NHASHENT - tval;
524 /* Find a directory entry, given its name. This entry returns a pointer
525 * to a locked buffer, and a pointer to a locked buffer (in previtem)
526 * referencing the found item (to aid the delete code). If no entry is
527 * found, however, no items are left locked, and a null pointer is
528 * returned instead. */
531 FindItem(dir_file_t dir, char *ename, struct DirBuffer *prevbuf,
532 struct DirBuffer *itembuf )
535 struct DirBuffer curr, prev;
536 struct DirHeader *dhp;
539 memset(prevbuf, 0, sizeof(struct DirBuffer));
540 memset(itembuf, 0, sizeof(struct DirBuffer));
542 code = DRead(dir, 0, &prev);
545 dhp = (struct DirHeader *)prev.data;
547 i = afs_dir_DirHash(ename);
548 if (dhp->hashTable[i] == 0) {
554 code = afs_dir_GetBlob(dir, (u_short) ntohs(dhp->hashTable[i]),
561 prev.data = &(dhp->hashTable[i]);
564 /* Look at each entry on the hash chain */
565 tp = (struct DirEntry *)curr.data;
566 if (!strcmp(ename, tp->name)) {
574 /* The end of the line */
580 prev.data = &(tp->next);
582 code = afs_dir_GetBlob(dir, (u_short) ntohs(tp->next), &curr);
592 FindFid (void *dir, afs_uint32 vnode, afs_uint32 unique,
593 struct DirBuffer *itembuf)
595 /* Find a directory entry, given the vnode and uniquifier of a object.
596 * This entry returns a pointer to a locked buffer. If no entry is found,
597 * however, no items are left locked, and a null pointer is returned
602 struct DirBuffer curr, header;
603 struct DirHeader *dhp;
606 memset(itembuf, 0, sizeof(struct DirBuffer));
608 code = DRead(dir, 0, &header);
611 dhp = (struct DirHeader *)header.data;
613 for (i=0; i<NHASHENT; i++) {
614 if (dhp->hashTable[i] != 0) {
615 code = afs_dir_GetBlob(dir, (u_short)ntohs(dhp->hashTable[i]),
618 DRelease(&header, 0);
622 while (curr.data != NULL) {
623 tp = (struct DirEntry *)curr.data;
625 if (vnode == ntohl(tp->fid.vnode)
626 && unique == ntohl(tp->fid.vunique)) {
627 DRelease(&header, 0);
638 code = afs_dir_GetBlob(dir, (u_short)ntohs(next), &curr);
640 DRelease(&header, 0);
646 DRelease(&header, 0);
651 afs_dir_InverseLookup(void *dir, afs_uint32 vnode, afs_uint32 unique,
652 char *name, afs_uint32 length)
654 /* Look for the name pointing to given vnode and unique in a directory */
655 struct DirBuffer entrybuf;
656 struct DirEntry *entry;
659 if (FindFid(dir, vnode, unique, &entrybuf) != 0)
661 entry = (struct DirEntry *)entrybuf.data;
663 if (strlen(entry->name) >= length)
666 strcpy(name, entry->name);
667 DRelease(&entrybuf, 0);
672 * Change an entry fid.
675 * \param entry The entry name.
676 * \param old_fid The old find in MKFid format (host order).
677 * It can be omitted if you don't need a safety check...
678 * \param new_fid The new find in MKFid format (host order).
681 afs_dir_ChangeFid(dir_file_t dir, char *entry, afs_uint32 *old_fid,
684 struct DirBuffer prevbuf, entrybuf;
685 struct DirEntry *firstitem;
686 struct MKFid *fid_old = (struct MKFid *) old_fid;
687 struct MKFid *fid_new = (struct MKFid *) new_fid;
690 if (FindItem(dir, entry, &prevbuf, &entrybuf) != 0)
692 firstitem = (struct DirEntry *)entrybuf.data;
693 DRelease(&prevbuf, 1);
697 ((htonl(fid_old->vnode) == firstitem->fid.vnode) &&
698 (htonl(fid_old->vunique) == firstitem->fid.vunique))) {
700 firstitem->fid.vnode = htonl(fid_new->vnode);
701 firstitem->fid.vunique = htonl(fid_new->vunique);
704 DRelease(&entrybuf, 1);