Beispiel #1
0
static void
swabvid(struct cpu_disklabel *pcpul)
{
	M_32_SWAP(pcpul->vid_oss);
	M_16_SWAP(pcpul->vid_osl);
	/*
	M_16_SWAP(pcpul->vid_osa_u);
	M_16_SWAP(pcpul->vid_osa_l);
	*/
	M_32_SWAP(pcpul->vid_cas);
}
/**
 * Flush out header onto disk
 */
static int
_ffdb_hput_header (ffdb_htab_t* hashp)
{
  ffdb_hashhdr_t *whdrp;
  ffdb_hashhdr_t whdr;
  unsigned int num_copied = 0;
  unsigned int chksum = 0;

  whdrp = &hashp->hdr;

  if (hashp->mborder == LITTLE_ENDIAN) {
    whdrp = &whdr;
    _ffdb_swap_header_copy(&hashp->hdr, whdrp);
  }

  /* calculate checksum value */
  chksum = __ffdb_crc32_checksum (chksum, (const unsigned char *)whdrp,
				  sizeof(ffdb_hashhdr_t) - sizeof(unsigned int));
  /* hashp->hdr.chksum = chksum; */
  if (hashp->mborder == LITTLE_ENDIAN) 
    M_32_SWAP(chksum);
  whdrp->chksum = chksum;


  /* write the header */
  lseek(hashp->fp, 0, SEEK_SET);
  num_copied = write(hashp->fp, whdrp, sizeof(ffdb_hashhdr_t));
  if (num_copied != sizeof(ffdb_hashhdr_t)) {
    fprintf(stderr, "hash: could not write hash header");
    return -1;
  }

  return 0;
}
Beispiel #3
0
static void
swap_header(HTAB *hashp)
{
	HASHHDR *hdrp;
	int i;

	hdrp = &hashp->hdr;

	M_32_SWAP(hdrp->magic);
	M_32_SWAP(hdrp->version);
	M_32_SWAP(hdrp->lorder);
	M_32_SWAP(hdrp->bsize);
	M_32_SWAP(hdrp->bshift);
	M_32_SWAP(hdrp->dsize);
	M_32_SWAP(hdrp->ssize);
	M_32_SWAP(hdrp->sshift);
	M_32_SWAP(hdrp->ovfl_point);
	M_32_SWAP(hdrp->last_freed);
	M_32_SWAP(hdrp->max_bucket);
	M_32_SWAP(hdrp->high_mask);
	M_32_SWAP(hdrp->low_mask);
	M_32_SWAP(hdrp->ffactor);
	M_32_SWAP(hdrp->nkeys);
	M_32_SWAP(hdrp->hdrpages);
	M_32_SWAP(hdrp->h_charkey);
	for (i = 0; i < NCACHED; i++) {
		M_32_SWAP(hdrp->spares[i]);
		M_16_SWAP(hdrp->bitmaps[i]);
	}
}
Beispiel #4
0
/*
 * __BT_OPEN -- Open a btree.
 *
 * Creates and fills a DB struct, and calls the routine that actually
 * opens the btree.
 *
 * Parameters:
 *	fname:	filename (NULL for in-memory trees)
 *	flags:	open flag bits
 *	mode:	open permission bits
 *	b:	BTREEINFO pointer
 *
 * Returns:
 *	NULL on failure, pointer to DB on success.
 *
 */
