shdconfig.c 22.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef lint
static const char rcsid[] =
  "$FreeBSD: src/sbin/shdconfig/shdconfig.c,v 1.16.2.2 2000/12/11 01:03:25 obrien Exp $";
#endif /* not lint */

#include <sys/param.h>
#include <sys/linker.h>
#include <sys/disklabel.h>
#include <sys/stat.h>
#include <sys/module.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/devicestat.h>
#include <sys/shdvar.h>

#include "pathnames.h"
#include "trie.h"

29
30
extern struct proc *curproc;

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define MAXBUF 65316
#define BLOCKSIZE 512

static	int lineno = 0;
static	int verbose = 0;
static	char *shdconf = _PATH_SHDCONF;

static	char *core = NULL;
static	char *kernel = NULL;

struct	flagval {
	char	*fv_flag;
	int	fv_val;
} flagvaltab[] = {
	{ "SHDF_ONETOONE",	SHDF_ONETOONE },
	{ "SHDF_COMPACT",	SHDF_COMPACT },
	{ "SHDF_COR",		SHDF_COR },
	{ "SHDF_COW",		SHDF_COW },
	{ NULL,			0 },
};

static	struct nlist nl[] = {
	{ "_shd_softc" },
#define SYM_SHDSOFTC		0
	{ "_numshd" },
#define SYM_NUMSHD		1
	{ NULL },
};

#define SHD_CONFIG		0	/* configure a device */
#define SHD_CONFIGALL		1	/* configure all devices */
#define SHD_UNCONFIG		2	/* unconfigure a device */
#define SHD_UNCONFIGALL		3	/* unconfigure all devices */
#define SHD_DUMP		4	/* dump a shd's configuration */

static	int checkdev __P((char *));
static	int do_io __P((char *, u_long, struct shd_ioctl *));
static	int do_single __P((int, char **, int, int));
static	int do_all __P((int));
static	int dump_shd __P((int, char **));
static	int getmaxpartitions __P((void));
static	int getrawpartition __P((void));
static	int flags_to_val __P((char *));
static	void print_shd_info __P((struct shd_softc *, kvm_t *));
static	char *resolve_shdname __P((char *));
static	void usage __P((void));
int save_checkpoint (char * shd, int version);
78
int load_checkpoint (char * shd, int version, int newversion);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

int
main(argc, argv)
	int argc;
	char **argv;
{
	int ch, options = 0, action = SHD_CONFIG;
	int flags = 0;

	if (modfind("shd") < 0) {
		/* Not present in kernel, try loading it */
		if (kldload("shd") < 0 || modfind("shd") < 0)
			warn("shd module not available!");
	}

			exit(do_single(argc, argv, action, flags));
	return (0);
}

