<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">Hi,</p><div class="">My memory disk block driver was compiled successfully.</div><div class="">But when I insmod my module, initialisation stall at add_disk &nbsp;in setup_dev function.</div><div class=""><br class=""></div><div class="">What’s wrong with my gendisk setup code?</div><div class="">Thanks.</div><div class=""><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">static void setup_dev(struct blkplay_dev *dev, int which)</span></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;// setup disk size</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;memset(dev, 0, sizeof(struct blkplay_dev));</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;size = nsectors * hardsect_size;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;data = vmalloc(dev-&gt;size);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;if (dev-&gt;data == NULL) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;printk(KERN_NOTICE "vmalloc failure.\n");</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;// init request queue</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;spin_lock_init(&amp;dev-&gt;lock);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;queue = blk_init_queue(blkplay_request, &amp;dev-&gt;lock);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;if (dev-&gt;queue == NULL) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;printk(KERN_NOTICE "init queue failure.\n");</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;goto out_vfree;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;//blk_queue_logical_block_size(dev-&gt;queue, hardsect_size);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;queue-&gt;queuedata = dev;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;gd = alloc_disk(BLKPLAY_MINORS);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;if (!dev-&gt;gd) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;printk(KERN_NOTICE "alloc_disk failure!\n");</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;goto out_vfree;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;gd-&gt;major = major;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;gd-&gt;first_minor = which * BLKPLAY_MINORS;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;gd-&gt;fops = &amp;blkplay_ops;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;gd-&gt;queue = dev-&gt;queue;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;dev-&gt;gd-&gt;private_data = dev;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;snprintf(dev-&gt;gd-&gt;disk_name, 32, "blkplay%c", which + 'a');</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;set_capacity(dev-&gt;gd, nsectors * (hardsect_size/KERNEL_SECTOR_SIZE));</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">printk(KERN_ALERT "5\n”);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">// *********************************************</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">// initialisation stall at the statement below.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">// *********************************************</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;add_disk(dev-&gt;gd);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">printk(KERN_ALERT "6\n");</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;return;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">out_vfree:</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;if (dev-&gt;data)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;vfree(dev-&gt;data);&nbsp;</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">The whole module code:</p><div class=""><br class=""></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/module.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/init.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/blkdev.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/genhd.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/bio.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/kernel.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/slab.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include &lt;linux/vmalloc.h&gt;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#include "debug.h"</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#define BLKPLAY_MINORS 16</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#define KERNEL_SECTOR_SIZE 512</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">#define DEVICE_NUMBER &nbsp; 1</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">int major = 0;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">int nsectors = 1024;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">int hardsect_size = 512;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static const char *module_name = "blkplay";</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">struct blkplay_dev {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; int size;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; uint8_t *data;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; spinlock_t lock;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; struct request_queue *queue;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; struct gendisk *gd;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">};</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">struct blkplay_dev * devices;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">int blkplay_open(struct block_device *dev, fmode_t mode)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; return 0;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">void blkplay_release(struct gendisk *disk, fmode_t mode)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">/*</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;* Handle an I/O request.</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;*/</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static void blkplay_transfer(struct blkplay_dev *dev, unsigned long sector, unsigned long nsect, char *buffer, int write)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned long offset = sector*KERNEL_SECTOR_SIZE;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>unsigned long nbytes = nsect*KERNEL_SECTOR_SIZE;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if ((offset + nbytes) &gt; dev-&gt;size) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>printk (KERN_NOTICE "Beyond-end write (%ld %ld)\n", offset, nbytes);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if (write)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>memcpy(dev-&gt;data + offset, buffer, nbytes);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>else</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>memcpy(buffer, dev-&gt;data + offset, nbytes);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">/*</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;* Transfer a single BIO.</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp;*/</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static int vmem_disk_xfer_bio(struct blkplay_dev *dev, struct bio *bio)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; struct bio_vec bvec;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; struct bvec_iter iter;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; sector_t sector = bio-&gt;bi_iter.bi_sector;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; bio_for_each_segment(bvec, bio, iter) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; char *buffer = __bio_kmap_atomic(bio, iter);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; blkplay_transfer(dev, sector, bio_cur_bytes(bio) &gt;&gt; 9,</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer, bio_data_dir(bio) == WRITE);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; sector += bio_cur_bytes(bio) &gt;&gt; 9;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; __bio_kunmap_atomic(buffer);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; return 0;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">void blkplay_request(struct request_queue *q)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; struct request *req;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; struct bio *bio;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; while (!blk_queue_stopped(q) &amp;&amp;&nbsp;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (req = blk_peek_request(q)) != NULL) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; struct blkplay_dev *dev = req-&gt;rq_disk-&gt;private_data;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; blk_start_request(req);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; if (req-&gt;cmd_type != REQ_TYPE_FS) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printk (KERN_NOTICE "Skip non-fs request\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blk_end_request_all(req, -EIO);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; __rq_for_each_bio(bio, req)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vmem_disk_xfer_bio(dev, bio);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; blk_end_request_all(req, 0);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">const struct block_device_operations blkplay_ops = {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; //.owner = THIS_MODULE,</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; //.open = blkplay_open,</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; //.release = blkplay_release,</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">};</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static void release_dev(struct blkplay_dev *dev)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; del_gendisk(dev-&gt;gd);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; put_disk(dev-&gt;gd);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; blk_cleanup_queue(dev-&gt;queue);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; vfree(dev-&gt;data);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static void setup_dev(struct blkplay_dev *dev, int which)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; // setup disk size</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; memset(dev, 0, sizeof(struct blkplay_dev));</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;size = nsectors * hardsect_size;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;data = vmalloc(dev-&gt;size);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; if (dev-&gt;data == NULL) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; printk(KERN_NOTICE "vmalloc failure.\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; return;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; // init request queue</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; spin_lock_init(&amp;dev-&gt;lock);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;queue = blk_init_queue(blkplay_request, &amp;dev-&gt;lock);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; if (dev-&gt;queue == NULL) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; printk(KERN_NOTICE "init queue failure.\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; goto out_vfree;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; //blk_queue_logical_block_size(dev-&gt;queue, hardsect_size);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;queue-&gt;queuedata = dev;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;gd = alloc_disk(BLKPLAY_MINORS);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; if (!dev-&gt;gd) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; printk(KERN_NOTICE "alloc_disk failure!\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; goto out_vfree;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;gd-&gt;major = major;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;gd-&gt;first_minor = which * BLKPLAY_MINORS;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;gd-&gt;fops = &amp;blkplay_ops;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;gd-&gt;queue = dev-&gt;queue;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; dev-&gt;gd-&gt;private_data = dev;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; snprintf(dev-&gt;gd-&gt;disk_name, 32, "blkplay%c", which + 'a');</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; set_capacity(dev-&gt;gd, nsectors * (hardsect_size/KERNEL_SECTOR_SIZE));</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">printk(KERN_ALERT "5\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; add_disk(dev-&gt;gd);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">printk(KERN_ALERT "6\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; return;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">out_vfree:</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; if (dev-&gt;data)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; vfree(dev-&gt;data);&nbsp;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static int __init blkplay_init(void)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; int i;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; int ret;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; major = register_blkdev(major, module_name);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; if (0 &gt;= major )</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; goto reg_blkplay_fail;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; } else {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; printk(KERN_ALERT "Allocate major number %d\n", major);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; PDEBUG("Init success!\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; devices = kmalloc(DEVICE_NUMBER * sizeof(struct blkplay_dev), GFP_KERNEL);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; if (devices == NULL) {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; printk(KERN_ALERT "Allocate memory for devices failure.\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; for (i = 0; i &lt; DEVICE_NUMBER; i++)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; printk(KERN_ALERT "Set up device for %d\n", i);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; setup_dev(devices + i, i);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; return 0;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">reg_blkplay_fail:</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; PDEBUG("Init Error!\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; return ret;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">static void __exit blkplay_exit(void)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">{</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; int i;</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; for (i = 0; i &lt; DEVICE_NUMBER; i++)</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; {</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; release_dev(&amp;devices[i]);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; unregister_blkdev(major, module_name);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; kfree(devices);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; PDEBUG("Exit.\n");</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">module_init(blkplay_init);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">module_exit(blkplay_exit);</p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></p>
<p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">MODULE_LICENSE("Dual GPL/BSD");</p></body></html>