DB *
__bt_open(const char *fname, int flags, mode_t mode, const BTREEINFO *openinfo,
    int dflags)
{
	struct stat sb;
	BTMETA m;
	BTREE *t;
	BTREEINFO b;
	DB *dbp;
	pgno_t ncache;
	ssize_t nr;
	size_t temp;
	int machine_lorder;

	t = NULL;

	/*
	 * Intention is to make sure all of the user's selections are okay
	 * here and then use them without checking.  Can't be complete, since
	 * we don't know the right page size, lorder or flags until the backing
	 * file is opened.  Also, the file's page size can cause the cachesize
	 * to change.
	 */
	machine_lorder = byteorder();
	if (openinfo) {
		b = *openinfo;

		/* Flags: R_DUP. */
		if (b.flags & ~(R_DUP))
			goto einval;

		/*
		 * Page size must be indx_t aligned and >= MINPSIZE.  Default
		 * page size is set farther on, based on the underlying file
		 * transfer size.
		 */
		if (b.psize &&
		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
		    b.psize & (sizeof(indx_t) - 1)))
			goto einval;

		/* Minimum number of keys per page; absolute minimum is 2. */
		if (b.minkeypage) {
			if (b.minkeypage < 2)
				goto einval;
		} else
			b.minkeypage = DEFMINKEYPAGE;

		/* If no comparison, use default comparison and prefix. */
		if (b.compare == NULL) {
			b.compare = __bt_defcmp;
			if (b.prefix == NULL)
				b.prefix = __bt_defpfx;
		}

		if (b.lorder == 0)
			b.lorder = machine_lorder;
	} else {
		b.compare = __bt_defcmp;
		b.cachesize = 0;
		b.flags = 0;
		b.lorder = machine_lorder;
		b.minkeypage = DEFMINKEYPAGE;
		b.prefix = __bt_defpfx;
		b.psize = 0;
	}

	/* Check for the ubiquitous PDP-11. */
	if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
		goto einval;

	/* Allocate and initialize DB and BTREE structures. */
	if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
		goto err;
	memset(t, 0, sizeof(BTREE));
	t->bt_fd = -1;			/* Don't close unopened fd on error. */
	t->bt_lorder = b.lorder;
	t->bt_order = NOT;
	t->bt_cmp = b.compare;
	t->bt_pfx = b.prefix;
	t->bt_rfd = -1;

	if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
		goto err;
	memset(t->bt_dbp, 0, sizeof(DB));
	if (t->bt_lorder != machine_lorder)
		F_SET(t, B_NEEDSWAP);

	dbp->type = DB_BTREE;
	dbp->internal = t;
	dbp->close = __bt_close;
	dbp->del = __bt_delete;
	dbp->fd = __bt_fd;
	dbp->get = __bt_get;
	dbp->put = __bt_put;
	dbp->seq = __bt_seq;
	dbp->sync = __bt_sync;

	/*
	 * If no file name was supplied, this is an in-memory btree and we
	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
	 */
	if (fname) {
		switch (flags & O_ACCMODE) {
		case O_RDONLY:
			F_SET(t, B_RDONLY);
			break;
		case O_RDWR:
			break;
		case O_WRONLY:
		default:
			goto einval;
		}
		
		if ((t->bt_fd = open(fname, flags, mode)) == -1)
			goto err;
		if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
			goto err;
	} else {
		if ((flags & O_ACCMODE) != O_RDWR)
			goto einval;
		if ((t->bt_fd = tmp()) == -1)
			goto err;
		F_SET(t, B_INMEM);
	}

	if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
		goto err;

	if (fstat(t->bt_fd, &sb))
		goto err;
	if (sb.st_size) {
		if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
			goto err;
		if (nr != sizeof(BTMETA))
			goto eftype;

		/*
		 * Read in the meta-data.  This can change the notion of what
		 * the lorder, page size and flags are, and, when the page size
		 * changes, the cachesize value can change too.  If the user
		 * specified the wrong byte order for an existing database, we
		 * don't bother to return an error, we just clear the NEEDSWAP
		 * bit.
		 */
		if (m.magic == BTREEMAGIC)
			F_CLR(t, B_NEEDSWAP);
		else {
			F_SET(t, B_NEEDSWAP);
			M_32_SWAP(m.magic);
			M_32_SWAP(m.version);
			M_32_SWAP(m.psize);
			M_32_SWAP(m.free);
			M_32_SWAP(m.nrecs);
			M_32_SWAP(m.flags);
		}
		if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
			goto eftype;
		if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
		    m.psize & (sizeof(indx_t) - 1))
			goto eftype;
		if (m.flags & ~SAVEMETA)
			goto eftype;
		b.psize = m.psize;
		F_SET(t, m.flags);
		t->bt_free = m.free;
		t->bt_nrecs = m.nrecs;
	} else {
		/*
		 * Set the page size to the best value for I/O to this file.
		 * Don't overflow the page offset type.
		 */
		if (b.psize == 0) {
			b.psize = sb.st_blksize;
			if (b.psize < MINPSIZE)
				b.psize = MINPSIZE;
			if (b.psize > MAX_PAGE_OFFSET + 1)
				b.psize = MAX_PAGE_OFFSET + 1;
		}

		/* Set flag if duplicates permitted. */
		if (!(b.flags & R_DUP))
			F_SET(t, B_NODUPS);

		t->bt_free = P_INVALID;
		t->bt_nrecs = 0;
		F_SET(t, B_METADIRTY);
	}

	t->bt_psize = b.psize;

	/* Set the cache size; must be a multiple of the page size. */
	if (b.cachesize && b.cachesize & (b.psize - 1))
		b.cachesize += (~b.cachesize & (b.psize - 1)) + 1;
	if (b.cachesize < b.psize * MINCACHE)
		b.cachesize = b.psize * MINCACHE;

	/* Calculate number of pages to cache. */
	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;

	/*
	 * The btree data structure requires that at least two keys can fit on
	 * a page, but other than that there's no fixed requirement.  The user
	 * specified a minimum number per page, and we translated that into the
	 * number of bytes a key/data pair can use before being placed on an
	 * overflow page.  This calculation includes the page header, the size
	 * of the index referencing the leaf item and the size of the leaf item
	 * structure.  Also, don't let the user specify a minkeypage such that
	 * a key/data pair won't fit even if both key and data are on overflow
	 * pages.
	 */
	temp = (t->bt_psize - BTDATAOFF) / b.minkeypage -
	    (sizeof(indx_t) + NBLEAFDBT(0, 0));
	_DBFIT(temp, indx_t);
	t->bt_ovflsize = (indx_t)temp;
	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
		t->bt_ovflsize =
		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);

	/* Initialize the buffer pool. */
	if ((t->bt_mp =
	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
		goto err;
	if (!F_ISSET(t, B_INMEM))
		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);

	/* Create a root page if new tree. */
	if (nroot(t) == RET_ERROR)
		goto err;

	/* Global flags. */
	if (dflags & DB_LOCK)
		F_SET(t, B_DB_LOCK);
	if (dflags & DB_SHMEM)
		F_SET(t, B_DB_SHMEM);
	if (dflags & DB_TXN)
		F_SET(t, B_DB_TXN);

	return (dbp);

einval:	errno = EINVAL;
	goto err;

eftype:	errno = EFTYPE;
	goto err;

err:	if (t) {
		if (t->bt_dbp)
			free(t->bt_dbp);
		if (t->bt_fd != -1)
			(void)close(t->bt_fd);
		free(t);
	}
	return (NULL);
}
Beispiel #5
0
/*
 * __BT_BPGIN, __BT_BPGOUT --
 *	Convert host-specific number layout to/from the host-independent
 *	format stored on disk.
 *
 * Parameters:
 *	t:	tree
 *	pg:	page number
 *	h:	page to convert
 */
