aoe: create and destroy debugfs directory for aoe
[linux-2.6/btrfs-unstable.git] / drivers / block / aoe / aoeblk.c
blobcb508b75437774e73bd243374926a9cb9a5cafcd
1 /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoeblk.c
4 * block device routines
5 */
7 #include <linux/kernel.h>
8 #include <linux/hdreg.h>
9 #include <linux/blkdev.h>
10 #include <linux/backing-dev.h>
11 #include <linux/fs.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>
21 #include <scsi/sg.h>
22 #include "aoe.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,
41 "%s%s\n",
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];
54 if (t == NULL)
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;
66 char *p;
68 memset(nds, 0, sizeof nds);
69 nd = nds;
70 ne = nd + ARRAY_SIZE(nds);
71 t = d->targets;
72 te = t + d->ntargets;
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;
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;
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 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 = {
117 .attr = { .name = "firmware-version", .mode = S_IRUGO },
118 .show = aoedisk_show_fwver,
120 static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
122 static struct attribute *aoe_attrs[] = {
123 &dev_attr_state.attr,
124 &dev_attr_mac.attr,
125 &dev_attr_netif.attr,
126 &dev_attr_firmware_version.attr,
127 &dev_attr_payload.attr,
128 NULL,
131 static const struct attribute_group attr_group = {
132 .attrs = aoe_attrs,
135 static int
136 aoedisk_add_sysfs(struct aoedev *d)
138 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
140 void
141 aoedisk_rm_sysfs(struct aoedev *d)
143 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
146 static int
147 aoeblk_open(struct block_device *bdev, fmode_t mode)
149 struct aoedev *d = bdev->bd_disk->private_data;
150 ulong flags;
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;
158 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
159 return -ENODEV;
161 mutex_lock(&aoeblk_mutex);
162 spin_lock_irqsave(&d->lock, flags);
163 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
164 d->nopen++;
165 spin_unlock_irqrestore(&d->lock, flags);
166 mutex_unlock(&aoeblk_mutex);
167 return 0;
169 spin_unlock_irqrestore(&d->lock, flags);
170 mutex_unlock(&aoeblk_mutex);
171 return -ENODEV;
174 static void
175 aoeblk_release(struct gendisk *disk, fmode_t mode)
177 struct aoedev *d = disk->private_data;
178 ulong flags;
180 spin_lock_irqsave(&d->lock, flags);
182 if (--d->nopen == 0) {
183 spin_unlock_irqrestore(&d->lock, flags);
184 aoecmd_cfg(d->aoemajor, d->aoeminor);
185 return;
187 spin_unlock_irqrestore(&d->lock, flags);
190 static void
191 aoeblk_request(struct request_queue *q)
193 struct aoedev *d;
194 struct request *rq;
196 d = q->queuedata;
197 if ((d->flags & DEVFL_UP) == 0) {
198 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
199 d->aoemajor, d->aoeminor);
200 while ((rq = blk_peek_request(q))) {
201 blk_start_request(rq);
202 aoe_end_request(d, rq, 1);
204 return;
206 aoecmd_work(d);
209 static int
210 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
212 struct aoedev *d = bdev->bd_disk->private_data;
214 if ((d->flags & DEVFL_UP) == 0) {
215 printk(KERN_ERR "aoe: disk not up\n");
216 return -ENODEV;
219 geo->cylinders = d->geo.cylinders;
220 geo->heads = d->geo.heads;
221 geo->sectors = d->geo.sectors;
222 return 0;
225 static int
226 aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
228 struct aoedev *d;
230 if (!arg)
231 return -EINVAL;
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;
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;
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);
250 return -ENOTTY;
253 static const struct block_device_operations aoe_bdops = {
254 .open = aoeblk_open,
255 .release = aoeblk_release,
256 .ioctl = aoeblk_ioctl,
257 .getgeo = aoeblk_getgeo,
258 .owner = THIS_MODULE,
261 /* alloc_disk and add_disk can sleep */
262 void
263 aoeblk_gdalloc(void *vp)
265 struct aoedev *d = vp;
266 struct gendisk *gd;
267 mempool_t *mp;
268 struct request_queue *q;
269 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
270 ulong flags;
271 int late = 0;
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;
284 gd = alloc_disk(AOE_PARTITIONS);
285 if (gd == NULL) {
286 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
287 d->aoemajor, d->aoeminor);
288 goto err;
291 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
292 buf_pool_cache);
293 if (mp == NULL) {
294 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
295 d->aoemajor, d->aoeminor);
296 goto err_disk;
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);
302 goto err_mempool;
305 spin_lock_irqsave(&d->lock, flags);
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);
311 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
312 q->backing_dev_info.name = "aoe";
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;
318 if (aoe_maxsectors)
319 blk_queue_max_hw_sectors(q, aoe_maxsectors);
320 gd->major = AOE_MAJOR;
321 gd->first_minor = d->sysminor;
322 gd->fops = &aoe_bdops;
323 gd->private_data = d;
324 set_capacity(gd, d->ssize);
325 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
326 d->aoemajor, d->aoeminor);
328 d->flags &= ~DEVFL_GDALLOC;
329 d->flags |= DEVFL_UP;
331 spin_unlock_irqrestore(&d->lock, flags);
333 add_disk(gd);
334 aoedisk_add_sysfs(d);
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);
340 return;
342 err_mempool:
343 mempool_destroy(mp);
344 err_disk:
345 put_disk(gd);
346 err:
347 spin_lock_irqsave(&d->lock, flags);
348 d->flags &= ~DEVFL_GD_NOW;
349 schedule_work(&d->work);
350 spin_unlock_irqrestore(&d->lock, flags);
353 void
354 aoeblk_exit(void)
356 debugfs_remove_recursive(aoe_debugfs_dir);
357 aoe_debugfs_dir = NULL;
358 kmem_cache_destroy(buf_pool_cache);
361 int __init
362 aoeblk_init(void)
364 buf_pool_cache = kmem_cache_create("aoe_bufs",
365 sizeof(struct buf),
366 0, 0, NULL);
367 if (buf_pool_cache == NULL)
368 return -ENOMEM;
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;
374 return 0;