Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
.\"	$NetBSD: bufferio.9,v 1.17 2017/03/18 19:01:01 riastradh Exp $
.\"
.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Taylor R. Campbell.
.\"
.\" 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``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 FOUNDATION 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.
.\"
.Dd March 29, 2015
.Dt BUFFERIO 9
.Os
.Sh NAME
.Nm BUFFERIO ,
.Nm biodone ,
.Nm biowait ,
.Nm getiobuf ,
.Nm putiobuf ,
.Nm nestiobuf_setup ,
.Nm nestiobuf_done
.Nd block I/O buffer transfers
.Sh SYNOPSIS
.In sys/buf.h
.Ft void
.Fn biodone "struct buf *bp"
.Ft int
.Fn biowait "struct buf *bp"
.Ft struct buf *
.Fn getiobuf "struct vnode *vp" "bool waitok"
.Ft void
.Fn putiobuf "struct buf *bp"
.Ft void
.Fn nestiobuf_setup "struct buf *mbp" "struct buf *bp" "int offset" \
        "size_t size"
.Ft void
.Fn nestiobuf_done "struct buf *mbp" "int donebytes" "int error"
.Sh DESCRIPTION
The
.Nm
subsystem manages block I/O buffer transfers, described by the
.Vt "struct buf"
structure, which serves multiple purposes between users in
.Nm ,
users in
.Xr buffercache 9 ,
and users in block device drivers to execute transfers to physical
disks.
.Sh BLOCK DEVICE USERS
Users of
.Nm
wishing to submit a buffer for block I/O transfer must obtain a
.Vt "struct buf" ,
e.g. via
.Fn getiobuf ,
fill its parameters, and submit it to a block device with
.Xr bdev_strategy 9 ,
usually via
.Xr VOP_STRATEGY 9 .
.Pp
The parameters to an I/O transfer described by
.Fa bp
are specified by the following
.Vt "struct buf"
fields:
.Bl -tag -offset abcd
.It Fa bp Ns Li "->b_flags"
Flags specifying the type of transfer.
.Bl -tag -compact
.It Dv B_READ
Transfer is read from device.
If not set, transfer is write to device.
.It Dv B_ASYNC
Asynchronous I/O.
Caller must not provide
.Fa bp Ns Li "->b_iodone"
and must not call
.Fn biowait bp .
.El
For legibility, callers should indicate writes by passing the
pseudo-flag
.Dv B_WRITE ,
which is zero.
.It Fa bp Ns Li "->b_data"
Pointer to kernel virtual address of source/target for transfer.
.It Fa bp Ns Li "->b_bcount"
Nonnegative number of bytes requested for transfer.
.It Fa bp Ns Li "->b_blkno"
Block number at which to do transfer.
.It Fa bp Ns Li "->b_iodone"
I/O completion callback.
.Dv B_ASYNC
must not be set in
.Fa bp Ns Li "->b_flags" .
.El
.Pp
Additionally, if the I/O transfer is a write associated with a
.Xr vnode 9
.Fa vp ,
then before the user submits it to a block device, the user must
increment
.Fa vp Ns Li "->v_numoutput" .
The user must not acquire
.Fa vp Ns Ap s
vnode lock between incrementing
.Fa vp Ns Li "->v_numoutput"
and submitting
.Fa bp
to a block device \(em doing so will likely cause deadlock with the
syncer.
.Pp
Block I/O transfer completion may be notified by the
.Fa bp Ns Li "->b_iodone"
callback, by signalling
.Fn biowait
waiters, or not at all in the
.Dv B_ASYNC
case.
.Bl -dash
.It
If the user sets the
.Fa bp Ns Li "->b_iodone"
callback to a
.Pf non- Dv NULL
function pointer, it will be called in soft interrupt context when the
I/O transfer is complete.
The user
.Em may not
call
.Fn biowait bp
in this case.
.It
If
.Dv B_ASYNC
is set, then the I/O transfer is asynchronous and the user will not be
notified when it is completed.
The user
.Em may not
call
.Fn biowait bp
in this case.
.It
Otherwise, if
.Fa bp Ns Li "->b_iodone"
is
.Dv NULL
and
.Dv B_ASYNC
is not specified, the user may wait for the I/O transfer to complete
with
.Fn biowait bp .
.El
.Pp
Once an I/O transfer has completed, its
.Vt "struct buf"
may be reused, but the user must first clear the
.Dv BO_DONE
flag of
.Fa bp Ns Li "->b_oflags"
before reusing it.
.Sh NESTED I/O TRANSFERS
Sometimes an I/O transfer from a single buffer in memory cannot go to a
single location on a block device: it must be split up into smaller
transfers for each segment of the memory buffer.
.Pp
After initializing the
.Li b_flags ,
.Li b_data ,
and
.Li b_bcount
parameters of an I/O transfer for the buffer, called the
.Em master
buffer, the user can issue smaller transfers for segments of the buffer
using
.Fn nestiobuf_setup .
When nested I/O transfers complete, in any order, they debit from the
amount of work left to be done in the master buffer.
If any segments of the buffer were skipped, the user can report this
with
.Fn nestiobuf_done
to debit the skipped part of the work.
.Pp
The master buffer's I/O transfer is completed when all nested buffers'
I/O transfers are completed, and if
.Fn nestiobuf_done
is called in the case of skipped segments.
.Pp
For writes associated with a vnode
.Fa vp ,
.Fn nestiobuf_setup
accounts for
.Fa vp Ns Li "->v_numoutput" ,
so the caller is not allowed to acquire
.Fa vp Ns Ap s
vnode lock before submitting the nested I/O transfer to a block
device.
However, the caller is responsible for accounting the master buffer in
.Fa vp Ns Li "->v_numoutput" .
This must be done very carefully because after incrementing
.Fa vp Ns Li "->v_numoutput" ,
the caller is not allowed to acquire
.Fa vp Ns Ap s
vnode lock before either calling
.Fn nestiobuf_done
or submitting the last nested I/O transfer to a block device.
.Pp
For example:
.Bd -literal -offset abcd
struct buf *mbp, *bp;
size_t skipped = 0;
unsigned i;
int error = 0;

mbp = getiobuf(vp, true);
mbp->b_data = data;
mbp->b_resid = mbp->b_bcount = datalen;
mbp->b_flags = B_WRITE;

KASSERT(0 < nsegs);
KASSERT(datalen == nsegs*segsz);
for (i = 0; i < nsegs; i++) {
	struct vnode *devvp;
	daddr_t blkno;

	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
	error = VOP_BMAP(vp, i*segsz, &devvp, &blkno, NULL);
	VOP_UNLOCK(vp);
	if (error == 0 && blkno == -1)
		error = EIO;
	if (error) {
		/* Give up early, don't try to handle holes.  */
		skipped += datalen - i*segsz;
		break;
	}

	bp = getiobuf(vp, true);
	nestiobuf_setup(bp, mbp, i*segsz, segsz);
	bp->b_blkno = blkno;
	if (i == nsegs - 1)	/* Last segment.  */
		break;
	VOP_STRATEGY(devvp, bp);
}

/*
 * Account v_numoutput for master write.
 * (Must not vn_lock before last VOP_STRATEGY!)
 */
mutex_enter(&vp->v_interlock);
vp->v_numoutput++;
mutex_exit(&vp->v_interlock);

if (skipped)
	nestiobuf_done(mbp, skipped, error);
else
	VOP_STRATEGY(devvp, bp);
.Ed
.Sh BLOCK DEVICE DRIVERS
Block device drivers implement a
.Sq strategy
method, in the
.Li d_strategy
member of
.Li struct bdevsw
.Pq Xr driver 9 ,
to queue a buffer for disk I/O.
The inputs to the strategy method are:
.Bl -tag -offset abcd
.It Fa bp Ns Li "->b_flags"
Flags specifying the type of transfer.
.Bl -tag -compact
.It Dv B_READ
Transfer is read from device.
If not set, transfer is write to device.
.El
.It Fa bp Ns Li "->b_data"
Pointer to kernel virtual address of source/target for transfer.
.It Fa bp Ns Li "->b_bcount"
Nonnegative number of bytes requested for transfer.
.It Fa bp Ns Li "->b_blkno"
Block number at which to do transfer, relative to partition start.
.El
.Pp
If the strategy method uses
.Xr bufq 9 ,
it must additionally initialize the following fields before queueing
.Fa bp
with
.Xr bufq_put 9 :
.Bl -tag -offset abcd
.It Fa bp Ns Li "->b_rawblkno"
Block number relative to volume start.
.El
.Pp
When the I/O transfer is complete, whether it succeeded or failed, the
strategy method must:
.Bl -dash
.It
Set
.Fa bp Ns Li "->b_error"
to zero on success, or to an
.Xr errno 2
error code on failure.
.It
Set
.Fa bp Ns Li "->b_resid"
to the number of bytes remaining to transfer, whether on success or
on failure.
If no bytes were transferred, this must be set to
.Fa bp Ns Li "->b_bcount" .
.It
Call
.Fn biodone bp .
.El
.Sh FUNCTIONS
.Bl -tag -width abcd
.It Fn biodone bp
Notify that the I/O transfer described by
.Fa bp
has completed.
.Pp
To be called by a block device driver.
Caller must first set
.Fa bp Ns Li "->b_error"
to an error code and
.Fa bp Ns Li "->b_resid"
to the number of bytes remaining to transfer.
.It Fn biowait bp
Wait for the synchronous I/O transfer described by
.Fa bp
to complete.
Returns the value of
.Fa bp Ns Li "->b_error" .
.Pp
To be called by a user requesting the I/O transfer.
.Pp
May not be called if
.Fa bp
has a callback or is asynchronous \(em that is, if
.Fa bp Ns Li "->b_iodone"
is set, or if
.Dv B_ASYNC
is set in
.Fa bp Ns Li "->b_flags" .
.It Fn getiobuf vp waitok
Allocate a
.Vt "struct buf"
for an I/O transfer.
If
.Fa vp
is
.Pf non- Dv NULL ,
the transfer is associated with it.
If
.Fa waitok
is false,
returns
.Dv NULL
if none can be allocated immediately.
.Pp
The resulting
.Vt "struct buf"
pointer must eventually be passed to
.Fn putiobuf
to release it.
Do
.Em not
use
.Xr brelse 9 .
.Pp
The buffer may not be used for an asynchronous I/O transfer, because
there is no way to know when it is completed and may be safely passed
to
.Fn putiobuf .
Asynchronous I/O transfers are allowed only for buffers in the
.Xr buffercache 9 .
.Pp
May sleep if
.Fa waitok
is true.
.It Fn putiobuf bp
Free
.Fa bp ,
which must have been allocated by
.Fn getiobuf .
Either
.Fa bp
must never have been submitted to a block device, or the I/O transfer
must have completed.
.El
.Sh CODE REFERENCES
The
.Nm
subsystem is implemented in
.Pa sys/kern/vfs_bio.c .
.Sh SEE ALSO
.Xr buffercache 9 ,
.Xr bufq 9
.Sh BUGS
The
.Nm
abstraction provides no way to cancel an I/O transfer once it has been
submitted to a block device.
.Pp
The
.Nm
abstraction provides no way to do I/O transfers with non-kernel pages,
e.g. directly to buffers in userland without copying into the kernel
first.
.Pp
The
.Vt "struct buf"
type is all mixed up with the
.Xr buffercache 9 .
.Pp
The
.Nm
abstraction is a totally idiotic API design.
.Pp
The
.Li v_numoutput
accounting required of
.Nm
callers is asinine.