static int
do_single(argc, argv, action, flags)
	int argc;
	char **argv;
	int action;
	int flags;
{
	struct shd_ioctl shio;
	char *shd, *cp, *cp2, *srcdisk, *copydisk;
	int noflags = 0, i, ileave, j;
108
109
110
        int version, maxversion;
        int old_ver, new_ver;

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

	bzero(&shio, sizeof(shio));

        *argv++; --argc;
	/* First argument is the shd to configure. */
	cp = *argv++; --argc;
	if ((shd = resolve_shdname(cp)) == NULL) {
		warnx("invalid shd name: %s", cp);
		return (1);
	}

	/* Next argument is the source disk */
	cp = *argv++; --argc;
	if ((j = checkdev(cp)) == 0)
		srcdisk = cp;
	else {
		warnx("%s: %s", cp, strerror(j));
		return (1);
	}

	/* Next argument is the 1st copy disk */
	cp = *argv++; --argc;
	if ((j = checkdev(cp)) == 0)
		copydisk = cp;
	else {
		warnx("%s: %s", cp, strerror(j));
		return (1);
	}

	/* Fill in the shio. */
	shio.shio_srcdisk = srcdisk;
	shio.shio_copydisk = copydisk;
	shio.shio_flags = flags;

        cp = *argv++; --argc;
        shio.shio_flags |= SHDF_INITIALIZE;

        if (strcmp (cp, "-i") == 0)
        {
	    if (do_io(shd, SHDIOCSET, &shio))
		return (1);
        }
        else
        if (strcmp (cp, "-c") == 0)
        {
            if (do_io(shd, SHDCHECKPOINT, &shio))
157
            {
158
                return (1);
159
            }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
        }
        else
        if (strcmp (cp, "-g") == 0)
        {
            if (do_io(shd, SHDGETCHECKPOINTS, &shio))
                return (1);
        }
        else
        if (strcmp (cp, "-e") == 0)
        {
            if (do_io(shd, SHDENABLECHECKPOINTING, &shio))
                return (1);
        }
        else
        if (strcmp (cp, "-d") == 0)
        {
            if (do_io(shd, SHDDISABLECHECKPOINTING, &shio))
                return (1);
        }
179
180
181
182
183
184
185
186
187
        else
        if (strcmp (cp, "-delete") == 0)
        {       
            shio.delete_start = atoi(*argv++); --argc;
            shio.delete_end = atoi(*argv++); --argc;
            if (do_io(shd, SHDDELETECHECKPOINTS, &shio))
                return (1);
        }

188
189
190
191
192
193
194
195
196
        else
        if (strcmp (cp, "-r") == 0)
        {       
            version = atoi(*argv++); --argc;
            printf ("Rolling back to version %d\n", version);
            shio.version = version;
            if (do_io(shd, SHDROLLBACK, &shio))
                return (1);
        }
197
        else
198
199
200
201
202
203
204
205
206
        if (strcmp (cp, "-rv") == 0)
        {   
            version = atoi(*argv++); --argc;
            printf ("Reboot version set to %d\n", version);
            shio.version = version;
            if (do_io(shd, SHDSETREBOOTVERSION, &shio))
                return (1);
        }
        else
207
208
209
210
211
        if (strcmp (cp, "-sm") == 0)
        {
            if (do_io(shd, SHDSAVECHECKPOINTMAP, &shio))
                return (1);
        }
212
        else    
213
        if (strcmp (cp, "-sv") == 0)
214
215
        {       
            version = atoi(*argv++); --argc;
216
217
218
219
220
221
            maxversion = atoi(*argv++); --argc;
            for (old_ver = version; old_ver <= maxversion; old_ver++)
            {
                printf ("Saving version %d\n", old_ver);
                save_checkpoint (shd, old_ver);
            }
222
223
224
225
        }               
        else
        if (strcmp (cp, "-l") == 0)
        {
226
227
228
229
230
231
232
233
            off_t off;
            long metadata_block [128];
            int fd_write_mdata = open ("/dev/ad0s4", O_RDWR);
            if (fd_write_mdata < 0)
            {
                perror ("error");
                return;
            }
234
            version = atoi(*argv++); --argc;
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            maxversion = atoi(*argv++); --argc;
            off = lseek (fd_write_mdata, 2 * BLOCKSIZE, SEEK_SET);
            for (i = 0; i < 128; i++)
                metadata_block[i] = 0;
            write (fd_write_mdata, &metadata_block, 512);
            close (fd_write_mdata);
            printf ("Loading version %d\n", version);
            new_ver = 1;
            for (old_ver = version; old_ver <= maxversion; old_ver++ )
            {
                load_checkpoint (shd, old_ver, new_ver);
                new_ver++; 
            }
            if (do_io(shd, SHDLOADCHECKPOINTMAP, &shio))
                return (1);
250
        }   
251
252
253
254
        else
        if (strcmp (cp, "-gm") == 0)
        {
            long buf[512];
255
            struct shd_modinfo mod;
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
            mod.command = 1; /* To initialize iterator */
            mod.buf = buf;
            mod.bufsiz = 512;

            for (i = 0; i < 512; i++)
                    buf[i] = 0;
            mod.retsiz = 0;
            if (do_io(shd, SHDGETMODBLOCKS, &mod))
                return (1);
            printf ("retsize = %d\n", mod.retsiz);
            for (i = 0; i < mod.retsiz; i++)
            {
                printf ("%ld <-> %ld\n", buf[i*2], buf[i*2+1]);
            }
            while (mod.retsiz > 0)
            {
                for (i = 0; i < 512; i++)
                    buf[i] = 0;
                mod.retsiz = 0;
                mod.command = 2;
                if (do_io(shd, SHDGETMODBLOCKS, &mod))
                    return (1);
                printf ("retsize = %d\n", mod.retsiz);
                for (i = 0; i < mod.retsiz; i++)
                {
                    printf ("%ld <-> %ld\n", buf[i*2], buf[i*2+1]);
                }
            } 

            mod.command = 3; /* Close iterator */
            if (do_io(shd, SHDGETMODBLOCKS, &mod))
                return (1);
        }

290
291
	/*printf("shd%d: ", shio.shio_unit);
	printf("%lu blocks ", (u_long)shio.shio_size);*/
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712

	return (0);
}

