~kris/9p

9hist

ref: 751bb35071ec3ab072f67992889399bbeb5d7793 9hist/port/devsd.c -rw-r--r-- 7.1 KiB
751bb350 — David du Colombier Plan 9 from Bell Labs 1998-05-12 28 years ago
                                                                                
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
/*
 *  SCSI disc driver
 */
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

enum
{
	TypeDA		= 0x00,		/* Direct Access */
	TypeSA		= 0x01,		/* Sequential Access */
	TypeWO		= 0x04,		/* Worm */
	TypeCD		= 0x05,		/* CD-ROM */
	TypeMO		= 0x07,		/* rewriteable Magneto-Optical */
	TYpeMC		= 0x08,		/* Medium Changer */
};

enum
{
	LogNpart	= 4,
	Npart		= 1<<LogNpart,
	Ndisk		= 32,
	Nlun		= 8,
};
#define DRIVE(qid)	((qid).path>>LogNpart)
#define PART(qid)	((qid).path&(Npart-1))

typedef struct Part Part;
typedef struct Disk Disk;

struct Part
{
	ulong	beg;
	ulong	end;
	char	name[NAMELEN];
};

struct Disk
{
	QLock;
	Target*	t;
	uchar	lun;
	char	id[NAMELEN];
	char	vol[NAMELEN];

	uchar*	inquire;
	int	partok;
	ulong	version;
	ulong	size;
	ulong	bsize;

	int	npart;
	Part	table[Npart];
};

int	ndisk;
Disk	disk[Ndisk];

static	int	sdrdpart(Disk*);
static	long	sdio(Chan*, int, char*, ulong, vlong);

static int types[] =
{
	TypeDA, TypeCD, TypeMO,
	-1,
};

static int
sdgen(Chan *c, Dirtab*, int, int s, Dir *dirp)
{
	Qid qid;
	Disk *d;
	Part *p;
	int unit;
	char name[2*NAMELEN];

	d = disk;
	while(s >= d->npart) {
		s -= d->npart;
		d++;
	}
	unit = d - disk;
	if(unit > ndisk)
		return -1;

	p = d->table+s;
	sprint(name, "%s%s", d->vol, p->name);
	name[NAMELEN-1] = '\0';
	qid = (Qid){(unit<<LogNpart)+s, 0};
	devdir(c, qid, name, (p->end - p->beg) * d->bsize, eve, 0666, dirp);
	return 1;
}

static void
sdinit(void)
{
	Disk *d;
	uchar *scratch, type;
	int dev, i, nbytes;

	dev = 0;
	scratch = scsialloc(0xFF);
	for(;;) {
		d = &disk[ndisk];
		dev = scsiinv(dev, types, &d->t, &d->inquire, d->id);
		if(dev < 0)
			break;

		/*
		 * Search for all the lun's.
		 */
		for(i = 0; i < Nlun; i++) {
			d->lun = i;

			/*
			 * A SCSI target does not support a lun if the
			 * the peripheral device type and qualifier fields
			 * in the response to an inquiry command are 0x7F.
			 */
			nbytes = 0xFF;
			memset(scratch, 0, nbytes);
			if(scsiinquiry(d->t, d->lun, scratch, &nbytes) != STok)
				continue;
			if(scratch[0] == 0x7F)
				continue;
			type = scratch[0] & 0x1F;

			/*
			 * Read-capacity is mandatory for TypeDA, TypeMO and TypeCD.
			 * It may return 'not ready' if TypeDA is not spun up,
			 * TypeMO or TypeCD are not loaded or just plain slow getting
			 * their act together after a reset.
			 * If 'not ready' comes back, try starting a TypeDA and punt
			 * the get capacity until the drive is attached.
			 */
			if(scsicap(d->t, d->lun, &d->size, &d->bsize) != STok) {
				nbytes = 0xFF;
				memset(scratch, 0, nbytes);
				scsireqsense(d->t, 0, scratch, &nbytes, 1);
				if((scratch[2] & 0x0F) != 0x02 && (scratch[2] & 0x0F) != 0)
					continue;
				if(type == TypeDA)
					scsistart(d->t, d->lun, 0);
				d->size = d->bsize = 0;
			}

			switch(type){

			case TypeDA:
			case TypeMO:
				sprint(d->vol, "sd%d", ndisk);
				break;

			case TypeCD:
				sprint(d->vol, "cd%d", ndisk);
				break;

			default:
				continue;
			}

			if(++ndisk >= Ndisk)
				break;
			d++;
			d->t = d[-1].t;
			d->inquire = d[-1].inquire;
			strcpy(d->id, d[-1].id);
		}

		if(ndisk >= Ndisk) {
			print("devsd: configure more disks\n");
			break;
		}
	}
	scsifree(scratch);
}

static Chan*
sdattach(char *spec)
{
	int i;

	for(i = 0; i < ndisk; i++){
		qlock(&disk[i]);
		if(disk[i].partok == 0)
			sdrdpart(&disk[i]);
		qunlock(&disk[i]);
	}

	return devattach('w', spec);
}

static int
sdwalk(Chan *c, char *name)
{
	return devwalk(c, name, 0, 0, sdgen);
}

static void
sdstat(Chan *c, char *db)
{
	devstat(c, db, 0, 0, sdgen);
}

static Chan*
sdopen(Chan *c, int omode)
{
	return devopen(c, omode, 0, 0, sdgen);
}

static void
sdclose(Chan *c)
{
	Disk *d;
	Part *p;

	if(c->qid.path & CHDIR)
		return;

	d = &disk[DRIVE(c->qid)];
	p = &d->table[PART(c->qid)];
	if((c->mode&3) != OREAD && strcmp(p->name, "partition") == 0){
		qlock(d);
		d->partok = 0;
		sdrdpart(d);
		qunlock(d);
	}
}

static long
sdread(Chan *c, void *a, long n, vlong off)
{

	if(c->qid.path & CHDIR)
		return devdirread(c, a, n, 0, 0, sdgen);

	return sdio(c, 0, a, n, off);
}

static long
sdwrite(Chan *c, char *a, long n, vlong off)
{
	Disk *d;

	d = &disk[DRIVE(c->qid)];

	if((d->inquire[0] & 0x1F) == TypeCD)
		error(Eperm);
	return sdio(c, 1, a, n, off);
}

Dev sddevtab = {
	'w',
	"sd",

	devreset,
	sdinit,
	sdattach,
	devclone,
	sdwalk,
	sdstat,
	sdopen,
	devcreate,
	sdclose,
	sdread,
	devbread,
	sdwrite,
	devbwrite,
	devremove,
	devwstat,
};

static int
sdrdpart(Disk *d)
{
	Part *p;
	int n, i;
	char *b, *line[Npart+2], *field[3];
	static char MAGIC[] = "plan9 partitions";

	if(d->partok)
		return STok;

	p = d->table;
	strcpy(p->name, "disk");
	p->beg = 0;
	p->end = 0;
	d->npart = 1;

	/*
	 * If the drive wasn't ready when a read-capacity was tried
	 * earlier (in sdinit()), try again.
	 */
	if(d->size == 0 || d->bsize == 0){
		n = scsicap(d->t, d->lun, &d->size, &d->bsize);
		if(n != STok){
			d->size = d->bsize = 0;
			return scsierrstr(n);
		}
	}

	p->end = d->size + 1;

	if((d->inquire[0] & 0x1F) == TypeCD)
		return STok;

	b = scsialloc(d->bsize);
	if(b == 0)
		return scsierrstr(STnomem);

	p++;
	strcpy(p->name, "partition");
	p->beg = d->table[0].end - 1;
	p->end = d->table[0].end;
	p++;
	d->npart = 2;

	scsibio(d->t, d->lun, SCSIread, b, 1, d->bsize, d->table[0].end-1);
	b[d->bsize-1] = '\0';

	/*
	 *  parse partition table.
	 */
	n = parsefields(b, line, nelem(line), "\n");
	if(n > 0 && strncmp(line[0], MAGIC, sizeof(MAGIC)-1) == 0) {
		for(i = 1; i < n; i++) {
			if(parsefields(line[i], field, nelem(field), " ") != 3)
				break;
			if(p >= &d->table[Npart])
				break;
			strncpy(p->name, field[0], NAMELEN);
			p->beg = strtoul(field[1], 0, 0);
			p->end = strtoul(field[2], 0, 0);
			if(p->beg > p->end || p->beg >= d->table[0].end)
				break;
			p++;
		}
	}
	d->npart = p - d->table;
	scsifree(b);

	d->partok = 1;

	return STok;
}

static long
sdio(Chan *c, int write, char *a, ulong len, vlong off)
{
	Disk *d;
	Part *p;
	uchar *b;
	ulong block, n, max, x;
	ulong offset;

	d = &disk[DRIVE(c->qid)];
	p = &d->table[PART(c->qid)];

	block = (off / d->bsize) + p->beg;
	n = (off + len + d->bsize - 1) / d->bsize + p->beg - block;
	max = SCSImaxxfer / d->bsize;
	if(n > max)
		n = max;
	if(block + n > p->end)
		n = p->end - block;
	if(block >= p->end || n == 0)
		return 0;

	b = scsialloc(n*d->bsize);
	if(b == 0)
		return scsierrstr(STnomem);

	offset = off % d->bsize;
	if(write) {
		if(offset || len % d->bsize) {
			x = scsibio(d->t, d->lun, SCSIread, b, n, d->bsize, block);
			if(x < 0){
				len = -1;
				goto buggery;
			}
			if(x < n * d->bsize) {
				n = x / d->bsize;
				x = n * d->bsize - offset;
				if(len > x)
					len = x;
			}
		}
		memmove(b + offset, a, len);
		x = scsibio(d->t, d->lun, SCSIwrite, b, n, d->bsize, block);
		if(x < 0){
			len = -1;
			goto buggery;
		}
		if(x < offset)
			len = 0;
		else
		if(len > x - offset)
			len = x - offset;
	}
	else {
		x = scsibio(d->t, d->lun, SCSIread, b, n, d->bsize, block);
		if(x < 0){
			len = -1;
			goto buggery;
		}
		if(x < offset)
			len = 0;
		else
		if(len > x - offset)
			len = x - offset;
		memmove(a, b+offset, len);
	}

buggery:
	scsifree(b);
	return len;
}