File: busy.c

package info (click to toggle)
reiser4progs 1.0.7-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,352 kB
  • ctags: 3,699
  • sloc: ansic: 33,483; sh: 8,489; makefile: 1,001
file content (611 lines) | stat: -rw-r--r-- 13,597 bytes parent folder | download | duplicates (8)
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
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
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   busy.c -- reiser4 busybox -- set of progs are used in debugging. */

#include "busy.h"
#include <sys/stat.h>

enum test_list {
	T_CREATE	= 0,
	T_MKDIR		= 1,
	T_MKNOD		= 2,
	T_SLINK		= 3,
	T_LINK		= 4,
	T_CRYCOM	= 5,
	T_LS		= 6,
	T_RM		= 7,
	T_CP		= 8,
	T_STAT		= 9,
	T_TRUNC		= 10,
	T_REG		= 11,
	T_SYM		= 12,
	T_LAST
};

busy_cmd_t tests[] = {
	[T_CREATE] = {
		.name = "create",
		.options = "PATH",
		.handler = create_cmd,
		.ops_num = 1,
		.info = "Creates a regular file on reiser4.",
	},
	[T_MKDIR] = {
		.name = "mkdir",
		.options = "PATH",
		.handler = create_cmd,
		.ops_num = 1,
		.info = "Creates a directory on reiser4.",
	},
	[T_MKNOD] = {
		.name = "mknod",
		.options = "PATH [char/fifo/block/sock] MAJOR MINOR",
		.handler = create_cmd,
		.ops_num = 4,
		.info = "Creates a device file on reiser4.",
	},
	[T_SLINK] = {
		.name = "ln-s",
		.options = "PATH link_name",
		.handler = create_cmd,
		.ops_num = 2,
		.info = "Creates a symlink on reiser4.",
	},
	[T_LINK] = {
		.name = "ln",
		.options = "PATH link_name",
		.handler = ln_cmd,
		.ops_num = 2,
		.info = "Creates a hard link on reiser4.",
	},
	[T_CRYCOM] = {
		.name = "crycom",
		.options = "PATH",
		.handler = create_cmd,
		.ops_num = 1,
		.info = "Creates a crypto-compression regular file on reiser4.",
	},
	[T_LS] = {
		.name = "ls",
		.options = "PATH",
		.handler = ls_cmd,
		.ops_num = 1,
		.info = "Lists a directory.",
	},
	[T_RM] = {
		.name = "rm",
		.options = "PATH",
		.handler = rm_cmd,
		.ops_num = 1,
		.info = "Removes a file.",
	},
	[T_CP] = {
		.name = "cp",
		.options = "PATH1 PATH2 in_offset out_offset count blk_size",
		.handler = cp_cmd,
		.ops_num = 6,
		.info = "Copies <count> blocks of <blk_size> bytes from "
			"<PATH1> to <PATH2>\n\tskipping <in_offset> bytes, "
			"seeking on <out_offset> bytes.\n\t<in_offset>, "
			"<out_offset> and <count> could be given -1, "
			"that is,\n\tstart, end, unlimited correspondingly.",
	},
	[T_STAT] = {
		.name = "stat",
		.options = "PATH",
		.handler = stat_cmd,
		.ops_num = 1,
		.info = "Stats a file.",
	},
	[T_TRUNC] = {
		.name = "trunc",
		.options = "PATH size",
		.handler = trunc_cmd,
		.ops_num = 2,
		.info = "Truncates a file.",
	},
	[T_REG] = {
		.name = "reg",
		.options = "PATH",
		.handler = reg_test,
		.ops_num = 1,
		.info = "Just a test. Creates some amount of files.",
	},
	[T_SYM] = {
		.name = "sym",
		.options = "PATH",
		.handler = sym_test,
		.ops_num = 1,
		.info = "Just a test. Creates some amount of symlinks.",
	}
};

static reiser4_fs_t *busy_fs_open(char *name) {
	aal_device_t *device;
	reiser4_fs_t *fs;

	if (!(device = aal_device_open(&file_ops, name, 512, O_RDWR))) {
		aal_error("Can't open device %s.", name);
		return NULL;
	}
    
	if (!(fs = reiser4_fs_open(device, 1))) {
		aal_error("Can't open filesystem on %s.", name);
		goto error_close_device;
	}

	if (!(fs->journal = reiser4_journal_open(fs, device))) {
		aal_error("Failed to open the journal on %s.", name);
		goto error_close_fs;
	}
	
	fs->tree->mpc_func = misc_mpressure_detect;
	
	if (reiser4_journal_replay(fs->journal)) {
		aal_error("Failed to replay the journal on %s.", name);
		goto error_close_journal;
	}
	
	reiser4_journal_close(fs->journal);
	fs->journal = NULL;

	reiser4_fs_sync(fs);
	return fs;

 error_close_journal:
	reiser4_journal_close(fs->journal);
 error_close_fs:
	reiser4_fs_close(fs);
 error_close_device:
	aal_device_close(device);
	return NULL;
}

