Example #1
0
static struct storage *
smu_alloc(const struct stevedore *st, size_t size)
{
	struct smu *smu;

	Lck_Lock(&smu_mtx);
	VSC_C_main->sma_nreq++;
	if (VSC_C_main->sma_nbytes + size > smu_max)
		size = 0;
	else {
		VSC_C_main->sma_nobj++;
		VSC_C_main->sma_nbytes += size;
		VSC_C_main->sma_balloc += size;
	}
	Lck_Unlock(&smu_mtx);

	if (size == 0)
		return (NULL);

	smu = umem_zalloc(sizeof *smu, UMEM_DEFAULT);
	if (smu == NULL)
		return (NULL);
	smu->sz = size;
	smu->s.priv = smu;
	smu->s.ptr = umem_alloc(size, UMEM_DEFAULT);
	XXXAN(smu->s.ptr);
	smu->s.len = 0;
	smu->s.space = size;
	smu->s.fd = -1;
	smu->s.stevedore = st;
	smu->s.magic = STORAGE_MAGIC;
	return (&smu->s);
}
Example #2
0
/*
 * Open /dev/fm and return a handle.  ver is the overall interface version.
 */
static fmd_agent_hdl_t *
fmd_agent_open_dev(int ver, int mode)
{
	fmd_agent_hdl_t *hdl;
	int fd, err;
	nvlist_t *nvl;

	if ((fd = open("/dev/fm", mode)) < 0)
		return (NULL); /* errno is set for us */

	if ((hdl = umem_alloc(sizeof (fmd_agent_hdl_t),
	    UMEM_DEFAULT)) == NULL) {
		err = errno;
		(void) close(fd);
		errno = err;
		return (NULL);
	}

	hdl->agent_devfd = fd;
	hdl->agent_version = ver;

	/*
	 * Get the individual interface versions.
	 */
	if ((err = fmd_agent_nvl_ioctl(hdl, FM_IOC_VERSIONS, ver, NULL, &nvl))
	    < 0) {
		(void) close(fd);
		umem_free(hdl, sizeof (fmd_agent_hdl_t));
		errno = err;
		return (NULL);
	}

	hdl->agent_ioc_versions = nvl;
	return (hdl);
}
Example #3
0
static int
ik_rec_alloc(struct btr_instance *tins, daos_iov_t *key_iov,
	      daos_iov_t *val_iov, struct btr_record *rec)
{
	TMMID(struct ik_rec)   irec_mmid;
	struct ik_rec	      *irec;
	char		      *vbuf;

	irec_mmid = umem_znew_typed(&tins->ti_umm, struct ik_rec);
	D_ASSERT(!TMMID_IS_NULL(irec_mmid)); /* lazy bone... */

	irec = umem_id2ptr_typed(&tins->ti_umm, irec_mmid);

	irec->ir_key = *(int *)key_iov->iov_buf;
	irec->ir_val_size = irec->ir_val_msize = val_iov->iov_len;

	irec->ir_val_mmid = umem_alloc(&tins->ti_umm, val_iov->iov_len);
	D_ASSERT(!UMMID_IS_NULL(irec->ir_val_mmid));

	vbuf = umem_id2ptr(&tins->ti_umm, irec->ir_val_mmid);
	memcpy(vbuf, (char *)val_iov->iov_buf, val_iov->iov_len);

	rec->rec_mmid = umem_id_t2u(irec_mmid);
	return 0;
}
Example #4
0
static int
ik_rec_update(struct btr_instance *tins, struct btr_record *rec,
	       daos_iov_t *key, daos_iov_t *val_iov)
{
	struct umem_instance	*umm = &tins->ti_umm;
	struct ik_rec		*irec;
	char			*val;
	TMMID(struct ik_rec)	 irec_mmid;

	irec_mmid = umem_id_u2t(rec->rec_mmid, struct ik_rec);
	irec = umem_id2ptr_typed(umm, irec_mmid);

	if (irec->ir_val_msize >= val_iov->iov_len) {
		umem_tx_add(umm, irec->ir_val_mmid, irec->ir_val_msize);

	} else {
		umem_tx_add_mmid_typed(umm, irec_mmid);
		umem_free(umm, irec->ir_val_mmid);

		irec->ir_val_msize = val_iov->iov_len;
		irec->ir_val_mmid = umem_alloc(umm, val_iov->iov_len);
		D_ASSERT(!UMMID_IS_NULL(irec->ir_val_mmid));
	}
	val = umem_id2ptr(umm, irec->ir_val_mmid);

	memcpy(val, val_iov->iov_buf, val_iov->iov_len);
	irec->ir_val_size = val_iov->iov_len;
	return 0;
}
Example #5
0
int main(int argc, char** argv){
    ZVM_UMEM_INIT;

    int c=0;
    int i;
    const int size = 1024*1024;
    for (i=0; i < 100000; i++){
	c+=size+i;
	void* addr = umem_alloc(size+i, UMEM_NOFAIL);
	if ( addr != NULL ){
	    printf("OK i=%d alloc size=%d, addr=%p\n", i, size+i, addr );
	    umem_free(addr, size+i);
	}
	else{
	    printf("FAIL i=%d alloc size=%d, addr=%p\n", i, size+i, addr );
	}
    }

    /* write to default device (in our case it is stdout) */
    printf("hello, world\n");

    /* write to user log (stderr) */
    fprintf(stderr, "hello, world\n");

    return 0;
}
Example #6
0
/*
 * The buf points into list of \n delimited lines.  We copy it,
 * removing the newline and adding a \0.
 */
