Ejemplo n.º 1
0
/**
 * Synchronize memory mapping with the file.
 * Called from process context.
 */
static void
tempesta_unmap_file(struct file *file, unsigned long addr, unsigned long len,
		    int node)
{
	mm_segment_t oldfs;
	MArea *ma;
	loff_t off = 0;
	ssize_t r;

	mutex_lock(&map_mtx);

	ma = ma_lookup(addr, node);
	if (!ma) {
		TDB_WARN("Cannot sync memory area for %#lx address at node"
			 " %d\n", addr, node);
		goto err;
	}

	oldfs = get_fs();
	set_fs(get_ds());

	r = kernel_write(file, (void *)ma->start, len, &off);
	if (r != len) {
		TDB_WARN("Cannot sync mapping %lx of size %lu pages\n",
			 ma->start, ma->pages);
		goto err_fs;
	}

err_fs:
	set_fs(oldfs);
err:
	fput(file);
	ma_free(addr, node);

	mutex_unlock(&map_mtx);
}
Ejemplo n.º 2
0
void *
tdb_htrie_open(void *addr, const char *fname, size_t size, int *fd)
{
	void *p;
	struct stat sb = { 0 };

	if (!stat(fname, &sb)) {
		printf("filesize: %ld\n", sb.st_size);
	} else {
		TDB_WARN("no files, create them\n");
	}

	if ((*fd = open(fname, O_RDWR|O_CREAT, O_RDWR)) < 0) {
		perror("ERROR: open failure");
		exit(1);
	}

	if (sb.st_size != size)
		if (fallocate(*fd, 0, 0, size)) {
			perror("ERROR: fallocate failure");
			exit(1);
		}

	/* Use MAP_SHARED to carry changes to underlying file. */
	p = mmap(addr, size, PROT_READ | PROT_WRITE, MAP_SHARED, *fd, 0);
	if (p != addr) {
		perror("ERROR: cannot mmap the file");
		exit(1);
	}
	printf("maped to %p\n", p);

	if (mlock(p, size)) {
		perror("ERROR: mlock failure");
		exit(1);
	}

	return p;
}