/************************************************************************
Reads or writes data. This operation is asynchronous (aio). */
ulint
fil_io(
/*===*/
/* out: DB_SUCCESS, or DB_TABLESPACE_DELETED
if we are trying to do i/o on a tablespace
which does not exist */
ulint type, /* in: OS_FILE_READ or OS_FILE_WRITE,
ORed to OS_FILE_LOG, if a log i/o
and ORed to OS_AIO_SIMULATED_WAKE_LATER
if simulated aio and we want to post a
batch of i/os; NOTE that a simulated batch
may introduce hidden chances of deadlocks,
because i/os are not actually handled until
all have been posted: use with great
caution! */
ibool sync, /* in: TRUE if synchronous aio is desired */
ulint space_id, /* in: space id */
ulint block_offset, /* in: offset in number of blocks */
ulint byte_offset, /* in: remainder of offset in bytes; in
aio this must be divisible by the OS block
size */
ulint len, /* in: how many bytes to read or write; this
must not cross a file boundary; in aio this
must be a block size multiple */
void* buf, /* in/out: buffer where to store read data
or from where to write; in aio this must be
appropriately aligned */
void* message) /* in: message for aio handler if non-sync
aio used, else ignored */
{
//1.找到对应的表空间结构
HASH_SEARCH(hash, system->spaces, space_id, space,
space->id == space_id);
……
//2.取得第一个文件结点
node = UT_LIST_GET_FIRST(space->chain);
for (;;) {
……
//文件的大小根据my.ini的配置而定
//第一个文件ibdata1是10M,因此对应的node->size为640
//第二个文件ibdata2是10M,因此对应的node->size为640
//3.假设我们查找的文件号为0-639,则对应为第一个文件。
if (node->size > block_offset) {
/* Found! */
break;
} else {
//4.假设我们查找的文件号>640,则查看是否在第二个文件中。
//假设是640,则在第二个文件的偏移量为0*16k字节处开始的一页,也就是文件开始处,也可以勉强称为第二个文件的第0页,实际上是640页。
//假设是641,则在第二个文件的偏移量为(641-640)*16k字节处开始的一页,也可以勉强称为第二个文件的第1页,实际上是641页。
block_offset -= node->size;
node = UT_LIST_GET_NEXT(chain, node);
}
}
……
//5.计算偏移量,见前面代码中的block_offset
offset_high = (block_offset >> (32 - UNIV_PAGE_SIZE_SHIFT));
offset_low = ((block_offset << UNIV_PAGE_SIZE_SHIFT) & 0xFFFFFFFFUL)
+ byte_offset;
……
//6.进行aio操作,offset_low指相对于文件头的字节偏移,len指长度,即获得长度,通常为16k
ret = os_aio(type, mode | wake_later, node->name, node->handle, buf,
offset_low, offset_high, len, node, message);
……
return(DB_SUCCESS);
}
|