static void busy_fs_close(reiser4_fs_t *fs) {
	aal_device_t *device;
	
	if (!fs) return;
	
	device = fs->device;
	reiser4_fs_close(fs);
	aal_device_close(device);
}

static void busy_print_usage(void) {
	unsigned int i;

	fprintf(stderr, "\nUsage: busy COMMAND ARGS\n");
	fprintf(stderr, "Commands with args: \n");
	
	for (i = 0; i < T_LAST; i++) {
		fprintf(stderr, "\t%s %s\n", tests[i].name, tests[i].options);
		fprintf(stderr, "\t%s\n\n", tests[i].info);
	}
		
	fprintf(stderr, "\nPATH = {device:path || ^path}\n");
	fprintf(stderr, "when working with reiser4 fs on 'device' and others "
		"respectively.\nFor others '^' is mandatory.\n\n");
}

static int busy_get_testno(char *name) {
	unsigned int i;

	for (i = 0; i < T_LAST; i++) {
		if (aal_strlen(name) != aal_strlen(tests[i].name))
			continue;
		
		if (!aal_memcmp(name , tests[i].name, 
				aal_strlen(tests[i].name)))
		{
			return i;
		}
	}

	return -1;
}

static errno_t busy_init(void) {
	int i;
	
	for (i = 0; i < EXCEPTION_TYPE_LAST; i++)
		misc_exception_set_stream(i, stderr);

	if (libreiser4_init()) {
		aal_error("Can't initialize libreiser4.");
		return 0x80;
	}

	return 0;
}

errno_t sym_test(busy_ctx_t *ctx) {
	reiser4_object_t *dir0, *dir1, *dir2, *sym0, *sym1, *reg0;
	reiser4_fs_t *fs = ctx->in.fs;
	char *path = ctx->in.path;
	char name[256];
	
	if (!(dir0 = reiser4_semantic_open(fs->tree, path, NULL, 1))) {
                aal_error("Can't open dir %s.", path);
                return -EIO;
        }

	if (!(dir1 = reiser4_dir_create(dir0, "test1 dir1"))) {
		aal_error("Failed to create a dir 'test1 dir1'.");
		return -EIO;
	}

	if (!(dir2 = reiser4_dir_create(dir0, "test1 dir2"))) {
		aal_error("Failed to create a dir 'test1 dir2'.");
		return -EIO;
	}
	
	if (!(reg0 = reiser4_reg_create(dir1, "test1 file0"))) {
		aal_error("Failed to create a reg 'test1 file0'.");
		return -EIO;
	}
	
	if (!(sym0 = reiser4_sym_create(dir2, "test1 sym0", 
					"../test1 dir1/test1 file0")))
	{
		aal_error("Failed to create a sym 'test1 sym0'.");
		return -EIO;
	}
	
	aal_snprintf(name, 256, "%s/test1 dir1/test1 file0", path);
	
	if (!(sym1 = reiser4_sym_create(dir2, "test1 sym1", name))) {
		aal_error("Failed to create a sym 'test1 sym1'.");
		return -EIO;
	}
	
	reiser4_object_close(dir0);
	reiser4_object_close(dir1);
	reiser4_object_close(dir2);
	reiser4_object_close(reg0);
	reiser4_object_close(sym0);
	reiser4_object_close(sym1);
	
	aal_snprintf(name, 256, "%s/test1 dir2/test1 sym0", path);

	if (!(sym0 = reiser4_semantic_open(fs->tree, name, NULL, 1))) {
		aal_error("Failed to open a sym '%s'.", name);
		return -EIO;
	}

	aal_snprintf(name, 256, "%s/test1 dir2/test1 sym1", path);
	
	if (!(sym1 = reiser4_semantic_open(fs->tree, name, NULL, 1))) {
		aal_error("Failed to create a sym '%s'.", name);
		return -EIO;
	}
	
	reiser4_object_close(sym0);
	reiser4_object_close(sym1);

	reiser4_tree_compress(fs->tree);
	return 0;
}

