view migout.c @ 0:cd351af3a8d2 migout

import source
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 27 Mar 2008 20:03:52 -0400
parents
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>

#include <xfs/dmapi.h>

#include "types.h"
#include "die.h"
#include "common.h"
#include "split.h"

#include "settings.h"

#define IBUFSIZE		1024

static inline u8 hex2bin(char c)
{
	if (c > '9')
		return c - 'a' + 10;
	return c - '0';
}

int extract_fields(char *buf, u8 *handle, size_t *hlen, dm_size_t *fsize)
{
	size_t off;

	*hlen = atoi(buf);

	if (*hlen > HANDLE_MAX_SIZE) {
		warn("handle length over maximum");
		return 1;
	}

	while(*buf++ != '\t')
		;

	for(off=0; off < *hlen; off++, buf+=2) {
		*handle = hex2bin(buf[0]) << 4 |
		          hex2bin(buf[1]);
		handle++;
	}

	buf +=2; /* skip space & tab */

	*fsize = atoi(buf);

	return 0;
}

int mig_files(dm_sessid_t sid)
{
	size_t hlen;
	dm_size_t fsize;
	dm_off_t off;
	dm_token_t token;
	int err;
	char ibuf[IBUFSIZE];
	u8 handle[HANDLE_MAX_SIZE];
	unsigned int change_start, change_end;

	while(fgets(ibuf, IBUFSIZE, stdin)) {
		err = extract_fields(ibuf, handle, &hlen, &fsize);
		if (err) {
			warn("mangled line");
			continue;
		}

		fprintf(stderr, "> handle (len=%d, size=%llu)\n", hlen, fsize);

		err = get_dmchange(sid, handle, hlen, DM_NO_TOKEN, &change_start);
		if (err) {
			warn("failed to get the dmapi change indicator");
			continue;
		}

		err = save_filedata(sid, handle, hlen, fsize);
		if (err) {
			warn("could not save file data");
			continue;
		}

		err = lock_file(sid, handle, hlen, DM_RIGHT_EXCL, &token);
		if (err) {
			warn("could not get exclusive file lock");
			continue;
		}

		err = get_dmchange(sid, handle, hlen, token, &change_end);
		if (err) {
			unlock_file(sid, token);
			warn("failed to get the dmapi change indicator");
			continue;
		}
		if (change_start != change_end) {
			unlock_file(sid, token);
			warn("file changed during stageout, ignoring");
			continue;
		}

		err = save_dataloc(sid, handle, hlen, token);
		if (err) {
			unlock_file(sid, token);
			warn("could not save data location");
			continue;
		}

		err = set_man_regions(sid, handle, hlen, token, fsize, &off);
		if (err) {
			unlock_file(sid, token);
			warn("could not set managed regions");
			continue;
		}

		err = make_nonres(sid, handle, hlen, token, off);
		if (err)
			clear_man_regions(sid, handle, hlen, token);

		unlock_file(sid, token);
	}

	return 0;
}

int main(int argc, char **argv)
{
	dm_sessid_t sid;
	int err;

	if (setup_dmapi(&sid))
		die("Cannot initialize dmapi session");

	err = mig_files(sid);

	if (dm_destroy_session(sid))
		warn("Cannot shutdown dmapi session");

	return err;
}