]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/block/aoe/aoeblk.c
block_device_operations->release() should return void
[mirror_ubuntu-zesty-kernel.git] / drivers / block / aoe / aoeblk.c
1 /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
2 /*
3 * aoeblk.c
4 * block device routines
5 */
6
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 <scsi/sg.h>
21 #include "aoe.h"
22
23 static DEFINE_MUTEX(aoeblk_mutex);
24 static struct kmem_cache *buf_pool_cache;
25
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");
31
32 static ssize_t aoedisk_show_state(struct device *dev,
33 struct device_attribute *attr, char *page)
34 {
35 struct gendisk *disk = dev_to_disk(dev);
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",
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 */
44 }
45 static ssize_t aoedisk_show_mac(struct device *dev,
46 struct device_attribute *attr, char *page)
47 {
48 struct gendisk *disk = dev_to_disk(dev);
49 struct aoedev *d = disk->private_data;
50 struct aoetgt *t = d->targets[0];
51
52 if (t == NULL)
53 return snprintf(page, PAGE_SIZE, "none\n");
54 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
55 }
56 static ssize_t aoedisk_show_netif(struct device *dev,
57 struct device_attribute *attr, char *page)
58 {
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;
64 char *p;
65
66 memset(nds, 0, sizeof nds);
67 nd = nds;
68 ne = nd + ARRAY_SIZE(nds);
69 t = d->targets;
70 te = t + d->ntargets;
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 }
82
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;
92 }
93 /* firmware version */
94 static ssize_t aoedisk_show_fwver(struct device *dev,
95 struct device_attribute *attr, char *page)
96 {
97 struct gendisk *disk = dev_to_disk(dev);
98 struct aoedev *d = disk->private_data;
99
100 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
101 }
102 static 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 }
110
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,
117 };
118 static DEVICE_ATTR(payload, S_IRUGO, aoedisk_show_payload, NULL);
119
120 static struct attribute *aoe_attrs[] = {
121 &dev_attr_state.attr,
122 &dev_attr_mac.attr,
123 &dev_attr_netif.attr,
124 &dev_attr_firmware_version.attr,
125 &dev_attr_payload.attr,
126 NULL,
127 };
128
129 static const struct attribute_group attr_group = {
130 .attrs = aoe_attrs,
131 };
132
133 static int
134 aoedisk_add_sysfs(struct aoedev *d)
135 {
136 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
137 }
138 void
139 aoedisk_rm_sysfs(struct aoedev *d)
140 {
141 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
142 }
143
144 static int
145 aoeblk_open(struct block_device *bdev, fmode_t mode)
146 {
147 struct aoedev *d = bdev->bd_disk->private_data;
148 ulong flags;
149
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
159 mutex_lock(&aoeblk_mutex);
160 spin_lock_irqsave(&d->lock, flags);
161 if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
162 d->nopen++;
163 spin_unlock_irqrestore(&d->lock, flags);
164 mutex_unlock(&aoeblk_mutex);
165 return 0;
166 }
167 spin_unlock_irqrestore(&d->lock, flags);
168 mutex_unlock(&aoeblk_mutex);
169 return -ENODEV;
170 }
171
172 static void
173 aoeblk_release(struct gendisk *disk, fmode_t mode)
174 {
175 struct aoedev *d = disk->private_data;
176 ulong flags;
177
178 spin_lock_irqsave(&d->lock, flags);
179
180 if (--d->nopen == 0) {
181 spin_unlock_irqrestore(&d->lock, flags);
182 aoecmd_cfg(d->aoemajor, d->aoeminor);
183 return;
184 }
185 spin_unlock_irqrestore(&d->lock, flags);
186 }
187
188 static void
189 aoeblk_request(struct request_queue *q)
190 {
191 struct aoedev *d;
192 struct request *rq;
193
194 d = q->queuedata;
195 if ((d->flags & DEVFL_UP) == 0) {
196 pr_info_ratelimited("aoe: device %ld.%d is not up\n",
197 d->aoemajor, d->aoeminor);
198 while ((rq = blk_peek_request(q))) {
199 blk_start_request(rq);
200 aoe_end_request(d, rq, 1);
201 }
202 return;
203 }
204 aoecmd_work(d);
205 }
206
207 static int
208 aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
209 {
210 struct aoedev *d = bdev->bd_disk->private_data;
211
212 if ((d->flags & DEVFL_UP) == 0) {
213 printk(KERN_ERR "aoe: disk not up\n");
214 return -ENODEV;
215 }
216
217 geo->cylinders = d->geo.cylinders;
218 geo->heads = d->geo.heads;
219 geo->sectors = d->geo.sectors;
220 return 0;
221 }
222
223 static int
224 aoeblk_ioctl(struct block_device *bdev, fmode_t mode, uint cmd, ulong arg)
225 {
226 struct aoedev *d;
227
228 if (!arg)
229 return -EINVAL;
230
231 d = bdev->bd_disk->private_data;
232 if ((d->flags & DEVFL_UP) == 0) {
233 pr_err("aoe: disk not up\n");
234 return -ENODEV;
235 }
236
237 if (cmd == HDIO_GET_IDENTITY) {
238 if (!copy_to_user((void __user *) arg, &d->ident,
239 sizeof(d->ident)))
240 return 0;
241 return -EFAULT;
242 }
243
244 /* udev calls scsi_id, which uses SG_IO, resulting in noise */
245 if (cmd != SG_IO)
246 pr_info("aoe: unknown ioctl 0x%x\n", cmd);
247
248 return -ENOTTY;
249 }
250
251 static const struct block_device_operations aoe_bdops = {
252 .open = aoeblk_open,
253 .release = aoeblk_release,
254 .ioctl = aoeblk_ioctl,
255 .getgeo = aoeblk_getgeo,
256 .owner = THIS_MODULE,
257 };
258
259 /* alloc_disk and add_disk can sleep */
260 void
261 aoeblk_gdalloc(void *vp)
262 {
263 struct aoedev *d = vp;
264 struct gendisk *gd;
265 mempool_t *mp;
266 struct request_queue *q;
267 enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
268 ulong flags;
269 int late = 0;
270
271 spin_lock_irqsave(&d->lock, flags);
272 if (d->flags & DEVFL_GDALLOC
273 && !(d->flags & DEVFL_TKILL)
274 && !(d->flags & DEVFL_GD_NOW))
275 d->flags |= DEVFL_GD_NOW;
276 else
277 late = 1;
278 spin_unlock_irqrestore(&d->lock, flags);
279 if (late)
280 return;
281
282 gd = alloc_disk(AOE_PARTITIONS);
283 if (gd == NULL) {
284 pr_err("aoe: cannot allocate disk structure for %ld.%d\n",
285 d->aoemajor, d->aoeminor);
286 goto err;
287 }
288
289 mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab,
290 buf_pool_cache);
291 if (mp == NULL) {
292 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
293 d->aoemajor, d->aoeminor);
294 goto err_disk;
295 }
296 q = blk_init_queue(aoeblk_request, &d->lock);
297 if (q == NULL) {
298 pr_err("aoe: cannot allocate block queue for %ld.%d\n",
299 d->aoemajor, d->aoeminor);
300 goto err_mempool;
301 }
302
303 spin_lock_irqsave(&d->lock, flags);
304 WARN_ON(!(d->flags & DEVFL_GD_NOW));
305 WARN_ON(!(d->flags & DEVFL_GDALLOC));
306 WARN_ON(d->flags & DEVFL_TKILL);
307 WARN_ON(d->gd);
308 WARN_ON(d->flags & DEVFL_UP);
309 blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
310 q->backing_dev_info.name = "aoe";
311 q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
312 d->bufpool = mp;
313 d->blkq = gd->queue = q;
314 q->queuedata = d;
315 d->gd = gd;
316 if (aoe_maxsectors)
317 blk_queue_max_hw_sectors(q, aoe_maxsectors);
318 gd->major = AOE_MAJOR;
319 gd->first_minor = d->sysminor;
320 gd->fops = &aoe_bdops;
321 gd->private_data = d;
322 set_capacity(gd, d->ssize);
323 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
324 d->aoemajor, d->aoeminor);
325
326 d->flags &= ~DEVFL_GDALLOC;
327 d->flags |= DEVFL_UP;
328
329 spin_unlock_irqrestore(&d->lock, flags);
330
331 add_disk(gd);
332 aoedisk_add_sysfs(d);
333
334 spin_lock_irqsave(&d->lock, flags);
335 WARN_ON(!(d->flags & DEVFL_GD_NOW));
336 d->flags &= ~DEVFL_GD_NOW;
337 spin_unlock_irqrestore(&d->lock, flags);
338 return;
339
340 err_mempool:
341 mempool_destroy(mp);
342 err_disk:
343 put_disk(gd);
344 err:
345 spin_lock_irqsave(&d->lock, flags);
346 d->flags &= ~DEVFL_GD_NOW;
347 schedule_work(&d->work);
348 spin_unlock_irqrestore(&d->lock, flags);
349 }
350
351 void
352 aoeblk_exit(void)
353 {
354 kmem_cache_destroy(buf_pool_cache);
355 }
356
357 int __init
358 aoeblk_init(void)
359 {
360 buf_pool_cache = kmem_cache_create("aoe_bufs",
361 sizeof(struct buf),
362 0, 0, NULL);
363 if (buf_pool_cache == NULL)
364 return -ENOMEM;
365
366 return 0;
367 }
368