Specifications

This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
Performing Direct I/O
|
439
needs to know about the operation, and return -EIOCBQUEUED to the caller. Remem-
bering the operation information includes arranging access to the user-space buffer;
once you return, you will not again have the opportunity to access that buffer while
running in the context of the calling process. In general, that means you will likely
have to set up a direct kernel mapping (with get_user_pages) or a DMA mapping.
The
-EIOCBQUEUED error code indicates that the operation is not yet complete, and its
final status will be posted later.
When “later” comes, your driver must inform the kernel that the operation has com-
pleted. That is done with a call to aio_complete:
int aio_complete(struct kiocb *iocb, long res, long res2);
Here, iocb is the same IOCB that was initially passed to you, and res is the usual
result status for the operation.
res2 is a second result code that will be returned to
user space; most asynchronous I/O implementations pass res2 as 0. Once you call
aio_complete, you should not touch the IOCB or user buffer again.
An asynchronous I/O example
The page-oriented scullp driver in the example source implements asynchronous I/O.
The implementation is simple, but it is enough to show how asynchronous opera-
tions should be structured.
The aio_read and aio_write methods don’t actually do much:
static ssize_t scullp_aio_read(struct kiocb *iocb, char *buf, size_t count,
loff_t pos)
{
return scullp_defer_op(0, iocb, buf, count, pos);
}
static ssize_t scullp_aio_write(struct kiocb *iocb, const char *buf,
size_t count, loff_t pos)
{
return scullp_defer_op(1, iocb, (char *) buf, count, pos);
}
These methods simply call a common function:
struct async_work {
struct kiocb *iocb;
int result;
struct work_struct work;
};
static int scullp_defer_op(int write, struct kiocb *iocb, char *buf,
size_t count, loff_t pos)
{
struct async_work *stuff;
int result;
,ch15.13676 Page 439 Friday, January 21, 2005 11:04 AM