Ed L. Cashin | 52e112b | 2008-02-08 04:20:09 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * aoeblk.c |
| 4 | * block device routines |
| 5 | */ |
| 6 | |
Andrew Morton | 027b180 | 2010-10-28 06:15:26 -0600 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/hdreg.h> |
| 9 | #include <linux/blkdev.h> |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 10 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/fs.h> |
| 12 | #include <linux/ioctl.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Andrew Morton | 027b180 | 2010-10-28 06:15:26 -0600 | [diff] [blame] | 14 | #include <linux/ratelimit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/genhd.h> |
| 16 | #include <linux/netdevice.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Paul Gortmaker | d5decd3 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "aoe.h" |
| 20 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 21 | static DEFINE_MUTEX(aoeblk_mutex); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 22 | static struct kmem_cache *buf_pool_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 24 | static ssize_t aoedisk_show_state(struct device *dev, |
| 25 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 27 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct aoedev *d = disk->private_data; |
| 29 | |
| 30 | return snprintf(page, PAGE_SIZE, |
| 31 | "%s%s\n", |
| 32 | (d->flags & DEVFL_UP) ? "up" : "down", |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 33 | (d->flags & DEVFL_KICKME) ? ",kickme" : |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 34 | (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); |
| 35 | /* I'd rather see nopen exported so we can ditch closewait */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 37 | static ssize_t aoedisk_show_mac(struct device *dev, |
| 38 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 40 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct aoedev *d = disk->private_data; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 42 | struct aoetgt *t = d->targets[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 44 | if (t == NULL) |
| 45 | return snprintf(page, PAGE_SIZE, "none\n"); |
Harvey Harrison | 411c41e | 2008-11-25 00:40:37 -0800 | [diff] [blame] | 46 | return snprintf(page, PAGE_SIZE, "%pm\n", t->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 48 | static ssize_t aoedisk_show_netif(struct device *dev, |
| 49 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 51 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct aoedev *d = disk->private_data; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 53 | struct net_device *nds[8], **nd, **nnd, **ne; |
| 54 | struct aoetgt **t, **te; |
| 55 | struct aoeif *ifp, *e; |
| 56 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 58 | memset(nds, 0, sizeof nds); |
| 59 | nd = nds; |
| 60 | ne = nd + ARRAY_SIZE(nds); |
| 61 | t = d->targets; |
| 62 | te = t + NTARGETS; |
| 63 | for (; t < te && *t; t++) { |
| 64 | ifp = (*t)->ifs; |
| 65 | e = ifp + NAOEIFS; |
| 66 | for (; ifp < e && ifp->nd; ifp++) { |
| 67 | for (nnd = nds; nnd < nd; nnd++) |
| 68 | if (*nnd == ifp->nd) |
| 69 | break; |
| 70 | if (nnd == nd && nd != ne) |
| 71 | *nd++ = ifp->nd; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | ne = nd; |
| 76 | nd = nds; |
| 77 | if (*nd == NULL) |
| 78 | return snprintf(page, PAGE_SIZE, "none\n"); |
| 79 | for (p = page; nd < ne; nd++) |
| 80 | p += snprintf(p, PAGE_SIZE - (p-page), "%s%s", |
| 81 | p == page ? "" : ",", (*nd)->name); |
| 82 | p += snprintf(p, PAGE_SIZE - (p-page), "\n"); |
| 83 | return p-page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 85 | /* firmware version */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 86 | static ssize_t aoedisk_show_fwver(struct device *dev, |
| 87 | struct device_attribute *attr, char *page) |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 88 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 89 | struct gendisk *disk = dev_to_disk(dev); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 90 | struct aoedev *d = disk->private_data; |
| 91 | |
| 92 | return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); |
| 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 95 | static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL); |
| 96 | static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL); |
| 97 | static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL); |
| 98 | static struct device_attribute dev_attr_firmware_version = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 99 | .attr = { .name = "firmware-version", .mode = S_IRUGO }, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 100 | .show = aoedisk_show_fwver, |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 101 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Greg Kroah-Hartman | 4ca5224 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 103 | static struct attribute *aoe_attrs[] = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 104 | &dev_attr_state.attr, |
| 105 | &dev_attr_mac.attr, |
| 106 | &dev_attr_netif.attr, |
| 107 | &dev_attr_firmware_version.attr, |
| 108 | NULL, |
Greg Kroah-Hartman | 4ca5224 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static const struct attribute_group attr_group = { |
| 112 | .attrs = aoe_attrs, |
| 113 | }; |
| 114 | |
| 115 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | aoedisk_add_sysfs(struct aoedev *d) |
| 117 | { |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 118 | return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | void |
| 121 | aoedisk_rm_sysfs(struct aoedev *d) |
| 122 | { |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 123 | sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 127 | aoeblk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 129 | struct aoedev *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | ulong flags; |
| 131 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 132 | mutex_lock(&aoeblk_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | spin_lock_irqsave(&d->lock, flags); |
| 134 | if (d->flags & DEVFL_UP) { |
| 135 | d->nopen++; |
| 136 | spin_unlock_irqrestore(&d->lock, flags); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 137 | mutex_unlock(&aoeblk_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | spin_unlock_irqrestore(&d->lock, flags); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 141 | mutex_unlock(&aoeblk_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return -ENODEV; |
| 143 | } |
| 144 | |
| 145 | static int |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 146 | aoeblk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 148 | struct aoedev *d = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | ulong flags; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | spin_lock_irqsave(&d->lock, flags); |
| 152 | |
Ed L. Cashin | 5f7702f | 2006-01-19 13:46:27 -0500 | [diff] [blame] | 153 | if (--d->nopen == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | spin_unlock_irqrestore(&d->lock, flags); |
| 155 | aoecmd_cfg(d->aoemajor, d->aoeminor); |
| 156 | return 0; |
| 157 | } |
| 158 | spin_unlock_irqrestore(&d->lock, flags); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 163 | static void |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 164 | aoeblk_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
David S. Miller | e9bb8fb | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 166 | struct sk_buff_head queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | struct aoedev *d; |
| 168 | struct buf *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | ulong flags; |
| 170 | |
| 171 | blk_queue_bounce(q, &bio); |
| 172 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 173 | if (bio == NULL) { |
| 174 | printk(KERN_ERR "aoe: bio is NULL\n"); |
| 175 | BUG(); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 176 | return; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | d = bio->bi_bdev->bd_disk->private_data; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 179 | if (d == NULL) { |
| 180 | printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n"); |
| 181 | BUG(); |
| 182 | bio_endio(bio, -ENXIO); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 183 | return; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 184 | } else if (bio->bi_io_vec == NULL) { |
| 185 | printk(KERN_ERR "aoe: bi_io_vec is NULL\n"); |
| 186 | BUG(); |
| 187 | bio_endio(bio, -ENXIO); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 188 | return; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | buf = mempool_alloc(d->bufpool, GFP_NOIO); |
| 191 | if (buf == NULL) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 192 | printk(KERN_INFO "aoe: buf allocation failure\n"); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 193 | bio_endio(bio, -ENOMEM); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 194 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | memset(buf, 0, sizeof(*buf)); |
| 197 | INIT_LIST_HEAD(&buf->bufs); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 198 | buf->stime = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | buf->bio = bio; |
| 200 | buf->resid = bio->bi_size; |
| 201 | buf->sector = bio->bi_sector; |
Ed L. Cashin | 392e484 | 2006-09-20 14:36:50 -0400 | [diff] [blame] | 202 | buf->bv = &bio->bi_io_vec[bio->bi_idx]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | buf->bv_resid = buf->bv->bv_len; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 204 | WARN_ON(buf->bv_resid == 0); |
| 205 | buf->bv_off = buf->bv->bv_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | spin_lock_irqsave(&d->lock, flags); |
| 208 | |
| 209 | if ((d->flags & DEVFL_UP) == 0) { |
Andrew Morton | 027b180 | 2010-10-28 06:15:26 -0600 | [diff] [blame] | 210 | pr_info_ratelimited("aoe: device %ld.%d is not up\n", |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 211 | d->aoemajor, d->aoeminor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | spin_unlock_irqrestore(&d->lock, flags); |
| 213 | mempool_free(buf, d->bufpool); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 214 | bio_endio(bio, -ENXIO); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 215 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | list_add_tail(&buf->bufs, &d->bufq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 220 | aoecmd_work(d); |
David S. Miller | e9bb8fb | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 221 | __skb_queue_head_init(&queue); |
| 222 | skb_queue_splice_init(&d->sendq, &queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | spin_unlock_irqrestore(&d->lock, flags); |
David S. Miller | e9bb8fb | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 225 | aoenet_xmit(&queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 229 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 231 | struct aoedev *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if ((d->flags & DEVFL_UP) == 0) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 234 | printk(KERN_ERR "aoe: disk not up\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return -ENODEV; |
| 236 | } |
| 237 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 238 | geo->cylinders = d->geo.cylinders; |
| 239 | geo->heads = d->geo.heads; |
| 240 | geo->sectors = d->geo.sectors; |
| 241 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 244 | static const struct block_device_operations aoe_bdops = { |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 245 | .open = aoeblk_open, |
| 246 | .release = aoeblk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 247 | .getgeo = aoeblk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | .owner = THIS_MODULE, |
| 249 | }; |
| 250 | |
| 251 | /* alloc_disk and add_disk can sleep */ |
| 252 | void |
| 253 | aoeblk_gdalloc(void *vp) |
| 254 | { |
| 255 | struct aoedev *d = vp; |
| 256 | struct gendisk *gd; |
| 257 | ulong flags; |
| 258 | |
| 259 | gd = alloc_disk(AOE_PARTITIONS); |
| 260 | if (gd == NULL) { |
Ed L. Cashin | 1d75981 | 2008-02-08 04:20:08 -0800 | [diff] [blame] | 261 | printk(KERN_ERR |
| 262 | "aoe: cannot allocate disk structure for %ld.%d\n", |
Ed L. Cashin | 6bb6285f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 263 | d->aoemajor, d->aoeminor); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 264 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 267 | d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (d->bufpool == NULL) { |
Ed L. Cashin | 1d75981 | 2008-02-08 04:20:08 -0800 | [diff] [blame] | 269 | printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n", |
Ed L. Cashin | 6bb6285f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 270 | d->aoemajor, d->aoeminor); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 271 | goto err_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 274 | d->blkq = blk_alloc_queue(GFP_KERNEL); |
| 275 | if (!d->blkq) |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 276 | goto err_mempool; |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 277 | blk_queue_make_request(d->blkq, aoeblk_make_request); |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 278 | d->blkq->backing_dev_info.name = "aoe"; |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 279 | if (bdi_init(&d->blkq->backing_dev_info)) |
| 280 | goto err_blkq; |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 281 | spin_lock_irqsave(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | gd->major = AOE_MAJOR; |
| 283 | gd->first_minor = d->sysminor * AOE_PARTITIONS; |
| 284 | gd->fops = &aoe_bdops; |
| 285 | gd->private_data = d; |
Tejun Heo | 80795ae | 2008-08-25 19:56:07 +0900 | [diff] [blame] | 286 | set_capacity(gd, d->ssize); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 287 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | d->aoemajor, d->aoeminor); |
| 289 | |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 290 | gd->queue = d->blkq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | d->gd = gd; |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 292 | d->flags &= ~DEVFL_GDALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | d->flags |= DEVFL_UP; |
| 294 | |
| 295 | spin_unlock_irqrestore(&d->lock, flags); |
| 296 | |
| 297 | add_disk(gd); |
| 298 | aoedisk_add_sysfs(d); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 299 | return; |
| 300 | |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 301 | err_blkq: |
| 302 | blk_cleanup_queue(d->blkq); |
| 303 | d->blkq = NULL; |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 304 | err_mempool: |
| 305 | mempool_destroy(d->bufpool); |
| 306 | err_disk: |
| 307 | put_disk(gd); |
| 308 | err: |
| 309 | spin_lock_irqsave(&d->lock, flags); |
| 310 | d->flags &= ~DEVFL_GDALLOC; |
| 311 | spin_unlock_irqrestore(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void |
| 315 | aoeblk_exit(void) |
| 316 | { |
| 317 | kmem_cache_destroy(buf_pool_cache); |
| 318 | } |
| 319 | |
| 320 | int __init |
| 321 | aoeblk_init(void) |
| 322 | { |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 323 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | sizeof(struct buf), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 325 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (buf_pool_cache == NULL) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |