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