static char *
mystrcpy(char *buf, int len)
{
	char *res = umem_alloc(len, UMEM_NOFAIL);

	(void) memcpy(res, buf, len - 1);
	res[len - 1] = '\0';
	return (res);
}
Example #7
0
int
main()
{
	if (daemonize_self() == 1)
		return (1);

	max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
	max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;

	assert(max_scf_fmri_size > 0);
	assert(max_scf_name_size > 0);

	if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
		syslog(LOG_ERR | LOG_DAEMON, "scf_handle_create failed: %s\n",
		    scf_strerror(scf_error()));
		return (1);
	}

	repository_rebind(h);

	scratch_fmri = umem_alloc(max_scf_fmri_size, UMEM_DEFAULT);
	scratch_name = umem_alloc(max_scf_name_size, UMEM_DEFAULT);

	if (scratch_fmri == NULL || scratch_name == NULL) {
		syslog(LOG_ERR | LOG_DAEMON, "Out of memory");
		return (1);
	}

	inst = scf_instance_create(h);
	snap = scf_snapshot_create(h);
	scratch_pg = scf_pg_create(h);
	scratch_prop = scf_property_create(h);
	scratch_v = scf_value_create(h);

	if (inst == NULL || snap == NULL || scratch_pg == NULL ||
	    scratch_prop == NULL || scratch_v == NULL) {
		syslog(LOG_ERR | LOG_DAEMON, "Initialization failed: %s\n",
		    scf_strerror(scf_error()));
		return (1);
	}

	return (repository_event_wait());
}
Example #8
0
/*ARGSUSED*/
taskq_t *
taskq_create(const char *name, int nthreads,
	int minalloc, int maxalloc, unsigned int flags)
{
	taskq_t *tq;
	int t;

	tq = umem_zalloc(sizeof(taskq_t), 0);
	if (!tq)
		return NULL;

	if (flags & TASKQ_THREADS_CPU_PCT) {
		int pct;
		assert(nthreads >= 0);
		assert(nthreads <= taskq_cpupct_max_percent);
		pct = MIN(nthreads, taskq_cpupct_max_percent);
		pct = MAX(pct, 0);

		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
	} else {
		assert(nthreads >= 1);
	}

	rwinit(&tq->tq_threadlock);
	mxinit(&tq->tq_lock);
	condinit(&tq->tq_dispatch_cv);
	condinit(&tq->tq_wait_cv);
	condinit(&tq->tq_maxalloc_cv);
	(void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1);
	tq->tq_flags = flags | TASKQ_ACTIVE;
	tq->tq_active = nthreads;
	tq->tq_nthreads = nthreads;
	tq->tq_minalloc = minalloc;
	tq->tq_maxalloc = maxalloc;
	tq->tq_task.tqent_next = &tq->tq_task;
	tq->tq_task.tqent_prev = &tq->tq_task;
	tq->tq_threadlist =
	    umem_alloc(nthreads * sizeof (pthread_t), UMEM_NOFAIL);

	if (flags & TASKQ_PREPOPULATE) {
		mxlock(&tq->tq_lock);
		while (minalloc-- > 0)
			task_free(tq, task_alloc(tq, UMEM_NOFAIL));
		mxunlock(&tq->tq_lock);
	}

	for (t = 0; t < nthreads; t++)
		pthread_create(&tq->tq_threadlist[t], NULL, taskq_thread, tq);

	return (tq);
}
Example #9
0
static taskq_ent_t *
task_alloc(taskq_t *tq, int tqflags)
{
	taskq_ent_t *t;
	struct timespec ts;
	int err;

again:	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
		tq->tq_freelist = t->tqent_next;
	} else {
		if (tq->tq_nalloc >= tq->tq_maxalloc) {
			if (!(tqflags & UMEM_NOFAIL))
				return (NULL);

			/*
			 * We don't want to exceed tq_maxalloc, but we can't
			 * wait for other tasks to complete (and thus free up
			 * task structures) without risking deadlock with
			 * the caller.  So, we just delay for one second
			 * to throttle the allocation rate. If we have tasks
			 * complete before one second timeout expires then
			 * taskq_ent_free will signal us and we will
			 * immediately retry the allocation.
			 */
			tq->tq_maxalloc_wait++;

			ts.tv_sec = 1;
			ts.tv_nsec = 0;
			err = condreltimedwait(&tq->tq_maxalloc_cv,
			    &tq->tq_lock, &ts);

			tq->tq_maxalloc_wait--;
			if (err == 0)
				goto again;		/* signaled */
		}
		mxunlock(&tq->tq_lock);

		t = umem_alloc(sizeof (taskq_ent_t), tqflags);

		mxlock(&tq->tq_lock);
		if (t != NULL)
			tq->tq_nalloc++;
	}
	return (t);
}
Example #10
0
static void
smu_trim(const struct storage *s, size_t size)
{
	struct smu *smu;
	void *p;

	CHECK_OBJ_NOTNULL(s, STORAGE_MAGIC);
	smu = s->priv;
	assert(smu->sz == smu->s.space);
	if ((p = umem_alloc(size, UMEM_DEFAULT)) != NULL) {
		memcpy(p, smu->s.ptr, size);
		umem_free(smu->s.ptr, smu->s.space);
		Lck_Lock(&smu_mtx);
		VSC_C_main->sma_nbytes -= (smu->sz - size);
		VSC_C_main->sma_bfree += smu->sz - size;
		smu->sz = size;
		Lck_Unlock(&smu_mtx);
		smu->s.ptr = p;
		smu->s.space = size;
	}
}
Example #11
0
void *
inj_alloc(size_t sz)
{
	return (umem_alloc(sz, UMEM_NOFAIL));
}
Example #12
0
/*
 * Perform /dev/fm ioctl.  The input and output data are represented by
 * name-value lists (nvlists).
 */
int
fmd_agent_nvl_ioctl(fmd_agent_hdl_t *hdl, int cmd, uint32_t ver,
    nvlist_t *innvl, nvlist_t **outnvlp)
{
	fm_ioc_data_t fid;
	int err = 0;
	char *inbuf = NULL, *outbuf = NULL;
	size_t insz = 0, outsz = 0;

	if (innvl != NULL) {
		if ((err = nvlist_size(innvl, &insz, NV_ENCODE_NATIVE)) != 0)
			return (err);
		if (insz > FM_IOC_MAXBUFSZ)
			return (ENAMETOOLONG);
		if ((inbuf = umem_alloc(insz, UMEM_DEFAULT)) == NULL)
			return (errno);

		if ((err = nvlist_pack(innvl, &inbuf, &insz,
		    NV_ENCODE_NATIVE, 0)) != 0) {
			umem_free(inbuf, insz);
			return (err);
		}
	}

	if (outnvlp != NULL) {
		outsz = FM_IOC_OUT_BUFSZ;
	}
	for (;;) {
		if (outnvlp != NULL) {
			outbuf = umem_alloc(outsz, UMEM_DEFAULT);
			if (outbuf == NULL) {
				err = errno;
				break;
			}
		}

		fid.fid_version = ver;
		fid.fid_insz = insz;
		fid.fid_inbuf = inbuf;
		fid.fid_outsz = outsz;
		fid.fid_outbuf = outbuf;

		if (ioctl(hdl->agent_devfd, cmd, &fid) < 0) {
			if (errno == ENAMETOOLONG && outsz != 0 &&
			    outsz < (FM_IOC_OUT_MAXBUFSZ / 2)) {
				umem_free(outbuf, outsz);
				outsz *= 2;
				outbuf = umem_alloc(outsz, UMEM_DEFAULT);
				if (outbuf == NULL) {
					err = errno;
					break;
				}
			} else {
				err = errno;
				break;
			}
		} else if (outnvlp != NULL) {
			err = nvlist_unpack(fid.fid_outbuf, fid.fid_outsz,
			    outnvlp, 0);
			break;
		} else {
			break;
		}
	}

	if (inbuf != NULL)
		umem_free(inbuf, insz);
	if (outbuf != NULL)
		umem_free(outbuf, outsz);

	return (err);
}
Example #13
0
static int
repository_event_wait()
{
	scf_propertygroup_t *pg;
	char *fmri, *scratch;
	const char *inst_name, *pg_name;
	ssize_t res;

	if ((fmri = umem_alloc(max_scf_fmri_size, UMEM_DEFAULT)) == NULL) {
		syslog(LOG_ERR | LOG_DAEMON, "Out of memory");
		return (1);
	}

	if ((scratch = umem_alloc(max_scf_fmri_size, UMEM_DEFAULT)) == NULL) {
		syslog(LOG_ERR | LOG_DAEMON, "Out of memory");
		return (1);
	}

	if ((pg = scf_pg_create(h)) == NULL) {
		syslog(LOG_ERR | LOG_DAEMON, "scf_pg_create failed: %s\n",
		    scf_strerror(scf_error()));
		return (1);
	}

	repository_notify_setup(h);

	for (;;) {
		/*
		 * Calling _scf_notify_wait which will block this thread
		 * until it's notified of a framework event.
		 *
		 * Note: fmri is only set on delete events.
		 */
		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
		if (res < 0) {
			syslog(LOG_ERR | LOG_DAEMON, "_scf_notify_wait "
			    "failed: %s\n", scf_strerror(scf_error()));
			repository_setup();
		} else if (res == 0) {
			if (repository_event_process(pg))
				syslog(LOG_ERR | LOG_DAEMON, "Service may have "
				    "incorrect IPfilter configuration\n");
		} else {
			/*
			 * The received event is a deletion of a service,
			 * instance or pg. If it's a deletion of an instance,
			 * update the instance's IPfilter configuration.
			 */
			syslog(LOG_DEBUG | LOG_DAEMON, "Deleted: %s", fmri);

			(void) strlcpy(scratch, fmri, max_scf_fmri_size);
			if (scf_parse_svc_fmri(scratch, NULL, NULL, &inst_name,
			    &pg_name, NULL) != SCF_SUCCESS)
				continue;

			if (inst_name != NULL && pg_name == NULL) {
				(void) ipfilter_update(fmri);
			}
		}
	}

	/*NOTREACHED*/
}
Example #14
0
int
main(int argc, char *argv[])
{
	off_t		len, off = 0;
	int		c, fd, options = 0, whence = SEEK_DATA;
	struct stat	statbuf;
	char		*fname;
	list_t		seg_list;
	seg_t		*seg = NULL;

	list_create(&seg_list, sizeof (seg_t), offsetof(seg_t, seg_node));

	while ((c = getopt(argc, argv, "dhv")) != -1) {
		switch (c) {
		case 'd':
			options |= PRINT_DATA;
			break;
		case 'h':
			options |= PRINT_HOLE;
			break;
		case 'v':
			options |= PRINT_VERBOSE;
			break;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		usage("Incorrect number of arguments.", 1);

	if ((fname = argv[0]) == NULL)
		usage("No filename provided.", 1);

	if ((fd = open(fname, O_LARGEFILE | O_RDONLY)) < 0) {
		perror("open failed");
		exit(1);
	}

	if (fstat(fd, &statbuf) != 0) {
		perror("fstat failed");
		exit(1);
	}
	len = statbuf.st_size;

	whence = starts_with_hole(fd);
	while ((off = lseek(fd, off, whence)) != -1) {
		seg_t	*s;

		seg = umem_alloc(sizeof (seg_t), UMEM_DEFAULT);
		seg->seg_type = whence;
		seg->seg_offset = off;

		list_insert_tail(&seg_list, seg);
		if ((s = list_prev(&seg_list, seg)) != NULL)
			s->seg_len = seg->seg_offset - s->seg_offset;

		whence = whence == SEEK_HOLE ? SEEK_DATA : SEEK_HOLE;
	}
	if (errno != ENXIO) {
		perror("lseek failed");
		exit(1);
	}
	(void) close(fd);

	/*
	 * If this file ends with a hole block, then populate the length of
	 * the last segment, otherwise this is the end of the file, so
	 * discard the remaining zero length segment.
	 */
	if (seg && seg->seg_offset != len) {
		seg->seg_len = len - seg->seg_offset;
	} else {
		(void) list_remove_tail(&seg_list);
	}

	print_list(&seg_list, fname, options);
	list_destroy(&seg_list);
	return (0);
}
Example #15
0
static void *
cpu_alloc(size_t size)
{
	return (umem_alloc(size, UMEM_DEFAULT));
}