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