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>
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 gendisk
* disk
, char *page
)
20 struct aoedev
*d
= disk
->private_data
;
22 return snprintf(page
, PAGE_SIZE
,
24 (d
->flags
& DEVFL_UP
) ? "up" : "down",
25 (d
->flags
& DEVFL_PAUSE
) ? ",paused" :
26 (d
->nopen
&& !(d
->flags
& DEVFL_UP
)) ? ",closewait" : "");
27 /* I'd rather see nopen exported so we can ditch closewait */
29 static ssize_t
aoedisk_show_mac(struct gendisk
* disk
, char *page
)
31 struct aoedev
*d
= disk
->private_data
;
33 return snprintf(page
, PAGE_SIZE
, "%012llx\n",
34 (unsigned long long)mac_addr(d
->addr
));
36 static ssize_t
aoedisk_show_netif(struct gendisk
* disk
, char *page
)
38 struct aoedev
*d
= disk
->private_data
;
40 return snprintf(page
, PAGE_SIZE
, "%s\n", d
->ifp
->name
);
42 /* firmware version */
43 static ssize_t
aoedisk_show_fwver(struct gendisk
* disk
, char *page
)
45 struct aoedev
*d
= disk
->private_data
;
47 return snprintf(page
, PAGE_SIZE
, "0x%04x\n", (unsigned int) d
->fw_ver
);
50 static struct disk_attribute disk_attr_state
= {
51 .attr
= {.name
= "state", .mode
= S_IRUGO
},
52 .show
= aoedisk_show_state
54 static struct disk_attribute disk_attr_mac
= {
55 .attr
= {.name
= "mac", .mode
= S_IRUGO
},
56 .show
= aoedisk_show_mac
58 static struct disk_attribute disk_attr_netif
= {
59 .attr
= {.name
= "netif", .mode
= S_IRUGO
},
60 .show
= aoedisk_show_netif
62 static struct disk_attribute disk_attr_fwver
= {
63 .attr
= {.name
= "firmware-version", .mode
= S_IRUGO
},
64 .show
= aoedisk_show_fwver
67 static struct attribute
*aoe_attrs
[] = {
68 &disk_attr_state
.attr
,
70 &disk_attr_netif
.attr
,
71 &disk_attr_fwver
.attr
,
75 static const struct attribute_group attr_group
= {
80 aoedisk_add_sysfs(struct aoedev
*d
)
82 return sysfs_create_group(&d
->gd
->kobj
, &attr_group
);
85 aoedisk_rm_sysfs(struct aoedev
*d
)
87 sysfs_remove_group(&d
->gd
->kobj
, &attr_group
);
91 aoeblk_open(struct inode
*inode
, struct file
*filp
)
96 d
= inode
->i_bdev
->bd_disk
->private_data
;
98 spin_lock_irqsave(&d
->lock
, flags
);
99 if (d
->flags
& DEVFL_UP
) {
101 spin_unlock_irqrestore(&d
->lock
, flags
);
104 spin_unlock_irqrestore(&d
->lock
, flags
);
109 aoeblk_release(struct inode
*inode
, struct file
*filp
)
114 d
= inode
->i_bdev
->bd_disk
->private_data
;
116 spin_lock_irqsave(&d
->lock
, flags
);
118 if (--d
->nopen
== 0) {
119 spin_unlock_irqrestore(&d
->lock
, flags
);
120 aoecmd_cfg(d
->aoemajor
, d
->aoeminor
);
123 spin_unlock_irqrestore(&d
->lock
, flags
);
129 aoeblk_make_request(struct request_queue
*q
, struct bio
*bio
)
136 blk_queue_bounce(q
, &bio
);
138 d
= bio
->bi_bdev
->bd_disk
->private_data
;
139 buf
= mempool_alloc(d
->bufpool
, GFP_NOIO
);
141 printk(KERN_INFO
"aoe: buf allocation failure\n");
142 bio_endio(bio
, -ENOMEM
);
145 memset(buf
, 0, sizeof(*buf
));
146 INIT_LIST_HEAD(&buf
->bufs
);
147 buf
->start_time
= jiffies
;
149 buf
->resid
= bio
->bi_size
;
150 buf
->sector
= bio
->bi_sector
;
151 buf
->bv
= &bio
->bi_io_vec
[bio
->bi_idx
];
152 WARN_ON(buf
->bv
->bv_len
== 0);
153 buf
->bv_resid
= buf
->bv
->bv_len
;
154 buf
->bufaddr
= page_address(buf
->bv
->bv_page
) + buf
->bv
->bv_offset
;
156 spin_lock_irqsave(&d
->lock
, flags
);
158 if ((d
->flags
& DEVFL_UP
) == 0) {
159 printk(KERN_INFO
"aoe: device %ld.%ld is not up\n",
160 d
->aoemajor
, d
->aoeminor
);
161 spin_unlock_irqrestore(&d
->lock
, flags
);
162 mempool_free(buf
, d
->bufpool
);
163 bio_endio(bio
, -ENXIO
);
167 list_add_tail(&buf
->bufs
, &d
->bufq
);
171 d
->sendq_hd
= d
->sendq_tl
= NULL
;
173 spin_unlock_irqrestore(&d
->lock
, flags
);
180 aoeblk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
182 struct aoedev
*d
= bdev
->bd_disk
->private_data
;
184 if ((d
->flags
& DEVFL_UP
) == 0) {
185 printk(KERN_ERR
"aoe: disk not up\n");
189 geo
->cylinders
= d
->geo
.cylinders
;
190 geo
->heads
= d
->geo
.heads
;
191 geo
->sectors
= d
->geo
.sectors
;
195 static struct block_device_operations aoe_bdops
= {
197 .release
= aoeblk_release
,
198 .getgeo
= aoeblk_getgeo
,
199 .owner
= THIS_MODULE
,
202 /* alloc_disk and add_disk can sleep */
204 aoeblk_gdalloc(void *vp
)
206 struct aoedev
*d
= vp
;
210 gd
= alloc_disk(AOE_PARTITIONS
);
212 printk(KERN_ERR
"aoe: cannot allocate disk structure for %ld.%ld\n",
213 d
->aoemajor
, d
->aoeminor
);
217 d
->bufpool
= mempool_create_slab_pool(MIN_BUFS
, buf_pool_cache
);
218 if (d
->bufpool
== NULL
) {
219 printk(KERN_ERR
"aoe: cannot allocate bufpool for %ld.%ld\n",
220 d
->aoemajor
, d
->aoeminor
);
224 blk_queue_make_request(&d
->blkq
, aoeblk_make_request
);
225 if (bdi_init(&d
->blkq
.backing_dev_info
))
227 spin_lock_irqsave(&d
->lock
, flags
);
228 gd
->major
= AOE_MAJOR
;
229 gd
->first_minor
= d
->sysminor
* AOE_PARTITIONS
;
230 gd
->fops
= &aoe_bdops
;
231 gd
->private_data
= d
;
232 gd
->capacity
= d
->ssize
;
233 snprintf(gd
->disk_name
, sizeof gd
->disk_name
, "etherd/e%ld.%ld",
234 d
->aoemajor
, d
->aoeminor
);
236 gd
->queue
= &d
->blkq
;
238 d
->flags
&= ~DEVFL_GDALLOC
;
239 d
->flags
|= DEVFL_UP
;
241 spin_unlock_irqrestore(&d
->lock
, flags
);
244 aoedisk_add_sysfs(d
);
248 mempool_destroy(d
->bufpool
);
252 spin_lock_irqsave(&d
->lock
, flags
);
253 d
->flags
&= ~DEVFL_GDALLOC
;
254 spin_unlock_irqrestore(&d
->lock
, flags
);
260 kmem_cache_destroy(buf_pool_cache
);
266 buf_pool_cache
= kmem_cache_create("aoe_bufs",
269 if (buf_pool_cache
== NULL
)