]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/block/aoe/aoechr.c
block: autoconvert trivial BKL users to private mutex
[net-next-2.6.git] / drivers / block / aoe / aoechr.c
CommitLineData
52e112b3 1/* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
1da177e4
LT
2/*
3 * aoechr.c
4 * AoE character device driver
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
24879a8e 9#include <linux/completion.h>
68e0d42f 10#include <linux/delay.h>
5a0e3ad6 11#include <linux/slab.h>
2a48fc0a 12#include <linux/mutex.h>
e9bb8fb0 13#include <linux/skbuff.h>
1da177e4
LT
14#include "aoe.h"
15
16enum {
17 //MINOR_STAT = 1, (moved to sysfs)
18 MINOR_ERR = 2,
19 MINOR_DISCOVER,
20 MINOR_INTERFACES,
3ae1c24e 21 MINOR_REVALIDATE,
262bf541 22 MINOR_FLUSH,
1da177e4 23 MSGSZ = 2048,
1da177e4
LT
24 NMSG = 100, /* message backlog to retain */
25};
26
27struct aoe_chardev {
28 ulong minor;
29 char name[32];
30};
31
32enum { EMFL_VALID = 1 };
33
34struct ErrMsg {
35 short flags;
36 short len;
37 char *msg;
38};
39
2a48fc0a 40static DEFINE_MUTEX(aoechr_mutex);
1da177e4
LT
41static struct ErrMsg emsgs[NMSG];
42static int emsgs_head_idx, emsgs_tail_idx;
24879a8e 43static struct completion emsgs_comp;
1da177e4
LT
44static spinlock_t emsgs_lock;
45static int nblocked_emsgs_readers;
deb36970 46static struct class *aoe_class;
1da177e4
LT
47static struct aoe_chardev chardevs[] = {
48 { MINOR_ERR, "err" },
49 { MINOR_DISCOVER, "discover" },
50 { MINOR_INTERFACES, "interfaces" },
3ae1c24e 51 { MINOR_REVALIDATE, "revalidate" },
262bf541 52 { MINOR_FLUSH, "flush" },
1da177e4
LT
53};
54
55static int
56discover(void)
57{
58 aoecmd_cfg(0xffff, 0xff);
59 return 0;
60}
61
62static int
63interfaces(const char __user *str, size_t size)
64{
65 if (set_aoe_iflist(str, size)) {
a12c93f0
EC
66 printk(KERN_ERR
67 "aoe: could not set interface list: too many interfaces\n");
1da177e4
LT
68 return -EINVAL;
69 }
70 return 0;
71}
72
3ae1c24e
EC
73static int
74revalidate(const char __user *str, size_t size)
75{
76 int major, minor, n;
77 ulong flags;
78 struct aoedev *d;
68e0d42f 79 struct sk_buff *skb;
3ae1c24e
EC
80 char buf[16];
81
82 if (size >= sizeof buf)
83 return -EINVAL;
84 buf[sizeof buf - 1] = '\0';
85 if (copy_from_user(buf, str, size))
86 return -EFAULT;
87
88 /* should be e%d.%d format */
89 n = sscanf(buf, "e%d.%d", &major, &minor);
90 if (n != 2) {
a12c93f0 91 printk(KERN_ERR "aoe: invalid device specification\n");
3ae1c24e
EC
92 return -EINVAL;
93 }
94 d = aoedev_by_aoeaddr(major, minor);
95 if (!d)
96 return -EINVAL;
3ae1c24e 97 spin_lock_irqsave(&d->lock, flags);
68e0d42f
EC
98 aoecmd_cleanslate(d);
99loop:
100 skb = aoecmd_ata_id(d);
3ae1c24e 101 spin_unlock_irqrestore(&d->lock, flags);
68e0d42f
EC
102 /* try again if we are able to sleep a bit,
103 * otherwise give up this revalidation
104 */
105 if (!skb && !msleep_interruptible(200)) {
106 spin_lock_irqsave(&d->lock, flags);
107 goto loop;
108 }
e9bb8fb0
DM
109 if (skb) {
110 struct sk_buff_head queue;
111 __skb_queue_head_init(&queue);
112 __skb_queue_tail(&queue, skb);
113 aoenet_xmit(&queue);
114 }
3ae1c24e 115 aoecmd_cfg(major, minor);
3ae1c24e
EC
116 return 0;
117}
118
1da177e4
LT
119void
120aoechr_error(char *msg)
121{
122 struct ErrMsg *em;
123 char *mp;
124 ulong flags, n;
125
126 n = strlen(msg);
127
128 spin_lock_irqsave(&emsgs_lock, flags);
129
130 em = emsgs + emsgs_tail_idx;
131 if ((em->flags & EMFL_VALID)) {
132bail: spin_unlock_irqrestore(&emsgs_lock, flags);
133 return;
134 }
135
136 mp = kmalloc(n, GFP_ATOMIC);
137 if (mp == NULL) {
a12c93f0 138 printk(KERN_ERR "aoe: allocation failure, len=%ld\n", n);
1da177e4
LT
139 goto bail;
140 }
141
142 memcpy(mp, msg, n);
143 em->msg = mp;
144 em->flags |= EMFL_VALID;
145 em->len = n;
146
147 emsgs_tail_idx++;
148 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
149
150 spin_unlock_irqrestore(&emsgs_lock, flags);
151
152 if (nblocked_emsgs_readers)
24879a8e 153 complete(&emsgs_comp);
1da177e4
LT
154}
155
156static ssize_t
157aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
158{
159 int ret = -EINVAL;
160
161 switch ((unsigned long) filp->private_data) {
162 default:
a12c93f0 163 printk(KERN_INFO "aoe: can't write to that file.\n");
1da177e4
LT
164 break;
165 case MINOR_DISCOVER:
166 ret = discover();
167 break;
168 case MINOR_INTERFACES:
169 ret = interfaces(buf, cnt);
170 break;
3ae1c24e
EC
171 case MINOR_REVALIDATE:
172 ret = revalidate(buf, cnt);
262bf541
EC
173 break;
174 case MINOR_FLUSH:
175 ret = aoedev_flush(buf, cnt);
1da177e4
LT
176 }
177 if (ret == 0)
178 ret = cnt;
179 return ret;
180}
181
182static int
183aoechr_open(struct inode *inode, struct file *filp)
184{
185 int n, i;
186
2a48fc0a 187 mutex_lock(&aoechr_mutex);
2017b376 188 n = iminor(inode);
1da177e4
LT
189 filp->private_data = (void *) (unsigned long) n;
190
191 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
579174a5 192 if (chardevs[i].minor == n) {
2a48fc0a 193 mutex_unlock(&aoechr_mutex);
1da177e4 194 return 0;
579174a5 195 }
2a48fc0a 196 mutex_unlock(&aoechr_mutex);
1da177e4
LT
197 return -EINVAL;
198}
199
200static int
201aoechr_rel(struct inode *inode, struct file *filp)
202{
203 return 0;
204}
205
206static ssize_t
207aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
208{
209 unsigned long n;
210 char *mp;
211 struct ErrMsg *em;
212 ssize_t len;
213 ulong flags;
214
215 n = (unsigned long) filp->private_data;
cf446f0d
EC
216 if (n != MINOR_ERR)
217 return -EFAULT;
218
219 spin_lock_irqsave(&emsgs_lock, flags);
1da177e4 220
cf446f0d
EC
221 for (;;) {
222 em = emsgs + emsgs_head_idx;
223 if ((em->flags & EMFL_VALID) != 0)
224 break;
225 if (filp->f_flags & O_NDELAY) {
1da177e4 226 spin_unlock_irqrestore(&emsgs_lock, flags);
cf446f0d
EC
227 return -EAGAIN;
228 }
229 nblocked_emsgs_readers++;
230
231 spin_unlock_irqrestore(&emsgs_lock, flags);
1da177e4 232
24879a8e 233 n = wait_for_completion_interruptible(&emsgs_comp);
1da177e4 234
cf446f0d 235 spin_lock_irqsave(&emsgs_lock, flags);
1da177e4 236
cf446f0d 237 nblocked_emsgs_readers--;
1da177e4 238
cf446f0d 239 if (n) {
1da177e4 240 spin_unlock_irqrestore(&emsgs_lock, flags);
cf446f0d 241 return -ERESTARTSYS;
1da177e4 242 }
cf446f0d
EC
243 }
244 if (em->len > cnt) {
245 spin_unlock_irqrestore(&emsgs_lock, flags);
246 return -EAGAIN;
247 }
248 mp = em->msg;
249 len = em->len;
250 em->msg = NULL;
251 em->flags &= ~EMFL_VALID;
1da177e4 252
cf446f0d
EC
253 emsgs_head_idx++;
254 emsgs_head_idx %= ARRAY_SIZE(emsgs);
1da177e4 255
cf446f0d 256 spin_unlock_irqrestore(&emsgs_lock, flags);
1da177e4 257
cf446f0d
EC
258 n = copy_to_user(buf, mp, len);
259 kfree(mp);
260 return n == 0 ? len : -EFAULT;
1da177e4
LT
261}
262
2b8693c0 263static const struct file_operations aoe_fops = {
1da177e4
LT
264 .write = aoechr_write,
265 .read = aoechr_read,
266 .open = aoechr_open,
267 .release = aoechr_rel,
268 .owner = THIS_MODULE,
269};
270
e454cea2 271static char *aoe_devnode(struct device *dev, mode_t *mode)
1ce8a0d3
KS
272{
273 return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
274}
275
1da177e4
LT
276int __init
277aoechr_init(void)
278{
279 int n, i;
280
281 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
282 if (n < 0) {
a12c93f0 283 printk(KERN_ERR "aoe: can't register char device\n");
1da177e4
LT
284 return n;
285 }
24879a8e 286 init_completion(&emsgs_comp);
1da177e4 287 spin_lock_init(&emsgs_lock);
deb36970 288 aoe_class = class_create(THIS_MODULE, "aoe");
1da177e4
LT
289 if (IS_ERR(aoe_class)) {
290 unregister_chrdev(AOE_MAJOR, "aoechr");
291 return PTR_ERR(aoe_class);
292 }
e454cea2 293 aoe_class->devnode = aoe_devnode;
1ce8a0d3 294
1da177e4 295 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
1ff9f542
GKH
296 device_create(aoe_class, NULL,
297 MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
298 chardevs[i].name);
1da177e4
LT
299
300 return 0;
301}
302
303void
304aoechr_exit(void)
305{
306 int i;
307
308 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
7ea7ed01 309 device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
deb36970 310 class_destroy(aoe_class);
1da177e4
LT
311 unregister_chrdev(AOE_MAJOR, "aoechr");
312}
313