1 /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
4 * block device routines
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
10 #include <linux/ioctl.h>
11 #include <linux/genhd.h>
12 #include <linux/netdevice.h>
15 static kmem_cache_t
*buf_pool_cache
;
17 static ssize_t
aoedisk_show_state(struct gendisk
* disk
, char *page
)
19 struct aoedev
*d
= disk
->private_data
;
21 return snprintf(page
, PAGE_SIZE
,
23 (d
->flags
& DEVFL_UP
) ? "up" : "down",
24 (d
->flags
& DEVFL_PAUSE
) ? ",paused" :
25 (d
->nopen
&& !(d
->flags
& DEVFL_UP
)) ? ",closewait" : "");
26 /* I'd rather see nopen exported so we can ditch closewait */
28 static ssize_t
aoedisk_show_mac(struct gendisk
* disk
, char *page
)
30 struct aoedev
*d
= disk
->private_data
;
32 return snprintf(page
, PAGE_SIZE
, "%012llx\n",
33 (unsigned long long)mac_addr(d
->addr
));
35 static ssize_t
aoedisk_show_netif(struct gendisk
* disk
, char *page
)
37 struct aoedev
*d
= disk
->private_data
;
39 return snprintf(page
, PAGE_SIZE
, "%s\n", d
->ifp
->name
);
41 /* firmware version */
42 static ssize_t
aoedisk_show_fwver(struct gendisk
* disk
, char *page
)
44 struct aoedev
*d
= disk
->private_data
;
46 return snprintf(page
, PAGE_SIZE
, "0x%04x\n", (unsigned int) d
->fw_ver
);
49 static struct disk_attribute disk_attr_state
= {
50 .attr
= {.name
= "state", .mode
= S_IRUGO
},
51 .show
= aoedisk_show_state
53 static struct disk_attribute disk_attr_mac
= {
54 .attr
= {.name
= "mac", .mode
= S_IRUGO
},
55 .show
= aoedisk_show_mac
57 static struct disk_attribute disk_attr_netif
= {
58 .attr
= {.name
= "netif", .mode
= S_IRUGO
},
59 .show
= aoedisk_show_netif
61 static struct disk_attribute disk_attr_fwver
= {
62 .attr
= {.name
= "firmware-version", .mode
= S_IRUGO
},
63 .show
= aoedisk_show_fwver
66 static struct attribute
*aoe_attrs
[] = {
67 &disk_attr_state
.attr
,
69 &disk_attr_netif
.attr
,
70 &disk_attr_fwver
.attr
,
73 static const struct attribute_group attr_group
= {
78 aoedisk_add_sysfs(struct aoedev
*d
)
80 return sysfs_create_group(&d
->gd
->kobj
, &attr_group
);
83 aoedisk_rm_sysfs(struct aoedev
*d
)
85 sysfs_remove_group(&d
->gd
->kobj
, &attr_group
);
89 aoeblk_open(struct inode
*inode
, struct file
*filp
)
94 d
= inode
->i_bdev
->bd_disk
->private_data
;
96 spin_lock_irqsave(&d
->lock
, flags
);
97 if (d
->flags
& DEVFL_UP
) {
99 spin_unlock_irqrestore(&d
->lock
, flags
);
102 spin_unlock_irqrestore(&d
->lock
, flags
);
107 aoeblk_release(struct inode
*inode
, struct file
*filp
)
112 d
= inode
->i_bdev
->bd_disk
->private_data
;
114 spin_lock_irqsave(&d
->lock
, flags
);
116 if (--d
->nopen
== 0) {
117 spin_unlock_irqrestore(&d
->lock
, flags
);
118 aoecmd_cfg(d
->aoemajor
, d
->aoeminor
);
121 spin_unlock_irqrestore(&d
->lock
, flags
);
127 aoeblk_make_request(request_queue_t
*q
, struct bio
*bio
)
134 blk_queue_bounce(q
, &bio
);
136 d
= bio
->bi_bdev
->bd_disk
->private_data
;
137 buf
= mempool_alloc(d
->bufpool
, GFP_NOIO
);
139 printk(KERN_INFO
"aoe: buf allocation failure\n");
140 bio_endio(bio
, bio
->bi_size
, -ENOMEM
);
143 memset(buf
, 0, sizeof(*buf
));
144 INIT_LIST_HEAD(&buf
->bufs
);
145 buf
->start_time
= jiffies
;
147 buf
->resid
= bio
->bi_size
;
148 buf
->sector
= bio
->bi_sector
;
149 buf
->bv
= &bio
->bi_io_vec
[bio
->bi_idx
];
150 WARN_ON(buf
->bv
->bv_len
== 0);
151 buf
->bv_resid
= buf
->bv
->bv_len
;
152 buf
->bufaddr
= page_address(buf
->bv
->bv_page
) + buf
->bv
->bv_offset
;
154 spin_lock_irqsave(&d
->lock
, flags
);
156 if ((d
->flags
& DEVFL_UP
) == 0) {
157 printk(KERN_INFO
"aoe: device %ld.%ld is not up\n",
158 d
->aoemajor
, d
->aoeminor
);
159 spin_unlock_irqrestore(&d
->lock
, flags
);
160 mempool_free(buf
, d
->bufpool
);
161 bio_endio(bio
, bio
->bi_size
, -ENXIO
);
165 list_add_tail(&buf
->bufs
, &d
->bufq
);
169 d
->sendq_hd
= d
->sendq_tl
= NULL
;
171 spin_unlock_irqrestore(&d
->lock
, flags
);
178 aoeblk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
180 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
182 if ((d
->flags
& DEVFL_UP
) == 0) {
183 printk(KERN_ERR
"aoe: disk not up\n");
187 geo
->cylinders
= d
->geo
.cylinders
;
188 geo
->heads
= d
->geo
.heads
;
189 geo
->sectors
= d
->geo
.sectors
;
193 static struct block_device_operations aoe_bdops
= {
195 .release
= aoeblk_release
,
196 .getgeo
= aoeblk_getgeo
,
197 .owner
= THIS_MODULE
,
200 /* alloc_disk and add_disk can sleep */
202 aoeblk_gdalloc(void *vp
)
204 struct aoedev
*d
= vp
;
208 gd
= alloc_disk(AOE_PARTITIONS
);
210 printk(KERN_ERR
"aoe: cannot allocate disk structure for %ld.%ld\n",
211 d
->aoemajor
, d
->aoeminor
);
212 spin_lock_irqsave(&d
->lock
, flags
);
213 d
->flags
&= ~DEVFL_GDALLOC
;
214 spin_unlock_irqrestore(&d
->lock
, flags
);
218 d
->bufpool
= mempool_create_slab_pool(MIN_BUFS
, buf_pool_cache
);
219 if (d
->bufpool
== NULL
) {
220 printk(KERN_ERR
"aoe: cannot allocate bufpool for %ld.%ld\n",
221 d
->aoemajor
, d
->aoeminor
);
223 spin_lock_irqsave(&d
->lock
, flags
);
224 d
->flags
&= ~DEVFL_GDALLOC
;
225 spin_unlock_irqrestore(&d
->lock
, flags
);
229 spin_lock_irqsave(&d
->lock
, flags
);
230 blk_queue_make_request(&d
->blkq
, aoeblk_make_request
);
231 gd
->major
= AOE_MAJOR
;
232 gd
->first_minor
= d
->sysminor
* AOE_PARTITIONS
;
233 gd
->fops
= &aoe_bdops
;
234 gd
->private_data
= d
;
235 gd
->capacity
= d
->ssize
;
236 snprintf(gd
->disk_name
, sizeof gd
->disk_name
, "etherd/e%ld.%ld",
237 d
->aoemajor
, d
->aoeminor
);
239 gd
->queue
= &d
->blkq
;
241 d
->flags
&= ~DEVFL_GDALLOC
;
242 d
->flags
|= DEVFL_UP
;
244 spin_unlock_irqrestore(&d
->lock
, flags
);
247 aoedisk_add_sysfs(d
);
253 kmem_cache_destroy(buf_pool_cache
);
259 buf_pool_cache
= kmem_cache_create("aoe_bufs",
262 if (buf_pool_cache
== NULL
)