* they are filled in to point
* at the desired IO operation.
* The following several routines allocate and free
* buffers with various side effects. In general the
* arguments to an allocate routine are a device and
* a block number, and the value is a pointer to
* to the buffer header; the buffer is marked "busy"
* so that no one else can touch it. If the block was
* already in core, no I/O need be done; if it is
* already busy, the process waits until it becomes free.
* The following routines allocate a buffer:
* Eventually the buffer must be released, possibly with the
* side effect of writing it out, by using one of
* Read in (if necessary) the block and return a buffer pointer.
if (bp
->b_flags
&B_DONE
) {
(*bdevsw
[major(dev
)].d_strategy
)(bp
);
* Read in the block, like bread, but also start I/O on the
* read-ahead block (which is not allocated to the caller)
breada(dev
, blkno
, rablkno
)
register struct buf
*bp
, *rabp
;
if (!incore(dev
, blkno
)) {
if ((bp
->b_flags
&B_DONE
) == 0) {
(*bdevsw
[major(dev
)].d_strategy
)(bp
);
if (rablkno
&& !incore(dev
, rablkno
)) {
rabp
= getblk(dev
, rablkno
);
if (rabp
->b_flags
& B_DONE
)
rabp
->b_flags
|= B_READ
|B_ASYNC
;
(*bdevsw
[major(dev
)].d_strategy
)(rabp
);
return(bread(dev
, blkno
));
* Write the buffer, waiting for completion.
* Then release the buffer.
bp
->b_flags
&= ~(B_READ
| B_DONE
| B_ERROR
| B_DELWRI
| B_AGE
);
(*bdevsw
[major(bp
->b_dev
)].d_strategy
)(bp
);
if ((flag
&B_ASYNC
) == 0) {
} else if (flag
& B_DELWRI
)
* Release the buffer, marking it so that if it is grabbed
* for another purpose it will be written out before being
* given up (e.g. when writing a partial block where it is
* assumed that another write for the same block will soon follow).
* This can't be done for magtape, since writes must be done
* in the same order as requested.
dp
= bdevsw
[major(bp
->b_dev
)].d_tab
;
bp
->b_flags
|= B_DELWRI
| B_DONE
;
* Release the buffer, start I/O on it, but don't wait for completion.
* release the buffer, with no I/O implied.
register struct buf
**backp
;
if (bp
->b_flags
&B_WANTED
)
if (bfreelist
.b_flags
&B_WANTED
) {
bfreelist
.b_flags
&= ~B_WANTED
;
wakeup((caddr_t
)&bfreelist
);
bp
->b_dev
= NODEV
; /* no assoc. on error */
if(bp
->b_flags
& B_AGE
) {
backp
= &bfreelist
.av_forw
;
bp
->av_back
= &bfreelist
;
backp
= &bfreelist
.av_back
;
bp
->av_forw
= &bfreelist
;
bp
->b_flags
&= ~(B_WANTED
|B_BUSY
|B_ASYNC
|B_AGE
);
* See if the block is associated with some buffer
* (mainly to avoid getting hung up on a wait in breada)
dp
= bdevsw
[major(dev
)].d_tab
;
for (bp
=dp
->b_forw
; bp
!= dp
; bp
= bp
->b_forw
)
if (bp
->b_blkno
==blkno
&& bp
->b_dev
==dev
)
* Assign a buffer for the given block. If the appropriate
* block is already associated, return it; otherwise search
* for the oldest non-busy buffer and reassign it.
if(major(dev
) >= nblkdev
)
dp
= bdevsw
[major(dev
)].d_tab
;
for (bp
=dp
->b_forw
; bp
!= dp
; bp
= bp
->b_forw
) {
if (bp
->b_blkno
!=blkno
|| bp
->b_dev
!=dev
)
if (bp
->b_flags
&B_BUSY
) {
sleep((caddr_t
)bp
, PRIBIO
+1);
while (dp
!= &bfreelist
) {
if (bfreelist
.av_forw
== &bfreelist
) {
bfreelist
.b_flags
|= B_WANTED
;
sleep((caddr_t
)&bfreelist
, PRIBIO
+1);
notavail(bp
= bfreelist
.av_forw
);
if (bp
->b_flags
& B_DELWRI
) {
bp
->b_back
->b_forw
= bp
->b_forw
;
bp
->b_forw
->b_back
= bp
->b_back
;
* not assigned to any particular device
while (bfreelist
.av_forw
== &bfreelist
) {
bfreelist
.b_flags
|= B_WANTED
;
sleep((caddr_t
)&bfreelist
, PRIBIO
+1);
notavail(bp
= bfreelist
.av_forw
);
if (bp
->b_flags
& B_DELWRI
) {
bp
->b_back
->b_forw
= bp
->b_forw
;
bp
->b_forw
->b_back
= bp
->b_back
;
bp
->b_dev
= (dev_t
)NODEV
;
* Wait for I/O completion on the buffer; return errors
while ((bp
->b_flags
&B_DONE
)==0)
sleep((caddr_t
)bp
, PRIBIO
);
* Unlink a buffer from the available list and mark it busy.
bp
->av_back
->av_forw
= bp
->av_forw
;
bp
->av_forw
->av_back
= bp
->av_back
;
* Mark I/O complete on a buffer, release it if I/O is asynchronous,
* and wake up anyone waiting for it.
bp
->b_flags
&= ~B_WANTED
;
* Zero the core associated with a buffer.
swap(blkno
, coreaddr
, count
, rdflg
)
if((swbuf2
.b_flags
&B_WANTED
) == 0)
while (bp
->b_flags
&B_BUSY
) {
sleep((caddr_t
)bp
, PSWP
+1);
bp
->b_flags
= B_BUSY
| B_PHYS
| rdflg
;
if (tcount
>= 01700) /* prevent byte-count wrap */
bp
->b_bcount
= ctob(tcount
);
bp
->b_blkno
= swplo
+blkno
;
bp
->b_un
.b_addr
= (caddr_t
)(coreaddr
<<6);
bp
->b_xmem
= (coreaddr
>>10) & 077;
(*bdevsw
[major(swapdev
)].d_strategy
)(bp
);
while((bp
->b_flags
&B_DONE
)==0)
sleep((caddr_t
)bp
, PSWP
);
if (bp
->b_flags
&B_WANTED
)
bp
->b_flags
&= ~(B_BUSY
|B_WANTED
);
if (bp
->b_flags
& B_ERROR
)
* make sure all write-behind blocks
* on dev (or NODEV for all)
* (from umount and update)
for (bp
= bfreelist
.av_forw
; bp
!= &bfreelist
; bp
= bp
->av_forw
) {
if (bp
->b_flags
&B_DELWRI
&& (dev
== NODEV
||dev
==bp
->b_dev
)) {
* Raw I/O. The arguments are
* The strategy routine for the device
* A buffer, which will always be a special buffer
* header owned exclusively by the device for this purpose
* Essentially all the work is computing physical addresses and
physio(strat
, bp
, dev
, rw
)
base
= (unsigned)u
.u_base
;
* Check odd base, odd count, and address wraparound
if (base
&01 || u
.u_count
&01 || base
>=base
+u
.u_count
)
ts
= (u
.u_tsize
+127) & ~0177;
* Check overlap with text. (ts and nb now
* Check that transfer is either entirely in the
* data or in the stack: that is, either
* the end is in the data or the start is in the stack
* (remember wraparound was already checked).
if ((((base
+u
.u_count
)>>6)&01777) >= ts
+u
.u_dsize
while (bp
->b_flags
&B_BUSY
) {
sleep((caddr_t
)bp
, PRIBIO
+1);
bp
->b_flags
= B_BUSY
| B_PHYS
| rw
;
* Compute physical address by simulating
* the segmentation hardware.
ts
= (u
.u_sep
? UDSA
: UISA
)->r
[nb
>>7] + (nb
&0177);
bp
->b_un
.b_addr
= (caddr_t
)((ts
<<6) + (base
&077));
bp
->b_xmem
= (ts
>>10) & 077;
bp
->b_blkno
= u
.u_offset
>> BSHIFT
;
bp
->b_bcount
= u
.u_count
;
u
.u_procp
->p_flag
|= SLOCK
;
while ((bp
->b_flags
&B_DONE
) == 0)
sleep((caddr_t
)bp
, PRIBIO
);
u
.u_procp
->p_flag
&= ~SLOCK
;
if (bp
->b_flags
&B_WANTED
)
bp
->b_flags
&= ~(B_BUSY
|B_WANTED
);
* Pick up the device's error number and pass it to the user;
* if there is an error but the number is 0 set a generalized
* code. Actually the latter is always true because devices
* don't yet return specific errors.
if ((u
.u_error
= bp
->b_error
)==0)