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