static int
do_all(action)
	int action;
{
	FILE *f;
	char line[_POSIX2_LINE_MAX];
	char *cp, **argv;
	int argc, rval;
	gid_t egid;

	rval = 0;
	egid = getegid();
	setegid(getgid());
	if ((f = fopen(shdconf, "r")) == NULL) {
		setegid(egid);
		warn("fopen: %s", shdconf);
		return (1);
	}
	setegid(egid);

	while (fgets(line, sizeof(line), f) != NULL) {
		argc = 0;
		argv = NULL;
		++lineno;
		if ((cp = strrchr(line, '\n')) != NULL)
			*cp = '\0';

		/* Break up the line and pass it's contents to do_single(). */
		if (line[0] == '\0')
			goto end_of_line;
		for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
			if (*cp == '#')
				break;
			if ((argv = realloc(argv,
			    sizeof(char *) * ++argc)) == NULL) {
				warnx("no memory to configure shds");
				return (1);
			}
			argv[argc - 1] = cp;
			/*
			 * If our action is to unconfigure all, then pass
			 * just the first token to do_single() and ignore
			 * the rest.  Since this will be encountered on
			 * our first pass through the line, the Right
			 * Thing will happen.
			 */
			if (action == SHD_UNCONFIGALL) {
				if (do_single(argc, argv, action, 0))
					rval = 1;
				goto end_of_line;
			}
		}
		if (argc != 0)
			if (do_single(argc, argv, action, 0))
				rval = 1;

 end_of_line:
		if (argv != NULL)
			free(argv);
	}

	(void)fclose(f);
	return (rval);
}

static int
checkdev(path)
	char *path;
{
	struct stat st;

	if (stat(path, &st) != 0)
		return (errno);

	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode) &&
	    !S_ISREG(st.st_mode))
		return (EINVAL);

	return (0);
}

static int
pathtounit(path, unitp)
	char *path;
	int *unitp;
{
	struct stat st;
	int maxpartitions;

	if (stat(path, &st) != 0)
		return (errno);

	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
		return (EINVAL);

	if ((maxpartitions = getmaxpartitions()) < 0)
		return (errno);

	*unitp = minor(st.st_rdev) / maxpartitions;

	return (0);
}

static char *
resolve_shdname(name)
	char *name;
{
	char c, *path;
	size_t len, newlen;
	int rawpart;

	if (name[0] == '/' || name[0] == '.') {
		/* Assume they gave the correct pathname. */
		return (strdup(name));
	}

	len = strlen(name);
	c = name[len - 1];

	newlen = len + 8;
	if ((path = malloc(newlen)) == NULL)
		return (NULL);
	bzero(path, newlen);

	if (isdigit(c)) {
		if ((rawpart = getrawpartition()) < 0) {
			free(path);
			return (NULL);
		}
		(void)sprintf(path, "%s%s%c", _PATH_DEV, name, 'a' + rawpart);
	} else
		(void)sprintf(path, "%s%s", _PATH_DEV, name);

	return (path);
}

