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