block: Use pointer to backing_dev_info from request_queue
[linux-2.6-block.git] / drivers / block / aoe / aoeblk.c
CommitLineData
ec345120 1/* Copyright (c) 2013 Coraid, Inc. See COPYING for GPL terms. */
1da177e4
LT
2/*
3 * aoeblk.c
4 * block device routines
5 */
6
027b180d 7#include <linux/kernel.h>
1da177e4
LT
8#include <linux/hdreg.h>
9#include <linux/blkdev.h>
43cbe2cb 10#include <linux/backing-dev.h>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/ioctl.h>
5a0e3ad6 13#include <linux/slab.h>
027b180d 14#include <linux/ratelimit.h>
1da177e4
LT
15#include <linux/genhd.h>
16#include <linux/netdevice.h>
2a48fc0a 17#include <linux/mutex.h>
d5decd3b 18#include <linux/export.h>
aa304fde 19#include <linux/moduleparam.h>
190519cd 20#include <linux/debugfs.h>
667be1e7 21#include <scsi/sg.h>
1da177e4
LT
22#include "aoe.h"
23
2a48fc0a 24static DEFINE_MUTEX(aoeblk_mutex);
e18b890b 25static struct kmem_cache *buf_pool_cache;
190519cd 26static struct dentry *aoe_debugfs_dir;
1da177e4 27
aa304fde
EC
28/* GPFS needs a larger value than the default. */
29static int aoe_maxsectors;
30module_param(aoe_maxsectors, int, 0644);
31MODULE_PARM_DESC(aoe_maxsectors,
32 "When nonzero, set the maximum number of sectors per I/O request");
33
edfaa7c3
KS
34static ssize_t aoedisk_show_state(struct device *dev,
35 struct device_attribute *attr, char *page)
1da177e4 36{
edfaa7c3 37 struct gendisk *disk = dev_to_disk(dev);
1da177e4
LT
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",
68e0d42f 43 (d->flags & DEVFL_KICKME) ? ",kickme" :
3ae1c24e
EC
44 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
45 /* I'd rather see nopen exported so we can ditch closewait */
1da177e4 46}
edfaa7c3
KS
47static ssize_t aoedisk_show_mac(struct device *dev,
48 struct device_attribute *attr, char *page)
1da177e4 49{
edfaa7c3 50 struct gendisk *disk = dev_to_disk(dev);
1da177e4 51 struct aoedev *d = disk->private_data;
68e0d42f 52 struct aoetgt *t = d->targets[0];
1da177e4 53
68e0d42f
EC
54 if (t == NULL)
55 return snprintf(page, PAGE_SIZE, "none\n");
411c41ee 56 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
1da177e4 57}
edfaa7c3
KS
58static ssize_t aoedisk_show_netif(struct device *dev,
59 struct device_attribute *attr, char *page)
1da177e4 60{
edfaa7c3 61 struct gendisk *disk = dev_to_disk(dev);
1da177e4 62 struct aoedev *d = disk->private_data;
68e0d42f
EC
63 struct net_device *nds[8], **nd, **nnd, **ne;
64 struct aoetgt **t, **te;
65 struct aoeif *ifp, *e;
66 char *p;
67
68 memset(nds, 0, sizeof nds);
69 nd = nds;
70 ne = nd + ARRAY_SIZE(nds);
71 t = d->targets;
71114ec4 72 te = t + d->ntargets;
68e0d42f
EC
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 }
1da177e4 84
68e0d42f
EC
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;
1da177e4 94}
4613ed27 95/* firmware version */
edfaa7c3
KS
96static ssize_t aoedisk_show_fwver(struct device *dev,
97 struct device_attribute *attr, char *page)
4613ed27 98{
edfaa7c3 99 struct gendisk *disk = dev_to_disk(dev);
4613ed27
EC
100 struct aoedev *d = disk->private_data;
101
102 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
103}
90a2508d
EC
104static 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}
1da177e4 112
1cf94797
EC
113static int aoedisk_debugfs_show(struct seq_file *s, void *ignored)
114{
115 struct aoedev *d;
2256c1c5
EC
116 struct aoetgt **t, **te;
117 struct aoeif *ifp, *ife;
1cf94797 118 unsigned long flags;
2256c1c5 119 char c;
1cf94797
EC
120
121 d = s->private;
2256c1c5
EC
122 seq_printf(s, "rttavg: %d rttdev: %d\n",
123 d->rttavg >> RTTSCALE,
124 d->rttdev >> RTTDSCALE);
125 seq_printf(s, "nskbpool: %d\n", skb_queue_len(&d->skbpool));
126 seq_printf(s, "kicked: %ld\n", d->kicked);
127 seq_printf(s, "maxbcnt: %ld\n", d->maxbcnt);
128 seq_printf(s, "ref: %ld\n", d->ref);
129
1cf94797 130 spin_lock_irqsave(&d->lock, flags);
2256c1c5
EC
131 t = d->targets;
132 te = t + d->ntargets;
133 for (; t < te && *t; t++) {
134 c = '\t';
135 seq_printf(s, "falloc: %ld\n", (*t)->falloc);
136 seq_printf(s, "ffree: %p\n",
137 list_empty(&(*t)->ffree) ? NULL : (*t)->ffree.next);
138 seq_printf(s, "%pm:%d:%d:%d\n", (*t)->addr, (*t)->nout,
139 (*t)->maxout, (*t)->nframes);
140 seq_printf(s, "\tssthresh:%d\n", (*t)->ssthresh);
141 seq_printf(s, "\ttaint:%d\n", (*t)->taint);
142 seq_printf(s, "\tr:%d\n", (*t)->rpkts);
143 seq_printf(s, "\tw:%d\n", (*t)->wpkts);
144 ifp = (*t)->ifs;
145 ife = ifp + ARRAY_SIZE((*t)->ifs);
146 for (; ifp->nd && ifp < ife; ifp++) {
147 seq_printf(s, "%c%s", c, ifp->nd->name);
148 c = ',';
149 }
150 seq_puts(s, "\n");
151 }
1cf94797
EC
152 spin_unlock_irqrestore(&d->lock, flags);
153
154 return 0;
155}
156
157static int aoe_debugfs_open(struct inode *inode, struct file *file)
158{
159 return single_open(file, aoedisk_debugfs_show, inode->i_private);
160}
161
edfaa7c3
KS
162static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
163static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
164static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
165static struct device_attribute dev_attr_firmware_version = {
01e8ef11 166 .attr = { .name = "firmware-version", .mode = S_IRUGO },
edfaa7c3 167 .show = aoedisk_show_fwver,
4613ed27 168};
90a2508d 169static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
1da177e4 170
4ca5224f 171static struct attribute *aoe_attrs[] = {
edfaa7c3
KS
172 &dev_attr_state.attr,
173 &dev_attr_mac.attr,
174 &dev_attr_netif.attr,
175 &dev_attr_firmware_version.attr,
90a2508d 176 &dev_attr_payload.attr,
edfaa7c3 177 NULL,
4ca5224f
GKH
178};
179
180static const struct attribute_group attr_group = {
181 .attrs = aoe_attrs,
182};
183
1cf94797
EC
184static const struct file_operations aoe_debugfs_fops = {
185 .open = aoe_debugfs_open,
186 .read = seq_read,
187 .llseek = seq_lseek,
188 .release = single_release,
189};
e8866cf2
EC
190
191static void
192aoedisk_add_debugfs(struct aoedev *d)
193{
194 struct dentry *entry;
195 char *p;
196
197 if (aoe_debugfs_dir == NULL)
198 return;
199 p = strchr(d->gd->disk_name, '/');
200 if (p == NULL)
201 p = d->gd->disk_name;
202 else
203 p++;
204 BUG_ON(*p == '\0');
205 entry = debugfs_create_file(p, 0444, aoe_debugfs_dir, d,
206 &aoe_debugfs_fops);
207 if (IS_ERR_OR_NULL(entry)) {
208 pr_info("aoe: cannot create debugfs file for %s\n",
209 d->gd->disk_name);
210 return;
211 }
212 BUG_ON(d->debugfs);
213 d->debugfs = entry;
214}
215void
216aoedisk_rm_debugfs(struct aoedev *d)
217{
e8866cf2
EC
218 debugfs_remove(d->debugfs);
219 d->debugfs = NULL;
220}
221
4ca5224f 222static int
1da177e4
LT
223aoedisk_add_sysfs(struct aoedev *d)
224{
ed9e1982 225 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
1da177e4
LT
226}
227void
228aoedisk_rm_sysfs(struct aoedev *d)
229{
ed9e1982 230 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
1da177e4
LT
231}
232
233static int
94562c17 234aoeblk_open(struct block_device *bdev, fmode_t mode)
1da177e4 235{
94562c17 236 struct aoedev *d = bdev->bd_disk->private_data;
1da177e4
LT
237 ulong flags;
238
e52a2932
EC
239 if (!virt_addr_valid(d)) {
240 pr_crit("aoe: invalid device pointer in %s\n",
241 __func__);
242 WARN_ON(1);
243 return -ENODEV;
244 }
245 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
246 return -ENODEV;
247
2a48fc0a 248 mutex_lock(&aoeblk_mutex);
1da177e4 249 spin_lock_irqsave(&d->lock, flags);
e52a2932 250 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
1da177e4
LT
251 d->nopen++;
252 spin_unlock_irqrestore(&d->lock, flags);
2a48fc0a 253 mutex_unlock(&aoeblk_mutex);
1da177e4
LT
254 return 0;
255 }
256 spin_unlock_irqrestore(&d->lock, flags);
2a48fc0a 257 mutex_unlock(&aoeblk_mutex);
1da177e4
LT
258 return -ENODEV;
259}
260
db2a144b 261static void
94562c17 262aoeblk_release(struct gendisk *disk, fmode_t mode)
1da177e4 263{
94562c17 264 struct aoedev *d = disk->private_data;
1da177e4
LT
265 ulong flags;
266
1da177e4
LT
267 spin_lock_irqsave(&d->lock, flags);
268
5f7702fd 269 if (--d->nopen == 0) {
1da177e4
LT
270 spin_unlock_irqrestore(&d->lock, flags);
271 aoecmd_cfg(d->aoemajor, d->aoeminor);
db2a144b 272 return;
1da177e4
LT
273 }
274 spin_unlock_irqrestore(&d->lock, flags);
1da177e4
LT
275}
276
5a7bbad2 277static void
69cf2d85 278aoeblk_request(struct request_queue *q)
1da177e4
LT
279{
280 struct aoedev *d;
69cf2d85 281 struct request *rq;
1da177e4 282
69cf2d85 283 d = q->queuedata;
1da177e4 284 if ((d->flags & DEVFL_UP) == 0) {
027b180d 285 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
a12c93f0 286 d->aoemajor, d->aoeminor);
69cf2d85
EC
287 while ((rq = blk_peek_request(q))) {
288 blk_start_request(rq);
289 aoe_end_request(d, rq, 1);
290 }
5a7bbad2 291 return;
1da177e4 292 }
3ae1c24e 293 aoecmd_work(d);
1da177e4
LT
294}
295
1da177e4 296static int
a885c8c4 297aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1da177e4 298{
a885c8c4 299 struct aoedev *d = bdev->bd_disk->private_data;
1da177e4 300
1da177e4 301 if ((d->flags & DEVFL_UP) == 0) {
a12c93f0 302 printk(KERN_ERR "aoe: disk not up\n");
1da177e4
LT
303 return -ENODEV;
304 }
305
a885c8c4
CH
306 geo->cylinders = d->geo.cylinders;
307 geo->heads = d->geo.heads;
308 geo->sectors = d->geo.sectors;
309 return 0;
1da177e4
LT
310}
311
667be1e7
EC
312static int
313aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
314{
315 struct aoedev *d;
316
317 if (!arg)
318 return -EINVAL;
319
320 d = bdev->bd_disk->private_data;
321 if ((d->flags & DEVFL_UP) == 0) {
322 pr_err("aoe: disk not up\n");
323 return -ENODEV;
324 }
325
326 if (cmd == HDIO_GET_IDENTITY) {
327 if (!copy_to_user((void __user *) arg, &d->ident,
328 sizeof(d->ident)))
329 return 0;
330 return -EFAULT;
331 }
332
333 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
334 if (cmd != SG_IO)
335 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
336
337 return -ENOTTY;
338}
339
83d5cde4 340static const struct block_device_operations aoe_bdops = {
94562c17
AV
341 .open = aoeblk_open,
342 .release = aoeblk_release,
667be1e7 343 .ioctl = aoeblk_ioctl,
a885c8c4 344 .getgeo = aoeblk_getgeo,
1da177e4
LT
345 .owner = THIS_MODULE,
346};
347
348/* alloc_disk and add_disk can sleep */
349void
350aoeblk_gdalloc(void *vp)
351{
352 struct aoedev *d = vp;
353 struct gendisk *gd;
69cf2d85
EC
354 mempool_t *mp;
355 struct request_queue *q;
356 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
1da177e4 357 ulong flags;
e52a2932
EC
358 int late = 0;
359
360 spin_lock_irqsave(&d->lock, flags);
361 if (d->flags & DEVFL_GDALLOC
362 && !(d->flags & DEVFL_TKILL)
363 && !(d->flags & DEVFL_GD_NOW))
364 d->flags |= DEVFL_GD_NOW;
365 else
366 late = 1;
367 spin_unlock_irqrestore(&d->lock, flags);
368 if (late)
369 return;
1da177e4
LT
370
371 gd = alloc_disk(AOE_PARTITIONS);
372 if (gd == NULL) {
69cf2d85 373 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
6bb6285f 374 d->aoemajor, d->aoeminor);
43cbe2cb 375 goto err;
1da177e4
LT
376 }
377
69cf2d85
EC
378 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
379 buf_pool_cache);
380 if (mp == NULL) {
1d75981a 381 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
6bb6285f 382 d->aoemajor, d->aoeminor);
43cbe2cb 383 goto err_disk;
1da177e4 384 }
69cf2d85
EC
385 q = blk_init_queue(aoeblk_request, &d->lock);
386 if (q == NULL) {
387 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
388 d->aoemajor, d->aoeminor);
0a41409c 389 goto err_mempool;
69cf2d85 390 }
1da177e4 391
43cbe2cb 392 spin_lock_irqsave(&d->lock, flags);
e52a2932
EC
393 WARN_ON(!(d->flags & DEVFL_GD_NOW));
394 WARN_ON(!(d->flags & DEVFL_GDALLOC));
395 WARN_ON(d->flags & DEVFL_TKILL);
396 WARN_ON(d->gd);
397 WARN_ON(d->flags & DEVFL_UP);
30e2bc08 398 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
dc3b17cc
JK
399 q->backing_dev_info->name = "aoe";
400 q->backing_dev_info->ra_pages = READ_AHEAD / PAGE_SIZE;
69cf2d85
EC
401 d->bufpool = mp;
402 d->blkq = gd->queue = q;
403 q->queuedata = d;
404 d->gd = gd;
aa304fde
EC
405 if (aoe_maxsectors)
406 blk_queue_max_hw_sectors(q, aoe_maxsectors);
1da177e4 407 gd->major = AOE_MAJOR;
0c966214 408 gd->first_minor = d->sysminor;
1da177e4
LT
409 gd->fops = &aoe_bdops;
410 gd->private_data = d;
80795aef 411 set_capacity(gd, d->ssize);
68e0d42f 412 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
1da177e4
LT
413 d->aoemajor, d->aoeminor);
414
3ae1c24e 415 d->flags &= ~DEVFL_GDALLOC;
1da177e4
LT
416 d->flags |= DEVFL_UP;
417
418 spin_unlock_irqrestore(&d->lock, flags);
419
420 add_disk(gd);
421 aoedisk_add_sysfs(d);
e8866cf2 422 aoedisk_add_debugfs(d);
e52a2932
EC
423
424 spin_lock_irqsave(&d->lock, flags);
425 WARN_ON(!(d->flags & DEVFL_GD_NOW));
426 d->flags &= ~DEVFL_GD_NOW;
427 spin_unlock_irqrestore(&d->lock, flags);
43cbe2cb
AM
428 return;
429
430err_mempool:
0a41409c 431 mempool_destroy(mp);
43cbe2cb
AM
432err_disk:
433 put_disk(gd);
434err:
435 spin_lock_irqsave(&d->lock, flags);
e52a2932
EC
436 d->flags &= ~DEVFL_GD_NOW;
437 schedule_work(&d->work);
43cbe2cb 438 spin_unlock_irqrestore(&d->lock, flags);
1da177e4
LT
439}
440
441void
442aoeblk_exit(void)
443{
190519cd
EC
444 debugfs_remove_recursive(aoe_debugfs_dir);
445 aoe_debugfs_dir = NULL;
1da177e4
LT
446 kmem_cache_destroy(buf_pool_cache);
447}
448
449int __init
450aoeblk_init(void)
451{
20c2df83 452 buf_pool_cache = kmem_cache_create("aoe_bufs",
1da177e4 453 sizeof(struct buf),
20c2df83 454 0, 0, NULL);
1da177e4
LT
455 if (buf_pool_cache == NULL)
456 return -ENOMEM;
190519cd
EC
457 aoe_debugfs_dir = debugfs_create_dir("aoe", NULL);
458 if (IS_ERR_OR_NULL(aoe_debugfs_dir)) {
459 pr_info("aoe: cannot create debugfs directory\n");
460 aoe_debugfs_dir = NULL;
461 }
1da177e4
LT
462 return 0;
463}
464