static int
do_io(path, cmd, shiop)
	char *path;
	u_long cmd;
	struct shd_ioctl *shiop;
{
	int fd;
	char *cp;

	if ((fd = open(path, O_RDWR, 0640)) < 0) {
		warn("open: %s", path);
		return (1);
	}

	if (ioctl(fd, cmd, shiop) < 0) {
		switch (cmd) {
                case SHDCHECKPOINT:
                        cp = "SHDCHECKPOINT";
                        break;

		case SHDIOCSET:
			cp = "SHDIOCSET";
			break;

		case SHDIOCCLR:
			cp = "SHDIOCCLR";
			break;

		default:
			cp = "unknown";
		}
		warn("ioctl (%s): %s", cp, path);
		return (1);
	}

	return (0);
}

#define KVM_ABORT(kd, str) {						\
	(void)kvm_close((kd));						\
	warnx("%s", (str));							\
	warnx("%s", kvm_geterr((kd)));					\
	return (1);							\
}

static int
dump_shd(argc, argv)
	int argc;
	char **argv;
{
	char errbuf[_POSIX2_LINE_MAX], *shd, *cp;
	struct shd_softc *cs, *kcs;
	size_t readsize;
	int i, error, numshd, numconfiged = 0;
	kvm_t *kd;

	bzero(errbuf, sizeof(errbuf));

	if ((kd = kvm_openfiles(kernel, core, NULL, O_RDONLY,
	    errbuf)) == NULL) {
		warnx("can't open kvm: %s", errbuf);
		return (1);
	}

	if (kvm_nlist(kd, nl))
		KVM_ABORT(kd, "shd-related symbols not available");

	/* Check to see how many shds are currently configured. */
	if (kvm_read(kd, nl[SYM_NUMSHD].n_value, (char *)&numshd,
	    sizeof(numshd)) != sizeof(numshd))
		KVM_ABORT(kd, "can't determine number of configured shds");

	if (numshd == 0) {
		printf("shd driver in kernel, but is uninitialized\n");
		goto done;
	}

	/* Allocate space for the configuration data. */
	readsize = numshd * sizeof(struct shd_softc);
	if ((cs = malloc(readsize)) == NULL) {
		warnx("no memory for configuration data");
		goto bad;
	}
	bzero(cs, readsize);

	/*
	 * Read the shd configuration data from the kernel and dump
	 * it to stdout.
	 */
	if (kvm_read(kd, nl[SYM_SHDSOFTC].n_value, (char *)&kcs,
	    sizeof(kcs)) != sizeof(kcs)) {
		free(cs);
		KVM_ABORT(kd, "can't find pointer to configuration data");
	}
	if (kvm_read(kd, (u_long)kcs, (char *)cs, readsize) != readsize) {
		free(cs);
		KVM_ABORT(kd, "can't read configuration data");
	}

	if (argc == 0) {
		for (i = 0; i < numshd; ++i)
			if (cs[i].sc_flags & SHDF_INITED) {
				++numconfiged;
				print_shd_info(&cs[i], kd);
			}

		if (numconfiged == 0)
			printf("no concatenated disks configured\n");
	} else {
		while (argc) {
			cp = *argv++; --argc;
			if ((shd = resolve_shdname(cp)) == NULL) {
				warnx("invalid shd name: %s", cp);
				continue;
			}
			if ((error = pathtounit(shd, &i)) != 0) {
				warnx("%s: %s", shd, strerror(error));
				continue;
			}
			if (i >= numshd) {
				warnx("shd%d not configured", i);
				continue;
			}
			if (cs[i].sc_flags & SHDF_INITED)
				print_shd_info(&cs[i], kd);
			else
				printf("shd%d not configured\n", i);
		}
	}

	free(cs);

 done:
	(void)kvm_close(kd);
	return (0);

 bad:
	(void)kvm_close(kd);
	return (1);
}

static void
print_shd_info(cs, kd)
	struct shd_softc *cs;
	kvm_t *kd;
{
#if 0
	static int header_printed = 0;
	struct shdcinfo *cip;
	size_t readsize;
	char path[MAXPATHLEN];
	int i;

	if (header_printed == 0 && verbose) {
		printf("# shd\t\tileave\tflags\tcompnent devices\n");
		header_printed = 1;
	}

	readsize = cs->sc_nshdisks * sizeof(struct shdcinfo);
	if ((cip = malloc(readsize)) == NULL) {
		warn("shd%d: can't allocate memory for component info",
		    cs->sc_unit);
		return;
	}
	bzero(cip, readsize);

	/* Dump out softc information. */
	printf("shd%d\t\t%d\t%d\t", cs->sc_unit, cs->sc_ileave,
	    cs->sc_cflags & SHDF_USERMASK);
	fflush(stdout);

	/* Read in the component info. */
	if (kvm_read(kd, (u_long)cs->sc_cinfo, (char *)cip,
	    readsize) != readsize) {
		printf("\n");
		warnx("can't read component info");
		warnx("%s", kvm_geterr(kd));
		goto done;
	}

	/* Read component pathname and display component info. */
	for (i = 0; i < cs->sc_nshdisks; ++i) {
		if (kvm_read(kd, (u_long)cip[i].ci_path, (char *)path,
		    cip[i].ci_pathlen) != cip[i].ci_pathlen) {
			printf("\n");
			warnx("can't read component pathname");
			warnx("%s", kvm_geterr(kd));
			goto done;
		}
		printf((i + 1 < cs->sc_nshdisks) ? "%s " : "%s\n", path);
		fflush(stdout);
	}

 done:
	free(cip);
#endif
}

static int
getmaxpartitions()
{
    return (MAXPARTITIONS);
}

static int
getrawpartition()
{
	return (RAW_PART);
}

static int
flags_to_val(flags)
	char *flags;
{
	char *cp, *tok;
	int i, tmp, val = ~SHDF_USERMASK;
	size_t flagslen;

	/*
	 * The most common case is that of NIL flags, so check for
	 * those first.
	 */
	if (strcmp("none", flags) == 0 || strcmp("0x0", flags) == 0 ||
	    strcmp("0", flags) == 0)
		return (0);

	flagslen = strlen(flags);

	/* Check for values represented by strings. */
	if ((cp = strdup(flags)) == NULL)
		err(1, "no memory to parse flags");
	tmp = 0;
	for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
		for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
			if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
				break;
		if (flagvaltab[i].fv_flag == NULL) {
			free(cp);
			goto bad_string;
		}
		tmp |= flagvaltab[i].fv_val;
	}

	/* If we get here, the string was ok. */
	free(cp);
	val = tmp;
	goto out;

 bad_string:

	/* Check for values represented in hex. */
	if (flagslen > 2 && flags[0] == '0' && flags[1] == 'x') {
		errno = 0;	/* to check for ERANGE */
		val = (int)strtol(&flags[2], &cp, 16);
		if ((errno == ERANGE) || (*cp != '\0'))
			return (-1);
		goto out;
	}

	/* Check for values represented in decimal. */
	errno = 0;	/* to check for ERANGE */
	val = (int)strtol(flags, &cp, 10);
	if ((errno == ERANGE) || (*cp != '\0'))
		return (-1);

 out:
	return (((val & ~SHDF_USERMASK) == 0) ? val : -1);
}

static void
usage()
{
	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
		"usage: shdconfig [-cv] shd [flags] srcdev [size] copydev [size]",
		"       shdconfig -C [-v] [-f config_file]",
		"       shdconfig -u [-v] shd [...]",
		"       shdconfig -U [-v] [-f config_file]",
		"       shdconfig -g [-M core] [-N system] [shd [...]]");
	exit(1);
}

713
714
715
716
717
718
719
720
721
722
char *
itoa(int value)
{
    static char buf[13];

    snprintf(buf, 12, "%d", value);
    return buf;
}


723
724
725
726
int save_checkpoint (char * shd, int version)
{
    struct shd_readbuf shread;
    char buffer [MAXBUF];
727
    long block [BLOCKSIZE/4 + 1];
728
    off_t off;
729
    int i, j;
730
    int ix;
731
732
733
734
    int fd_read;
    int fd_write_data;
    int fd_write_mdata;
    int size;
735
736
    int read_start;
    int num_blocks;
737
    char path[256];
738
739
740
    char ver_buf[2]; 
    char *ver = &ver_buf; 
    ver = itoa (version);
741
742
743
744
    strcpy (path, "/users/saggarwa/image");
    strcat (path, ver);
    strcat (path, ".data");
    
745
    fd_read = open ("/dev/ad0s4", O_RDWR);
746
    if (fd_read < 0)
747
    {
748
        perror ("error");
749
750
751
752
        return;
    }

    /* Open the data file to write to */
753
    
754
    fd_write_data = open (path, O_CREAT);
755
    if (fd_write_data < 0)
756
757
758
759
    {
        perror ("error");
        return;
    }
760
    close (fd_write_data);
761
    fd_write_data = open (path, O_WRONLY);
762
    if (fd_write_data < 0)
763
    {
764
765
        perror ("error");
        return;
766
767
768
769
    }

    /* Open the metadata file to write to */

770
771
772
773
774
    strcpy (path, "/users/saggarwa/image");
    strcat (path, ver);
    strcat (path, ".mdata");

    fd_write_mdata = open (path, O_CREAT);
775
    if (fd_write_mdata < 0)
776
    {
777
        perror ("error");
778
779
        return;
    }
780
    close (fd_write_mdata);
781
    fd_write_mdata = open (path, O_WRONLY);
782
    if (fd_write_mdata < 0)
783
    {
784
785
        perror ("error");
        return;
786
    }
787

788
789
790
    shread.block_num = 2;
    shread.buf = &block;
    if (do_io(shd, SHDREADBLOCK, &shread))
791
792
793
794
    {
        perror ("error");
        return (1); 
    }
795

796
797
    read_start = (long) block[(2*version - 2)]; 
    num_blocks = (long) block[(2*version - 1)];
798
799
    write (fd_write_mdata, &read_start, sizeof (long));
    write (fd_write_mdata, &num_blocks, sizeof (long));
800
    for (ix = 0; ix < num_blocks; ix++)
801
    {
802
803
804
805
806
807
808
809
810
811
812
813
814
815
        shread.block_num = read_start;
        if (do_io(shd, SHDREADBLOCK, &shread))
                return (1);
        for (i = 0; i < 126; i+=3)
        {
             if (0 == block [i])
                 break;
             off = lseek (fd_read,  block[i+1] * BLOCKSIZE, SEEK_SET);
             size = read (fd_read, &buffer, block[i+2] * BLOCKSIZE);
             if (size < 0)
                 perror ("error");
             size = write (fd_write_data, &buffer, size);
             if (size < 0)
                 perror ("error");
816
817
             for (j = 0; j < MAXBUF; j++)
                 buffer[j] = 0;
818
819
             write (fd_write_mdata, &block[i], sizeof (long));
             write (fd_write_mdata, &block[i+1], sizeof (long));
820
             write (fd_write_mdata, &block[i+2], sizeof (long));
821
822
        } 
        read_start++;
823
824
825
826
827
    }

    close (fd_read);
    close (fd_write_data);
    close (fd_write_mdata);
828
829
}

830
int load_checkpoint (char * shd, int version, int newversion)
831
832
833
{
    char buffer [MAXBUF];
    long block [BLOCKSIZE/4 + 1];
834
    long metadata_block [128];
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
    off_t off;
    int i;   
    int ix;
    int fd_read_data; 
    int fd_read_mdata;
    int fd_write_data;
    int fd_write_mdata; 
    int size;    
    int write_start; 
    int num_blocks;
    long byte_count = 0;
    long block_count = 0;
    long chunk_size;
    long key;
    long value;
850
    char path[256];
851
852
853
    char ver_buf[2];
    char *ver = &ver_buf;
    ver = itoa (version); 
854
855
856
857
858
859
    strcpy (path, "/users/saggarwa/image");
    strcat (path, ver);
    strcat (path, ".data");

    for (i = 0; i < 128; i++)
        metadata_block[i] = 0;
860
861
862
863
864
865
866
867
868
869
870
871
    fd_write_data = open ("/dev/ad0s4", O_RDWR);
    if (fd_write_data < 0)
    {        
        perror ("error");
        return;
    }
    fd_write_mdata = open ("/dev/ad0s4", O_RDWR);
    if (fd_write_mdata < 0)
    {
        perror ("error");
        return;
    }
872

873
874
    /* Open the data file to read from */
    
875
    fd_read_data = open (path, O_RDWR);
876
877
878
879
880
881
882
883
    if (fd_read_data < 0)
    {
        perror ("error");
        return;
    }

    /* Open the metadata file to read from */

884
885
886
887
888
889
    strcpy (path, "/users/saggarwa/image");
    strcat (path, ver);
    strcat (path, ".mdata");


    fd_read_mdata = open (path, O_RDWR);
890
891
892
893
894
895
    if (fd_read_mdata < 0)
    {
        perror ("error");
        return;
    }

896
    for (i = 0; i < 128; i++)
897
        block[i] = 0;
898

899
900
901
    for (i = 0; i < MAXBUF; i++)
        buffer[i] = 0;

902
903
    read (fd_read_mdata, &write_start, sizeof (long)); 
    read (fd_read_mdata, &num_blocks, sizeof (long));
904
905
906
907
908
909
910
    off = lseek (fd_write_mdata, 2 * BLOCKSIZE, SEEK_SET);
    read (fd_write_mdata, &metadata_block, 512); 
    metadata_block[2*newversion - 2] = write_start;
    metadata_block[2*newversion - 1] = num_blocks;
    off = lseek (fd_write_mdata, 2 * BLOCKSIZE, SEEK_SET);
    write (fd_write_mdata, &metadata_block, 512);
    
911
912
913
914
915
916
917
918
919
920
921
922
    off = lseek (fd_write_mdata, write_start * BLOCKSIZE, SEEK_SET);
    while (read (fd_read_mdata, &key, sizeof (long))) 
    {
        read (fd_read_mdata, &value, sizeof (long));
        read (fd_read_mdata, &chunk_size, sizeof (long)); 
        off = lseek (fd_write_data, value * BLOCKSIZE, SEEK_SET); 
        size = read (fd_read_data, &buffer, chunk_size * BLOCKSIZE);
        if (size < 0)
            perror ("error"); 
        size = write (fd_write_data, &buffer, size);
        if (size < 0)
            perror ("error"); 
923
924
        for (i = 0; i < MAXBUF; i++)
            buffer[i] = 0;
925
926
927
928
929
930
931
        block[byte_count++] = key;
        block[byte_count++]= value;
        block[byte_count++] = chunk_size; 

        if (byte_count >= 125)
        {
            byte_count = 0;
932
            off = lseek (fd_write_mdata, write_start * BLOCKSIZE, SEEK_SET); 
933
934
935
936
937
938
939
940
941
942
943
            write (fd_write_mdata, &block, BLOCKSIZE);
            for (i=0; i<128; i++)
                block[i] = 0; 
            write_start++;
            block_count++;
            if (block_count >= num_blocks) 
                break; 
        } 
    }
    if (byte_count > 0)
    {
944
        off = lseek (fd_write_mdata, write_start * BLOCKSIZE, SEEK_SET);
945
946
947
948
949
950
        write (fd_write_mdata, &block, BLOCKSIZE);
    }
    close (fd_write_data);
    close (fd_write_mdata);
    close (fd_read_data);
    close (fd_read_mdata);
951
952
953
954
955
956
}

/* Local Variables: */
/* c-argdecl-indent: 8 */
/* c-indent-level: 8 */
/* End: */