summaryrefslogtreecommitdiff
path: root/emu/port/devds.c
blob: 480a24d7d3ed36793c451759e9e8b8a7750b477c (plain)
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
/*
 * (file system) device subsystems
 * '#k'. 
 * Follows device config in Ken's file server.
 * Builds mirrors, device cats, interleaving, and partition of devices out of
 * other (inner) devices.
 *
 * This code is from Plan 9, and subject to the Lucent Public License 1.02.
 * Only the name changed for Inferno (name clash).
 */

#include "dat.h"
#include "fns.h"
#include "../port/error.h"

enum {
	Fmirror,	// mirror of others
	Fcat,		// catenation of others
	Finter,		// interleaving of others
	Fpart,		// part of others

	Blksize	= 8*1024,	// for Finter only
	Maxconf	= 1024,		// max length for config

	Nfsdevs = 64,
	Ndevs	= 8,

	Qtop	= 0,	// top dir (contains "ds")
	Qdir	= 1,	// actual dir
	Qctl	= 2,	// ctl file
	Qfirst	= 3,	// first fs file
};

#define	Cfgstr	"fsdev:\n"

typedef struct Fsdev Fsdev;

struct Fsdev
{
	int	type;
	char	*name;		// name for this fsdev
	vlong	start;		// start address (for Fpart)
	vlong	size;		// min(idev sizes)
	int	ndevs;		// number of inner devices
	char	*iname[Ndevs];	// inner device names
	Chan	*idev[Ndevs];	// inner devices
	vlong	isize[Ndevs];	// sizes for inneer devices
};

/*
 * Once configured, a fsdev is never removed. The name of those
 * configured is never nil. We have no locks here.
 */
static Fsdev	fsdev[Nfsdevs];

static Qid	tqid = {Qtop, 0, QTDIR};
static Qid	dqid = {Qdir, 0, QTDIR};
static Qid	cqid = {Qctl, 0, 0};

static Cmdtab configs[] = {
	Fmirror,"mirror",	0,
	Fcat,	"cat",		0,
	Finter,	"inter",	0,
	Fpart,	"part",		5,
};

static char	_confstr[Maxconf];
static int	configed;


static Fsdev*
path2dev(int i, int mustexist)
{
	if (i < 0 || i >= nelem(fsdev))
		error("bug: bad index in devfsdev");
	if (mustexist && fsdev[i].name == nil)
		error(Enonexist);

	if (fsdev[i].name == nil)
		return nil;
	else
		return &fsdev[i];
}

static Fsdev*
devalloc(void)
{
	int	i;

	for (i = 0; i < nelem(fsdev); i++)
		if (fsdev[i].name == nil)
			break;
	if (i == nelem(fsdev))
		error(Enodev);

	return &fsdev[i];
}

static void
setdsize(Fsdev* mp)
{
	uchar	buf[128];	/* old DIRLEN plus a little should be plenty */
	int	i;
	Chan	*mc;
	Dir	d;
	long	l;

	if (mp->type != Fpart){
		mp->start= 0;
		mp->size = 0LL;
	}
	for (i = 0; i < mp->ndevs; i++){
		mc = mp->idev[i];
		l = devtab[mc->type]->stat(mc, buf, sizeof(buf));
		convM2D(buf, l, &d, nil);
		mp->isize[i] = d.length;
		switch(mp->type){
		case Fmirror:
			if (mp->size == 0LL || mp->size > d.length)
				mp->size = d.length;
			break;
		case Fcat:
			mp->size += d.length;
			break;
		case Finter:
			// truncate to multiple of Blksize
			d.length = (d.length & ~(Blksize-1));
			mp->isize[i] = d.length;
			mp->size += d.length;
			break;
		case Fpart:
			// should raise errors here?
			if (mp->start > d.length)
				mp->start = d.length;
			if (d.length < mp->start + mp->size)
				mp->size = d.length - mp->start;
			break;
		}
	}
}

static void
mpshut(Fsdev *mp)
{
	int	i;
	char	*nm;

	nm = mp->name;
	mp->name = nil;		// prevent others from using this.
	if (nm)
		free(nm);
	for (i = 0; i < mp->ndevs; i++){
		if (mp->idev[i] != nil)
			cclose(mp->idev[i]);
		if (mp->iname[i])
			free(mp->iname[i]);
	}
	memset(mp, 0, sizeof(*mp));
}


