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