errno_t reg_test(busy_ctx_t *ctx) {
	reiser4_fs_t *fs = ctx->in.fs;
	reiser4_object_t *dir;
	char *path = ctx->in.path;
	
	int i, k;
	char name[6][256];

	//FILE *file;
	reiser4_object_t *object;
	entry_hint_t entry;

//	misc_param_override("hash=deg_hash");
//	misc_param_override("policy=tails");

	if (!(dir = reiser4_semantic_open(fs->tree, path, NULL, 1))) {
                aal_error("Can't open dir %s.", path);
                return -EINVAL;
        }
	
	srandom(time(0));

	//		file = fopen("/home/umka/tmp/out", "r");

	//		while (!feof(file)) {
	for (i = 0; i < 1000; i++) {
		//                        int j, count;
		//			char part[256];
		uint64_t size[3] = {51200, 0, 1024};

		aal_snprintf(name[0], 256, "test0 file name%d", i/*random()*/);
		aal_snprintf(name[1], 256, "test0 file name%d.c", i/*random()*/);
		aal_snprintf(name[2], 256, "test0 file name%d.o", i/*random()*/);
		/*
		aal_snprintf(name[3], 256, "test0 very very long file name%d", i);
		aal_snprintf(name[4], 256, "test0 very very long file name%d.c", i);
		aal_snprintf(name[5], 256, "test0 very very long file name%d.o", i);

		//			fscanf(file, "%s %s\n", name, part);
		//			strcat(name, " ");
		//			strcat(name, part);
		//			printf("%s\n", name);
		*/
		for (k = 0; k < 3; k++) {
			int count, j;
			
			if (!(object = reiser4_reg_create(dir, name[k])))
				continue;


			if (reiser4_object_write(object, NULL, size[k]) < 0) {
				aal_error("Can't write data to file %s.", 
					  name[k]);
				break;
			}

			count = 1000;

			for (j = 0; j < count; j++) {
				/* reiser4_object_seek(object, 
				   reiser4_object_offset(object) + 8193);*/

				if (reiser4_object_write(object, name[k],
							 aal_strlen(name[k])) < 0)
				{
					aal_error("Can't write data "
						  "to file %s.", name[k]);
					break;
				}
			}

			if (object) {
				reiser4_object_close(object);
			} else {
				aal_error("Can't create an object '%s'.", 
					  name[k]);
			}

			/* Get the second file and write there some more. */
			if (reiser4_object_lookup(dir, name[k], &entry) != 
			    PRESENT)  
			{
				aal_error("Can't find just created object %s.",
					  name[1]);
				break;
			}

			if (!(object = reiser4_object_obtain(fs->tree, dir, 
							     &entry.object))) 
			{
				aal_error("Can't open the object %s.", name[1]);
				break;
			}

			if (reiser4_object_write(object, NULL, 1024) < 0) {
				aal_error("Can't write data to file %s.", 
					  name[k]);
				break;
			}

			reiser4_object_close(object);
		}
	}
	
	reiser4_object_close(dir);
	reiser4_tree_compress(fs->tree);
	return 0;
}

static errno_t busy_path_parse(busy_target_t *target, char *path) {
	int i, len;
	
	aal_assert("vpf-1708", target != NULL);
	aal_assert("vpf-1709", path != NULL);

	i = 0;
	len = aal_strlen(path);
	
	if (path[0] != '^') {
		for (i = 0; i < 256 && i < len; i++) {
			if (path[i] == ':')
				break;

			target->path[i] = path[i];
		}

		if (i == len || path[i] != ':') {
			aal_error("Wrong PATH format detected.");
			return -EINVAL;
		}
			
		target->path[i] = 0;

		if (!(target->fs = busy_fs_open(target->path)))
			return -EINVAL;

		path += i + 1;
	} else {
		path++;
	}
	
	target->path[0] = 0;

	aal_memset(target->path, 0, PATH_MAXLEN);
	aal_strncpy(target->path, path, PATH_MAXLEN - 1);
	
	return 0;
}

static void busy_ctx_fini(busy_ctx_t *ctx) {
	if (!ctx) return;

	if (ctx->in.fs)
		busy_fs_close(ctx->in.fs);
	if (ctx->out.fs)
		busy_fs_close(ctx->out.fs);
}

static int busy_set_objtype(int testno) {
	if (testno == T_CREATE)
		return REG_OBJECT;
	if (testno == T_MKDIR)
		return DIR_OBJECT;
	if (testno == T_MKNOD)
		return SPL_OBJECT;
	if (testno == T_SYM)
		return SYM_OBJECT;
	if (testno == T_CRYCOM) {
		return REG_OBJECT | OBJFLAG_CRYCOM;
	}
	return -1;
}

static errno_t busy_ctx_init(busy_ctx_t *ctx, int count, char *params[]) {
	aal_assert("vpf-1707", ctx != NULL);

	aal_memset(ctx, 0, sizeof(*ctx));
	
	if (count < 3) {
		busy_print_usage();
		return 0x10;
	}
	
	/* Get the test number. */
	if ((ctx->testno = busy_get_testno(params[1])) == -1) {
		aal_error("Cannot find test '%s'.", params[1]);
		return 0x10;
	}

	if (!tests[ctx->testno].handler) {
		aal_error("The test '%s' is not implemented yet.", params[1]);
		return 0x10;
	}

	if (tests[ctx->testno].ops_num + 2 != count) {
		aal_error("Wrong parameters are given to the test '%s'.", 
			  params[1]);
		return 0x10;
	}
	
	/* First parameter should be a PATH on reiser4. */
	if (busy_path_parse(&ctx->in, params[2]))
		return 0x8;
	
	if (ctx->testno == T_CP) {
		/* The second parameter is also PATH. */
		if (busy_path_parse(&ctx->out, params[3])) {
			busy_ctx_fini(ctx);
			return 0x8;
		}
	}

	if (ctx->testno == T_SLINK || ctx->testno == T_LINK) {
		/* The second parameter is NAME. */
		if (busy_path_parse(&ctx->out, params[3])) {
			busy_ctx_fini(ctx);
			return 0x8;
		}
	}

	ctx->objtype = busy_set_objtype(ctx->testno);
	
	if (ctx->testno == T_MKNOD) {
		long long num;
		
		/* Parse Special device type & rdev. */
		if (!aal_strcmp(params[3], "char"))
			ctx->mode |= S_IFCHR;
		else if (!aal_strcmp(params[3], "fifo"))
			ctx->mode |= S_IFIFO;
		else if (!aal_strcmp(params[3], "block"))
			ctx->mode |= S_IFBLK;
		else if (!aal_strcmp(params[3], "sock"))
			ctx->mode |= S_IFSOCK;
		else {
			aal_error("The special file type '%s' is not correct.",
				  params[3]);
			return 0x10;
		}

		if ((num = misc_str2long(params[4], 0)) == INVAL_DIG) {
			aal_error("Invalid major is specified: '%s'.", 
				  params[4]);
			return 0x10;
		}
		
		ctx->rdev = num << 8;
		
		if ((num = misc_str2long(params[5], 0)) == INVAL_DIG) {
			aal_error("Invalid MINOR is specified: '%s'.", 
				  params[5]);
			return 0x10;
		}

		ctx->rdev |= num;
	}
	
	if (ctx->testno == T_CP) {
		/* Parse numbers given to the test "cp". */
		ctx->in.offset = misc_str2long(params[4], 0);
		ctx->out.offset = misc_str2long(params[5], 0);
		ctx->count = misc_str2long(params[6], 0);
		ctx->blksize = misc_str2long(params[7], 0);
		
		if (ctx->in.offset == INVAL_DIG || ctx->in.offset < 0) {
			aal_error("Invalid input offset is specified: '%s'.", 
				  params[4]);
			return 0x10;
		}

		if (ctx->out.offset == INVAL_DIG || ctx->out.offset < 0) {
			aal_error("Invalid output offset is specified: '%s'.", 
				  params[5]);
			return 0x10;
		}

		if (ctx->count == INVAL_DIG) {
			aal_error("Invalid block count is specified: '%s'.", 
				  params[6]);
			return 0x10;
		}

		if (ctx->blksize == INVAL_DIG || ctx->blksize <= 0) {
			aal_error("Invalid block size is specified: '%s'.", 
				  params[7]);
			return 0x10;
		}
	}

	if (ctx->testno == T_TRUNC) {
		ctx->count = misc_str2long(params[3], 0);
		if (ctx->count == INVAL_DIG || ctx->count < 0) {
			aal_error("Invalid size is specified: '%s'.", 
				  params[4]);
			return 0x10;
		}
	}
	
	return 0;
}


int main(int argc, char *argv[]) {
	busy_ctx_t ctx;
	errno_t res;
	
	if (busy_init())
	    return 0x80;

	aal_memset(&ctx, 0, sizeof(ctx));
	if ((res = busy_ctx_init(&ctx, argc, argv))) {
		libreiser4_fini();
		return res;
	}

	res = tests[ctx.testno].handler(&ctx);
	
	busy_ctx_fini(&ctx);
	libreiser4_fini();
    
	return res;
}