void
__bt_pgin(void *t, pgno_t pg, void *pp)
{
    PAGE *h;
    indx_t i, top;
    u_char flags;
    char *p;

    if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
        return;
    if (pg == P_META) {
        mswap(pp);
        return;
    }

    h = pp;
    M_32_SWAP(h->pgno);
    M_32_SWAP(h->prevpg);
    M_32_SWAP(h->nextpg);
    M_32_SWAP(h->flags);
    M_16_SWAP(h->lower);
    M_16_SWAP(h->upper);

    top = NEXTINDEX(h);
    if ((h->flags & P_TYPE) == P_BINTERNAL)
        for (i = 0; i < top; i++) {
            M_16_SWAP(h->linp[i]);
            p = (char *)GETBINTERNAL(h, i);
            P_32_SWAP(p);
            p += sizeof(u_int32_t);
            P_32_SWAP(p);
            p += sizeof(pgno_t);
            if (*(u_char *)p & P_BIGKEY) {
                p += sizeof(u_char);
                P_32_SWAP(p);
                p += sizeof(pgno_t);
                P_32_SWAP(p);
            }
        }
    else if ((h->flags & P_TYPE) == P_BLEAF)
        for (i = 0; i < top; i++) {
            M_16_SWAP(h->linp[i]);
            p = (char *)GETBLEAF(h, i);
            P_32_SWAP(p);
            p += sizeof(u_int32_t);
            P_32_SWAP(p);
            p += sizeof(u_int32_t);
            flags = *(u_char *)p;
            if (flags & (P_BIGKEY | P_BIGDATA)) {
                p += sizeof(u_char);
                if (flags & P_BIGKEY) {
                    P_32_SWAP(p);
                    p += sizeof(pgno_t);
                    P_32_SWAP(p);
                }
                if (flags & P_BIGDATA) {
                    p += sizeof(u_int32_t);
                    P_32_SWAP(p);
                    p += sizeof(pgno_t);
                    P_32_SWAP(p);
                }
            }
        }
}
/**
 * Swap headers only if the byte order is little endian
 */
static void
_ffdb_swap_header (ffdb_htab_t* hashp)
{
  ffdb_hashhdr_t *hdrp;
  int i;

  hdrp = &hashp->hdr;

  M_32_SWAP(hdrp->magic);
  M_32_SWAP(hdrp->version);
  M_32_SWAP(hdrp->lorder);
  M_32_SWAP(hdrp->bsize);
  M_32_SWAP(hdrp->bshift);
  M_32_SWAP(hdrp->ovfl_point);
  M_32_SWAP(hdrp->max_bucket);
  M_32_SWAP(hdrp->high_mask);
  M_32_SWAP(hdrp->low_mask);
  M_32_SWAP(hdrp->ffactor);
  M_32_SWAP(hdrp->nkeys);
  M_32_SWAP(hdrp->hdrpages);
  M_32_SWAP(hdrp->uinfolen);
  M_16_SWAP(hdrp->uinfo_page);
  M_16_SWAP(hdrp->uinfo_npages);
  M_16_SWAP(hdrp->cfig_page);
  M_16_SWAP(hdrp->cfig_npages);
  M_32_SWAP(hdrp->num_cfigs);
  M_32_SWAP(hdrp->h_charkey);
  M_32_SWAP(hdrp->num_moved_pages);
  for (i = 0; i < NCACHED; i++) 
    M_32_SWAP(hdrp->spares[i]);
  for (i = 0; i < NCACHED; i++) 
    M_32_SWAP(hdrp->free_pages[i]);
  M_32_SWAP(hdrp->chksum);
}