]>
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> | |
12 | #include <linux/genhd.h> | |
13 | #include <linux/netdevice.h> | |
14 | #include "aoe.h" | |
15 | ||
e18b890b | 16 | static struct kmem_cache *buf_pool_cache; |
1da177e4 | 17 | |
edfaa7c3 KS |
18 | static ssize_t aoedisk_show_state(struct device *dev, |
19 | struct device_attribute *attr, char *page) | |
1da177e4 | 20 | { |
edfaa7c3 | 21 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 LT |
22 | struct aoedev *d = disk->private_data; |
23 | ||
24 | return snprintf(page, PAGE_SIZE, | |
25 | "%s%s\n", | |
26 | (d->flags & DEVFL_UP) ? "up" : "down", | |
68e0d42f | 27 | (d->flags & DEVFL_KICKME) ? ",kickme" : |
3ae1c24e EC |
28 | (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); |
29 | /* I'd rather see nopen exported so we can ditch closewait */ | |
1da177e4 | 30 | } |
edfaa7c3 KS |
31 | static ssize_t aoedisk_show_mac(struct device *dev, |
32 | struct device_attribute *attr, char *page) | |
1da177e4 | 33 | { |
edfaa7c3 | 34 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 | 35 | struct aoedev *d = disk->private_data; |
68e0d42f | 36 | struct aoetgt *t = d->targets[0]; |
1da177e4 | 37 | |
68e0d42f EC |
38 | if (t == NULL) |
39 | return snprintf(page, PAGE_SIZE, "none\n"); | |
1eb0da4c | 40 | return snprintf(page, PAGE_SIZE, "%012llx\n", mac_addr(t->addr)); |
1da177e4 | 41 | } |
edfaa7c3 KS |
42 | static ssize_t aoedisk_show_netif(struct device *dev, |
43 | struct device_attribute *attr, char *page) | |
1da177e4 | 44 | { |
edfaa7c3 | 45 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 | 46 | struct aoedev *d = disk->private_data; |
68e0d42f EC |
47 | struct net_device *nds[8], **nd, **nnd, **ne; |
48 | struct aoetgt **t, **te; | |
49 | struct aoeif *ifp, *e; | |
50 | char *p; | |
51 | ||
52 | memset(nds, 0, sizeof nds); | |
53 | nd = nds; | |
54 | ne = nd + ARRAY_SIZE(nds); | |
55 | t = d->targets; | |
56 | te = t + NTARGETS; | |
57 | for (; t < te && *t; t++) { | |
58 | ifp = (*t)->ifs; | |
59 | e = ifp + NAOEIFS; | |
60 | for (; ifp < e && ifp->nd; ifp++) { | |
61 | for (nnd = nds; nnd < nd; nnd++) | |
62 | if (*nnd == ifp->nd) | |
63 | break; | |
64 | if (nnd == nd && nd != ne) | |
65 | *nd++ = ifp->nd; | |
66 | } | |
67 | } | |
1da177e4 | 68 | |
68e0d42f EC |
69 | ne = nd; |
70 | nd = nds; | |
71 | if (*nd == NULL) | |
72 | return snprintf(page, PAGE_SIZE, "none\n"); | |
73 | for (p = page; nd < ne; nd++) | |
74 | p += snprintf(p, PAGE_SIZE - (p-page), "%s%s", | |
75 | p == page ? "" : ",", (*nd)->name); | |
76 | p += snprintf(p, PAGE_SIZE - (p-page), "\n"); | |
77 | return p-page; | |
1da177e4 | 78 | } |
4613ed27 | 79 | /* firmware version */ |
edfaa7c3 KS |
80 | static ssize_t aoedisk_show_fwver(struct device *dev, |
81 | struct device_attribute *attr, char *page) | |
4613ed27 | 82 | { |
edfaa7c3 | 83 | struct gendisk *disk = dev_to_disk(dev); |
4613ed27 EC |
84 | struct aoedev *d = disk->private_data; |
85 | ||
86 | return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); | |
87 | } | |
1da177e4 | 88 | |
edfaa7c3 KS |
89 | static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL); |
90 | static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL); | |
91 | static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL); | |
92 | static struct device_attribute dev_attr_firmware_version = { | |
93 | .attr = { .name = "firmware-version", .mode = S_IRUGO, .owner = THIS_MODULE }, | |
94 | .show = aoedisk_show_fwver, | |
4613ed27 | 95 | }; |
1da177e4 | 96 | |
4ca5224f | 97 | static struct attribute *aoe_attrs[] = { |
edfaa7c3 KS |
98 | &dev_attr_state.attr, |
99 | &dev_attr_mac.attr, | |
100 | &dev_attr_netif.attr, | |
101 | &dev_attr_firmware_version.attr, | |
102 | NULL, | |
4ca5224f GKH |
103 | }; |
104 | ||
105 | static const struct attribute_group attr_group = { | |
106 | .attrs = aoe_attrs, | |
107 | }; | |
108 | ||
109 | static int | |
1da177e4 LT |
110 | aoedisk_add_sysfs(struct aoedev *d) |
111 | { | |
ed9e1982 | 112 | return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
1da177e4 LT |
113 | } |
114 | void | |
115 | aoedisk_rm_sysfs(struct aoedev *d) | |
116 | { | |
ed9e1982 | 117 | sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
1da177e4 LT |
118 | } |
119 | ||
120 | static int | |
121 | aoeblk_open(struct inode *inode, struct file *filp) | |
122 | { | |
123 | struct aoedev *d; | |
124 | ulong flags; | |
125 | ||
126 | d = inode->i_bdev->bd_disk->private_data; | |
127 | ||
128 | spin_lock_irqsave(&d->lock, flags); | |
129 | if (d->flags & DEVFL_UP) { | |
130 | d->nopen++; | |
131 | spin_unlock_irqrestore(&d->lock, flags); | |
132 | return 0; | |
133 | } | |
134 | spin_unlock_irqrestore(&d->lock, flags); | |
135 | return -ENODEV; | |
136 | } | |
137 | ||
138 | static int | |
139 | aoeblk_release(struct inode *inode, struct file *filp) | |
140 | { | |
141 | struct aoedev *d; | |
142 | ulong flags; | |
143 | ||
144 | d = inode->i_bdev->bd_disk->private_data; | |
145 | ||
146 | spin_lock_irqsave(&d->lock, flags); | |
147 | ||
5f7702fd | 148 | if (--d->nopen == 0) { |
1da177e4 LT |
149 | spin_unlock_irqrestore(&d->lock, flags); |
150 | aoecmd_cfg(d->aoemajor, d->aoeminor); | |
151 | return 0; | |
152 | } | |
153 | spin_unlock_irqrestore(&d->lock, flags); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
158 | static int | |
165125e1 | 159 | aoeblk_make_request(struct request_queue *q, struct bio *bio) |
1da177e4 | 160 | { |
e9bb8fb0 | 161 | struct sk_buff_head queue; |
1da177e4 LT |
162 | struct aoedev *d; |
163 | struct buf *buf; | |
1da177e4 LT |
164 | ulong flags; |
165 | ||
166 | blk_queue_bounce(q, &bio); | |
167 | ||
68e0d42f EC |
168 | if (bio == NULL) { |
169 | printk(KERN_ERR "aoe: bio is NULL\n"); | |
170 | BUG(); | |
171 | return 0; | |
172 | } | |
1da177e4 | 173 | d = bio->bi_bdev->bd_disk->private_data; |
68e0d42f EC |
174 | if (d == NULL) { |
175 | printk(KERN_ERR "aoe: bd_disk->private_data is NULL\n"); | |
176 | BUG(); | |
177 | bio_endio(bio, -ENXIO); | |
178 | return 0; | |
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 | ||
241 | static struct block_device_operations aoe_bdops = { | |
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 | ||
1da177e4 | 271 | blk_queue_make_request(&d->blkq, aoeblk_make_request); |
43cbe2cb AM |
272 | if (bdi_init(&d->blkq.backing_dev_info)) |
273 | goto err_mempool; | |
274 | spin_lock_irqsave(&d->lock, flags); | |
1da177e4 LT |
275 | gd->major = AOE_MAJOR; |
276 | gd->first_minor = d->sysminor * AOE_PARTITIONS; | |
277 | gd->fops = &aoe_bdops; | |
278 | gd->private_data = d; | |
80795aef | 279 | set_capacity(gd, d->ssize); |
68e0d42f | 280 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d", |
1da177e4 LT |
281 | d->aoemajor, d->aoeminor); |
282 | ||
283 | gd->queue = &d->blkq; | |
284 | d->gd = gd; | |
3ae1c24e | 285 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
286 | d->flags |= DEVFL_UP; |
287 | ||
288 | spin_unlock_irqrestore(&d->lock, flags); | |
289 | ||
290 | add_disk(gd); | |
291 | aoedisk_add_sysfs(d); | |
43cbe2cb AM |
292 | return; |
293 | ||
294 | err_mempool: | |
295 | mempool_destroy(d->bufpool); | |
296 | err_disk: | |
297 | put_disk(gd); | |
298 | err: | |
299 | spin_lock_irqsave(&d->lock, flags); | |
300 | d->flags &= ~DEVFL_GDALLOC; | |
301 | spin_unlock_irqrestore(&d->lock, flags); | |
1da177e4 LT |
302 | } |
303 | ||
304 | void | |
305 | aoeblk_exit(void) | |
306 | { | |
307 | kmem_cache_destroy(buf_pool_cache); | |
308 | } | |
309 | ||
310 | int __init | |
311 | aoeblk_init(void) | |
312 | { | |
20c2df83 | 313 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
1da177e4 | 314 | sizeof(struct buf), |
20c2df83 | 315 | 0, 0, NULL); |
1da177e4 LT |
316 | if (buf_pool_cache == NULL) |
317 | return -ENOMEM; | |
318 | ||
319 | return 0; | |
320 | } | |
321 |