1 /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
4 * block device routines
7 #include <linux/kernel.h>
8 #include <linux/hdreg.h>
9 #include <linux/blkdev.h>
10 #include <linux/backing-dev.h>
12 #include <linux/ioctl.h>
13 #include <linux/slab.h>
14 #include <linux/ratelimit.h>
15 #include <linux/genhd.h>
16 #include <linux/netdevice.h>
17 #include <linux/mutex.h>
18 #include <linux/export.h>
19 #include <linux/moduleparam.h>
22 static DEFINE_MUTEX(aoeblk_mutex);
23 static struct kmem_cache *buf_pool_cache;
25 /* GPFS needs a larger value than the default. */
26 static int aoe_maxsectors;
27 module_param(aoe_maxsectors, int, 0644);
28 MODULE_PARM_DESC(aoe_maxsectors,
29 "When nonzero, set the maximum number of sectors per I/O request");
31 static ssize_t aoedisk_show_state(struct device *dev,
32 struct device_attribute *attr, char *page)
34 struct gendisk *disk = dev_to_disk(dev);
35 struct aoedev *d = disk->private_data;
37 return snprintf(page, PAGE_SIZE,
39 (d->flags & DEVFL_UP) ? "up" : "down",
40 (d->flags & DEVFL_KICKME) ? ",kickme" :
41 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
42 /* I'd rather see nopen exported so we can ditch closewait */
44 static ssize_t aoedisk_show_mac(struct device *dev,
45 struct device_attribute *attr, char *page)
47 struct gendisk *disk = dev_to_disk(dev);
48 struct aoedev *d = disk->private_data;
49 struct aoetgt *t = d->targets[0];
52 return snprintf(page, PAGE_SIZE, "none\n");
53 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
55 static ssize_t aoedisk_show_netif(struct device *dev,
56 struct device_attribute *attr, char *page)
58 struct gendisk *disk = dev_to_disk(dev);
59 struct aoedev *d = disk->private_data;
60 struct net_device *nds[8], **nd, **nnd, **ne;
61 struct aoetgt **t, **te;
62 struct aoeif *ifp, *e;
65 memset(nds, 0, sizeof nds);
67 ne = nd + ARRAY_SIZE(nds);
70 for (; t < te && *t; t++) {
73 for (; ifp < e && ifp->nd; ifp++) {
74 for (nnd = nds; nnd < nd; nnd++)
77 if (nnd == nd && nd != ne)
85 return snprintf(page, PAGE_SIZE, "none\n");
86 for (p = page; nd < ne; nd++)
87 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
88 p == page ? "" : ",", (*nd)->name);
89 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
92 /* firmware version */
93 static ssize_t aoedisk_show_fwver(struct device *dev,
94 struct device_attribute *attr, char *page)
96 struct gendisk *disk = dev_to_disk(dev);
97 struct aoedev *d = disk->private_data;
99 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
101 static ssize_t aoedisk_show_payload(struct device *dev,
102 struct device_attribute *attr, char *page)
104 struct gendisk *disk = dev_to_disk(dev);
105 struct aoedev *d = disk->private_data;
107 return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
110 static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
111 static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
112 static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
113 static struct device_attribute dev_attr_firmware_version = {
114 .attr = { .name = "firmware-version", .mode = S_IRUGO },
115 .show = aoedisk_show_fwver,
117 static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
119 static struct attribute *aoe_attrs[] = {
120 &dev_attr_state.attr,
122 &dev_attr_netif.attr,
123 &dev_attr_firmware_version.attr,
124 &dev_attr_payload.attr,
128 static const struct attribute_group attr_group = {
133 aoedisk_add_sysfs(struct aoedev *d)
135 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
138 aoedisk_rm_sysfs(struct aoedev *d)
140 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
144 aoeblk_open(struct block_device *bdev, fmode_t mode)
146 struct aoedev *d = bdev->bd_disk->private_data;
149 mutex_lock(&aoeblk_mutex);
150 spin_lock_irqsave(&d->lock, flags);
151 if (d->flags & DEVFL_UP) {
153 spin_unlock_irqrestore(&d->lock, flags);
154 mutex_unlock(&aoeblk_mutex);
157 spin_unlock_irqrestore(&d->lock, flags);
158 mutex_unlock(&aoeblk_mutex);
163 aoeblk_release(struct gendisk *disk, fmode_t mode)
165 struct aoedev *d = disk->private_data;
168 spin_lock_irqsave(&d->lock, flags);
170 if (--d->nopen == 0) {
171 spin_unlock_irqrestore(&d->lock, flags);
172 aoecmd_cfg(d->aoemajor, d->aoeminor);
175 spin_unlock_irqrestore(&d->lock, flags);
181 aoeblk_request(struct request_queue *q)
187 if ((d->flags & DEVFL_UP) == 0) {
188 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
189 d->aoemajor, d->aoeminor);
190 while ((rq = blk_peek_request(q))) {
191 blk_start_request(rq);
192 aoe_end_request(d, rq, 1);
200 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
202 struct aoedev *d = bdev->bd_disk->private_data;
204 if ((d->flags & DEVFL_UP) == 0) {
205 printk(KERN_ERR "aoe: disk not up\n");
209 geo->cylinders = d->geo.cylinders;
210 geo->heads = d->geo.heads;
211 geo->sectors = d->geo.sectors;
215 static const struct block_device_operations aoe_bdops = {
217 .release = aoeblk_release,
218 .getgeo = aoeblk_getgeo,
219 .owner = THIS_MODULE,
222 /* alloc_disk and add_disk can sleep */
224 aoeblk_gdalloc(void *vp)
226 struct aoedev *d = vp;
229 struct request_queue *q;
230 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
233 gd = alloc_disk(AOE_PARTITIONS);
235 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
236 d->aoemajor, d->aoeminor);
240 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
243 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
244 d->aoemajor, d->aoeminor);
247 q = blk_init_queue(aoeblk_request, &d->lock);
249 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
250 d->aoemajor, d->aoeminor);
255 d->blkq = blk_alloc_queue(GFP_KERNEL);
258 d->blkq->backing_dev_info.name = "aoe";
259 if (bdi_init(&d->blkq->backing_dev_info))
261 spin_lock_irqsave(&d->lock, flags);
262 blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS);
263 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
265 d->blkq = gd->queue = q;
269 blk_queue_max_hw_sectors(q, aoe_maxsectors);
270 gd->major = AOE_MAJOR;
271 gd->first_minor = d->sysminor;
272 gd->fops = &aoe_bdops;
273 gd->private_data = d;
274 set_capacity(gd, d->ssize);
275 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
276 d->aoemajor, d->aoeminor);
278 d->flags &= ~DEVFL_GDALLOC;
279 d->flags |= DEVFL_UP;
281 spin_unlock_irqrestore(&d->lock, flags);
284 aoedisk_add_sysfs(d);
288 blk_cleanup_queue(d->blkq);
291 mempool_destroy(d->bufpool);
295 spin_lock_irqsave(&d->lock, flags);
296 d->flags &= ~DEVFL_GDALLOC;
297 spin_unlock_irqrestore(&d->lock, flags);
303 kmem_cache_destroy(buf_pool_cache);
309 buf_pool_cache = kmem_cache_create("aoe_bufs",
312 if (buf_pool_cache == NULL)