1 /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
4 * block device routines
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/backing-dev.h>
11 #include <linux/ioctl.h>
12 #include <linux/genhd.h>
13 #include <linux/netdevice.h>
16 static struct kmem_cache
*buf_pool_cache
;
18 static ssize_t
aoedisk_show_state(struct device
*dev
,
19 struct device_attribute
*attr
, char *page
)
21 struct gendisk
*disk
= dev_to_disk(dev
);
22 struct aoedev
*d
= disk
->private_data
;
24 return snprintf(page
, PAGE_SIZE
,
26 (d
->flags
& DEVFL_UP
) ? "up" : "down",
27 (d
->flags
& DEVFL_KICKME
) ? ",kickme" :
28 (d
->nopen
&& !(d
->flags
& DEVFL_UP
)) ? ",closewait" : "");
29 /* I'd rather see nopen exported so we can ditch closewait */
31 static ssize_t
aoedisk_show_mac(struct device
*dev
,
32 struct device_attribute
*attr
, char *page
)
34 struct gendisk
*disk
= dev_to_disk(dev
);
35 struct aoedev
*d
= disk
->private_data
;
36 struct aoetgt
*t
= d
->targets
[0];
39 return snprintf(page
, PAGE_SIZE
, "none\n");
40 return snprintf(page
, PAGE_SIZE
, "%pm\n", t
->addr
);
42 static ssize_t
aoedisk_show_netif(struct device
*dev
,
43 struct device_attribute
*attr
, char *page
)
45 struct gendisk
*disk
= dev_to_disk(dev
);
46 struct aoedev
*d
= disk
->private_data
;
47 struct net_device
*nds
[8], **nd
, **nnd
, **ne
;
48 struct aoetgt
**t
, **te
;
49 struct aoeif
*ifp
, *e
;
52 memset(nds
, 0, sizeof nds
);
54 ne
= nd
+ ARRAY_SIZE(nds
);
57 for (; t
< te
&& *t
; t
++) {
60 for (; ifp
< e
&& ifp
->nd
; ifp
++) {
61 for (nnd
= nds
; nnd
< nd
; nnd
++)
64 if (nnd
== nd
&& nd
!= ne
)
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");
79 /* firmware version */
80 static ssize_t
aoedisk_show_fwver(struct device
*dev
,
81 struct device_attribute
*attr
, char *page
)
83 struct gendisk
*disk
= dev_to_disk(dev
);
84 struct aoedev
*d
= disk
->private_data
;
86 return snprintf(page
, PAGE_SIZE
, "0x%04x\n", (unsigned int) d
->fw_ver
);
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
},
94 .show
= aoedisk_show_fwver
,
97 static struct attribute
*aoe_attrs
[] = {
100 &dev_attr_netif
.attr
,
101 &dev_attr_firmware_version
.attr
,
105 static const struct attribute_group attr_group
= {
110 aoedisk_add_sysfs(struct aoedev
*d
)
112 return sysfs_create_group(&disk_to_dev(d
->gd
)->kobj
, &attr_group
);
115 aoedisk_rm_sysfs(struct aoedev
*d
)
117 sysfs_remove_group(&disk_to_dev(d
->gd
)->kobj
, &attr_group
);
121 aoeblk_open(struct block_device
*bdev
, fmode_t mode
)
123 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
126 spin_lock_irqsave(&d
->lock
, flags
);
127 if (d
->flags
& DEVFL_UP
) {
129 spin_unlock_irqrestore(&d
->lock
, flags
);
132 spin_unlock_irqrestore(&d
->lock
, flags
);
137 aoeblk_release(struct gendisk
*disk
, fmode_t mode
)
139 struct aoedev
*d
= disk
->private_data
;
142 spin_lock_irqsave(&d
->lock
, flags
);
144 if (--d
->nopen
== 0) {
145 spin_unlock_irqrestore(&d
->lock
, flags
);
146 aoecmd_cfg(d
->aoemajor
, d
->aoeminor
);
149 spin_unlock_irqrestore(&d
->lock
, flags
);
155 aoeblk_make_request(struct request_queue
*q
, struct bio
*bio
)
157 struct sk_buff_head queue
;
162 blk_queue_bounce(q
, &bio
);
165 printk(KERN_ERR
"aoe: bio is NULL\n");
169 d
= bio
->bi_bdev
->bd_disk
->private_data
;
171 printk(KERN_ERR
"aoe: bd_disk->private_data is NULL\n");
173 bio_endio(bio
, -ENXIO
);
175 } else if (bio_rw_flagged(bio
, BIO_RW_BARRIER
)) {
176 bio_endio(bio
, -EOPNOTSUPP
);
178 } else if (bio
->bi_io_vec
== NULL
) {
179 printk(KERN_ERR
"aoe: bi_io_vec is NULL\n");
181 bio_endio(bio
, -ENXIO
);
184 buf
= mempool_alloc(d
->bufpool
, GFP_NOIO
);
186 printk(KERN_INFO
"aoe: buf allocation failure\n");
187 bio_endio(bio
, -ENOMEM
);
190 memset(buf
, 0, sizeof(*buf
));
191 INIT_LIST_HEAD(&buf
->bufs
);
192 buf
->stime
= jiffies
;
194 buf
->resid
= bio
->bi_size
;
195 buf
->sector
= bio
->bi_sector
;
196 buf
->bv
= &bio
->bi_io_vec
[bio
->bi_idx
];
197 buf
->bv_resid
= buf
->bv
->bv_len
;
198 WARN_ON(buf
->bv_resid
== 0);
199 buf
->bv_off
= buf
->bv
->bv_offset
;
201 spin_lock_irqsave(&d
->lock
, flags
);
203 if ((d
->flags
& DEVFL_UP
) == 0) {
204 printk(KERN_INFO
"aoe: device %ld.%d is not up\n",
205 d
->aoemajor
, d
->aoeminor
);
206 spin_unlock_irqrestore(&d
->lock
, flags
);
207 mempool_free(buf
, d
->bufpool
);
208 bio_endio(bio
, -ENXIO
);
212 list_add_tail(&buf
->bufs
, &d
->bufq
);
215 __skb_queue_head_init(&queue
);
216 skb_queue_splice_init(&d
->sendq
, &queue
);
218 spin_unlock_irqrestore(&d
->lock
, flags
);
225 aoeblk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
227 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
229 if ((d
->flags
& DEVFL_UP
) == 0) {
230 printk(KERN_ERR
"aoe: disk not up\n");
234 geo
->cylinders
= d
->geo
.cylinders
;
235 geo
->heads
= d
->geo
.heads
;
236 geo
->sectors
= d
->geo
.sectors
;
240 static const struct block_device_operations aoe_bdops
= {
242 .release
= aoeblk_release
,
243 .getgeo
= aoeblk_getgeo
,
244 .owner
= THIS_MODULE
,
247 /* alloc_disk and add_disk can sleep */
249 aoeblk_gdalloc(void *vp
)
251 struct aoedev
*d
= vp
;
255 gd
= alloc_disk(AOE_PARTITIONS
);
258 "aoe: cannot allocate disk structure for %ld.%d\n",
259 d
->aoemajor
, d
->aoeminor
);
263 d
->bufpool
= mempool_create_slab_pool(MIN_BUFS
, buf_pool_cache
);
264 if (d
->bufpool
== NULL
) {
265 printk(KERN_ERR
"aoe: cannot allocate bufpool for %ld.%d\n",
266 d
->aoemajor
, d
->aoeminor
);
270 d
->blkq
= blk_alloc_queue(GFP_KERNEL
);
273 blk_queue_make_request(d
->blkq
, aoeblk_make_request
);
274 d
->blkq
->backing_dev_info
.name
= "aoe";
275 if (bdi_init(&d
->blkq
->backing_dev_info
))
277 spin_lock_irqsave(&d
->lock
, flags
);
278 gd
->major
= AOE_MAJOR
;
279 gd
->first_minor
= d
->sysminor
* AOE_PARTITIONS
;
280 gd
->fops
= &aoe_bdops
;
281 gd
->private_data
= d
;
282 set_capacity(gd
, d
->ssize
);
283 snprintf(gd
->disk_name
, sizeof gd
->disk_name
, "etherd/e%ld.%d",
284 d
->aoemajor
, d
->aoeminor
);
288 d
->flags
&= ~DEVFL_GDALLOC
;
289 d
->flags
|= DEVFL_UP
;
291 spin_unlock_irqrestore(&d
->lock
, flags
);
294 aoedisk_add_sysfs(d
);
298 blk_cleanup_queue(d
->blkq
);
301 mempool_destroy(d
->bufpool
);
305 spin_lock_irqsave(&d
->lock
, flags
);
306 d
->flags
&= ~DEVFL_GDALLOC
;
307 spin_unlock_irqrestore(&d
->lock
, flags
);
313 kmem_cache_destroy(buf_pool_cache
);
319 buf_pool_cache
= kmem_cache_create("aoe_bufs",
322 if (buf_pool_cache
== NULL
)