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/slab.h>
13 #include <linux/genhd.h>
14 #include <linux/netdevice.h>
15 #include <linux/smp_lock.h>
18 static struct kmem_cache
*buf_pool_cache
;
20 static ssize_t
aoedisk_show_state(struct device
*dev
,
21 struct device_attribute
*attr
, char *page
)
23 struct gendisk
*disk
= dev_to_disk(dev
);
24 struct aoedev
*d
= disk
->private_data
;
26 return snprintf(page
, PAGE_SIZE
,
28 (d
->flags
& DEVFL_UP
) ? "up" : "down",
29 (d
->flags
& DEVFL_KICKME
) ? ",kickme" :
30 (d
->nopen
&& !(d
->flags
& DEVFL_UP
)) ? ",closewait" : "");
31 /* I'd rather see nopen exported so we can ditch closewait */
33 static ssize_t
aoedisk_show_mac(struct device
*dev
,
34 struct device_attribute
*attr
, char *page
)
36 struct gendisk
*disk
= dev_to_disk(dev
);
37 struct aoedev
*d
= disk
->private_data
;
38 struct aoetgt
*t
= d
->targets
[0];
41 return snprintf(page
, PAGE_SIZE
, "none\n");
42 return snprintf(page
, PAGE_SIZE
, "%pm\n", t
->addr
);
44 static ssize_t
aoedisk_show_netif(struct device
*dev
,
45 struct device_attribute
*attr
, char *page
)
47 struct gendisk
*disk
= dev_to_disk(dev
);
48 struct aoedev
*d
= disk
->private_data
;
49 struct net_device
*nds
[8], **nd
, **nnd
, **ne
;
50 struct aoetgt
**t
, **te
;
51 struct aoeif
*ifp
, *e
;
54 memset(nds
, 0, sizeof nds
);
56 ne
= nd
+ ARRAY_SIZE(nds
);
59 for (; t
< te
&& *t
; t
++) {
62 for (; ifp
< e
&& ifp
->nd
; ifp
++) {
63 for (nnd
= nds
; nnd
< nd
; nnd
++)
66 if (nnd
== nd
&& nd
!= ne
)
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");
81 /* firmware version */
82 static ssize_t
aoedisk_show_fwver(struct device
*dev
,
83 struct device_attribute
*attr
, char *page
)
85 struct gendisk
*disk
= dev_to_disk(dev
);
86 struct aoedev
*d
= disk
->private_data
;
88 return snprintf(page
, PAGE_SIZE
, "0x%04x\n", (unsigned int) d
->fw_ver
);
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
= {
95 .attr
= { .name
= "firmware-version", .mode
= S_IRUGO
},
96 .show
= aoedisk_show_fwver
,
99 static struct attribute
*aoe_attrs
[] = {
100 &dev_attr_state
.attr
,
102 &dev_attr_netif
.attr
,
103 &dev_attr_firmware_version
.attr
,
107 static const struct attribute_group attr_group
= {
112 aoedisk_add_sysfs(struct aoedev
*d
)
114 return sysfs_create_group(&disk_to_dev(d
->gd
)->kobj
, &attr_group
);
117 aoedisk_rm_sysfs(struct aoedev
*d
)
119 sysfs_remove_group(&disk_to_dev(d
->gd
)->kobj
, &attr_group
);
123 aoeblk_open(struct block_device
*bdev
, fmode_t mode
)
125 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
129 spin_lock_irqsave(&d
->lock
, flags
);
130 if (d
->flags
& DEVFL_UP
) {
132 spin_unlock_irqrestore(&d
->lock
, flags
);
136 spin_unlock_irqrestore(&d
->lock
, flags
);
142 aoeblk_release(struct gendisk
*disk
, fmode_t mode
)
144 struct aoedev
*d
= disk
->private_data
;
147 spin_lock_irqsave(&d
->lock
, flags
);
149 if (--d
->nopen
== 0) {
150 spin_unlock_irqrestore(&d
->lock
, flags
);
151 aoecmd_cfg(d
->aoemajor
, d
->aoeminor
);
154 spin_unlock_irqrestore(&d
->lock
, flags
);
160 aoeblk_make_request(struct request_queue
*q
, struct bio
*bio
)
162 struct sk_buff_head queue
;
167 blk_queue_bounce(q
, &bio
);
170 printk(KERN_ERR
"aoe: bio is NULL\n");
174 d
= bio
->bi_bdev
->bd_disk
->private_data
;
176 printk(KERN_ERR
"aoe: bd_disk->private_data is NULL\n");
178 bio_endio(bio
, -ENXIO
);
180 } else if (bio
->bi_rw
& REQ_HARDBARRIER
) {
181 bio_endio(bio
, -EOPNOTSUPP
);
183 } else if (bio
->bi_io_vec
== NULL
) {
184 printk(KERN_ERR
"aoe: bi_io_vec is NULL\n");
186 bio_endio(bio
, -ENXIO
);
189 buf
= mempool_alloc(d
->bufpool
, GFP_NOIO
);
191 printk(KERN_INFO
"aoe: buf allocation failure\n");
192 bio_endio(bio
, -ENOMEM
);
195 memset(buf
, 0, sizeof(*buf
));
196 INIT_LIST_HEAD(&buf
->bufs
);
197 buf
->stime
= jiffies
;
199 buf
->resid
= bio
->bi_size
;
200 buf
->sector
= bio
->bi_sector
;
201 buf
->bv
= &bio
->bi_io_vec
[bio
->bi_idx
];
202 buf
->bv_resid
= buf
->bv
->bv_len
;
203 WARN_ON(buf
->bv_resid
== 0);
204 buf
->bv_off
= buf
->bv
->bv_offset
;
206 spin_lock_irqsave(&d
->lock
, flags
);
208 if ((d
->flags
& DEVFL_UP
) == 0) {
209 printk(KERN_INFO
"aoe: device %ld.%d is not up\n",
210 d
->aoemajor
, d
->aoeminor
);
211 spin_unlock_irqrestore(&d
->lock
, flags
);
212 mempool_free(buf
, d
->bufpool
);
213 bio_endio(bio
, -ENXIO
);
217 list_add_tail(&buf
->bufs
, &d
->bufq
);
220 __skb_queue_head_init(&queue
);
221 skb_queue_splice_init(&d
->sendq
, &queue
);
223 spin_unlock_irqrestore(&d
->lock
, flags
);
230 aoeblk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
232 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
234 if ((d
->flags
& DEVFL_UP
) == 0) {
235 printk(KERN_ERR
"aoe: disk not up\n");
239 geo
->cylinders
= d
->geo
.cylinders
;
240 geo
->heads
= d
->geo
.heads
;
241 geo
->sectors
= d
->geo
.sectors
;
245 static const struct block_device_operations aoe_bdops
= {
247 .release
= aoeblk_release
,
248 .getgeo
= aoeblk_getgeo
,
249 .owner
= THIS_MODULE
,
252 /* alloc_disk and add_disk can sleep */
254 aoeblk_gdalloc(void *vp
)
256 struct aoedev
*d
= vp
;
260 gd
= alloc_disk(AOE_PARTITIONS
);
263 "aoe: cannot allocate disk structure for %ld.%d\n",
264 d
->aoemajor
, d
->aoeminor
);
268 d
->bufpool
= mempool_create_slab_pool(MIN_BUFS
, buf_pool_cache
);
269 if (d
->bufpool
== NULL
) {
270 printk(KERN_ERR
"aoe: cannot allocate bufpool for %ld.%d\n",
271 d
->aoemajor
, d
->aoeminor
);
275 d
->blkq
= blk_alloc_queue(GFP_KERNEL
);
278 blk_queue_make_request(d
->blkq
, aoeblk_make_request
);
279 d
->blkq
->backing_dev_info
.name
= "aoe";
280 if (bdi_init(&d
->blkq
->backing_dev_info
))
282 spin_lock_irqsave(&d
->lock
, flags
);
283 gd
->major
= AOE_MAJOR
;
284 gd
->first_minor
= d
->sysminor
* AOE_PARTITIONS
;
285 gd
->fops
= &aoe_bdops
;
286 gd
->private_data
= d
;
287 set_capacity(gd
, d
->ssize
);
288 snprintf(gd
->disk_name
, sizeof gd
->disk_name
, "etherd/e%ld.%d",
289 d
->aoemajor
, d
->aoeminor
);
293 d
->flags
&= ~DEVFL_GDALLOC
;
294 d
->flags
|= DEVFL_UP
;
296 spin_unlock_irqrestore(&d
->lock
, flags
);
299 aoedisk_add_sysfs(d
);
303 blk_cleanup_queue(d
->blkq
);
306 mempool_destroy(d
->bufpool
);
310 spin_lock_irqsave(&d
->lock
, flags
);
311 d
->flags
&= ~DEVFL_GDALLOC
;
312 spin_unlock_irqrestore(&d
->lock
, flags
);
318 kmem_cache_destroy(buf_pool_cache
);
324 buf_pool_cache
= kmem_cache_create("aoe_bufs",
327 if (buf_pool_cache
== NULL
)