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