Commit | Line | Data |
---|---|---|
fea05a26 | 1 | /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */ |
1da177e4 LT |
2 | /* |
3 | * aoeblk.c | |
4 | * block device routines | |
5 | */ | |
6 | ||
027b180d | 7 | #include <linux/kernel.h> |
1da177e4 LT |
8 | #include <linux/hdreg.h> |
9 | #include <linux/blkdev.h> | |
43cbe2cb | 10 | #include <linux/backing-dev.h> |
1da177e4 LT |
11 | #include <linux/fs.h> |
12 | #include <linux/ioctl.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
027b180d | 14 | #include <linux/ratelimit.h> |
1da177e4 LT |
15 | #include <linux/genhd.h> |
16 | #include <linux/netdevice.h> | |
2a48fc0a | 17 | #include <linux/mutex.h> |
d5decd3b | 18 | #include <linux/export.h> |
1da177e4 LT |
19 | #include "aoe.h" |
20 | ||
2a48fc0a | 21 | static DEFINE_MUTEX(aoeblk_mutex); |
e18b890b | 22 | static struct kmem_cache *buf_pool_cache; |
1da177e4 | 23 | |
edfaa7c3 KS |
24 | static ssize_t aoedisk_show_state(struct device *dev, |
25 | struct device_attribute *attr, char *page) | |
1da177e4 | 26 | { |
edfaa7c3 | 27 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 LT |
28 | struct aoedev *d = disk->private_data; |
29 | ||
30 | return snprintf(page, PAGE_SIZE, | |
31 | "%s%s\n", | |
32 | (d->flags & DEVFL_UP) ? "up" : "down", | |
68e0d42f | 33 | (d->flags & DEVFL_KICKME) ? ",kickme" : |
3ae1c24e EC |
34 | (d->nopen && !(d->flags & DEVFL_UP)) ? ",closewait" : ""); |
35 | /* I'd rather see nopen exported so we can ditch closewait */ | |
1da177e4 | 36 | } |
edfaa7c3 KS |
37 | static ssize_t aoedisk_show_mac(struct device *dev, |
38 | struct device_attribute *attr, char *page) | |
1da177e4 | 39 | { |
edfaa7c3 | 40 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 | 41 | struct aoedev *d = disk->private_data; |
68e0d42f | 42 | struct aoetgt *t = d->targets[0]; |
1da177e4 | 43 | |
68e0d42f EC |
44 | if (t == NULL) |
45 | return snprintf(page, PAGE_SIZE, "none\n"); | |
411c41ee | 46 | return snprintf(page, PAGE_SIZE, "%pm\n", t->addr); |
1da177e4 | 47 | } |
edfaa7c3 KS |
48 | static ssize_t aoedisk_show_netif(struct device *dev, |
49 | struct device_attribute *attr, char *page) | |
1da177e4 | 50 | { |
edfaa7c3 | 51 | struct gendisk *disk = dev_to_disk(dev); |
1da177e4 | 52 | struct aoedev *d = disk->private_data; |
68e0d42f EC |
53 | struct net_device *nds[8], **nd, **nnd, **ne; |
54 | struct aoetgt **t, **te; | |
55 | struct aoeif *ifp, *e; | |
56 | char *p; | |
57 | ||
58 | memset(nds, 0, sizeof nds); | |
59 | nd = nds; | |
60 | ne = nd + ARRAY_SIZE(nds); | |
61 | t = d->targets; | |
62 | te = t + NTARGETS; | |
63 | for (; t < te && *t; t++) { | |
64 | ifp = (*t)->ifs; | |
65 | e = ifp + NAOEIFS; | |
66 | for (; ifp < e && ifp->nd; ifp++) { | |
67 | for (nnd = nds; nnd < nd; nnd++) | |
68 | if (*nnd == ifp->nd) | |
69 | break; | |
70 | if (nnd == nd && nd != ne) | |
71 | *nd++ = ifp->nd; | |
72 | } | |
73 | } | |
1da177e4 | 74 | |
68e0d42f EC |
75 | ne = nd; |
76 | nd = nds; | |
77 | if (*nd == NULL) | |
78 | return snprintf(page, PAGE_SIZE, "none\n"); | |
79 | for (p = page; nd < ne; nd++) | |
80 | p += snprintf(p, PAGE_SIZE - (p-page), "%s%s", | |
81 | p == page ? "" : ",", (*nd)->name); | |
82 | p += snprintf(p, PAGE_SIZE - (p-page), "\n"); | |
83 | return p-page; | |
1da177e4 | 84 | } |
4613ed27 | 85 | /* firmware version */ |
edfaa7c3 KS |
86 | static ssize_t aoedisk_show_fwver(struct device *dev, |
87 | struct device_attribute *attr, char *page) | |
4613ed27 | 88 | { |
edfaa7c3 | 89 | struct gendisk *disk = dev_to_disk(dev); |
4613ed27 EC |
90 | struct aoedev *d = disk->private_data; |
91 | ||
92 | return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); | |
93 | } | |
1da177e4 | 94 | |
edfaa7c3 KS |
95 | static DEVICE_ATTR(state, S_IRUGO, aoedisk_show_state, NULL); |
96 | static DEVICE_ATTR(mac, S_IRUGO, aoedisk_show_mac, NULL); | |
97 | static DEVICE_ATTR(netif, S_IRUGO, aoedisk_show_netif, NULL); | |
98 | static struct device_attribute dev_attr_firmware_version = { | |
01e8ef11 | 99 | .attr = { .name = "firmware-version", .mode = S_IRUGO }, |
edfaa7c3 | 100 | .show = aoedisk_show_fwver, |
4613ed27 | 101 | }; |
1da177e4 | 102 | |
4ca5224f | 103 | static struct attribute *aoe_attrs[] = { |
edfaa7c3 KS |
104 | &dev_attr_state.attr, |
105 | &dev_attr_mac.attr, | |
106 | &dev_attr_netif.attr, | |
107 | &dev_attr_firmware_version.attr, | |
108 | NULL, | |
4ca5224f GKH |
109 | }; |
110 | ||
111 | static const struct attribute_group attr_group = { | |
112 | .attrs = aoe_attrs, | |
113 | }; | |
114 | ||
115 | static int | |
1da177e4 LT |
116 | aoedisk_add_sysfs(struct aoedev *d) |
117 | { | |
ed9e1982 | 118 | return sysfs_create_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
1da177e4 LT |
119 | } |
120 | void | |
121 | aoedisk_rm_sysfs(struct aoedev *d) | |
122 | { | |
ed9e1982 | 123 | sysfs_remove_group(&disk_to_dev(d->gd)->kobj, &attr_group); |
1da177e4 LT |
124 | } |
125 | ||
126 | static int | |
94562c17 | 127 | aoeblk_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 128 | { |
94562c17 | 129 | struct aoedev *d = bdev->bd_disk->private_data; |
1da177e4 LT |
130 | ulong flags; |
131 | ||
2a48fc0a | 132 | mutex_lock(&aoeblk_mutex); |
1da177e4 LT |
133 | spin_lock_irqsave(&d->lock, flags); |
134 | if (d->flags & DEVFL_UP) { | |
135 | d->nopen++; | |
136 | spin_unlock_irqrestore(&d->lock, flags); | |
2a48fc0a | 137 | mutex_unlock(&aoeblk_mutex); |
1da177e4 LT |
138 | return 0; |
139 | } | |
140 | spin_unlock_irqrestore(&d->lock, flags); | |
2a48fc0a | 141 | mutex_unlock(&aoeblk_mutex); |
1da177e4 LT |
142 | return -ENODEV; |
143 | } | |
144 | ||
145 | static int | |
94562c17 | 146 | aoeblk_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 147 | { |
94562c17 | 148 | struct aoedev *d = disk->private_data; |
1da177e4 LT |
149 | ulong flags; |
150 | ||
1da177e4 LT |
151 | spin_lock_irqsave(&d->lock, flags); |
152 | ||
5f7702fd | 153 | if (--d->nopen == 0) { |
1da177e4 LT |
154 | spin_unlock_irqrestore(&d->lock, flags); |
155 | aoecmd_cfg(d->aoemajor, d->aoeminor); | |
156 | return 0; | |
157 | } | |
158 | spin_unlock_irqrestore(&d->lock, flags); | |
159 | ||
160 | return 0; | |
161 | } | |
162 | ||
5a7bbad2 | 163 | static void |
69cf2d85 | 164 | aoeblk_request(struct request_queue *q) |
1da177e4 LT |
165 | { |
166 | struct aoedev *d; | |
69cf2d85 | 167 | struct request *rq; |
1da177e4 | 168 | |
69cf2d85 | 169 | d = q->queuedata; |
1da177e4 | 170 | if ((d->flags & DEVFL_UP) == 0) { |
027b180d | 171 | pr_info_ratelimited("aoe: device %ld.%d is not up\n", |
a12c93f0 | 172 | d->aoemajor, d->aoeminor); |
69cf2d85 EC |
173 | while ((rq = blk_peek_request(q))) { |
174 | blk_start_request(rq); | |
175 | aoe_end_request(d, rq, 1); | |
176 | } | |
5a7bbad2 | 177 | return; |
1da177e4 | 178 | } |
3ae1c24e | 179 | aoecmd_work(d); |
1da177e4 LT |
180 | } |
181 | ||
1da177e4 | 182 | static int |
a885c8c4 | 183 | aoeblk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 184 | { |
a885c8c4 | 185 | struct aoedev *d = bdev->bd_disk->private_data; |
1da177e4 | 186 | |
1da177e4 | 187 | if ((d->flags & DEVFL_UP) == 0) { |
a12c93f0 | 188 | printk(KERN_ERR "aoe: disk not up\n"); |
1da177e4 LT |
189 | return -ENODEV; |
190 | } | |
191 | ||
a885c8c4 CH |
192 | geo->cylinders = d->geo.cylinders; |
193 | geo->heads = d->geo.heads; | |
194 | geo->sectors = d->geo.sectors; | |
195 | return 0; | |
1da177e4 LT |
196 | } |
197 | ||
83d5cde4 | 198 | static const struct block_device_operations aoe_bdops = { |
94562c17 AV |
199 | .open = aoeblk_open, |
200 | .release = aoeblk_release, | |
a885c8c4 | 201 | .getgeo = aoeblk_getgeo, |
1da177e4 LT |
202 | .owner = THIS_MODULE, |
203 | }; | |
204 | ||
205 | /* alloc_disk and add_disk can sleep */ | |
206 | void | |
207 | aoeblk_gdalloc(void *vp) | |
208 | { | |
209 | struct aoedev *d = vp; | |
210 | struct gendisk *gd; | |
69cf2d85 EC |
211 | mempool_t *mp; |
212 | struct request_queue *q; | |
213 | enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, }; | |
1da177e4 LT |
214 | ulong flags; |
215 | ||
216 | gd = alloc_disk(AOE_PARTITIONS); | |
217 | if (gd == NULL) { | |
69cf2d85 | 218 | pr_err("aoe: cannot allocate disk structure for %ld.%d\n", |
6bb6285f | 219 | d->aoemajor, d->aoeminor); |
43cbe2cb | 220 | goto err; |
1da177e4 LT |
221 | } |
222 | ||
69cf2d85 EC |
223 | mp = mempool_create(MIN_BUFS, mempool_alloc_slab, mempool_free_slab, |
224 | buf_pool_cache); | |
225 | if (mp == NULL) { | |
1d75981a | 226 | printk(KERN_ERR "aoe: cannot allocate bufpool for %ld.%d\n", |
6bb6285f | 227 | d->aoemajor, d->aoeminor); |
43cbe2cb | 228 | goto err_disk; |
1da177e4 | 229 | } |
69cf2d85 EC |
230 | q = blk_init_queue(aoeblk_request, &d->lock); |
231 | if (q == NULL) { | |
232 | pr_err("aoe: cannot allocate block queue for %ld.%d\n", | |
233 | d->aoemajor, d->aoeminor); | |
234 | mempool_destroy(mp); | |
235 | goto err_disk; | |
236 | } | |
1da177e4 | 237 | |
7135a71b EC |
238 | d->blkq = blk_alloc_queue(GFP_KERNEL); |
239 | if (!d->blkq) | |
43cbe2cb | 240 | goto err_mempool; |
d993831f | 241 | d->blkq->backing_dev_info.name = "aoe"; |
7135a71b EC |
242 | if (bdi_init(&d->blkq->backing_dev_info)) |
243 | goto err_blkq; | |
43cbe2cb | 244 | spin_lock_irqsave(&d->lock, flags); |
3d5b0605 | 245 | blk_queue_max_hw_sectors(d->blkq, BLK_DEF_MAX_SECTORS); |
69cf2d85 EC |
246 | q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE; |
247 | d->bufpool = mp; | |
248 | d->blkq = gd->queue = q; | |
249 | q->queuedata = d; | |
250 | d->gd = gd; | |
1da177e4 LT |
251 | gd->major = AOE_MAJOR; |
252 | gd->first_minor = d->sysminor * AOE_PARTITIONS; | |
253 | gd->fops = &aoe_bdops; | |
254 | gd->private_data = d; | |
80795aef | 255 | set_capacity(gd, d->ssize); |
68e0d42f | 256 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%d", |
1da177e4 LT |
257 | d->aoemajor, d->aoeminor); |
258 | ||
3ae1c24e | 259 | d->flags &= ~DEVFL_GDALLOC; |
1da177e4 LT |
260 | d->flags |= DEVFL_UP; |
261 | ||
262 | spin_unlock_irqrestore(&d->lock, flags); | |
263 | ||
264 | add_disk(gd); | |
265 | aoedisk_add_sysfs(d); | |
43cbe2cb AM |
266 | return; |
267 | ||
7135a71b EC |
268 | err_blkq: |
269 | blk_cleanup_queue(d->blkq); | |
270 | d->blkq = NULL; | |
43cbe2cb AM |
271 | err_mempool: |
272 | mempool_destroy(d->bufpool); | |
273 | err_disk: | |
274 | put_disk(gd); | |
275 | err: | |
276 | spin_lock_irqsave(&d->lock, flags); | |
277 | d->flags &= ~DEVFL_GDALLOC; | |
278 | spin_unlock_irqrestore(&d->lock, flags); | |
1da177e4 LT |
279 | } |
280 | ||
281 | void | |
282 | aoeblk_exit(void) | |
283 | { | |
284 | kmem_cache_destroy(buf_pool_cache); | |
285 | } | |
286 | ||
287 | int __init | |
288 | aoeblk_init(void) | |
289 | { | |
20c2df83 | 290 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
1da177e4 | 291 | sizeof(struct buf), |
20c2df83 | 292 | 0, 0, NULL); |
1da177e4 LT |
293 | if (buf_pool_cache == NULL) |
294 | return -ENOMEM; | |
295 | ||
296 | return 0; | |
297 | } | |
298 |