]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/block/aoe/aoeblk.c
block: autoconvert trivial BKL users to private mutex
[net-next-2.6.git] / drivers / block / aoe / aoeblk.c
CommitLineData
52e112b3 1/* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
1da177e4
LT
2/*
3 * aoeblk.c
4 * block device routines
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
43cbe2cb 9#include <linux/backing-dev.h>
1da177e4
LT
10#include <linux/fs.h>
11#include <linux/ioctl.h>
5a0e3ad6 12#include <linux/slab.h>
1da177e4
LT
13#include <linux/genhd.h>
14#include <linux/netdevice.h>
2a48fc0a 15#include <linux/mutex.h>
1da177e4
LT
16#include "aoe.h"
17
2a48fc0a 18static DEFINE_MUTEX(aoeblk_mutex);
e18b890b 19static struct kmem_cache *buf_pool_cache;
1da177e4 20
edfaa7c3
KS
21static ssize_t aoedisk_show_state(struct device *dev,
22 struct device_attribute *attr, char *page)
1da177e4 23{
edfaa7c3 24 struct gendisk *disk = dev_to_disk(dev);
1da177e4
LT
25 struct aoedev *d = disk->private_data;
26
27 return snprintf(page, PAGE_SIZE,
28 "%s%s\n",
29 (d->flags & DEVFL_UP) ? "up" : "down",
68e0d42f 30 (d->flags & DEVFL_KICKME) ? ",kickme" :
3ae1c24e
EC
31 (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : "");
32 /* I'd rather see nopen exported so we can ditch closewait */
1da177e4 33}
edfaa7c3
KS
34static ssize_t aoedisk_show_mac(struct device *dev,
35 struct device_attribute *attr, char *page)
1da177e4 36{
edfaa7c3 37 struct gendisk *disk = dev_to_disk(dev);
1da177e4 38 struct aoedev *d = disk->private_data;
68e0d42f 39 struct aoetgt *t = d->targets[0];
1da177e4 40
68e0d42f
EC
41 if (t == NULL)
42 return snprintf(page, PAGE_SIZE, "none\n");
411c41ee 43 return snprintf(page, PAGE_SIZE, "%pm\n", t->addr);
1da177e4 44}
edfaa7c3
KS
45static ssize_t aoedisk_show_netif(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
EC
50 struct net_device *nds[8], **nd, **nnd, **ne;
51 struct aoetgt **t, **te;
52 struct aoeif *ifp, *e;
53 char *p;
54
55 memset(nds, 0, sizeof nds);
56 nd = nds;
57 ne = nd + ARRAY_SIZE(nds);
58 t = d->targets;
59 te = t + NTARGETS;
60 for (; t < te && *t; t++) {
61 ifp = (*t)->ifs;
62 e = ifp + NAOEIFS;
63 for (; ifp < e && ifp->nd; ifp++) {
64 for (nnd = nds; nnd < nd; nnd++)
65 if (*nnd == ifp->nd)
66 break;
67 if (nnd == nd && nd != ne)
68 *nd++ = ifp->nd;
69 }
70 }
1da177e4 71
68e0d42f
EC
72 ne = nd;
73 nd = nds;
74 if (*nd == NULL)
75 return snprintf(page, PAGE_SIZE, "none\n");
76 for (p = page; nd < ne; nd++)
77 p += snprintf(p, PAGE_SIZE - (p-page), "%s%s",
78 p == page ? "" : ",", (*nd)->name);
79 p += snprintf(p, PAGE_SIZE - (p-page), "\n");
80 return p-page;
1da177e4 81}
4613ed27 82/* firmware version */
edfaa7c3
KS
83static ssize_t aoedisk_show_fwver(struct device *dev,
84 struct device_attribute *attr, char *page)
4613ed27 85{
edfaa7c3 86 struct gendisk *disk = dev_to_disk(dev);
4613ed27
EC
87 struct aoedev *d = disk->private_data;
88
89 return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver);
90}
1da177e4 91
edfaa7c3
KS
92static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL);
93static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL);
94static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL);
95static struct device_attribute dev_attr_firmware_version = {
01e8ef11 96 .attr = { .name = "firmware-version", .mode = S_IRUGO },
edfaa7c3 97 .show = aoedisk_show_fwver,
4613ed27 98};
1da177e4 99
4ca5224f 100static struct attribute *aoe_attrs[] = {
edfaa7c3
KS
101 &dev_attr_state.attr,
102 &dev_attr_mac.attr,
103 &dev_attr_netif.attr,
104 &dev_attr_firmware_version.attr,
105 NULL,
4ca5224f
GKH
106};
107
108static const struct attribute_group attr_group = {
109 .attrs = aoe_attrs,
110};
111
112static int
1da177e4
LT
113aoedisk_add_sysfs(struct aoedev *d)
114{
ed9e1982 115 return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group);
1da177e4
LT
116}
117void
118aoedisk_rm_sysfs(struct aoedev *d)
119{
ed9e1982 120 sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group);
1da177e4
LT
121}
122
123static int
94562c17 124aoeblk_open(struct block_device *bdev, fmode_t mode)
1da177e4 125{
94562c17 126 struct aoedev *d = bdev->bd_disk->private_data;
1da177e4
LT
127 ulong flags;
128
2a48fc0a 129 mutex_lock(&aoeblk_mutex);
1da177e4
LT
130 spin_lock_irqsave(&d->lock, flags);
131 if (d->flags & DEVFL_UP) {
132 d->nopen++;
133 spin_unlock_irqrestore(&d->lock, flags);
2a48fc0a 134 mutex_unlock(&aoeblk_mutex);
1da177e4
LT
135 return 0;
136 }
137 spin_unlock_irqrestore(&d->lock, flags);
2a48fc0a 138 mutex_unlock(&aoeblk_mutex);
1da177e4
LT
139 return -ENODEV;
140}
141
142static int
94562c17 143aoeblk_release(struct gendisk *disk, fmode_t mode)
1da177e4 144{
94562c17 145 struct aoedev *d = disk->private_data;
1da177e4
LT
146 ulong flags;
147
1da177e4
LT
148 spin_lock_irqsave(&d->lock, flags);
149
5f7702fd 150 if (--d->nopen == 0) {
1da177e4
LT
151 spin_unlock_irqrestore(&d->lock, flags);
152 aoecmd_cfg(d->aoemajor, d->aoeminor);
153 return 0;
154 }
155 spin_unlock_irqrestore(&d->lock, flags);
156
157 return 0;
158}
159
160static int
165125e1 161aoeblk_make_request(struct request_queue *q, struct bio *bio)
1da177e4 162{
e9bb8fb0 163 struct sk_buff_head queue;
1da177e4
LT
164 struct aoedev *d;
165 struct buf *buf;
1da177e4
LT
166 ulong flags;
167
168 blk_queue_bounce(q, &bio);
169
68e0d42f
EC
170 if (bio == NULL) {
171 printk(KERN_ERR "aoe: bio is NULL\n");
172 BUG();
173 return 0;
174 }
1da177e4 175 d = bio->bi_bdev->bd_disk->private_data;
68e0d42f
EC
176 if (d == NULL) {
177 printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n");
178 BUG();
179 bio_endio(bio, -ENXIO);
180 return 0;
7b6d91da 181 } else if (bio->bi_rw & REQ_HARDBARRIER) {
18d8217b
EC
182 bio_endio(bio, -EOPNOTSUPP);
183 return 0;
68e0d42f
EC
184 } else if (bio->bi_io_vec == NULL) {
185 printk(KERN_ERR "aoe: bi_io_vec is NULL\n");
186 BUG();
187 bio_endio(bio, -ENXIO);
188 return 0;
189 }
1da177e4
LT
190 buf = mempool_alloc(d->bufpool, GFP_NOIO);
191 if (buf == NULL) {
a12c93f0 192 printk(KERN_INFO "aoe: buf allocation failure\n");
6712ecf8 193 bio_endio(bio, -ENOMEM);
1da177e4
LT
194 return 0;
195 }
196 memset(buf, 0, sizeof(*buf));
197 INIT_LIST_HEAD(&buf->bufs);
68e0d42f 198 buf->stime = jiffies;
1da177e4
LT
199 buf->bio = bio;
200 buf->resid = bio->bi_size;
201 buf->sector = bio->bi_sector;
392e4845 202 buf->bv = &bio->bi_io_vec[bio->bi_idx];
1da177e4 203 buf->bv_resid = buf->bv->bv_len;
68e0d42f
EC
204 WARN_ON(buf->bv_resid == 0);
205 buf->bv_off = buf->bv->bv_offset;
1da177e4
LT
206
207 spin_lock_irqsave(&d->lock, flags);
208
209 if ((d->flags & DEVFL_UP) == 0) {
1d75981a 210 printk(KERN_INFO "aoe: device %ld.%d is not up\n",
a12c93f0 211 d->aoemajor, d->aoeminor);
1da177e4
LT
212 spin_unlock_irqrestore(&d->lock, flags);
213 mempool_free(buf, d->bufpool);
6712ecf8 214 bio_endio(bio, -ENXIO);
1da177e4
LT
215 return 0;
216 }
217
218 list_add_tail(&buf->bufs, &d->bufq);
1da177e4 219
3ae1c24e 220 aoecmd_work(d);
e9bb8fb0
DM
221 __skb_queue_head_init(&queue);
222 skb_queue_splice_init(&d->sendq, &queue);
1da177e4
LT
223
224 spin_unlock_irqrestore(&d->lock, flags);
e9bb8fb0 225 aoenet_xmit(&queue);
3ae1c24e 226
1da177e4
LT
227 return 0;
228}
229
1da177e4 230static int
a885c8c4 231aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1da177e4 232{
a885c8c4 233 struct aoedev *d = bdev->bd_disk->private_data;
1da177e4 234
1da177e4 235 if ((d->flags & DEVFL_UP) == 0) {
a12c93f0 236 printk(KERN_ERR "aoe: disk not up\n");
1da177e4
LT
237 return -ENODEV;
238 }
239
a885c8c4
CH
240 geo->cylinders = d->geo.cylinders;
241 geo->heads = d->geo.heads;
242 geo->sectors = d->geo.sectors;
243 return 0;
1da177e4
LT
244}
245
83d5cde4 246static const struct block_device_operations aoe_bdops = {
94562c17
AV
247 .open = aoeblk_open,
248 .release = aoeblk_release,
a885c8c4 249 .getgeo = aoeblk_getgeo,
1da177e4
LT
250 .owner = THIS_MODULE,
251};
252
253/* alloc_disk and add_disk can sleep */
254void
255aoeblk_gdalloc(void *vp)
256{
257 struct aoedev *d = vp;
258 struct gendisk *gd;
259 ulong flags;
260
261 gd = alloc_disk(AOE_PARTITIONS);
262 if (gd == NULL) {
1d75981a
EC
263 printk(KERN_ERR
264 "aoe: cannot allocate disk structure for %ld.%d\n",
6bb6285f 265 d->aoemajor, d->aoeminor);
43cbe2cb 266 goto err;
1da177e4
LT
267 }
268
93d2341c 269 d->bufpool = mempool_create_slab_pool(MIN_BUFS, buf_pool_cache);
1da177e4 270 if (d->bufpool == NULL) {
1d75981a 271 printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n",
6bb6285f 272 d->aoemajor, d->aoeminor);
43cbe2cb 273 goto err_disk;
1da177e4
LT
274 }
275
7135a71b
EC
276 d->blkq = blk_alloc_queue(GFP_KERNEL);
277 if (!d->blkq)
43cbe2cb 278 goto err_mempool;
7135a71b 279 blk_queue_make_request(d->blkq, aoeblk_make_request);
d993831f 280 d->blkq->backing_dev_info.name = "aoe";
7135a71b
EC
281 if (bdi_init(&d->blkq->backing_dev_info))
282 goto err_blkq;
43cbe2cb 283 spin_lock_irqsave(&d->lock, flags);
1da177e4
LT
284 gd->major = AOE_MAJOR;
285 gd->first_minor = d->sysminor * AOE_PARTITIONS;
286 gd->fops = &aoe_bdops;
287 gd->private_data = d;
80795aef 288 set_capacity(gd, d->ssize);
68e0d42f 289 snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d",
1da177e4
LT
290 d->aoemajor, d->aoeminor);
291
7135a71b 292 gd->queue = d->blkq;
1da177e4 293 d->gd = gd;
3ae1c24e 294 d->flags &= ~DEVFL_GDALLOC;
1da177e4
LT
295 d->flags |= DEVFL_UP;
296
297 spin_unlock_irqrestore(&d->lock, flags);
298
299 add_disk(gd);
300 aoedisk_add_sysfs(d);
43cbe2cb
AM
301 return;
302
7135a71b
EC
303err_blkq:
304 blk_cleanup_queue(d->blkq);
305 d->blkq = NULL;
43cbe2cb
AM
306err_mempool:
307 mempool_destroy(d->bufpool);
308err_disk:
309 put_disk(gd);
310err:
311 spin_lock_irqsave(&d->lock, flags);
312 d->flags &= ~DEVFL_GDALLOC;
313 spin_unlock_irqrestore(&d->lock, flags);
1da177e4
LT
314}
315
316void
317aoeblk_exit(void)
318{
319 kmem_cache_destroy(buf_pool_cache);
320}
321
322int __init
323aoeblk_init(void)
324{
20c2df83 325 buf_pool_cache = kmem_cache_create("aoe_bufs",
1da177e4 326 sizeof(struct buf),
20c2df83 327 0, 0, NULL);
1da177e4
LT
328 if (buf_pool_cache == NULL)
329 return -ENOMEM;
330
331 return 0;
332}
333