]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/doc/psd/03.iosys/iosys
Merge llvm-project main llvmorg-17-init-19304-gd0b54bb50e51
[FreeBSD/FreeBSD.git] / share / doc / psd / 03.iosys / iosys
1 .\" Copyright (C) Caldera International Inc. 2001-2002.  All rights reserved.
2 .\" 
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions are
5 .\" met:
6 .\" 
7 .\" Redistributions of source code and documentation must retain the above
8 .\" copyright notice, this list of conditions and the following
9 .\" disclaimer.
10 .\" 
11 .\" Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 
15 .\" All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" 
18 .\" This product includes software developed or owned by Caldera
19 .\" International, Inc.  Neither the name of Caldera International, Inc.
20 .\" nor the names of other contributors may be used to endorse or promote
21 .\" products derived from this software without specific prior written
22 .\" permission.
23 .\" 
24 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
25 .\" INTERNATIONAL, INC.  AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 .\" DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
35 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 .\" 
37 .EH 'PSD:3-%''The UNIX I/O System'
38 .OH 'The UNIX I/O System''PSD:3-%'
39 .TL
40 The UNIX I/O System
41 .AU
42 Dennis M. Ritchie
43 .AI
44 AT&T Bell Laboratories
45 Murray Hill, NJ
46 .PP
47 This paper gives an overview of the workings of the UNIX\(dg
48 .FS
49 \(dgUNIX is a Trademark of Bell Laboratories.
50 .FE
51 I/O system.
52 It was written with an eye toward providing
53 guidance to writers of device driver routines,
54 and is oriented more toward describing the environment
55 and nature of device drivers than the implementation
56 of that part of the file system which deals with
57 ordinary files.
58 .PP
59 It is assumed that the reader has a good knowledge
60 of the overall structure of the file system as discussed
61 in the paper ``The UNIX Time-sharing System.''
62 A more detailed discussion
63 appears in
64 ``UNIX Implementation;''
65 the current document restates parts of that one,
66 but is still more detailed.
67 It is most useful in
68 conjunction with a copy of the system code,
69 since it is basically an exegesis of that code.
70 .SH
71 Device Classes
72 .PP
73 There are two classes of device:
74 .I block
75 and
76 .I character.
77 The block interface is suitable for devices
78 like disks, tapes, and DECtape
79 which work, or can work, with addressible 512-byte blocks.
80 Ordinary magnetic tape just barely fits in this category,
81 since by use of forward
82 and
83 backward spacing any block can be read, even though
84 blocks can be written only at the end of the tape.
85 Block devices can at least potentially contain a mounted
86 file system.
87 The interface to block devices is very highly structured;
88 the drivers for these devices share a great many routines
89 as well as a pool of buffers.
90 .PP
91 Character-type devices have a much
92 more straightforward interface, although
93 more work must be done by the driver itself.
94 .PP
95 Devices of both types are named by a
96 .I major
97 and a
98 .I minor
99 device number.
100 These numbers are generally stored as an integer
101 with the minor device number
102 in the low-order 8 bits and the major device number
103 in the next-higher 8 bits;
104 macros
105 .I major
106 and
107 .I minor
108 are available to access these numbers.
109 The major device number selects which driver will deal with
110 the device; the minor device number is not used
111 by the rest of the system but is passed to the
112 driver at appropriate times.
113 Typically the minor number
114 selects a subdevice attached to
115 a given controller, or one of
116 several similar hardware interfaces.
117 .PP
118 The major device numbers for block and character devices
119 are used as indices in separate tables;
120 they both start at 0 and therefore overlap.
121 .SH
122 Overview of I/O
123 .PP
124 The purpose of
125 the
126 .I open
127 and
128 .I creat
129 system calls is to set up entries in three separate
130 system tables.
131 The first of these is the
132 .I u_ofile
133 table,
134 which is stored in the system's per-process
135 data area
136 .I u.
137 This table is indexed by
138 the file descriptor returned by the
139 .I open
140 or
141 .I creat,
142 and is accessed during
143 a
144 .I read,
145 .I write,
146 or other operation on the open file.
147 An entry contains only
148 a pointer to the corresponding
149 entry of the
150 .I file
151 table,
152 which is a per-system data base.
153 There is one entry in the
154 .I file
155 table for each
156 instance of
157 .I open
158 or
159 .I creat.
160 This table is per-system because the same instance
161 of an open file must be shared among the several processes
162 which can result from
163 .I forks
164 after the file is opened.
165 A
166 .I file
167 table entry contains
168 flags which indicate whether the file
169 was open for reading or writing or is a pipe, and
170 a count which is used to decide when all processes
171 using the entry have terminated or closed the file
172 (so the entry can be abandoned).
173 There is also a 32-bit file offset
174 which is used to indicate where in the file the next read
175 or write will take place.
176 Finally, there is a pointer to the
177 entry for the file in the
178 .I inode
179 table,
180 which contains a copy of the file's i-node.
181 .PP
182 Certain open files can be designated ``multiplexed''
183 files, and several other flags apply to such
184 channels.
185 In such a case, instead of an offset,
186 there is a pointer to an associated multiplex channel table.
187 Multiplex channels will not be discussed here.
188 .PP
189 An entry in the
190 .I file
191 table corresponds precisely to an instance of
192 .I open
193 or
194 .I creat;
195 if the same file is opened several times,
196 it will have several
197 entries in this table.
198 However,
199 there is at most one entry
200 in the
201 .I inode
202 table for a given file.
203 Also, a file may enter the
204 .I inode
205 table not only because it is open,
206 but also because it is the current directory
207 of some process or because it
208 is a special file containing a currently-mounted
209 file system.
210 .PP
211 An entry in the
212 .I inode
213 table differs somewhat from the
214 corresponding i-node as stored on the disk;
215 the modified and accessed times are not stored,
216 and the entry is augmented
217 by a flag word containing information about the entry,
218 a count used to determine when it may be
219 allowed to disappear,
220 and the device and i-number
221 whence the entry came.
222 Also, the several block numbers that give addressing
223 information for the file are expanded from
224 the 3-byte, compressed format used on the disk to full
225 .I long
226 quantities.
227 .PP
228 During the processing of an
229 .I open
230 or
231 .I creat
232 call for a special file,
233 the system always calls the device's
234 .I open
235 routine to allow for any special processing
236 required (rewinding a tape, turning on
237 the data-terminal-ready lead of a modem, etc.).
238 However,
239 the
240 .I close
241 routine is called only when the last
242 process closes a file,
243 that is, when the i-node table entry
244 is being deallocated.
245 Thus it is not feasible
246 for a device to maintain, or depend on,
247 a count of its users, although it is quite
248 possible to
249 implement an exclusive-use device which cannot
250 be reopened until it has been closed.
251 .PP
252 When a
253 .I read
254 or
255 .I write
256 takes place,
257 the user's arguments
258 and the
259 .I file
260 table entry are used to set up the
261 variables
262 .I u.u_base,
263 .I u.u_count,
264 and
265 .I u.u_offset
266 which respectively contain the (user) address
267 of the I/O target area, the byte-count for the transfer,
268 and the current location in the file.
269 If the file referred to is
270 a character-type special file, the appropriate read
271 or write routine is called; it is responsible
272 for transferring data and updating the
273 count and current location appropriately
274 as discussed below.
275 Otherwise, the current location is used to calculate
276 a logical block number in the file.
277 If the file is an ordinary file the logical block
278 number must be mapped (possibly using indirect blocks)
279 to a physical block number; a block-type
280 special file need not be mapped.
281 This mapping is performed by the
282 .I bmap
283 routine.
284 In any event, the resulting physical block number
285 is used, as discussed below, to
286 read or write the appropriate device.
287 .SH
288 Character Device Drivers
289 .PP
290 The
291 .I cdevsw
292 table specifies the interface routines present for
293 character devices.
294 Each device provides five routines:
295 open, close, read, write, and special-function
296 (to implement the
297 .I ioctl
298 system call).
299 Any of these may be missing.
300 If a call on the routine
301 should be ignored,
302 (e.g.
303 .I open
304 on non-exclusive devices that require no setup)
305 the
306 .I cdevsw
307 entry can be given as
308 .I nulldev;
309 if it should be considered an error,
310 (e.g.
311 .I write
312 on read-only devices)
313 .I nodev
314 is used.
315 For terminals,
316 the
317 .I cdevsw
318 structure also contains a pointer to the
319 .I tty
320 structure associated with the terminal.
321 .PP
322 The
323 .I open
324 routine is called each time the file
325 is opened with the full device number as argument.
326 The second argument is a flag which is
327 non-zero only if the device is to be written upon.
328 .PP
329 The
330 .I close
331 routine is called only when the file
332 is closed for the last time,
333 that is when the very last process in
334 which the file is open closes it.
335 This means it is not possible for the driver to
336 maintain its own count of its users.
337 The first argument is the device number;
338 the second is a flag which is non-zero
339 if the file was open for writing in the process which
340 performs the final
341 .I close.
342 .PP
343 When
344 .I write
345 is called, it is supplied the device
346 as argument.
347 The per-user variable
348 .I u.u_count
349 has been set to
350 the number of characters indicated by the user;
351 for character devices, this number may be 0
352 initially.
353 .I u.u_base
354 is the address supplied by the user from which to start
355 taking characters.
356 The system may call the
357 routine internally, so the
358 flag
359 .I u.u_segflg
360 is supplied that indicates,
361 if
362 .I on,
363 that
364 .I u.u_base
365 refers to the system address space instead of
366 the user's.
367 .PP
368 The
369 .I write
370 routine
371 should copy up to
372 .I u.u_count
373 characters from the user's buffer to the device,
374 decrementing
375 .I u.u_count
376 for each character passed.
377 For most drivers, which work one character at a time,
378 the routine
379 .I "cpass( )"
380 is used to pick up characters
381 from the user's buffer.
382 Successive calls on it return
383 the characters to be written until
384 .I u.u_count
385 goes to 0 or an error occurs,
386 when it returns \(mi1.
387 .I Cpass
388 takes care of interrogating
389 .I u.u_segflg
390 and updating
391 .I u.u_count.
392 .PP
393 Write routines which want to transfer
394 a probably large number of characters into an internal
395 buffer may also use the routine
396 .I "iomove(buffer, offset, count, flag)"
397 which is faster when many characters must be moved.
398 .I Iomove
399 transfers up to
400 .I count
401 characters into the
402 .I buffer
403 starting
404 .I offset
405 bytes from the start of the buffer;
406 .I flag
407 should be
408 .I B_WRITE
409 (which is 0) in the write case.
410 Caution:
411 the caller is responsible for making sure
412 the count is not too large and is non-zero.
413 As an efficiency note,
414 .I iomove
415 is much slower if any of
416 .I "buffer+offset, count"
417 or
418 .I u.u_base
419 is odd.
420 .PP
421 The device's
422 .I read
423 routine is called under conditions similar to
424 .I write,
425 except that
426 .I u.u_count
427 is guaranteed to be non-zero.
428 To return characters to the user, the routine
429 .I "passc(c)"
430 is available; it takes care of housekeeping
431 like
432 .I cpass
433 and returns \(mi1 as the last character
434 specified by
435 .I u.u_count
436 is returned to the user;
437 before that time, 0 is returned.
438 .I Iomove
439 is also usable as with
440 .I write;
441 the flag should be
442 .I B_READ
443 but the same cautions apply.
444 .PP
445 The ``special-functions'' routine
446 is invoked by the
447 .I stty
448 and
449 .I gtty
450 system calls as follows:
451 .I "(*p) (dev, v)"
452 where
453 .I p
454 is a pointer to the device's routine,
455 .I dev
456 is the device number,
457 and
458 .I v
459 is a vector.
460 In the
461 .I gtty
462 case,
463 the device is supposed to place up to 3 words of status information
464 into the vector; this will be returned to the caller.
465 In the
466 .I stty
467 case,
468 .I v
469 is 0;
470 the device should take up to 3 words of
471 control information from
472 the array
473 .I "u.u_arg[0...2]."
474 .PP
475 Finally, each device should have appropriate interrupt-time
476 routines.
477 When an interrupt occurs, it is turned into a C-compatible call
478 on the devices's interrupt routine.
479 The interrupt-catching mechanism makes
480 the low-order four bits of the ``new PS'' word in the
481 trap vector for the interrupt available
482 to the interrupt handler.
483 This is conventionally used by drivers
484 which deal with multiple similar devices
485 to encode the minor device number.
486 After the interrupt has been processed,
487 a return from the interrupt handler will
488 return from the interrupt itself.
489 .PP
490 A number of subroutines are available which are useful
491 to character device drivers.
492 Most of these handlers, for example, need a place
493 to buffer characters in the internal interface
494 between their ``top half'' (read/write)
495 and ``bottom half'' (interrupt) routines.
496 For relatively low data-rate devices, the best mechanism
497 is the character queue maintained by the
498 routines
499 .I getc
500 and
501 .I putc.
502 A queue header has the structure
503 .DS
504 struct {
505         int     c_cc;   /* character count */
506         char    *c_cf;  /* first character */
507         char    *c_cl;  /* last character */
508 } queue;
509 .DE
510 A character is placed on the end of a queue by
511 .I "putc(c, &queue)"
512 where
513 .I c
514 is the character and
515 .I queue
516 is the queue header.
517 The routine returns \(mi1 if there is no space
518 to put the character, 0 otherwise.
519 The first character on the queue may be retrieved
520 by
521 .I "getc(&queue)"
522 which returns either the (non-negative) character
523 or \(mi1 if the queue is empty.
524 .PP
525 Notice that the space for characters in queues is
526 shared among all devices in the system
527 and in the standard system there are only some 600
528 character slots available.
529 Thus device handlers,
530 especially write routines, must take
531 care to avoid gobbling up excessive numbers of characters.
532 .PP
533 The other major help available
534 to device handlers is the sleep-wakeup mechanism.
535 The call
536 .I "sleep(event, priority)"
537 causes the process to wait (allowing other processes to run)
538 until the
539 .I event
540 occurs;
541 at that time, the process is marked ready-to-run
542 and the call will return when there is no
543 process with higher
544 .I priority.
545 .PP
546 The call
547 .I "wakeup(event)"
548 indicates that the
549 .I event
550 has happened, that is, causes processes sleeping
551 on the event to be awakened.
552 The
553 .I event
554 is an arbitrary quantity agreed upon
555 by the sleeper and the waker-up.
556 By convention, it is the address of some data area used
557 by the driver, which guarantees that events
558 are unique.
559 .PP
560 Processes sleeping on an event should not assume
561 that the event has really happened;
562 they should check that the conditions which
563 caused them to sleep no longer hold.
564 .PP
565 Priorities can range from 0 to 127;
566 a higher numerical value indicates a less-favored
567 scheduling situation.
568 A distinction is made between processes sleeping
569 at priority less than the parameter
570 .I PZERO
571 and those at numerically larger priorities.
572 The former cannot
573 be interrupted by signals, although it
574 is conceivable that it may be swapped out.
575 Thus it is a bad idea to sleep with
576 priority less than PZERO on an event which might never occur.
577 On the other hand, calls to
578 .I sleep
579 with larger priority
580 may never return if the process is terminated by
581 some signal in the meantime.
582 Incidentally, it is a gross error to call
583 .I sleep
584 in a routine called at interrupt time, since the process
585 which is running is almost certainly not the
586 process which should go to sleep.
587 Likewise, none of the variables in the user area
588 ``\fIu\fB.\fR''
589 should be touched, let alone changed, by an interrupt routine.
590 .PP
591 If a device driver
592 wishes to wait for some event for which it is inconvenient
593 or impossible to supply a
594 .I wakeup,
595 (for example, a device going on-line, which does not
596 generally cause an interrupt),
597 the call
598 .I "sleep(&lbolt, priority)
599 may be given.
600 .I Lbolt
601 is an external cell whose address is awakened once every 4 seconds
602 by the clock interrupt routine.
603 .PP
604 The routines
605 .I "spl4( ), spl5( ), spl6( ), spl7( )"
606 are available to
607 set the processor priority level as indicated to avoid
608 inconvenient interrupts from the device.
609 .PP
610 If a device needs to know about real-time intervals,
611 then
612 .I "timeout(func, arg, interval)
613 will be useful.
614 This routine arranges that after
615 .I interval
616 sixtieths of a second, the
617 .I func
618 will be called with
619 .I arg
620 as argument, in the style
621 .I "(*func)(arg).
622 Timeouts are used, for example,
623 to provide real-time delays after function characters
624 like new-line and tab in typewriter output,
625 and to terminate an attempt to
626 read the 201 Dataphone
627 .I dp
628 if there is no response within a specified number
629 of seconds.
630 Notice that the number of sixtieths of a second is limited to 32767,
631 since it must appear to be positive,
632 and that only a bounded number of timeouts
633 can be going on at once.
634 Also, the specified
635 .I func
636 is called at clock-interrupt time, so it should
637 conform to the requirements of interrupt routines
638 in general.
639 .SH
640 The Block-device Interface
641 .PP
642 Handling of block devices is mediated by a collection
643 of routines that manage a set of buffers containing
644 the images of blocks of data on the various devices.
645 The most important purpose of these routines is to assure
646 that several processes that access the same block of the same
647 device in multiprogrammed fashion maintain a consistent
648 view of the data in the block.
649 A secondary but still important purpose is to increase
650 the efficiency of the system by
651 keeping in-core copies of blocks that are being
652 accessed frequently.
653 The main data base for this mechanism is the
654 table of buffers
655 .I buf.
656 Each buffer header contains a pair of pointers
657 .I "(b_forw, b_back)"
658 which maintain a doubly-linked list
659 of the buffers associated with a particular
660 block device, and a
661 pair of pointers
662 .I "(av_forw, av_back)"
663 which generally maintain a doubly-linked list of blocks
664 which are ``free,'' that is,
665 eligible to be reallocated for another transaction.
666 Buffers that have I/O in progress
667 or are busy for other purposes do not appear in this list.
668 The buffer header
669 also contains the device and block number to which the
670 buffer refers, and a pointer to the actual storage associated with
671 the buffer.
672 There is a word count
673 which is the negative of the number of words
674 to be transferred to or from the buffer;
675 there is also an error byte and a residual word
676 count used to communicate information
677 from an I/O routine to its caller.
678 Finally, there is a flag word
679 with bits indicating the status of the buffer.
680 These flags will be discussed below.
681 .PP
682 Seven routines constitute
683 the most important part of the interface with the
684 rest of the system.
685 Given a device and block number,
686 both
687 .I bread
688 and
689 .I getblk
690 return a pointer to a buffer header for the block;
691 the difference is that
692 .I bread
693 is guaranteed to return a buffer actually containing the
694 current data for the block,
695 while
696 .I getblk
697 returns a buffer which contains the data in the
698 block only if it is already in core (whether it is
699 or not is indicated by the
700 .I B_DONE
701 bit; see below).
702 In either case the buffer, and the corresponding
703 device block, is made ``busy,''
704 so that other processes referring to it
705 are obliged to wait until it becomes free.
706 .I Getblk
707 is used, for example,
708 when a block is about to be totally rewritten,
709 so that its previous contents are
710 not useful;
711 still, no other process can be allowed to refer to the block
712 until the new data is placed into it.
713 .PP
714 The
715 .I breada
716 routine is used to implement read-ahead.
717 it is logically similar to
718 .I bread,
719 but takes as an additional argument the number of
720 a block (on the same device) to be read asynchronously
721 after the specifically requested block is available.
722 .PP
723 Given a pointer to a buffer,
724 the
725 .I brelse
726 routine
727 makes the buffer again available to other processes.
728 It is called, for example, after
729 data has been extracted following a
730 .I bread.
731 There are three subtly-different write routines,
732 all of which take a buffer pointer as argument,
733 and all of which logically release the buffer for
734 use by others and place it on the free list.
735 .I Bwrite
736 puts the
737 buffer on the appropriate device queue,
738 waits for the write to be done,
739 and sets the user's error flag if required.
740 .I Bawrite
741 places the buffer on the device's queue, but does not wait
742 for completion, so that errors cannot be reflected directly to
743 the user.
744 .I Bdwrite
745 does not start any I/O operation at all,
746 but merely marks
747 the buffer so that if it happens
748 to be grabbed from the free list to contain
749 data from some other block, the data in it will
750 first be written
751 out.
752 .PP
753 .I Bwrite
754 is used when one wants to be sure that
755 I/O takes place correctly, and that
756 errors are reflected to the proper user;
757 it is used, for example, when updating i-nodes.
758 .I Bawrite
759 is useful when more overlap is desired
760 (because no wait is required for I/O to finish)
761 but when it is reasonably certain that the
762 write is really required.
763 .I Bdwrite
764 is used when there is doubt that the write is
765 needed at the moment.
766 For example,
767 .I bdwrite
768 is called when the last byte of a
769 .I write
770 system call falls short of the end of a
771 block, on the assumption that
772 another
773 .I write
774 will be given soon which will re-use the same block.
775 On the other hand,
776 as the end of a block is passed,
777 .I bawrite
778 is called, since probably the block will
779 not be accessed again soon and one might as
780 well start the writing process as soon as possible.
781 .PP
782 In any event, notice that the routines
783 .I "getblk"
784 and
785 .I bread
786 dedicate the given block exclusively to the
787 use of the caller, and make others wait,
788 while one of
789 .I "brelse, bwrite, bawrite,"
790 or
791 .I bdwrite
792 must eventually be called to free the block for use by others.
793 .PP
794 As mentioned, each buffer header contains a flag
795 word which indicates the status of the buffer.
796 Since they provide
797 one important channel for information between the drivers and the
798 block I/O system, it is important to understand these flags.
799 The following names are manifest constants which
800 select the associated flag bits.
801 .IP B_READ 10
802 This bit is set when the buffer is handed to the device strategy routine
803 (see below) to indicate a read operation.
804 The symbol
805 .I B_WRITE
806 is defined as 0 and does not define a flag; it is provided
807 as a mnemonic convenience to callers of routines like
808 .I swap
809 which have a separate argument
810 which indicates read or write.
811 .IP B_DONE 10
812 This bit is set
813 to 0 when a block is handed to the device strategy
814 routine and is turned on when the operation completes,
815 whether normally as the result of an error.
816 It is also used as part of the return argument of
817 .I getblk
818 to indicate if 1 that the returned
819 buffer actually contains the data in the requested block.
820 .IP B_ERROR 10
821 This bit may be set to 1 when
822 .I B_DONE
823 is set to indicate that an I/O or other error occurred.
824 If it is set the
825 .I b_error
826 byte of the buffer header may contain an error code
827 if it is non-zero.
828 If
829 .I b_error
830 is 0 the nature of the error is not specified.
831 Actually no driver at present sets
832 .I b_error;
833 the latter is provided for a future improvement
834 whereby a more detailed error-reporting
835 scheme may be implemented.
836 .IP B_BUSY 10
837 This bit indicates that the buffer header is not on
838 the free list, i.e. is
839 dedicated to someone's exclusive use.
840 The buffer still remains attached to the list of
841 blocks associated with its device, however.
842 When
843 .I getblk
844 (or
845 .I bread,
846 which calls it) searches the buffer list
847 for a given device and finds the requested
848 block with this bit on, it sleeps until the bit
849 clears.
850 .IP B_PHYS 10
851 This bit is set for raw I/O transactions that
852 need to allocate the Unibus map on an 11/70.
853 .IP B_MAP 10
854 This bit is set on buffers that have the Unibus map allocated,
855 so that the
856 .I iodone
857 routine knows to deallocate the map.
858 .IP B_WANTED 10
859 This flag is used in conjunction with the
860 .I B_BUSY
861 bit.
862 Before sleeping as described
863 just above,
864 .I getblk
865 sets this flag.
866 Conversely, when the block is freed and the busy bit
867 goes down (in
868 .I brelse)
869 a
870 .I wakeup
871 is given for the block header whenever
872 .I B_WANTED
873 is on.
874 This strategem avoids the overhead
875 of having to call
876 .I wakeup
877 every time a buffer is freed on the chance that someone
878 might want it.
879 .IP B_AGE
880 This bit may be set on buffers just before releasing them; if it
881 is on,
882 the buffer is placed at the head of the free list, rather than at the
883 tail.
884 It is a performance heuristic
885 used when the caller judges that the same block will not soon be used again.
886 .IP B_ASYNC 10
887 This bit is set by
888 .I bawrite
889 to indicate to the appropriate device driver
890 that the buffer should be released when the
891 write has been finished, usually at interrupt time.
892 The difference between
893 .I bwrite
894 and
895 .I bawrite
896 is that the former starts I/O, waits until it is done, and
897 frees the buffer.
898 The latter merely sets this bit and starts I/O.
899 The bit indicates that
900 .I relse
901 should be called for the buffer on completion.
902 .IP B_DELWRI 10
903 This bit is set by
904 .I bdwrite
905 before releasing the buffer.
906 When
907 .I getblk,
908 while searching for a free block,
909 discovers the bit is 1 in a buffer it would otherwise grab,
910 it causes the block to be written out before reusing it.
911 .SH
912 Block Device Drivers
913 .PP
914 The
915 .I bdevsw
916 table contains the names of the interface routines
917 and that of a table for each block device.
918 .PP
919 Just as for character devices, block device drivers may supply
920 an
921 .I open
922 and a
923 .I close
924 routine
925 called respectively on each open and on the final close
926 of the device.
927 Instead of separate read and write routines,
928 each block device driver has a
929 .I strategy
930 routine which is called with a pointer to a buffer
931 header as argument.
932 As discussed, the buffer header contains
933 a read/write flag, the core address,
934 the block number, a (negative) word count,
935 and the major and minor device number.
936 The role of the strategy routine
937 is to carry out the operation as requested by the
938 information in the buffer header.
939 When the transaction is complete the
940 .I B_DONE
941 (and possibly the
942 .I B_ERROR)
943 bits should be set.
944 Then if the
945 .I B_ASYNC
946 bit is set,
947 .I brelse
948 should be called;
949 otherwise,
950 .I wakeup.
951 In cases where the device
952 is capable, under error-free operation,
953 of transferring fewer words than requested,
954 the device's word-count register should be placed
955 in the residual count slot of
956 the buffer header;
957 otherwise, the residual count should be set to 0.
958 This particular mechanism is really for the benefit
959 of the magtape driver;
960 when reading this device
961 records shorter than requested are quite normal,
962 and the user should be told the actual length of the record.
963 .PP
964 Although the most usual argument
965 to the strategy routines
966 is a genuine buffer header allocated as discussed above,
967 all that is actually required
968 is that the argument be a pointer to a place containing the
969 appropriate information.
970 For example the
971 .I swap
972 routine, which manages movement
973 of core images to and from the swapping device,
974 uses the strategy routine
975 for this device.
976 Care has to be taken that
977 no extraneous bits get turned on in the
978 flag word.
979 .PP
980 The device's table specified by
981 .I bdevsw
982 has a
983 byte to contain an active flag and an error count,
984 a pair of links which constitute the
985 head of the chain of buffers for the device
986 .I "(b_forw, b_back),"
987 and a first and last pointer for a device queue.
988 Of these things, all are used solely by the device driver
989 itself
990 except for the buffer-chain pointers.
991 Typically the flag encodes the state of the
992 device, and is used at a minimum to
993 indicate that the device is currently engaged in
994 transferring information and no new command should be issued.
995 The error count is useful for counting retries
996 when errors occur.
997 The device queue is used to remember stacked requests;
998 in the simplest case it may be maintained as a first-in
999 first-out list.
1000 Since buffers which have been handed over to
1001 the strategy routines are never
1002 on the list of free buffers,
1003 the pointers in the buffer which maintain the free list
1004 .I "(av_forw, av_back)"
1005 are also used to contain the pointers
1006 which maintain the device queues.
1007 .PP
1008 A couple of routines
1009 are provided which are useful to block device drivers.
1010 .I "iodone(bp)"
1011 arranges that the buffer to which
1012 .I bp
1013 points be released or awakened,
1014 as appropriate,
1015 when the
1016 strategy module has finished with the buffer,
1017 either normally or after an error.
1018 (In the latter case the
1019 .I B_ERROR
1020 bit has presumably been set.)
1021 .PP
1022 The routine
1023 .I "geterror(bp)"
1024 can be used to examine the error bit in a buffer header
1025 and arrange that any error indication found therein is
1026 reflected to the user.
1027 It may be called only in the non-interrupt
1028 part of a driver when I/O has completed
1029 .I (B_DONE
1030 has been set).
1031 .SH
1032 Raw Block-device I/O
1033 .PP
1034 A scheme has been set up whereby block device drivers may
1035 provide the ability to transfer information
1036 directly between the user's core image and the device
1037 without the use of buffers and in blocks as large as
1038 the caller requests.
1039 The method involves setting up a character-type special file
1040 corresponding to the raw device
1041 and providing
1042 .I read
1043 and
1044 .I write
1045 routines which set up what is usually a private,
1046 non-shared buffer header with the appropriate information
1047 and call the device's strategy routine.
1048 If desired, separate
1049 .I open
1050 and
1051 .I close
1052 routines may be provided but this is usually unnecessary.
1053 A special-function routine might come in handy, especially for
1054 magtape.
1055 .PP
1056 A great deal of work has to be done to generate the
1057 ``appropriate information''
1058 to put in the argument buffer for
1059 the strategy module;
1060 the worst part is to map relocated user addresses to physical addresses.
1061 Most of this work is done by
1062 .I "physio(strat, bp, dev, rw)
1063 whose arguments are the name of the
1064 strategy routine
1065 .I strat,
1066 the buffer pointer
1067 .I bp,
1068 the device number
1069 .I dev,
1070 and a read-write flag
1071 .I rw
1072 whose value is either
1073 .I B_READ
1074 or
1075 .I B_WRITE.
1076 .I Physio
1077 makes sure that the user's base address and count are
1078 even (because most devices work in words)
1079 and that the core area affected is contiguous
1080 in physical space;
1081 it delays until the buffer is not busy, and makes it
1082 busy while the operation is in progress;
1083 and it sets up user error return information.