Ed Cashin | fea05a2 | 2012-10-04 17:16:38 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012 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> |
Ed Cashin | aa304fd | 2012-12-17 16:03:32 -0800 | [diff] [blame] | 19 | #include <linux/moduleparam.h> |
Ed Cashin | 667be1e | 2012-12-17 16:03:42 -0800 | [diff] [blame^] | 20 | #include <scsi/sg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "aoe.h" |
| 22 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 23 | static DEFINE_MUTEX(aoeblk_mutex); |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 24 | static struct kmem_cache *buf_pool_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Ed Cashin | aa304fd | 2012-12-17 16:03:32 -0800 | [diff] [blame] | 26 | /* GPFS needs a larger value than the default. */ |
| 27 | static int aoe_maxsectors; |
| 28 | module_param(aoe_maxsectors, int, 0644); |
| 29 | MODULE_PARM_DESC(aoe_maxsectors, |
| 30 | "When nonzero, set the maximum number of sectors per I/O request"); |
| 31 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 32 | static ssize_t aoedisk_show_state(struct device *dev, |
| 33 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 35 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | struct aoedev *d = disk->private_data; |
| 37 | |
| 38 | return snprintf(page, PAGE_SIZE, |
| 39 | "%s%s\n", |
| 40 | (d->flags & DEVFL_UP) ? "up" : "down", |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 41 | (d->flags & DEVFL_KICKME) ? ",kickme" : |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 42 | (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); |
| 43 | /* I'd rather see nopen exported so we can ditch closewait */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 45 | static ssize_t aoedisk_show_mac(struct device *dev, |
| 46 | struct device_attribute *attr, char *page) |
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 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | struct aoedev *d = disk->private_data; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 50 | struct aoetgt *t = d->targets[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 52 | if (t == NULL) |
| 53 | return snprintf(page, PAGE_SIZE, "none\n"); |
Harvey Harrison | 411c41e | 2008-11-25 00:40:37 -0800 | [diff] [blame] | 54 | return snprintf(page, PAGE_SIZE, "%pm\n", t->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 56 | static ssize_t aoedisk_show_netif(struct device *dev, |
| 57 | struct device_attribute *attr, char *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 59 | struct gendisk *disk = dev_to_disk(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct aoedev *d = disk->private_data; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 61 | struct net_device *nds[8], **nd, **nnd, **ne; |
| 62 | struct aoetgt **t, **te; |
| 63 | struct aoeif *ifp, *e; |
| 64 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 66 | memset(nds, 0, sizeof nds); |
| 67 | nd = nds; |
| 68 | ne = nd + ARRAY_SIZE(nds); |
| 69 | t = d->targets; |
| 70 | te = t + NTARGETS; |
| 71 | for (; t < te && *t; t++) { |
| 72 | ifp = (*t)->ifs; |
| 73 | e = ifp + NAOEIFS; |
| 74 | for (; ifp < e && ifp->nd; ifp++) { |
| 75 | for (nnd = nds; nnd < nd; nnd++) |
| 76 | if (*nnd == ifp->nd) |
| 77 | break; |
| 78 | if (nnd == nd && nd != ne) |
| 79 | *nd++ = ifp->nd; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ne = nd; |
| 84 | nd = nds; |
| 85 | if (*nd == NULL) |
| 86 | return snprintf(page, PAGE_SIZE, "none\n"); |
| 87 | for (p = page; nd < ne; nd++) |
| 88 | p += snprintf(p, PAGE_SIZE - (p-page), "%s%s", |
| 89 | p == page ? "" : ",", (*nd)->name); |
| 90 | p += snprintf(p, PAGE_SIZE - (p-page), "\n"); |
| 91 | return p-page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 93 | /* firmware version */ |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 94 | static ssize_t aoedisk_show_fwver(struct device *dev, |
| 95 | struct device_attribute *attr, char *page) |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 96 | { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 97 | struct gendisk *disk = dev_to_disk(dev); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 98 | struct aoedev *d = disk->private_data; |
| 99 | |
| 100 | return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); |
| 101 | } |
Ed Cashin | 90a2508 | 2012-12-17 16:03:34 -0800 | [diff] [blame] | 102 | static ssize_t aoedisk_show_payload(struct device *dev, |
| 103 | struct device_attribute *attr, char *page) |
| 104 | { |
| 105 | struct gendisk *disk = dev_to_disk(dev); |
| 106 | struct aoedev *d = disk->private_data; |
| 107 | |
| 108 | return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt); |
| 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 111 | static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL); |
| 112 | static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL); |
| 113 | static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL); |
| 114 | static struct device_attribute dev_attr_firmware_version = { |
Parag Warudkar | 01e8ef1 | 2008-10-18 20:28:50 -0700 | [diff] [blame] | 115 | .attr = { .name = "firmware-version", .mode = S_IRUGO }, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 116 | .show = aoedisk_show_fwver, |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 117 | }; |
Ed Cashin | 90a2508 | 2012-12-17 16:03:34 -0800 | [diff] [blame] | 118 | static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Greg Kroah-Hartman | 4ca5224 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 120 | static struct attribute *aoe_attrs[] = { |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 121 | &dev_attr_state.attr, |
| 122 | &dev_attr_mac.attr, |
| 123 | &dev_attr_netif.attr, |
| 124 | &dev_attr_firmware_version.attr, |
Ed Cashin | 90a2508 | 2012-12-17 16:03:34 -0800 | [diff] [blame] | 125 | &dev_attr_payload.attr, |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 126 | NULL, |
Greg Kroah-Hartman | 4ca5224 | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | static const struct attribute_group attr_group = { |
| 130 | .attrs = aoe_attrs, |
| 131 | }; |
| 132 | |
| 133 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | aoedisk_add_sysfs(struct aoedev *d) |
| 135 | { |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 136 | return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | void |
| 139 | aoedisk_rm_sysfs(struct aoedev *d) |
| 140 | { |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 141 | sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 145 | aoeblk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 147 | struct aoedev *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | ulong flags; |
| 149 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 150 | mutex_lock(&aoeblk_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | spin_lock_irqsave(&d->lock, flags); |
| 152 | if (d->flags & DEVFL_UP) { |
| 153 | d->nopen++; |
| 154 | spin_unlock_irqrestore(&d->lock, flags); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 155 | mutex_unlock(&aoeblk_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | spin_unlock_irqrestore(&d->lock, flags); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 159 | mutex_unlock(&aoeblk_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return -ENODEV; |
| 161 | } |
| 162 | |
| 163 | static int |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 164 | aoeblk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Al Viro | 94562c1 | 2008-03-02 09:23:18 -0500 | [diff] [blame] | 166 | struct aoedev *d = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | ulong flags; |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | spin_lock_irqsave(&d->lock, flags); |
| 170 | |
Ed L. Cashin | 5f7702f | 2006-01-19 13:46:27 -0500 | [diff] [blame] | 171 | if (--d->nopen == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | spin_unlock_irqrestore(&d->lock, flags); |
| 173 | aoecmd_cfg(d->aoemajor, d->aoeminor); |
| 174 | return 0; |
| 175 | } |
| 176 | spin_unlock_irqrestore(&d->lock, flags); |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 181 | static void |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 182 | aoeblk_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
| 184 | struct aoedev *d; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 185 | struct request *rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 187 | d = q->queuedata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if ((d->flags & DEVFL_UP) == 0) { |
Andrew Morton | 027b180 | 2010-10-28 06:15:26 -0600 | [diff] [blame] | 189 | pr_info_ratelimited("aoe: device %ld.%d is not up\n", |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 190 | d->aoemajor, d->aoeminor); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 191 | while ((rq = blk_peek_request(q))) { |
| 192 | blk_start_request(rq); |
| 193 | aoe_end_request(d, rq, 1); |
| 194 | } |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 195 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 197 | aoecmd_work(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 201 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 203 | struct aoedev *d = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if ((d->flags & DEVFL_UP) == 0) { |
Ed L. Cashin | a12c93f | 2006-09-20 14:36:51 -0400 | [diff] [blame] | 206 | printk(KERN_ERR "aoe: disk not up\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return -ENODEV; |
| 208 | } |
| 209 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 210 | geo->cylinders = d->geo.cylinders; |
| 211 | geo->heads = d->geo.heads; |
| 212 | geo->sectors = d->geo.sectors; |
| 213 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Ed Cashin | 667be1e | 2012-12-17 16:03:42 -0800 | [diff] [blame^] | 216 | static int |
| 217 | aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg) |
| 218 | { |
| 219 | struct aoedev *d; |
| 220 | |
| 221 | if (!arg) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | d = bdev->bd_disk->private_data; |
| 225 | if ((d->flags & DEVFL_UP) == 0) { |
| 226 | pr_err("aoe: disk not up\n"); |
| 227 | return -ENODEV; |
| 228 | } |
| 229 | |
| 230 | if (cmd == HDIO_GET_IDENTITY) { |
| 231 | if (!copy_to_user((void __user *) arg, &d->ident, |
| 232 | sizeof(d->ident))) |
| 233 | return 0; |
| 234 | return -EFAULT; |
| 235 | } |
| 236 | |
| 237 | /* udev calls scsi_id, which uses SG_IO, resulting in noise */ |
| 238 | if (cmd != SG_IO) |
| 239 | pr_info("aoe: unknown ioctl 0x%x\n", cmd); |
| 240 | |
| 241 | return -ENOTTY; |
| 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, |
Ed Cashin | 667be1e | 2012-12-17 16:03:42 -0800 | [diff] [blame^] | 247 | .ioctl = aoeblk_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 248 | .getgeo = aoeblk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | .owner = THIS_MODULE, |
| 250 | }; |
| 251 | |
| 252 | /* alloc_disk and add_disk can sleep */ |
| 253 | void |
| 254 | aoeblk_gdalloc(void *vp) |
| 255 | { |
| 256 | struct aoedev *d = vp; |
| 257 | struct gendisk *gd; |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 258 | mempool_t *mp; |
| 259 | struct request_queue *q; |
| 260 | enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | ulong flags; |
| 262 | |
| 263 | gd = alloc_disk(AOE_PARTITIONS); |
| 264 | if (gd == NULL) { |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 265 | pr_err("aoe: cannot allocate disk structure for %ld.%d\n", |
Ed L. Cashin | 6bb6285f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 266 | d->aoemajor, d->aoeminor); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 267 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 270 | mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab, |
| 271 | buf_pool_cache); |
| 272 | if (mp == NULL) { |
Ed L. Cashin | 1d75981 | 2008-02-08 04:20:08 -0800 | [diff] [blame] | 273 | printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n", |
Ed L. Cashin | 6bb6285f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 274 | d->aoemajor, d->aoeminor); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 275 | goto err_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 277 | q = blk_init_queue(aoeblk_request, &d->lock); |
| 278 | if (q == NULL) { |
| 279 | pr_err("aoe: cannot allocate block queue for %ld.%d\n", |
| 280 | d->aoemajor, d->aoeminor); |
| 281 | mempool_destroy(mp); |
| 282 | goto err_disk; |
| 283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 285 | d->blkq = blk_alloc_queue(GFP_KERNEL); |
| 286 | if (!d->blkq) |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 287 | goto err_mempool; |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 288 | d->blkq->backing_dev_info.name = "aoe"; |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 289 | if (bdi_init(&d->blkq->backing_dev_info)) |
| 290 | goto err_blkq; |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 291 | spin_lock_irqsave(&d->lock, flags); |
Ed Cashin | 3d5b060 | 2012-10-04 17:16:20 -0700 | [diff] [blame] | 292 | blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS); |
Ed Cashin | 69cf2d85 | 2012-10-04 17:16:23 -0700 | [diff] [blame] | 293 | q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE; |
| 294 | d->bufpool = mp; |
| 295 | d->blkq = gd->queue = q; |
| 296 | q->queuedata = d; |
| 297 | d->gd = gd; |
Ed Cashin | aa304fd | 2012-12-17 16:03:32 -0800 | [diff] [blame] | 298 | if (aoe_maxsectors) |
| 299 | blk_queue_max_hw_sectors(q, aoe_maxsectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | gd->major = AOE_MAJOR; |
Ed Cashin | 0c96621 | 2012-10-04 17:16:40 -0700 | [diff] [blame] | 301 | gd->first_minor = d->sysminor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | gd->fops = &aoe_bdops; |
| 303 | gd->private_data = d; |
Tejun Heo | 80795ae | 2008-08-25 19:56:07 +0900 | [diff] [blame] | 304 | set_capacity(gd, d->ssize); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 305 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | d->aoemajor, d->aoeminor); |
| 307 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 308 | d->flags &= ~DEVFL_GDALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | d->flags |= DEVFL_UP; |
| 310 | |
| 311 | spin_unlock_irqrestore(&d->lock, flags); |
| 312 | |
| 313 | add_disk(gd); |
| 314 | aoedisk_add_sysfs(d); |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 315 | return; |
| 316 | |
Ed Cashin | 7135a71b | 2009-09-09 14:10:18 +0200 | [diff] [blame] | 317 | err_blkq: |
| 318 | blk_cleanup_queue(d->blkq); |
| 319 | d->blkq = NULL; |
Andrew Morton | 43cbe2c | 2007-12-10 15:49:13 -0800 | [diff] [blame] | 320 | err_mempool: |
| 321 | mempool_destroy(d->bufpool); |
| 322 | err_disk: |
| 323 | put_disk(gd); |
| 324 | err: |
| 325 | spin_lock_irqsave(&d->lock, flags); |
| 326 | d->flags &= ~DEVFL_GDALLOC; |
| 327 | spin_unlock_irqrestore(&d->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void |
| 331 | aoeblk_exit(void) |
| 332 | { |
| 333 | kmem_cache_destroy(buf_pool_cache); |
| 334 | } |
| 335 | |
| 336 | int __init |
| 337 | aoeblk_init(void) |
| 338 | { |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 339 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | sizeof(struct buf), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 341 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if (buf_pool_cache == NULL) |
| 343 | return -ENOMEM; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |