Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */ |
| 2 | /* |
| 3 | * aoeblk.c |
| 4 | * block device routines |
| 5 | */ |
| 6 | |
| 7 | #include <linux/hdreg.h> |
| 8 | #include <linux/blkdev.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/ioctl.h> |
| 11 | #include <linux/genhd.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include "aoe.h" |
| 14 | |
| 15 | static kmem_cache_t *buf_pool_cache; |
| 16 | |
| 17 | /* add attributes for our block devices in sysfs */ |
| 18 | static ssize_t aoedisk_show_state(struct gendisk * disk, char *page) |
| 19 | { |
| 20 | struct aoedev *d = disk->private_data; |
| 21 | |
| 22 | return snprintf(page, PAGE_SIZE, |
| 23 | "%s%s\n", |
| 24 | (d->flags & DEVFL_UP) ? "up" : "down", |
| 25 | (d->flags & DEVFL_CLOSEWAIT) ? ",closewait" : ""); |
| 26 | } |
| 27 | static ssize_t aoedisk_show_mac(struct gendisk * disk, char *page) |
| 28 | { |
| 29 | struct aoedev *d = disk->private_data; |
| 30 | |
| 31 | return snprintf(page, PAGE_SIZE, "%012llx\n", |
| 32 | (unsigned long long)mac_addr(d->addr)); |
| 33 | } |
| 34 | static ssize_t aoedisk_show_netif(struct gendisk * disk, char *page) |
| 35 | { |
| 36 | struct aoedev *d = disk->private_data; |
| 37 | |
| 38 | return snprintf(page, PAGE_SIZE, "%s\n", d->ifp->name); |
| 39 | } |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 40 | /* firmware version */ |
| 41 | static ssize_t aoedisk_show_fwver(struct gendisk * disk, char *page) |
| 42 | { |
| 43 | struct aoedev *d = disk->private_data; |
| 44 | |
| 45 | return snprintf(page, PAGE_SIZE, "0x%04x\n", (unsigned int) d->fw_ver); |
| 46 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | static struct disk_attribute disk_attr_state = { |
| 49 | .attr = {.name = "state", .mode = S_IRUGO }, |
| 50 | .show = aoedisk_show_state |
| 51 | }; |
| 52 | static struct disk_attribute disk_attr_mac = { |
| 53 | .attr = {.name = "mac", .mode = S_IRUGO }, |
| 54 | .show = aoedisk_show_mac |
| 55 | }; |
| 56 | static struct disk_attribute disk_attr_netif = { |
| 57 | .attr = {.name = "netif", .mode = S_IRUGO }, |
| 58 | .show = aoedisk_show_netif |
| 59 | }; |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 60 | static struct disk_attribute disk_attr_fwver = { |
| 61 | .attr = {.name = "firmware-version", .mode = S_IRUGO }, |
| 62 | .show = aoedisk_show_fwver |
| 63 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | static void |
| 66 | aoedisk_add_sysfs(struct aoedev *d) |
| 67 | { |
| 68 | sysfs_create_file(&d->gd->kobj, &disk_attr_state.attr); |
| 69 | sysfs_create_file(&d->gd->kobj, &disk_attr_mac.attr); |
| 70 | sysfs_create_file(&d->gd->kobj, &disk_attr_netif.attr); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 71 | sysfs_create_file(&d->gd->kobj, &disk_attr_fwver.attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | void |
| 74 | aoedisk_rm_sysfs(struct aoedev *d) |
| 75 | { |
| 76 | sysfs_remove_link(&d->gd->kobj, "state"); |
| 77 | sysfs_remove_link(&d->gd->kobj, "mac"); |
| 78 | sysfs_remove_link(&d->gd->kobj, "netif"); |
Ed L Cashin | 4613ed2 | 2005-04-29 10:24:25 -0400 | [diff] [blame] | 79 | sysfs_remove_link(&d->gd->kobj, "firmware-version"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int |
| 83 | aoeblk_open(struct inode *inode, struct file *filp) |
| 84 | { |
| 85 | struct aoedev *d; |
| 86 | ulong flags; |
| 87 | |
| 88 | d = inode->i_bdev->bd_disk->private_data; |
| 89 | |
| 90 | spin_lock_irqsave(&d->lock, flags); |
| 91 | if (d->flags & DEVFL_UP) { |
| 92 | d->nopen++; |
| 93 | spin_unlock_irqrestore(&d->lock, flags); |
| 94 | return 0; |
| 95 | } |
| 96 | spin_unlock_irqrestore(&d->lock, flags); |
| 97 | return -ENODEV; |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | aoeblk_release(struct inode *inode, struct file *filp) |
| 102 | { |
| 103 | struct aoedev *d; |
| 104 | ulong flags; |
| 105 | |
| 106 | d = inode->i_bdev->bd_disk->private_data; |
| 107 | |
| 108 | spin_lock_irqsave(&d->lock, flags); |
| 109 | |
| 110 | if (--d->nopen == 0 && (d->flags & DEVFL_CLOSEWAIT)) { |
| 111 | d->flags &= ~DEVFL_CLOSEWAIT; |
| 112 | spin_unlock_irqrestore(&d->lock, flags); |
| 113 | aoecmd_cfg(d->aoemajor, d->aoeminor); |
| 114 | return 0; |
| 115 | } |
| 116 | spin_unlock_irqrestore(&d->lock, flags); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int |
| 122 | aoeblk_make_request(request_queue_t *q, struct bio *bio) |
| 123 | { |
| 124 | struct aoedev *d; |
| 125 | struct buf *buf; |
| 126 | struct sk_buff *sl; |
| 127 | ulong flags; |
| 128 | |
| 129 | blk_queue_bounce(q, &bio); |
| 130 | |
| 131 | d = bio->bi_bdev->bd_disk->private_data; |
| 132 | buf = mempool_alloc(d->bufpool, GFP_NOIO); |
| 133 | if (buf == NULL) { |
| 134 | printk(KERN_INFO "aoe: aoeblk_make_request: buf allocation " |
| 135 | "failure\n"); |
| 136 | bio_endio(bio, bio->bi_size, -ENOMEM); |
| 137 | return 0; |
| 138 | } |
| 139 | memset(buf, 0, sizeof(*buf)); |
| 140 | INIT_LIST_HEAD(&buf->bufs); |
ecashin@coraid.com | 0c6f0e7 | 2005-04-18 22:00:22 -0700 | [diff] [blame] | 141 | buf->start_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | buf->bio = bio; |
| 143 | buf->resid = bio->bi_size; |
| 144 | buf->sector = bio->bi_sector; |
| 145 | buf->bv = buf->bio->bi_io_vec; |
| 146 | buf->bv_resid = buf->bv->bv_len; |
| 147 | buf->bufaddr = page_address(buf->bv->bv_page) + buf->bv->bv_offset; |
| 148 | |
| 149 | spin_lock_irqsave(&d->lock, flags); |
| 150 | |
| 151 | if ((d->flags & DEVFL_UP) == 0) { |
| 152 | printk(KERN_INFO "aoe: aoeblk_make_request: device %ld.%ld is not up\n", |
| 153 | d->aoemajor, d->aoeminor); |
| 154 | spin_unlock_irqrestore(&d->lock, flags); |
| 155 | mempool_free(buf, d->bufpool); |
| 156 | bio_endio(bio, bio->bi_size, -ENXIO); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | list_add_tail(&buf->bufs, &d->bufq); |
| 161 | aoecmd_work(d); |
| 162 | |
ecashin@coraid.com | a4b3836 | 2005-04-18 22:00:22 -0700 | [diff] [blame] | 163 | sl = d->sendq_hd; |
| 164 | d->sendq_hd = d->sendq_tl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | spin_unlock_irqrestore(&d->lock, flags); |
| 167 | |
| 168 | aoenet_xmit(sl); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* This ioctl implementation expects userland to have the device node |
| 173 | * permissions set so that only priviledged users can open an aoe |
| 174 | * block device directly. |
| 175 | */ |
| 176 | static int |
| 177 | aoeblk_ioctl(struct inode *inode, struct file *filp, uint cmd, ulong arg) |
| 178 | { |
| 179 | struct aoedev *d; |
| 180 | |
| 181 | if (!arg) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | d = inode->i_bdev->bd_disk->private_data; |
| 185 | if ((d->flags & DEVFL_UP) == 0) { |
| 186 | printk(KERN_ERR "aoe: aoeblk_ioctl: disk not up\n"); |
| 187 | return -ENODEV; |
| 188 | } |
| 189 | |
| 190 | if (cmd == HDIO_GETGEO) { |
| 191 | d->geo.start = get_start_sect(inode->i_bdev); |
| 192 | if (!copy_to_user((void __user *) arg, &d->geo, sizeof d->geo)) |
| 193 | return 0; |
| 194 | return -EFAULT; |
| 195 | } |
| 196 | printk(KERN_INFO "aoe: aoeblk_ioctl: unknown ioctl %d\n", cmd); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | static struct block_device_operations aoe_bdops = { |
| 201 | .open = aoeblk_open, |
| 202 | .release = aoeblk_release, |
| 203 | .ioctl = aoeblk_ioctl, |
| 204 | .owner = THIS_MODULE, |
| 205 | }; |
| 206 | |
| 207 | /* alloc_disk and add_disk can sleep */ |
| 208 | void |
| 209 | aoeblk_gdalloc(void *vp) |
| 210 | { |
| 211 | struct aoedev *d = vp; |
| 212 | struct gendisk *gd; |
| 213 | ulong flags; |
| 214 | |
| 215 | gd = alloc_disk(AOE_PARTITIONS); |
| 216 | if (gd == NULL) { |
| 217 | printk(KERN_ERR "aoe: aoeblk_gdalloc: cannot allocate disk " |
| 218 | "structure for %ld.%ld\n", d->aoemajor, d->aoeminor); |
| 219 | spin_lock_irqsave(&d->lock, flags); |
| 220 | d->flags &= ~DEVFL_WORKON; |
| 221 | spin_unlock_irqrestore(&d->lock, flags); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | d->bufpool = mempool_create(MIN_BUFS, |
| 226 | mempool_alloc_slab, mempool_free_slab, |
| 227 | buf_pool_cache); |
| 228 | if (d->bufpool == NULL) { |
| 229 | printk(KERN_ERR "aoe: aoeblk_gdalloc: cannot allocate bufpool " |
| 230 | "for %ld.%ld\n", d->aoemajor, d->aoeminor); |
| 231 | put_disk(gd); |
| 232 | spin_lock_irqsave(&d->lock, flags); |
| 233 | d->flags &= ~DEVFL_WORKON; |
| 234 | spin_unlock_irqrestore(&d->lock, flags); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | spin_lock_irqsave(&d->lock, flags); |
| 239 | blk_queue_make_request(&d->blkq, aoeblk_make_request); |
| 240 | gd->major = AOE_MAJOR; |
| 241 | gd->first_minor = d->sysminor * AOE_PARTITIONS; |
| 242 | gd->fops = &aoe_bdops; |
| 243 | gd->private_data = d; |
| 244 | gd->capacity = d->ssize; |
| 245 | snprintf(gd->disk_name, sizeof gd->disk_name, "etherd/e%ld.%ld", |
| 246 | d->aoemajor, d->aoeminor); |
| 247 | |
| 248 | gd->queue = &d->blkq; |
| 249 | d->gd = gd; |
| 250 | d->flags &= ~DEVFL_WORKON; |
| 251 | d->flags |= DEVFL_UP; |
| 252 | |
| 253 | spin_unlock_irqrestore(&d->lock, flags); |
| 254 | |
| 255 | add_disk(gd); |
| 256 | aoedisk_add_sysfs(d); |
| 257 | |
| 258 | printk(KERN_INFO "aoe: %012llx e%lu.%lu v%04x has %llu " |
| 259 | "sectors\n", (unsigned long long)mac_addr(d->addr), |
| 260 | d->aoemajor, d->aoeminor, |
| 261 | d->fw_ver, (long long)d->ssize); |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | aoeblk_exit(void) |
| 266 | { |
| 267 | kmem_cache_destroy(buf_pool_cache); |
| 268 | } |
| 269 | |
| 270 | int __init |
| 271 | aoeblk_init(void) |
| 272 | { |
| 273 | buf_pool_cache = kmem_cache_create("aoe_bufs", |
| 274 | sizeof(struct buf), |
| 275 | 0, 0, NULL, NULL); |
| 276 | if (buf_pool_cache == NULL) |
| 277 | return -ENOMEM; |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |