File: rdsinit.c

package info (click to toggle)
rvm 1.13%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,800 kB
  • ctags: 2,012
  • sloc: ansic: 20,307; sh: 8,996; makefile: 115
file content (502 lines) | stat: -rw-r--r-- 13,796 bytes parent folder | download | duplicates (2)
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
29
30
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
78
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
108
109
110
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
290
291
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
/* BLURB gpl

                           Coda File System
                              Release 5

          Copyright (c) 1987-1999 Carnegie Mellon University
                  Additional copyrights listed below

This  code  is  distributed "AS IS" without warranty of any kind under
the terms of the GNU General Public Licence Version 2, as shown in the
file  LICENSE.  The  technical and financial  contributors to Coda are
listed in the file CREDITS.

                        Additional copyrights
                           none currently

#*/


/*
 * This module holds the routines to initialize the datadevice of a heap.
 * Hopefully it'll someday be merged with something hank wrote to initialize
 * the log.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <rvm/rvm.h>
#include <rvm/rvm_segment.h>
#include <rvm/rds.h>
#include "rds_private.h"

#ifdef __STDC__
#include <string.h>
#define BZERO(D,L)   memset((D),0,(L))
#else
#define BZERO(D,L)   bzero((D),(L))
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif

void
PollAndYield() {
}

enum round_dir { UP, DOWN, NO_ROUND };

jmp_buf jmpbuf_quit;

char *usage[]={
"Usage: rdsinit [-f] log data_seg [ datalen saddr hlen slen nl chunk ] \n",
"  where\n",
"    -f       supplied parameter are firm, do not ask for confirm\n",
"    log      is the name of log file/device\n",
"    data_seg is the name of data segment file/device\n",
"    datalen  gives the length of data segment\n",
"    saddr    is the starting address of rvm\n",
"    hlen     is the heap length\n",
"    slen     is the static length\n",
"    nl       is the number of free list used by RDS\n",
"    chunk    is the chunksize\n",
""
};

static char *welcome[]={
"\n",
"============================================================\n",
"  Getting parameters for RDS\n",
"============================================================\n",
""
};

char *explain_datalen[]={
"1) length of data segment file/device\n",
"   There will be two regions in the segment:\n",
"   one is for the heap, the other is for the static.\n",
"   Some extra space (usually one page) will also \n", 
"   be needed for the segment header.\n",
"   Length of data segment must be a multiple of pagesize.\n",
"   Please make sure you have enough space in your disk.\n",
""
};

char *explain_saddr[]={
"2) starting address of rvm\n",
"   This is where heap and static will be mapped into virtual memory, it\n",
"   must be larger than the largest possible break point of your\n",
"   application, and it should not be in conflict other use of vm (such as\n",
"   shared libraries).  Also, it must be on a page boundary\n", 
"   (In CMU, we use 0x20000000 (536870912) with Linux and BSD44,\n",
"   0x70000000 (1879048192) with Mach and BSD44 without problem.)\n",
""
};

char *explain_hlen[]={
"3) heap length\n",
"   It is the size of the dynamic heap to be managed by RDS,\n",
"   it must be an integral multiple of pagesize.\n",
""
};

char *explain_slen[]={
"4) static length\n",
"   It is the size of the statically allocated location to be\n",
"   managed by your application.\n",
"   It must be an integral multiple of pagesize.\n",
""
};

char *explain_nl[]={
"5) nlists\n",
"   It is the number of free list used by RDS.\n",
"   (nlist=100 is a reasonable choice.)\n",
""
};

char *explain_chunk[]={
"6) chunksize\n",
"   The free list are maintained in sizes of one chunk to n chunk,\n",
"   where n is the number of free lists.\n",
"   It must be an integral multiple of sizeof(char *) of your system,\n",
"   and must at least be RDS_MIN_CHUNK_SIZE.\n", 
"   (chunksize=32 is a reasonable choice.)\n",
""
};

static void print_msg(msg)
     char **msg;
{
    int i=0;
    while (msg[i][0] != '\0')
	printf("%s", msg[i++]);
}

static int round_to_multiple(val, n, dir)
     long           val;
     long           n;
     enum round_dir dir;
{
    long rem;

    if (dir==NO_ROUND || n==0)		/* don't round */
	return(val);
    
    if ((rem = val % n) != 0) { /* need round up/down */
	if (dir==UP)
	    return ( val + n -rem );
	else if (dir==DOWN)
	    return ( val - rem );
    }
 
    return( val );

}


static int confirm_rounded_value(pvalue, base, unit, round_dir, min)
     unsigned long  *pvalue;
     int            base;
     int            unit;
     enum round_dir round_dir;
     int            min;
{
    char          string[120];
    unsigned long t1=0, t2=0;	/* temporary values */
    
    if (base == 16) {
	sprintf(string,  "0x%lx (in decimal %lu)", *pvalue, *pvalue);
    } else if (base == 10) {
	sprintf(string, "%ld (in hex 0x%lx)", *pvalue, *pvalue);
    } else if (base == 8) {
	sprintf(string, "0%lo (in decimal %lu)", *pvalue, *pvalue);
    } else {
	assert(0);		/* illegal base value */
    }

    printf("   ====> You have entered %s. ", string);

    if (*pvalue<min) {
	printf("\n");
	printf("         Umm... minimum is %d (in hex 0x%x) ",
	       min, min);
        t1 = min;
    } else {
	t1 = *pvalue;
    }
    
    t2 = round_to_multiple(t1, unit, round_dir);

    if (t2 != t1) {
    	if (base == 16) {
    	    sprintf(string, "0x%lx (in decimal %lu)", t2, t2);
    	} else if (base == 10) {
    	    sprintf(string, "%ld (in hex 0x%lx)", t2, t2);
    	} else if (base == 8) {
    	    sprintf(string, "0%lo (in decimal %lu)", t2, t2);
    	} else {
    	    assert(0);		/* illegal base value */
    	}

	printf("\n");
        printf("         Umm... not a multiple of %d.  I need to round it\n",
	       unit);
        printf("         %s to %s. ",
               (round_dir == UP ? "up" : "down"), string);

    }
    printf("Accept ? (y|n|q) ");
    fgets(string, 120, stdin);
    if (strcmp(string,"y\n") == 0 || strcmp(string,"Y\n") == 0
	  || strcmp(string, "\n") == 0 ) {
	*pvalue = t2;
	return 1;		/* confirmed */
    } else if (strcmp(string,"q\n")==0 || strcmp(string, "Q\n")==0)
	longjmp(jmpbuf_quit, 1); /* quit */
    else			/* all other cases: re-enter */
	return 0;		/* not confirmed */
}

/* determine if the numeric string is in hex, octal or decimal notation */
static int find_base(string)
     char *string;
{
    if (string[0] == '0')
	return ( string[1]=='x' ? 16 : 8 );
    else
	return 10;
}
		

static int get_valid_parm(argc, argv, pdatalen,
			   pstatic_addr, phlen, pslen,
			   pnlists,pchunksize,firm)
     int          argc;
     char         **argv;
     long         *pdatalen;
     char         **pstatic_addr;
     unsigned long *phlen;
     unsigned long *pslen;
     unsigned long *pnlists;
     unsigned long *pchunksize;
     int          firm;
{
    char string[80];
    int  paraOK=0;
    int  para_given=0;
    int  base;

    if ( argc == 9 ) {
	/* accept the 6 parameter as cmd-line argument */
	*pdatalen       = strtoul(argv[3], NULL, 0);
	*pstatic_addr = (char *)strtoul(argv[4], NULL, 0);
	*phlen        = strtoul(argv[5], NULL, 0);
	*pslen        = strtoul(argv[6], NULL, 0);
	*pnlists      = strtoul(argv[7], NULL, 0);
	*pchunksize   = strtoul(argv[8], NULL, 0);
	    
	para_given = 1;
	if (firm)
	    return 0;		/* no need to get confirm from user */
    }

    /* looping to get parameters from user interactively, and confirm */
    do {
	if (para_given) {
	    para_given = 0;	/* skip the first time of asking */
	    goto confirm;
	}
	print_msg(welcome);
	print_msg(explain_datalen);
        do {
            printf("   ==> please enter length of data segment:");
    	    fgets(string,80,stdin);
    	    base = find_base(string);
    	    *pdatalen = strtoul(string, NULL, 0);
	} while (!confirm_rounded_value(pdatalen, base, RVM_PAGE_SIZE, UP,
					3*RVM_PAGE_SIZE));

	print_msg(explain_saddr);
        do {
            printf("   ==> please enter starting address of rvm: ");
	    fgets(string,80,stdin);
	    base = find_base(string);
	    *pstatic_addr = (char *)strtoul(string, NULL, 0);
        } while (!confirm_rounded_value((unsigned long *)pstatic_addr, 
					base, RVM_PAGE_SIZE,UP,
					0x2000000)); 
				/* note 0x4000000 is just a very loose lower
				*  bound.  Actual number should be much
				*  larger (and system-dependent)
				*/
    get_heap_n_static:
	print_msg(explain_hlen);
        do {
            printf("   ==> please enter the heap length: ");
	    fgets(string,80,stdin);
	    base = find_base(string);
	    *phlen = strtoul(string, NULL, 0);
        } while (!confirm_rounded_value(phlen, base, RVM_PAGE_SIZE, UP,
					RVM_PAGE_SIZE));

        print_msg(explain_slen);
        do {
            printf("   ==> please enter the static length: ");
	    fgets(string,80,stdin);
            base = find_base(string);
	    *pslen = strtoul(string, NULL, 0);
	} while (!confirm_rounded_value(pslen, base, RVM_PAGE_SIZE, UP,
					RVM_PAGE_SIZE));

	if (*pdatalen<*phlen+*pslen+RVM_SEGMENT_HDR_SIZE) {
	    printf("\n");
	    printf("   Sorry ! your heap len + static len is too large !\n");
	    printf("   their sum must be less than 0x%lx (%ld)\n", 
		   *pdatalen-RVM_PAGE_SIZE, *pdatalen-RVM_SEGMENT_HDR_SIZE);
	    printf("   please re-enter\n\n");
	    goto get_heap_n_static;
	}

	print_msg(explain_nl);
        do {
            printf("   ==> please enter a decimal value for nlists: ");
	    fgets(string,80,stdin);
	    base = find_base(string);
	    *pnlists = strtoul(string, NULL, 0);
        } while (!confirm_rounded_value(pnlists, base, 0, NO_ROUND,
					1));

	print_msg(explain_chunk);
        do {
            printf("   ==> please enter a decimal value for chunksize: ");
	    fgets(string,80,stdin);
	    base = find_base(string);
	    *pchunksize = strtoul(string, NULL, 0);
	} while (!confirm_rounded_value(pchunksize, base, sizeof(char *), UP,
					32));

    confirm:
	printf("\n");
        printf("The following parameters are chosen:\n");
        printf("   length of data segment: %#10lx (%10ld)\n",
	       *pdatalen, *pdatalen);
        printf("  starting address of rvm: %#10lx (%10lu)\n",
	       (unsigned long)*pstatic_addr, (unsigned long)*pstatic_addr);
        printf("                 heap len: %#10x (%10d)\n",
	       *phlen, *phlen);
        printf("               static len: %#10x (%10d)\n",
	       *pslen, *pslen);
        printf("                   nlists: %#10x (%10d)\n",
	       *pnlists, *pnlists);
        printf("               chunk size: %#10x (%10d)\n",
	       *pchunksize, *pchunksize);
        printf("Do you agree with these parameters ? (y|n|q) ");
	fgets(string,80,stdin);
	if (strcmp(string,"y\n") == 0 || strcmp(string,"Y\n") == 0
	    || strcmp(string, "\n") == 0 )
	    paraOK = 1;
	else if (strcmp(string,"q\n") == 0 || strcmp(string,"Q\n") == 0)
	    longjmp(jmpbuf_quit,1);
    } while (!paraOK);
    return 0;
}



extern char *ortarg;
extern int optind, opterr, optopt;


int main(argc, argv)
     int  argc;
     char *argv[];
{
    rvm_options_t       *options;       /* options descriptor ptr */
    rvm_return_t	ret;
    int err, fd, i;
    char *static_addr=NULL, buf[4096];
    char *logName, *dataName;
    unsigned long slen=0, hlen=0, nlists=0, chunksize=0;
    rvm_offset_t DataLen;
    long         datalen=0;
    int firm=0, opt_unknown=0;
    int c, arg_used=0;

    /* first check if we have -l and -c options */
    while ( (c = getopt(argc, argv, "f")) != EOF ) {	
	switch (c) {
	case 'f':
	    firm = 1;
	    arg_used ++;
	    break;
	default:
	    opt_unknown = 1;
	    break;
	}
    }
    argc -= arg_used;
    argv += arg_used;
	    
    if ((argc !=3 && argc !=9) || opt_unknown) {
	print_msg(usage);
	exit(-1);
    }

    logName = argv[1];
    dataName = argv[2];

    /* initialized RVM */
    options = rvm_malloc_options();
    options->log_dev = logName;
	      
    ret = RVM_INIT(options);
    if  (ret != RVM_SUCCESS) {
	printf("?  rvm_initialize failed, code: %s\n",rvm_return(ret));
	exit(-1);
    } else
	printf("rvm_initialize succeeded.\n");

    if (setjmp(jmpbuf_quit) != 0) {
        printf("rdsinit quit.  No permanent change is made\n");
        exit(-1);
    }

    ret = get_valid_parm(argc, argv, &datalen,
			 &static_addr, &hlen, &slen, &nlists, &chunksize,
			 firm);
    if (ret < 0) {
	printf("? invalid rdsinit parameters\n");
        printf("rdsinit quit.  No permanent change is made\n");
	exit(-1);
    }

    {
        int minchunksize = sizeof(free_block_t) + sizeof(guard_t);
    	if (chunksize < minchunksize) {
	    printf("? chucksize should be at least %d bytes\n", minchunksize);
	    printf("rdsinit quit.  No permanent change is made\n");
	    exit(-1);
	}
    }

    fd = open(dataName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 00644);
    if (fd < 0) {
	printf("?  Couldn't truncate %s.\n", dataName);
	exit(-1);
    }

    printf("Going to initialize data file to zero, could take awhile.\n");
    lseek(fd, 0, 0);
    BZERO(buf, 4096);
    for (i = 0; i < datalen; i+= 4096) {
	if (write(fd, buf, 4096) != 4096) {
	    printf("?  Couldn't write to %s.\n", dataName);
	    exit(-1);
	}
    }
    printf("done.\n");
    
    close(fd);


    DataLen = RVM_MK_OFFSET(0, datalen);
    rds_zap_heap(dataName, DataLen, static_addr, slen, hlen, nlists,
		 chunksize, &err);
    if (err == SUCCESS)
	printf("rds_zap_heap completed successfully.\n");
    else
        {
        if (err > SUCCESS)
            printf("?  ERROR: rds_zap_heap %s.\n",
                   rvm_return(err));
        else
            printf("?  ERROR: rds_zap_heap, code: %d\n", err);
        exit(-1);
        }

    ret = rvm_terminate();
    if (ret != RVM_SUCCESS)
        {
	printf("\n? Error in rvm_terminate, ret = %s\n", rvm_return(ret));
        exit(-1);
        }
    else
	printf("rvm_terminate succeeded.\n");

    return 0;
}