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>
23 static DEFINE_MUTEX(aoeblk_mutex);
24 static struct kmem_cache *buf_pool_cache;
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");
32 static ssize_t aoedisk_show_state(struct device *dev,
33 struct device_attribute *attr, char *page)
35 struct gendisk *disk = dev_to_disk(dev);
36 struct aoedev *d = disk->private_data;
38 return snprintf(page, PAGE_SIZE,
40 (d->flags & DEVFL_UP) ? "up" : "down",
41 (d->flags & DEVFL_KICKME) ? ",kickme" :
42 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
43 /* I'd rather see nopen exported so we can ditch closewait */
45 static ssize_t aoedisk_show_mac(struct device *dev,
46 struct device_attribute *attr, char *page)
48 struct gendisk *disk = dev_to_disk(dev);
49 struct aoedev *d = disk->private_data;
50 struct aoetgt *t = d->targets[0];
53 return snprintf(page, PAGE_SIZE, "none\n");
54 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
56 static ssize_t aoedisk_show_netif(struct device *dev,
57 struct device_attribute *attr, char *page)
59 struct gendisk *disk = dev_to_disk(dev);
60 struct aoedev *d = disk->private_data;
61 struct net_device *nds[8], **nd, **nnd, **ne;
62 struct aoetgt **t, **te;
63 struct aoeif *ifp, *e;
66 memset(nds, 0, sizeof nds);
68 ne = nd + ARRAY_SIZE(nds);
71 for (; t < te && *t; t++) {
74 for (; ifp < e && ifp->nd; ifp++) {
75 for (nnd = nds; nnd < nd; nnd++)
78 if (nnd == nd && nd != ne)
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");
93 /* firmware version */
94 static ssize_t aoedisk_show_fwver(struct device *dev,
95 struct device_attribute *attr, char *page)
97 struct gendisk *disk = dev_to_disk(dev);
98 struct aoedev *d = disk->private_data;
100 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
102 static ssize_t aoedisk_show_payload(struct device *dev,
103 struct device_attribute *attr, char *page)
105 struct gendisk *disk = dev_to_disk(dev);
106 struct aoedev *d = disk->private_data;
108 return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
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 = {
115 .attr = { .name = "firmware-version", .mode = S_IRUGO },
116 .show = aoedisk_show_fwver,
118 static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
120 static struct attribute *aoe_attrs[] = {
121 &dev_attr_state.attr,
123 &dev_attr_netif.attr,
124 &dev_attr_firmware_version.attr,
125 &dev_attr_payload.attr,
129 static const struct attribute_group attr_group = {
134 aoedisk_add_sysfs(struct aoedev *d)
136 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
139 aoedisk_rm_sysfs(struct aoedev *d)
141 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
145 aoeblk_open(struct block_device *bdev, fmode_t mode)
147 struct aoedev *d = bdev->bd_disk->private_data;
150 mutex_lock(&aoeblk_mutex);
151 spin_lock_irqsave(&d->lock, flags);
152 if (d->flags & DEVFL_UP) {
154 spin_unlock_irqrestore(&d->lock, flags);
155 mutex_unlock(&aoeblk_mutex);
158 spin_unlock_irqrestore(&d->lock, flags);
159 mutex_unlock(&aoeblk_mutex);
164 aoeblk_release(struct gendisk *disk, fmode_t mode)
166 struct aoedev *d = disk->private_data;
169 spin_lock_irqsave(&d->lock, flags);
171 if (--d->nopen == 0) {
172 spin_unlock_irqrestore(&d->lock, flags);
173 aoecmd_cfg(d->aoemajor, d->aoeminor);
176 spin_unlock_irqrestore(&d->lock, flags);
182 aoeblk_request(struct request_queue *q)
188 if ((d->flags & DEVFL_UP) == 0) {
189 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
190 d->aoemajor, d->aoeminor);
191 while ((rq = blk_peek_request(q))) {
192 blk_start_request(rq);
193 aoe_end_request(d, rq, 1);
201 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
203 struct aoedev *d = bdev->bd_disk->private_data;
205 if ((d->flags & DEVFL_UP) == 0) {
206 printk(KERN_ERR "aoe: disk not up\n");
210 geo->cylinders = d->geo.cylinders;
211 geo->heads = d->geo.heads;
212 geo->sectors = d->geo.sectors;
217 aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
224 d = bdev->bd_disk->private_data;
225 if ((d->flags & DEVFL_UP) == 0) {
226 pr_err("aoe: disk not up\n");
230 if (cmd == HDIO_GET_IDENTITY) {
231 if (!copy_to_user((void __user *) arg, &d->ident,
237 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
239 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
244 static const struct block_device_operations aoe_bdops = {
246 .release = aoeblk_release,
247 .ioctl = aoeblk_ioctl,
248 .getgeo = aoeblk_getgeo,
249 .owner = THIS_MODULE,
252 /* alloc_disk and add_disk can sleep */
254 aoeblk_gdalloc(void *vp)
256 struct aoedev *d = vp;
259 struct request_queue *q;
260 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
263 gd = alloc_disk(AOE_PARTITIONS);
265 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
266 d->aoemajor, d->aoeminor);
270 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
273 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
274 d->aoemajor, d->aoeminor);
277 q = blk_init_queue(aoeblk_request, &d->lock);
279 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
280 d->aoemajor, d->aoeminor);
285 d->blkq = blk_alloc_queue(GFP_KERNEL);
288 d->blkq->backing_dev_info.name = "aoe";
289 if (bdi_init(&d->blkq->backing_dev_info))
291 spin_lock_irqsave(&d->lock, flags);
292 blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS);
293 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
295 d->blkq = gd->queue = q;
299 blk_queue_max_hw_sectors(q, aoe_maxsectors);
300 gd->major = AOE_MAJOR;
301 gd->first_minor = d->sysminor;
302 gd->fops = &aoe_bdops;
303 gd->private_data = d;
304 set_capacity(gd, d->ssize);
305 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
306 d->aoemajor, d->aoeminor);
308 d->flags &= ~DEVFL_GDALLOC;
309 d->flags |= DEVFL_UP;
311 spin_unlock_irqrestore(&d->lock, flags);
314 aoedisk_add_sysfs(d);
318 blk_cleanup_queue(d->blkq);
321 mempool_destroy(d->bufpool);
325 spin_lock_irqsave(&d->lock, flags);
326 d->flags &= ~DEVFL_GDALLOC;
327 spin_unlock_irqrestore(&d->lock, flags);
333 kmem_cache_destroy(buf_pool_cache);
339 buf_pool_cache = kmem_cache_create("aoe_bufs",
342 if (buf_pool_cache == NULL)