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>
20 #include <linux/debugfs.h>
24 static DEFINE_MUTEX(aoeblk_mutex);
25 static struct kmem_cache *buf_pool_cache;
26 static struct dentry *aoe_debugfs_dir;
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");
34 static ssize_t aoedisk_show_state(struct device *dev,
35 struct device_attribute *attr, char *page)
37 struct gendisk *disk = dev_to_disk(dev);
38 struct aoedev *d = disk->private_data;
40 return snprintf(page, PAGE_SIZE,
42 (d->flags & DEVFL_UP) ? "up" : "down",
43 (d->flags & DEVFL_KICKME) ? ",kickme" :
44 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
45 /* I'd rather see nopen exported so we can ditch closewait */
47 static ssize_t aoedisk_show_mac(struct device *dev,
48 struct device_attribute *attr, char *page)
50 struct gendisk *disk = dev_to_disk(dev);
51 struct aoedev *d = disk->private_data;
52 struct aoetgt *t = d->targets[0];
55 return snprintf(page, PAGE_SIZE, "none\n");
56 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
58 static ssize_t aoedisk_show_netif(struct device *dev,
59 struct device_attribute *attr, char *page)
61 struct gendisk *disk = dev_to_disk(dev);
62 struct aoedev *d = disk->private_data;
63 struct net_device *nds[8], **nd, **nnd, **ne;
64 struct aoetgt **t, **te;
65 struct aoeif *ifp, *e;
68 memset(nds, 0, sizeof nds);
70 ne = nd + ARRAY_SIZE(nds);
73 for (; t < te && *t; t++) {
76 for (; ifp < e && ifp->nd; ifp++) {
77 for (nnd = nds; nnd < nd; nnd++)
80 if (nnd == nd && nd != ne)
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");
95 /* firmware version */
96 static ssize_t aoedisk_show_fwver(struct device *dev,
97 struct device_attribute *attr, char *page)
99 struct gendisk *disk = dev_to_disk(dev);
100 struct aoedev *d = disk->private_data;
102 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
104 static ssize_t aoedisk_show_payload(struct device *dev,
105 struct device_attribute *attr, char *page)
107 struct gendisk *disk = dev_to_disk(dev);
108 struct aoedev *d = disk->private_data;
110 return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
113 static int aoedisk_debugfs_show(struct seq_file *s, void *ignored)
119 spin_lock_irqsave(&d->lock, flags);
120 seq_printf(s, "%s\n", d->gd->disk_name); /* place holder */
121 spin_unlock_irqrestore(&d->lock, flags);
126 static int aoe_debugfs_open(struct inode *inode, struct file *file)
128 return single_open(file, aoedisk_debugfs_show, inode->i_private);
131 static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
132 static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
133 static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
134 static struct device_attribute dev_attr_firmware_version = {
135 .attr = { .name = "firmware-version", .mode = S_IRUGO },
136 .show = aoedisk_show_fwver,
138 static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
140 static struct attribute *aoe_attrs[] = {
141 &dev_attr_state.attr,
143 &dev_attr_netif.attr,
144 &dev_attr_firmware_version.attr,
145 &dev_attr_payload.attr,
149 static const struct attribute_group attr_group = {
153 static const struct file_operations aoe_debugfs_fops = {
154 .open = aoe_debugfs_open,
157 .release = single_release,
161 aoedisk_add_debugfs(struct aoedev *d)
163 struct dentry *entry;
166 if (aoe_debugfs_dir == NULL)
168 p = strchr(d->gd->disk_name, '/');
170 p = d->gd->disk_name;
174 entry = debugfs_create_file(p, 0444, aoe_debugfs_dir, d,
176 if (IS_ERR_OR_NULL(entry)) {
177 pr_info("aoe: cannot create debugfs file for %s\n",
185 aoedisk_rm_debugfs(struct aoedev *d)
187 BUG_ON(d->debugfs == NULL);
188 debugfs_remove(d->debugfs);
193 aoedisk_add_sysfs(struct aoedev *d)
195 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
198 aoedisk_rm_sysfs(struct aoedev *d)
200 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
204 aoeblk_open(struct block_device *bdev, fmode_t mode)
206 struct aoedev *d = bdev->bd_disk->private_data;
209 if (!virt_addr_valid(d)) {
210 pr_crit("aoe: invalid device pointer in %s\n",
215 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
218 mutex_lock(&aoeblk_mutex);
219 spin_lock_irqsave(&d->lock, flags);
220 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
222 spin_unlock_irqrestore(&d->lock, flags);
223 mutex_unlock(&aoeblk_mutex);
226 spin_unlock_irqrestore(&d->lock, flags);
227 mutex_unlock(&aoeblk_mutex);
232 aoeblk_release(struct gendisk *disk, fmode_t mode)
234 struct aoedev *d = disk->private_data;
237 spin_lock_irqsave(&d->lock, flags);
239 if (--d->nopen == 0) {
240 spin_unlock_irqrestore(&d->lock, flags);
241 aoecmd_cfg(d->aoemajor, d->aoeminor);
244 spin_unlock_irqrestore(&d->lock, flags);
248 aoeblk_request(struct request_queue *q)
254 if ((d->flags & DEVFL_UP) == 0) {
255 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
256 d->aoemajor, d->aoeminor);
257 while ((rq = blk_peek_request(q))) {
258 blk_start_request(rq);
259 aoe_end_request(d, rq, 1);
267 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
269 struct aoedev *d = bdev->bd_disk->private_data;
271 if ((d->flags & DEVFL_UP) == 0) {
272 printk(KERN_ERR "aoe: disk not up\n");
276 geo->cylinders = d->geo.cylinders;
277 geo->heads = d->geo.heads;
278 geo->sectors = d->geo.sectors;
283 aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
290 d = bdev->bd_disk->private_data;
291 if ((d->flags & DEVFL_UP) == 0) {
292 pr_err("aoe: disk not up\n");
296 if (cmd == HDIO_GET_IDENTITY) {
297 if (!copy_to_user((void __user *) arg, &d->ident,
303 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
305 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
310 static const struct block_device_operations aoe_bdops = {
312 .release = aoeblk_release,
313 .ioctl = aoeblk_ioctl,
314 .getgeo = aoeblk_getgeo,
315 .owner = THIS_MODULE,
318 /* alloc_disk and add_disk can sleep */
320 aoeblk_gdalloc(void *vp)
322 struct aoedev *d = vp;
325 struct request_queue *q;
326 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
330 spin_lock_irqsave(&d->lock, flags);
331 if (d->flags & DEVFL_GDALLOC
332 && !(d->flags & DEVFL_TKILL)
333 && !(d->flags & DEVFL_GD_NOW))
334 d->flags |= DEVFL_GD_NOW;
337 spin_unlock_irqrestore(&d->lock, flags);
341 gd = alloc_disk(AOE_PARTITIONS);
343 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
344 d->aoemajor, d->aoeminor);
348 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
351 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
352 d->aoemajor, d->aoeminor);
355 q = blk_init_queue(aoeblk_request, &d->lock);
357 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
358 d->aoemajor, d->aoeminor);
362 spin_lock_irqsave(&d->lock, flags);
363 WARN_ON(!(d->flags & DEVFL_GD_NOW));
364 WARN_ON(!(d->flags & DEVFL_GDALLOC));
365 WARN_ON(d->flags & DEVFL_TKILL);
367 WARN_ON(d->flags & DEVFL_UP);
368 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
369 q->backing_dev_info.name = "aoe";
370 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
372 d->blkq = gd->queue = q;
376 blk_queue_max_hw_sectors(q, aoe_maxsectors);
377 gd->major = AOE_MAJOR;
378 gd->first_minor = d->sysminor;
379 gd->fops = &aoe_bdops;
380 gd->private_data = d;
381 set_capacity(gd, d->ssize);
382 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
383 d->aoemajor, d->aoeminor);
385 d->flags &= ~DEVFL_GDALLOC;
386 d->flags |= DEVFL_UP;
388 spin_unlock_irqrestore(&d->lock, flags);
391 aoedisk_add_sysfs(d);
392 aoedisk_add_debugfs(d);
394 spin_lock_irqsave(&d->lock, flags);
395 WARN_ON(!(d->flags & DEVFL_GD_NOW));
396 d->flags &= ~DEVFL_GD_NOW;
397 spin_unlock_irqrestore(&d->lock, flags);
405 spin_lock_irqsave(&d->lock, flags);
406 d->flags &= ~DEVFL_GD_NOW;
407 schedule_work(&d->work);
408 spin_unlock_irqrestore(&d->lock, flags);
414 debugfs_remove_recursive(aoe_debugfs_dir);
415 aoe_debugfs_dir = NULL;
416 kmem_cache_destroy(buf_pool_cache);
422 buf_pool_cache = kmem_cache_create("aoe_bufs",
425 if (buf_pool_cache == NULL)
427 aoe_debugfs_dir = debugfs_create_dir("aoe", NULL);
428 if (IS_ERR_OR_NULL(aoe_debugfs_dir)) {
429 pr_info("aoe: cannot create debugfs directory\n");
430 aoe_debugfs_dir = NULL;