summaryrefslogtreecommitdiffstats
path: root/sys/dev/slice/slice_base.c
blob: 4aa3792449caafe35cb7e13d9aebefc363622d88 (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
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*-
 * Copyright (C) 1997,1998 Julian Elischer.  All rights reserved.
 * julian@freebsd.org
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 *	$Id: slice_base.c,v 1.5 1998/07/13 08:22:56 julian Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>		/* SYSINIT stuff */
#include <sys/fcntl.h>		/* FREAD/FWRITE */
#include <sys/conf.h>		/* cdevsw stuff */
#include <sys/malloc.h>		/* malloc region definitions */
#include <sys/buf.h>		/* buffers for IO */
#include <sys/queue.h>		/* linked lists etc. */
#include <sys/stat.h>		/* S_IFCHR, S_IFBLK */
#include <sys/sysctl.h>		/* the sysctl for shooting self in foot */
/*#include <sys/devfsext.h> */	/* DEVFS defintitions */
#include <dev/slice/slice.h>	/* temporary location */

#define SLICESPL() splbio()

static void sl_async_done(struct buf *bp);


static int slicexclusive = 0; /* default value == "foot shootable" */

/*
 * Make a new type available. Just link it in, but first make sure there is
 * no name collision.
 */

static sh_p     types;

int
sl_newtype(sh_p tp)
{
	if (sl_findtype(tp->name)) {
		return (EEXIST);
	}
	tp->next = types;
	types = tp;
	return (0);
}

/*
 * Look for a type of the name given.
 */
sh_p
sl_findtype(char *type)
{
	sh_p            tp;

	tp = types;
	while (tp) {
		if (strcmp(tp->name, type) == 0)
			return (tp);
		tp = tp->next;
	}
	return (NULL);
}

/*
 * Make a handler instantiation of the requested type.
 * don't take no for an answer.
 * force it to mark it's new territory.
 * Must be called from a within a user context.
 * 
 */
static int
sl_make_handler(sl_p slice, char *type)
{
	sh_p            handler_up;

	/*
	 * check that the type makes sense.
	 */
	if (type == NULL) {
		return (EINVAL);
	}
	handler_up = sl_findtype(type);
	if (handler_up == NULL) {
		return (ENXIO);
	}

	/*
	 * and call the constructor
	 */
	slice->flags |= SLF_DONT_ARGUE;
	return( (*handler_up->claim) (slice));
}

/*
 * lock and unlock Slices while doing operations such as open().
 * gets a reference on the slice..
 * XXX This doesn't work for SMP.
 */
int
slice_lock(struct slice *slice)
{
	int s = SLICESPL();
	slice->refs++;
	while ( slice->flags & (SLF_LOCKED | SLF_INVALID)) {
		if (slice->flags & SLF_INVALID) {
			sl_unref(slice);
			splx(s);
			return (ENXIO);
		}
		slice->flags |= SLF_WANTED;
		tsleep(slice, PRIBIO, "slice_lock", 0);
	}
	slice->flags |= SLF_LOCKED;
	splx(s);
	return (0);
}

/*
 * Releases a slice
 * Assumes that if we had it locked, no-one else could invalidate it.
 * We can still hold a reference on it.
 */
int
slice_unlock(struct slice *slice)
{
	int s = SLICESPL();
	slice->flags &= ~SLF_LOCKED;
	if ( slice->flags & SLF_WANTED) {
		slice->flags &= ~SLF_WANTED;
		wakeup(slice);
	}
	splx(s);
	return (0);
}

/*
 * create a new slice. Link it into the structures.
 * As of yet it has no upper handler.
 */
int
sl_make_slice(sh_p handler_down, void *private_down,
	      struct slicelimits * limits,
	      sl_p * slicepp, char *name)
{
	sl_p            slice;

	/*
	 * Allocate storage for this instance .
	 */
	slice = malloc(sizeof(*slice), M_DEVBUF, M_NOWAIT);
	if (slice == NULL) {
		printf("slice failed to allocate driver storage\n");
		return (ENOMEM);
	}
	bzero(slice, sizeof(*slice));
	if (name) {
		slice->name = malloc(strlen(name) + 1, M_DEVBUF, M_NOWAIT);
		if (slice->name == NULL) {
			printf("slice failed name storage\n");
			free(slice, M_DEVBUF);
			return (ENOMEM);
		}
		strcpy(slice->name, name);
	}
	slice->handler_down = handler_down;
	slice->private_down = private_down;
	handler_down->refs++;
	slice->limits = *limits;
	slice_add_device(slice);
	slice->refs = 1; /* one for our downward creator */
	*slicepp = slice;
	return (0);
}

/*
 * Forceably start a shutdown process on a slice. Either call it's shutdown
 * method, or do the default shutdown if there is no type-specific method.
 * XXX Really should say who called us.
 * Should be called at SLICESPL (splbio)
 */
void
sl_rmslice(sl_p slice)
{
RR;
	/*
	 * An extra reference so it doesn't go away while we are not looking.
	 */
	slice->refs++;

	if (slice->flags & SLF_INVALID) {
		/*
		 * If it's already shutting down, let it die without further
		 * taunting. "go away or I'll taunt you a second time, you
		 * silly eenglish pig-dog"
		 */
		sl_unref(slice);/* possibly the last reference */
		return;
	}

	/*
	 * Mark it as invalid so any newcomers know not to try use it.
	 * No real need to LOCK it.
	 */
	slice->flags &= ~SLF_OPEN_STATE;
	slice->flags |= SLF_INVALID;

	/*
	 * remove the device appendages.
	 * Any open vnodes SHOULD go to deadfs.
	 */
	slice_remove_device(slice);

	/*
	 * Propogate the damage upwards.
 	 * Note that the revoke method is not optional.
	 * The upper handler releases it's reference so refs--.
	 */
	if (slice->handler_up) {
		(*slice->handler_up->revoke) (slice->private_up);
	}
	sl_unref(slice);	/* One for the lower handler that called us */
	sl_unref(slice);	/* possibly the last reference */
}



void
sl_unref(sl_p slice)
{
	if ((--(slice->refs)) == 0) {
		FREE(slice, M_DEVBUF);
	}
}


/***********************************************************************
 * Handler probing state machine support.
 ***********************************************************************/

/*
 * Ask all known handler types if a given slice is handled by them.
 * If the slice specifies a type, then just find that.
 * This will be done asynchronously. The claim operation may simply
 * queue the work to be done.  When this item has been rejected, 
 * control will pass to slice_probe_next(). 
 * This  starts up the generic probeing state machine, which
 * will start up the probing state machine for each handler in turn,
 * until one has claimed the device, or there are no more handlers.
 * 
 */
void
slice_start_probe(sl_p slice)
{
	sh_p            tp = types;

	if (slice->probeinfo.type == NULL) {
		if(slice->handler_up == NULL) {
			slice->probeinfo.trial_handler = tp;
			slice->flags |= SLF_PROBING;
			printf("%s: probing for %s.. ",slice->name, tp->name);  
			(*tp->claim) (slice);
		}
		return;
	}

	/*
	 * Null string ("") means "don't even try". Caller probably
	 * should pre-trap such cases but we'll check here too.
	 * Notice that the PROBING bit is not set.
	 * This means that we should not do a full probe,
	 * but just this one handler.
	 */
	if (slice->probeinfo.type[0]) {
		tp = sl_findtype(slice->probeinfo.type);
		if (tp) {
			printf("%s: attaching %s..\n", slice->name, tp->name);
			(*tp->claim) (slice);
		}
	}
}

/*
 * Move the slice probe type, on to the next type
 * and call that.  Called from failed probes.
 * Don't do anything if the PROBING flag has been cleared.
 */
void
slice_probe_next(sl_p slice)
{
	sh_p            tp = slice->probeinfo.trial_handler;
	
	if ((slice->flags & SLF_PROBING) == 0)
		return;
	if (tp != NULL) {
		if (slice->probeinfo.trial_handler = tp = tp->next) {
			printf("%s: probing for %s.. ",slice->name, tp->name);  
			(*tp->claim) (slice);
			return;
		}
	}
	slice->flags &= ~SLF_PROBING;
}


/*
 * Given a slice,  launch an IOrequest for information
 * This is not a bulk IO routine but meant for probes etc.
 * This routine may be called at interrupt time. It schedules an 
 * IO that will be completed asynchronously. On completion the
 * Block IO system will call sl_async_done, which will trigger
 * a completion event for the handler's probe state machine.
 */
int
slice_request_block(sl_p slice, int blknum)
{
	struct buf     *bp;
	int		s;

RR;
	s = splbio();
#ifdef	PARANOID
	if ( slice->private_up == NULL) {
		panic("slice_request_block: no pd");
	}
	if (slice->flags & SLF_PROBE_STATE) {
		panic("slice_request_block: 2nd IO");
	}
#endif	/* PARANOID */
	bp = geteblk((int) slice->limits.blksize);
	if (bp == NULL) {
		return (ENOMEM);
	}
	slice->flags |= SLF_WAIT_READ;
	bp->b_iodone = &sl_async_done;
	bp->b_flags |= B_CALL;
	bp->b_dev    = (dev_t)slice; /* XXX HACK ALERT! */
	bp->b_pblkno = bp->b_blkno = blknum;
	bp->b_bcount = slice->limits.blksize;
	bp->b_flags |= B_BUSY | B_READ;
	sliceio(slice, bp, SLW_ABOVE);
	splx(s);
	return (0);
}

/*
 * Write a block on behalf of a handler.
 * This is not a bulk IO routine but meant for probes etc.
 * I think that perhaps it should attempt to do sliceopen()
 * calls on the slice first. (XXX?) no, they may block?
 */
int
slice_writeblock(struct slice * slice, int blkno,
			void (*iodone )(struct buf *),
			caddr_t data, int len)
{
	struct buf     *bp;
	int             error = 0;

#ifdef	PARANOID
	if ( slice->handler_up == NULL) {
		panic("slice_writeblock: no handler");
	}
	if (slice->flags & SLF_PROBE_STATE) {
		panic("slice_writeblock: 2nd IO");
	}
#endif	/* PARANOID */
	if (len > slice->limits.blksize)
		return (EINVAL);
	bp = geteblk((int) slice->limits.blksize);
	if (bp == NULL) {
		return (ENOMEM);
	}
	slice->flags |= SLF_WAIT_WRITE;
	bcopy(data, bp->b_data, len);
	bp->b_iodone = sl_async_done;
	bp->b_flags |= B_CALL;
	bp->b_dev    = (dev_t)slice; /* XXX HACK ALERT! */
	bp->b_pblkno = bp->b_blkno = blkno;
	bp->b_bcount = slice->limits.blksize;
	bp->b_flags |= B_BUSY | B_WRITE;
	sliceio(slice, bp, SLW_ABOVE);
	return (0);
}

/* 
 * called with an argument of a bp when it is completed.
 * Th eslice is extracted from the operation and the completion event
 * is used to trigger that slice's state machine to make the next move.
 */
static void
sl_async_done(struct buf *bp)
{
	sl_p		slice;
	int		error;

RR;
	if (bp->b_dev < 0xf0000000)
		panic ("b_dev used in SLICE code");
	slice = (struct slice *)bp->b_dev; /* XXX HACK! */

#ifdef	PARANOID
	if ( slice->handler_up == NULL) {
		panic("sl_async_done: no pd");
	}
	if (bp->b_flags & B_READ) {
		if ((slice->flags & SLF_PROBE_STATE) != SLF_WAIT_READ)
			panic("sl_async_done: unexpected read completion");
	} else {
		if ((slice->flags & SLF_PROBE_STATE) != SLF_WAIT_WRITE)
			panic("sl_async_done: unexpected write completion");
	}
#endif	/* PARANOID */

	/*
	 * if the IO failed, then abandon the probes and 
	 * return. Possibly ask the lower layer to try again later?
	 * It's assumed that a a revoke will abort the state machine.
	 * XXX Maybe we should call the done() routine anyhow
	 * and let each handler detect the failure..
	 */
	if (bp->b_flags & B_ERROR) {
		(* slice->handler_up->revoke)(slice->private_up);

		/* (* slice->handler_down->SOMETHING) (slice->private_down); */

		bp->b_flags |= B_INVAL | B_AGE;
		brelse(bp);
		slice->flags &= ~SLF_PROBING;
		return;
	}
		
	/*
	 * Call the handler's done() routine. This will
	 * examine the result of the probe and do whatever is needed.
	 * Check for abnormal error conditions. (return value non 0)
	 * Not claiming the slice is not an error condition.
	 */
	if ( (* slice->handler_up->done)(slice, bp)) {
		slice->flags &= ~SLF_PROBING;
		return;
	}

	/*
	 * If the handler has left itself there, or cleared
	 * the PROBING bit, then consider
	 * probing to have come to a close. So just return.
	 * XXX An IO error would be a great hint to abandon probing as well.
	 * we catch that on the way up but we might want to give
	 * the handler a chance to clean up state?
	 */
	if (slice->handler_up || ((slice->flags & SLF_PROBING) == 0)) {
		slice->flags &= ~SLF_PROBING;
		return;
	}


	/*
	 * The handler didn't claim it. Nor did it abort the 
	 * probing sequence. 
	 * Ok, so we should try the next handler to probe.
	 */
	slice_probe_next(slice);
}

/*
 * functions that are used to call the next level down.
 */
void 
sliceio(sl_p slice, struct buf * bp, enum slc_who who)
{
	/* XXX do shortcuts here */

	if (slice->flags & SLF_INVALID) {
		bp->b_error = ENXIO;
		goto bad;
	}
	/*
	 * if it's from above, assume it hasn't
	 * broken it's agreement about read/write.
	 * A higher level slice would have caught it.
	 * Make no such assumption if it's this device.
	 */
	if (who == SLW_DEVICE) {
		if (((slice->flags & SLF_OPEN_DEV_WR) == 0) &&
		( (bp->b_flags & B_READ) == B_WRITE )) {
			bp->b_error = EROFS;
			goto bad;
		}
	}
	(*slice->handler_down->IOreq) (slice->private_down, bp);
	return;
bad:
	bp->b_flags |= B_ERROR;
	/* toss transfer, we're done early */
	biodone(bp);
	return;
}

/*
 * Try open a slice.
 * don't forget to say if we are above (1) or the dev (0).
 *
 * We really need to add a lot of support for CHANGING
 * what we have openned.. i.e if we have ABOVE open R/W
 * and DEVICE open R/O, then closing the device
 * should downgrade our open to those items below us to R/O.
 * This would need support in both open and close routines in both
 * slice and handler code.
 * 
 * ((*) == Illegal state.. (how did we get here?))
 * (must have been in "shoot foot mode"). 
 * A bit already set can be set again. (may represent part of an upgrade)
 * This may not hold true if we are in an 'illegal state'.
 * Some such opens will fail in an attempt to revert to a legal state.
 * success = ((request & allowed[state]) == request)
 */
#define UP_RDWR		SLF_OPEN_UP
#define CHR_RDWR	SLF_OPEN_CHR
#define CHR_RD		SLF_OPEN_CHR_RD
#define BLK_RDWR	SLF_OPEN_BLK
#define BLK_RD		SLF_OPEN_BLK_RD
static u_char allowed[64] = {
/* Present state  |  requested states allowed		*/
/* UP  CHR BLK    |      UP    CHR    BLK		*/
/* R W R W R W    |     R W    R W    R W		*/
/* 0 0 0 0 0 0		1 1    1 1    1 1	*/( UP_RDWR|CHR_RDWR|BLK_RDWR ),
/* 0 0 0 0 0 1		0 0    1 0    1 1	*/( CHR_RD|BLK_RDWR ),
/* 0 0 0 0 1 0		1 1    1 1    1 1	*/( UP_RDWR|CHR_RDWR|BLK_RDWR ),
/* 0 0 0 0 1 1		0 0    1 0    1 1	*/( CHR_RD|BLK_RDWR ),
/* 0 0 0 1 0 0		0 0    1 1    1 0	*/( CHR_RDWR|BLK_RD ),
/* 0 0 0 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 0 0 1 1 0		0 0    1 1    1 0	*/( CHR_RDWR|BLK_RD ),
/* 0 0 0 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 0 1 0 0 0		1 1    1 1    1 1	*/( UP_RDWR|CHR_RDWR|BLK_RDWR ),
/* 0 0 1 0 0 1		0 0    1 0    1 1	*/( CHR_RD|BLK_RDWR ),
/* 0 0 1 0 1 0		1 1    1 1    1 1	*/( UP_RDWR|CHR_RDWR|BLK_RDWR ),
/* 0 0 1 0 1 1		0 0    1 0    1 1	*/( CHR_RD|BLK_RDWR ),
/* 0 0 1 1 0 0		0 0    1 1    1 0	*/( CHR_RDWR|BLK_RD ),
/* 0 0 1 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 0 1 1 1 0		0 0    1 1    1 0	*/( CHR_RDWR|BLK_RD ),
/* 0 0 1 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 0 0 0 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 0 1 0 0 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 0 0 1 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 0 1 0 0 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 0 1 0 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 0 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 0 1 1 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 0 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 1 0 0 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 0 1 1 0 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 1 0 1 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 0 1 1 0 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 1 1 0 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 1 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 1 1 1 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 0 1 1 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 0 0 0 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 0 0 0 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 0 0 1 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 0 0 0 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 0 1 0 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 0 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 0 1 1 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 0 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 1 0 0 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 0 1 0 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 1 0 1 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 0 1 0 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 1 1 0 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 1 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 1 1 1 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 0 1 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 0 0 0 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 1 0 0 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 0 0 1 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 1 0 0 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 0 1 0 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 0 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 0 1 1 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 0 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 1 0 0 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 1 1 0 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 1 0 1 0		1 1    1 0    1 0	*/( UP_RDWR|CHR_RD|BLK_RD ),
/* 1 1 1 0 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 1 1 0 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 1 1 0 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 1 1 1 0		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ),
/* 1 1 1 1 1 1		0 0    1 0    1 0 (*)	*/( CHR_RD|BLK_RD ) };

int
sliceopen(struct slice *slice, int flags, int mode,
	struct proc * p, enum slc_who who)
{
	int s;
	int	error;
	int	sl_flags = slice->flags & SLF_OPEN_STATE;
	int	or_flags;
	int	and_flags;
	int	dn_flags;
	int	odn_flags;

	
	if (slice->flags & SLF_INVALID)
		return (ENXIO);
	/*
	 * Firstly, don't allow re-opens of what is already open
	 */
	if (error = slice_lock(slice))
		return (error);
	error = EBUSY;	/* default answer */
	switch (who) {
	case	SLW_ABOVE:
		or_flags = ((flags & FREAD)  ? SLF_OPEN_UP_RD : 0);
		or_flags |= ((flags & FWRITE) ? SLF_OPEN_UP_WR : 0);
		and_flags = ~SLF_OPEN_UP;
		break;
	case	SLW_DEVICE:
		switch (mode & S_IFMT) {
		case S_IFCHR:
			or_flags = ((flags & FREAD)  ? SLF_OPEN_CHR_RD : 0);
			or_flags |= ((flags & FWRITE) ? SLF_OPEN_CHR_WR : 0);
			and_flags = ~SLF_OPEN_CHR;
			break;
		case S_IFBLK:
			or_flags = ((flags & FREAD)  ? SLF_OPEN_BLK_RD : 0);
			or_flags |= ((flags & FWRITE) ? SLF_OPEN_BLK_WR : 0);
			and_flags = ~SLF_OPEN_BLK;
			break;
		default:
			panic("slice: bad open type");
		}
/* XXX 	only accumulate flags as we don't know about all closes */
/* XXX */	if ( or_flags )
/* XXX */		and_flags = ~0;
		break;
	default:
		panic("slice: bad request source");
	}
	/* 
	 * Be appropriatly paranoid depending on the system mode.
	 * This is also probably wrong XXX
	 */
	switch(slicexclusive) {
	case 2:
		/*
		 * if any one path has it open, we forbid any other
		 * paths. Only allow an upgrade/downgrade from
		 * the same source as the present openner.
		 */
		if ( sl_flags & and_flags)
			goto reject;
	case 1: /*
		 * The behaviour is encoded into the state array given above.
	 	 */
 		if ((or_flags & allowed[sl_flags]) != or_flags)
			goto reject;
		break;
	case 0: /*
		 * Permission is granted to shoot self in foot.
		 * All three of UPPER, CHAR and BLK can be open at once.
		 */
		break;
	}
	/*
	 * Get the old open mode and the new open mode.
	 * If we already have it open in this way, don't do it again.
	 * 
	 * XXX More thought needed for the locking and open-flags.
	 * For now ignore the existance of flags other than FWRITE & FREAD.
	 */
	odn_flags = (sl_flags & SLF_OPEN_WR) ? FWRITE : 0;
	odn_flags |= (sl_flags & SLF_OPEN_RD) ? FREAD : 0;
	sl_flags &= and_flags;
	sl_flags |= or_flags;
	dn_flags = (sl_flags & SLF_OPEN_WR) ? FWRITE : 0;
	dn_flags |= (sl_flags & SLF_OPEN_RD) ? FREAD : 0;
	error = 0;
	if (dn_flags != odn_flags) {
		if ((error = (*slice->handler_down->open) (slice->private_down,
				      dn_flags, mode, p)) != 0) { 
			goto reject;
		}
	}
	slice->flags &= ~SLF_OPEN_STATE;
	slice->flags |= sl_flags;
#if 1	/* it was basically a close */
	if ((slice->flags & SLF_OPEN_STATE) == SLF_CLOSED) {
		sh_p	tp;

		/*
		 * If we had an upper handler, ask it to check if it's still
		 * valid. it may decide to self destruct.
		 */
		if (slice->handler_up) {
			(*slice->handler_up->verify)(slice);
		}
		/*
		 * If we don't have an upper handler, check if
		 * maybe there is now a suitable environment for one.
		 * We may end up with a different handler
		 * from what we had above. Maybe we should clear the hint?
		 * Maybe we should ask the lower one to re-issue the request?
		 */
		if (slice->handler_up == NULL) {
			slice_start_probe(slice);
		}
	}
#endif
reject:
	slice_unlock(slice);
	if ((slice->flags & SLF_INVALID) == SLF_INVALID)
		error = ENODEV; /* we've been zapped while down there! */
	sl_unref(slice); /* slice_lock gave us a ref.*/
	return (error);
}

#if 0
void
sliceclose(struct slice *slice, int flags, int mode,
	struct proc * p, enum slc_who who)
{
	sh_p	tp;

	if (slice->flags & SLF_INVALID) 
		return ;
	if (slice_lock(slice))
		return ;
	switch (who) {
	case	SLW_ABOVE:
		slice->flags &= ~SLF_OPEN_UP;
		break;
	case	SLW_DEVICE:
		switch (mode & S_IFMT) {
		case S_IFCHR:
			slice->flags &= ~SLF_OPEN_CHR;
			break;
		case S_IFBLK:
			slice->flags &= ~SLF_OPEN_BLK;
			break;
		default:
			panic("slice: bad open type");
		}
		/*
		 * If we had an upper handler, ask it to check if it's still
		 * valid. it may decide to self destruct.
		 */
		if (slice->handler_up) {
			(*slice->handler_up->verify)(slice);
		}
		/*
		 * If we don't have an upper handler, check if
		 * maybe there is now a suitable environment for one.
		 * We may end up with a different handler
		 * from what we had above. Maybe we should clear the hint?
		 * Maybe we should ask the lower one to re-issue the request?
		 */
		if (slice->handler_up == NULL) {
			if ((tp = slice_start_probe(slice)) != NULL) {
				(*tp->constructor)(slice);
			}
		}
		break;
	}
	/*
	 * Last-close semantics strike again
	 * This may refine to a downgrade if we closed (say) the last writer
	 * but there are still readers.
	 * probably open/close should merge to one 'mode-change' function.
	 * (except for a vnode reference with no mode)
	 */
	if ( (slice->flags & SLF_OPEN_STATE) == 0)
		(*slice->handler_down->close) (slice->private_down,
				      flags, mode, p);
	slice_unlock(slice);
	sl_unref(slice);
	return ;
}
#endif /* 0 */

/*
 * control behaviour of slices WRT sharing:
 * 2 = no sharing
 * 1 = read on a device already mounted (or parent of) is ok. No writes.
 * 0 = go ahead.. shoot yourself in the foot.
 */
static int
sysctl_kern_slicexclusive SYSCTL_HANDLER_ARGS
{
	int error;
	int new_val = slicexclusive;

	error = sysctl_handle_int(oidp, &new_val, 0, req);
	if (error == 0) {
		if ((new_val >= 0) && (new_val < 3)) {
			slicexclusive = new_val;
		} else {
			error = EINVAL;
		}
	}
	return (error);
}

SYSCTL_PROC(_kern, OID_AUTO, slicexclusive, CTLTYPE_INT|CTLFLAG_RW,
	0, sizeof slicexclusive, sysctl_kern_slicexclusive, "I", "");

OpenPOWER on IntegriCloud