static void
mconfig(char* a, long n)	// "name idev0 idev1"
{
	static	QLock	lck;
	Cmdbuf	*cb;
	Cmdtab	*ct;
	Fsdev	*mp;
	int	i;
	char	*oldc;
	char	*c;
	vlong	size, start;

	size = 0;
	start = 0;
	if (_confstr[0] == 0)
		seprint(_confstr, _confstr+sizeof(_confstr), Cfgstr);
	oldc = _confstr + strlen(_confstr);
	qlock(&lck);
	if (waserror()){
		*oldc = 0;
		qunlock(&lck);
		nexterror();
	}
	cb = parsecmd(a, n);
	if(waserror()){
		free(cb);
		nexterror();
	}
	c = oldc;
	for (i = 0; i < cb->nf; i++)
		c = seprint(c, _confstr+sizeof(_confstr), "%s ", cb->f[i]);
	*(c-1) = '\n';
	ct = lookupcmd(cb, configs, nelem(configs));
	cb->f++;	// skip command
	cb->nf--;
	if (ct->index == Fpart){
		size = strtoll(cb->f[3], nil, 10);
		cb->nf--;
		start = strtoll(cb->f[2], nil, 10);
		cb->nf--;
	}
	for (i = 0; i < nelem(fsdev); i++)
		if (fsdev[i].name != nil && strcmp(fsdev[i].name, cb->f[0])==0)
			error(Eexist);
	if (cb->nf - 1 > Ndevs)
		error("too many devices; fix me");
	for (i = 0; i < cb->nf; i++)
		validname(cb->f[i], (i != 0));
	mp = devalloc();
	if(waserror()){
		mpshut(mp);
		nexterror();
	}
	mp->type = ct->index;
	if (mp->type == Fpart){
		mp->size = size;
		mp->start = start;
	}
	kstrdup(&mp->name, cb->f[0]);
	for (i = 1; i < cb->nf; i++){
		kstrdup(&mp->iname[i-1], cb->f[i]);
		mp->idev[i-1] = namec(mp->iname[i-1], Aopen, ORDWR, 0);
		if (mp->idev[i-1] == nil)
			error(Egreg);
		mp->ndevs++;
	}
	setdsize(mp);
	poperror();
	free(cb);
	poperror();
	poperror();
	configed = 1;
	qunlock(&lck);
	
}

static void
rdconf(void)
{
	int	mustrd;
	char	*s;
	char	*c;
	char	*p;
	char	*e;
	Chan	*cc;

	mustrd = 0;
	s = "/dev/sdC0/fscfg";
	if (waserror()){
		configed = 1;
		if (!mustrd)
			return;
		nexterror();
	}
	cc = namec(s, Aopen, OREAD, 0);
	if(waserror()){
		cclose(cc);
		nexterror();
	}
	devtab[cc->type]->read(cc, _confstr, sizeof(_confstr), 0);
	poperror();
	cclose(cc);
	if (strncmp(_confstr, Cfgstr, strlen(Cfgstr)) != 0)
		error("config string must start with `fsdev:'");
	kstrdup(&c, _confstr + strlen(Cfgstr));
	if(waserror()){
		free(c);
		nexterror();
	}
	memset(_confstr, 0, sizeof(_confstr));
	for (p = c; p != nil && *p != 0; p = e){
		e = strchr(p, '\n');
		if (e == p){
			e++;
			continue;
		}
		if (e == nil)
			e = p + strlen(p);
		mconfig(p, e - p);
	}
	poperror();
	poperror();
}


static int
mgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
{
	Qid	qid;
	Fsdev	*mp;

	if (c->qid.path == Qtop){
		switch(i){
		case DEVDOTDOT:
			devdir(c, tqid, "#k", 0, eve, DMDIR|0775, dp);
			return 1;
		case 0:
			devdir(c, dqid, "ds", 0, eve, DMDIR|0775, dp);
			return 1;
		default:
			return -1;
		}
	}
	if (c->qid.path != Qdir){
		switch(i){
		case DEVDOTDOT:
			devdir(c, dqid, "ds", 0, eve, DMDIR|0775, dp);
			return 1;
		default:
			return -1;
		}
	}
	switch(i){
	case DEVDOTDOT:
		devdir(c, tqid, "#k", 0, eve, DMDIR|0775, dp);
		return 1;
	case 0:
		devdir(c, cqid, "ctl", 0, eve, 0664, dp);
		return 1;
	}
	i--;	// for ctl
	qid.path = Qfirst + i;
	qid.vers = 0;
	qid.type = 0;
	mp = path2dev(i, 0);
	if (mp == nil)
		return -1;
	kstrcpy(up->genbuf, mp->name, sizeof(up->genbuf));
	devdir(c, qid, up->genbuf, mp->size, eve, 0664, dp);
	return 1;
}

static Chan*
mattach(char *spec)
{
	*_confstr = 0;
	return devattach(L'k', spec);
}

static Walkqid*
mwalk(Chan *c, Chan *nc, char **name, int nname)
{
	if (!configed)
		rdconf();
	return devwalk(c, nc, name, nname, 0, 0, mgen);
}

static int
mstat(Chan *c, uchar *db, int n)
{
	Dir	d;
	Fsdev	*mp;
	int	p;

	p = c->qid.path;
	memset(&d, 0, sizeof(d));
	switch(p){
	case Qtop:
		devdir(c, tqid, "#k", 0, eve, DMDIR|0775, &d);
		break;
	case Qdir:
		devdir(c, dqid, "ds", 0, eve, DMDIR|0775, &d);
		break;
	case Qctl:
		devdir(c, cqid, "ctl", 0, eve, 0664, &d);
		break;
	default:
		mp = path2dev(p - Qfirst, 1);
		devdir(c, c->qid, mp->name, mp->size, eve, 0664, &d);
	}
	n = convD2M(&d, db, n);
	if (n == 0)
		error(Ebadarg);
	return n;
}

static Chan*
mopen(Chan *c, int omode)
{
	if((c->qid.type & QTDIR) && omode != OREAD)
		error(Eperm);
	if (omode & OTRUNC)
		omode &= ~OTRUNC;
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

static void
mclose(Chan *c)
{
	// that's easy
}

static long
catio(Fsdev *mp, int isread, void *a, long n, vlong off)
{
	int	i;
	Chan*	mc;
	long	l, wl, res;
	//print("catio %d %p %ld %lld\n", isread, a, n, off);
	res = n;
	for (i = 0; n >= 0 && i < mp->ndevs ; i++){
		mc = mp->idev[i];
		if (off > mp->isize[i]){
			off -= mp->isize[i];
			continue;
		}
		if (off + n > mp->isize[i])
			l = mp->isize[i] - off;
		else
			l = n;
		//print("\tdev %d %p %ld %lld\n", i, a, l, off);

		if (isread)
			wl = devtab[mc->type]->read(mc, a, l, off);
		else
			wl = devtab[mc->type]->write(mc, a, l, off);
		if (wl != l)
			error("#k: write failed");
		a = (char*)a + l;
		off = 0;
		n -= l;
	}
	//print("\tres %ld\n", res - n);
	return res - n;
}

static long
interio(Fsdev *mp, int isread, void *a, long n, vlong off)
{
	int	i;
	Chan*	mc;
	long	l, wl, wsz;
	vlong	woff, blk, mblk;
	long	boff, res;

	blk  = off / Blksize;
	boff = off % Blksize;
	wsz  = Blksize - boff;
	res = n;
	while(n > 0){
		i    = blk % mp->ndevs;
		mc   = mp->idev[i];
		mblk = blk / mp->ndevs;
		woff = mblk * Blksize + boff;
		if (n > wsz)
			l = wsz;
		else
			l = n;
		if (isread)
			wl = devtab[mc->type]->read(mc, a, l, woff);
		else
			wl = devtab[mc->type]->write(mc, a, l, woff);
		if (wl != l || l == 0)
			error(Eio);
		a = (char*)a + l;
		n -= l;
		blk++;
		boff = 0;
		wsz = Blksize;
	}
	return res;
}

static long
mread(Chan *c, void *a, long n, vlong off)
{
	int	i;
	Fsdev	*mp;
	Chan	*mc;
	long	l;
	long	res;

	if (c->qid.type & QTDIR)
		return devdirread(c, a, n, 0, 0, mgen);
	if (c->qid.path == Qctl)
		return readstr((long)off, a, n, _confstr + strlen(Cfgstr));
	i = c->qid.path - Qfirst;
	mp = path2dev(i, 1);

	if (off >= mp->size)
		return 0;
	if (off + n > mp->size)
		n = mp->size - off;
	if (n == 0)
		return 0;

	res = -1;
	switch(mp->type){
	case Fmirror:
		for (i = 0; i < mp->ndevs; i++){
			mc = mp->idev[i];
			if (waserror()){
				// if a read fails we let the user know and try
				// another device.
				print("#k: mread: (%llx %d): %s\n",
					c->qid.path, i, up->env->errstr);
				continue;
			}
			l = devtab[mc->type]->read(mc, a, n, off);
			poperror();
			if (l >=0){
				res = l;
				break;
			}
		}
		if (i == mp->ndevs)
			error(Eio);
		break;
	case Fcat:
		res = catio(mp, 1, a, n, off);
		break;
	case Finter:
		res = interio(mp, 1, a, n, off);
		break;
	case Fpart:
		off += mp->start;
		mc = mp->idev[0];
		res = devtab[mc->type]->read(mc, a, n, off);
		break;
	}
	return res;
}

static long
mwrite(Chan *c, void *a, long n, vlong off)
{
	Fsdev	*mp;
	long	l, res;
	int	i;
	Chan	*mc;

	if (c->qid.type & QTDIR)
		error(Eperm);
	if (c->qid.path == Qctl){
		mconfig(a, n);
		return n;
	}
	mp = path2dev(c->qid.path - Qfirst, 1);

	if (off >= mp->size)
		return 0;
	if (off + n > mp->size)
		n = mp->size - off;
	if (n == 0)
		return 0;
	res = n;
	switch(mp->type){
	case Fmirror:
		for (i = mp->ndevs-1; i >=0; i--){
			mc = mp->idev[i];
			l = devtab[mc->type]->write(mc, a, n, off);
			if (l < res)
				res = l;
		}
		break;
	case Fcat:
		res = catio(mp, 0, a, n, off);
		break;
	case Finter:
		res = interio(mp, 0, a, n, off);
		break;
	case Fpart:
		mc = mp->idev[0];
		off += mp->start;
		l = devtab[mc->type]->write(mc, a, n, off);
		if (l < res)
			res = l;
		break;
	}
	return res;
}

Dev dsdevtab = {
	'k',
	"ds",

	devinit,
	mattach,
	mwalk,
	mstat,
	mopen,
	devcreate,
	mclose,
	mread,
	devbread,
	mwrite,
	devbwrite,
	devremove,
	devwstat,
};