]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/block/aoe/aoeblk.c
mtd_blktrans_ops->release() should return void
[mirror_ubuntu-kernels.git] / drivers / block / aoe / aoeblk.c
CommitLineData
fea05a26 1/* Copyright (c) 2012 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>
667be1e7 20#include <scsi/sg.h>
1da177e4
LT
21#include "aoe.h"
22
2a48fc0a 23static DEFINE_MUTEX(aoeblk_mutex);
e18b890b 24static struct kmem_cache *buf_pool_cache;
1da177e4 25
aa304fde
EC
26/* GPFS needs a larger value than the default. */
27static int aoe_maxsectors;
28module_param(aoe_maxsectors, int, 0644);
29MODULE_PARM_DESC(aoe_maxsectors,
30 "When nonzero, set the maximum number of sectors per I/O request");
31
edfaa7c3
KS
32static ssize_t aoedisk_show_state(struct device *dev,
33 struct device_attribute *attr, char *page)
1da177e4 34{
edfaa7c3 35 struct gendisk *disk = dev_to_disk(dev);
1da177e4
LT
36 struct aoedev *d = disk->private_data;
37
38 return snprintf(page, PAGE_SIZE,
39 "%s%s\n",
40 (d->flags & DEVFL_UP) ? "up" : "down",
68e0d42f 41 (d->flags & DEVFL_KICKME) ? ",kickme" :
3ae1c24e
EC
42 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
43 /* I'd rather see nopen exported so we can ditch closewait */
1da177e4 44}
edfaa7c3
KS
45static ssize_t aoedisk_show_mac(struct device *dev,
46 struct device_attribute *attr, char *page)
1da177e4 47{
edfaa7c3 48 struct gendisk *disk = dev_to_disk(dev);
1da177e4 49 struct aoedev *d = disk->private_data;
68e0d42f 50 struct aoetgt *t = d->targets[0];
1da177e4 51
68e0d42f
EC
52 if (t == NULL)
53 return snprintf(page, PAGE_SIZE, "none\n");
411c41ee 54 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
1da177e4 55}
edfaa7c3
KS
56static ssize_t aoedisk_show_netif(struct device *dev,
57 struct device_attribute *attr, char *page)
1da177e4 58{
edfaa7c3 59 struct gendisk *disk = dev_to_disk(dev);
1da177e4 60 struct aoedev *d = disk->private_data;
68e0d42f
EC
61 struct net_device *nds[8], **nd, **nnd, **ne;
62 struct aoetgt **t, **te;
63 struct aoeif *ifp, *e;
64 char *p;
65
66 memset(nds, 0, sizeof nds);
67 nd = nds;
68 ne = nd + ARRAY_SIZE(nds);
69 t = d->targets;
71114ec4 70 te = t + d->ntargets;
68e0d42f
EC
71 for (; t < te && *t; t++) {
72 ifp = (*t)->ifs;
73 e = ifp + NAOEIFS;
74 for (; ifp < e && ifp->nd; ifp++) {
75 for (nnd = nds; nnd < nd; nnd++)
76 if (*nnd == ifp->nd)
77 break;
78 if (nnd == nd && nd != ne)
79 *nd++ = ifp->nd;
80 }
81 }
1da177e4 82
68e0d42f
EC
83 ne = nd;
84 nd = nds;
85 if (*nd == NULL)
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");
91 return p-page;
1da177e4 92}
4613ed27 93/* firmware version */
edfaa7c3
KS
94static ssize_t aoedisk_show_fwver(struct device *dev,
95 struct device_attribute *attr, char *page)
4613ed27 96{
edfaa7c3 97 struct gendisk *disk = dev_to_disk(dev);
4613ed27
EC
98 struct aoedev *d = disk->private_data;
99
100 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
101}
90a2508d
EC
102static ssize_t aoedisk_show_payload(struct device *dev,
103 struct device_attribute *attr, char *page)
104{
105 struct gendisk *disk = dev_to_disk(dev);
106 struct aoedev *d = disk->private_data;
107
108 return snprintf(page, PAGE_SIZE, "%lu\n", d->maxbcnt);
109}
1da177e4 110
edfaa7c3
KS
111static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
112static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
113static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
114static struct device_attribute dev_attr_firmware_version = {
01e8ef11 115 .attr = { .name = "firmware-version", .mode = S_IRUGO },
edfaa7c3 116 .show = aoedisk_show_fwver,
4613ed27 117};
90a2508d 118static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
1da177e4 119
4ca5224f 120static struct attribute *aoe_attrs[] = {
edfaa7c3
KS
121 &dev_attr_state.attr,
122 &dev_attr_mac.attr,
123 &dev_attr_netif.attr,
124 &dev_attr_firmware_version.attr,
90a2508d 125 &dev_attr_payload.attr,
edfaa7c3 126 NULL,
4ca5224f
GKH
127};
128
129static const struct attribute_group attr_group = {
130 .attrs = aoe_attrs,
131};
132
133static int
1da177e4
LT
134aoedisk_add_sysfs(struct aoedev *d)
135{
ed9e1982 136 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
1da177e4
LT
137}
138void
139aoedisk_rm_sysfs(struct aoedev *d)
140{
ed9e1982 141 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
1da177e4
LT
142}
143
144static int
94562c17 145aoeblk_open(struct block_device *bdev, fmode_t mode)
1da177e4 146{
94562c17 147 struct aoedev *d = bdev->bd_disk->private_data;
1da177e4
LT
148 ulong flags;
149
e52a2932
EC
150 if (!virt_addr_valid(d)) {
151 pr_crit("aoe: invalid device pointer in %s\n",
152 __func__);
153 WARN_ON(1);
154 return -ENODEV;
155 }
156 if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
157 return -ENODEV;
158
2a48fc0a 159 mutex_lock(&aoeblk_mutex);
1da177e4 160 spin_lock_irqsave(&d->lock, flags);
e52a2932 161 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
1da177e4
LT
162 d->nopen++;
163 spin_unlock_irqrestore(&d->lock, flags);
2a48fc0a 164 mutex_unlock(&aoeblk_mutex);
1da177e4
LT
165 return 0;
166 }
167 spin_unlock_irqrestore(&d->lock, flags);
2a48fc0a 168 mutex_unlock(&aoeblk_mutex);
1da177e4
LT
169 return -ENODEV;
170}
171
172static int
94562c17 173aoeblk_release(struct gendisk *disk, fmode_t mode)
1da177e4 174{
94562c17 175 struct aoedev *d = disk->private_data;
1da177e4
LT
176 ulong flags;
177
1da177e4
LT
178 spin_lock_irqsave(&d->lock, flags);
179
5f7702fd 180 if (--d->nopen == 0) {
1da177e4
LT
181 spin_unlock_irqrestore(&d->lock, flags);
182 aoecmd_cfg(d->aoemajor, d->aoeminor);
183 return 0;
184 }
185 spin_unlock_irqrestore(&d->lock, flags);
186
187 return 0;
188}
189
5a7bbad2 190static void
69cf2d85 191aoeblk_request(struct request_queue *q)
1da177e4
LT
192{
193 struct aoedev *d;
69cf2d85 194 struct request *rq;
1da177e4 195
69cf2d85 196 d = q->queuedata;
1da177e4 197 if ((d->flags & DEVFL_UP) == 0) {
027b180d 198 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
a12c93f0 199 d->aoemajor, d->aoeminor);
69cf2d85
EC
200 while ((rq = blk_peek_request(q))) {
201 blk_start_request(rq);
202 aoe_end_request(d, rq, 1);
203 }
5a7bbad2 204 return;
1da177e4 205 }
3ae1c24e 206 aoecmd_work(d);
1da177e4
LT
207}
208
1da177e4 209static int
a885c8c4 210aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1da177e4 211{
a885c8c4 212 struct aoedev *d = bdev->bd_disk->private_data;
1da177e4 213
1da177e4 214 if ((d->flags & DEVFL_UP) == 0) {
a12c93f0 215 printk(KERN_ERR "aoe: disk not up\n");
1da177e4
LT
216 return -ENODEV;
217 }
218
a885c8c4
CH
219 geo->cylinders = d->geo.cylinders;
220 geo->heads = d->geo.heads;
221 geo->sectors = d->geo.sectors;
222 return 0;
1da177e4
LT
223}
224
667be1e7
EC
225static int
226aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
227{
228 struct aoedev *d;
229
230 if (!arg)
231 return -EINVAL;
232
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;
237 }
238
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;
244 }
245
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);
249
250 return -ENOTTY;
251}
252
83d5cde4 253static const struct block_device_operations aoe_bdops = {
94562c17
AV
254 .open = aoeblk_open,
255 .release = aoeblk_release,
667be1e7 256 .ioctl = aoeblk_ioctl,
a885c8c4 257 .getgeo = aoeblk_getgeo,
1da177e4
LT
258 .owner = THIS_MODULE,
259};
260
261/* alloc_disk and add_disk can sleep */
262void
263aoeblk_gdalloc(void *vp)
264{
265 struct aoedev *d = vp;
266 struct gendisk *gd;
69cf2d85
EC
267 mempool_t *mp;
268 struct request_queue *q;
269 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
1da177e4 270 ulong flags;
e52a2932
EC
271 int late = 0;
272
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;
1da177e4
LT
283
284 gd = alloc_disk(AOE_PARTITIONS);
285 if (gd == NULL) {
69cf2d85 286 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
6bb6285f 287 d->aoemajor, d->aoeminor);
43cbe2cb 288 goto err;
1da177e4
LT
289 }
290
69cf2d85
EC
291 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
292 buf_pool_cache);
293 if (mp == NULL) {
1d75981a 294 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
6bb6285f 295 d->aoemajor, d->aoeminor);
43cbe2cb 296 goto err_disk;
1da177e4 297 }
69cf2d85
EC
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);
0a41409c 302 goto err_mempool;
69cf2d85 303 }
1da177e4 304
43cbe2cb 305 spin_lock_irqsave(&d->lock, flags);
e52a2932
EC
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);
0a41409c
EC
311 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
312 q->backing_dev_info.name = "aoe";
69cf2d85
EC
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;
aa304fde
EC
318 if (aoe_maxsectors)
319 blk_queue_max_hw_sectors(q, aoe_maxsectors);
1da177e4 320 gd->major = AOE_MAJOR;
0c966214 321 gd->first_minor = d->sysminor;
1da177e4
LT
322 gd->fops = &aoe_bdops;
323 gd->private_data = d;
80795aef 324 set_capacity(gd, d->ssize);
68e0d42f 325 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
1da177e4
LT
326 d->aoemajor, d->aoeminor);
327
3ae1c24e 328 d->flags &= ~DEVFL_GDALLOC;
1da177e4
LT
329 d->flags |= DEVFL_UP;
330
331 spin_unlock_irqrestore(&d->lock, flags);
332
333 add_disk(gd);
334 aoedisk_add_sysfs(d);
e52a2932
EC
335
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);
43cbe2cb
AM
340 return;
341
342err_mempool:
0a41409c 343 mempool_destroy(mp);
43cbe2cb
AM
344err_disk:
345 put_disk(gd);
346err:
347 spin_lock_irqsave(&d->lock, flags);
e52a2932
EC
348 d->flags &= ~DEVFL_GD_NOW;
349 schedule_work(&d->work);
43cbe2cb 350 spin_unlock_irqrestore(&d->lock, flags);
1da177e4
LT
351}
352
353void
354aoeblk_exit(void)
355{
356 kmem_cache_destroy(buf_pool_cache);
357}
358
359int __init
360aoeblk_init(void)
361{
20c2df83 362 buf_pool_cache = kmem_cache_create("aoe_bufs",
1da177e4 363 sizeof(struct buf),
20c2df83 364 0, 0, NULL);
1da177e4
LT
365 if (buf_pool_cache == NULL)
366 return -ENOMEM;
367
368 return 0;
369}
370