int dm_io_async_bvec(unsigned int num_regions, #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
struct dm_io_region *where, #else
struct io_region *where, #endif
int rw, struct bio_vec *bvec, io_notify_fn fn, void *context) { struct dm_io_request iorq; iorq.bi_rw = rw; iorq.mem.type = DM_IO_BVEC; iorq.mem.ptr.bvec = bvec; iorq.notify.fn = fn; iorq.notify.context = context; iorq.client = flashcache_io_client; return dm_io(&iorq, num_regions, where, NULL); } #endif
int flashcache_dm_io_async_vm(struct cache_c *dmc, unsigned int num_regions, #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
struct io_region *where, #else
struct dm_io_region *where, #endif
int rw, void *data, io_notify_fn fn, void *context) { unsigned long error_bits = 0; int error; struct dm_io_request io_req = { .bi_rw = rw, .mem.type = DM_IO_VMA, .mem.ptr.vma = data, .mem.offset = 0, .notify.fn = fn, .notify.context = context, .client = flashcache_io_client, }; error = dm_io(&io_req, 1, where, &error_bits); if (error) return error; if (error_bits) return error_bits; return 0; } #endif
上面兩個函數都使用struct dm_io_request 來包裝了請求,其中的只有兩種請求的類型是不同的,第一個函數對應的是DM_IO_BVEC,第二個函數是DM_IO_VMA。linux
其實我開始一直不明白,爲何要使用這兩個函數讓硬盤與內存打交道,不事後來看了dm_io發現其中的io服務類型有多種不一樣類型,這兩個函數的調用分別對應不一樣的io類型。下面先看一下dm_io相關的數據結構。ios
dm_ioc#
dm-io爲device mapper提供同步或者異步的io服務。數組
使用dm-io必須設置dm_io_region結構(2.6.26版本之前叫io_region),該結構定義了io操做的區域,讀通常針對一個dm_io_region區,而寫能夠針對一組dm_io_region區。數據結構
struct dm_io_region { struct block_device *bdev; sector_t sector; sector_t count; /* If this is zero the region is ignored. */ };
struct io_region { struct block_device *bdev; sector_t sector; sector_t count; };
dm-io一共有四種dm_io_mem_type類型(老一點的內核版本只有前面三種,Flashcache主要使用DM_IO_BVEC):app
enum dm_io_mem_type { DM_IO_PAGE_LIST,/* Page list */ DM_IO_BVEC, /* Bio vector */ DM_IO_VMA, /* Virtual memory area */ DM_IO_KMEM, /* Kernel memory */ }; struct dm_io_memory { enum dm_io_mem_type type; union { struct page_list *pl; struct bio_vec *bvec; void *vma; void *addr; } ptr; unsigned offset; };
struct page_list { struct page_list *next; struct page *page; }; int dm_io_sync(unsigned int num_regions, struct io_region *where, int rw, struct page_list *pl, unsigned int offset, unsigned long *error_bits); int dm_io_async(unsigned int num_regions, struct io_region *where, int rw, struct page_list *pl, unsigned int offset, io_notify_fn fn, void *context);
int dm_io_sync_bvec(unsigned int num_regions, struct io_region *where, int rw, struct bio_vec *bvec, unsigned long *error_bits); int dm_io_async_bvec(unsigned int num_regions, struct io_region *where, int rw, struct bio_vec *bvec, io_notify_fn fn, void *context);
int dm_io_sync_vm(unsigned int num_regions, struct io_region *where, int rw, void *data, unsigned long *error_bits); int dm_io_async_vm(unsigned int num_regions, struct io_region *where, int rw, void *data, io_notify_fn fn, void *context);
dm-io經過dm_io_request結構來封裝請求的類型,若是設置了dm_io_notify.fn則是異步IO,不然是同步IO。異步
struct dm_io_request { int bi_rw; /* READ|WRITE - not READA */
struct dm_io_memory mem; /* Memory to use for io */
struct dm_io_notify notify; /* Synchronous if notify.fn is NULL */
struct dm_io_client *client; /* Client memory handler */ };
使用dm_io服務前前須要經過dm_io_client_create函數(在2.6.22版本前是dm_io_get)先建立dm_io_client結構,爲dm-io的執行過程當中分配內存池。使用dm-io服務完畢後,則須要調用dm_io_client_destroy函數(在2.6.22版本前是dm_io_put)釋放內存池。async
struct dm_io_client { mempool_t *pool; struct bio_set *bios; };
dm-io函數執行具體的io請求。函數
int dm_io(struct dm_io_request *io_req, unsigned num_regions, struct dm_io_region *where, unsigned long *sync_error_bits) { int r; struct dpages dp; r = dp_init(io_req, &dp); if (r) return r; if (!io_req->notify.fn) return sync_io(io_req->client, num_regions, where, io_req->bi_rw, &dp, sync_error_bits); return async_io(io_req->client, num_regions, where, io_req->bi_rw, &dp, io_req->notify.fn, io_req->notify.context); }
對於第二種狀況,磁盤跟磁盤以前的交互。這種狀況只用於將ssd中髒塊寫入disk中。this
int dm_kcopyd_copy(struct dm_kcopyd_client *kc, struct dm_io_region *from, unsigned int num_dests, struct dm_io_region *dests, unsigned int flags, dm_kcopyd_notify_fn fn, void *context)
第一個參數dm_kcopyd_client,在使用kcopyd異步拷貝服務時,必須先建立一個對應的client,首先要分配「kcopyd客戶端」結構,調用函數以下:
kcopyd_client_create(FLASHCACHE_COPY_PAGES, &flashcache_kcp_client);
建立dm_kcopyd_client結構。