1 /* Copyright (c) 2012 Coraid, Inc. See COPYING for GPL terms. */
4 * AoE character device driver
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
13 #include <linux/skbuff.h>
14 #include <linux/export.h>
18 //MINOR_STAT = 1, (moved to sysfs)
25 NMSG
= 100, /* message backlog to retain */
33 enum { EMFL_VALID
= 1 };
41 static DEFINE_MUTEX(aoechr_mutex
);
42 static struct ErrMsg emsgs
[NMSG
];
43 static int emsgs_head_idx
, emsgs_tail_idx
;
44 static struct completion emsgs_comp
;
45 static spinlock_t emsgs_lock
;
46 static int nblocked_emsgs_readers
;
47 static struct class *aoe_class
;
48 static struct aoe_chardev chardevs
[] = {
50 { MINOR_DISCOVER
, "discover" },
51 { MINOR_INTERFACES
, "interfaces" },
52 { MINOR_REVALIDATE
, "revalidate" },
53 { MINOR_FLUSH
, "flush" },
59 aoecmd_cfg(0xffff, 0xff);
64 interfaces(const char __user
*str
, size_t size
)
66 if (set_aoe_iflist(str
, size
)) {
68 "aoe: could not set interface list: too many interfaces\n");
75 revalidate(const char __user
*str
, size_t size
)
83 if (size
>= sizeof buf
)
85 buf
[sizeof buf
- 1] = '\0';
86 if (copy_from_user(buf
, str
, size
))
89 n
= sscanf(buf
, "e%d.%d", &major
, &minor
);
91 pr_err("aoe: invalid device specification %s\n", buf
);
94 d
= aoedev_by_aoeaddr(major
, minor
);
97 spin_lock_irqsave(&d
->lock
, flags
);
99 aoecmd_cfg(major
, minor
);
101 skb
= aoecmd_ata_id(d
);
102 spin_unlock_irqrestore(&d
->lock
, flags
);
103 /* try again if we are able to sleep a bit,
104 * otherwise give up this revalidation
106 if (!skb
&& !msleep_interruptible(250)) {
107 spin_lock_irqsave(&d
->lock
, flags
);
112 struct sk_buff_head queue
;
113 __skb_queue_head_init(&queue
);
114 __skb_queue_tail(&queue
, skb
);
121 aoechr_error(char *msg
)
129 spin_lock_irqsave(&emsgs_lock
, flags
);
131 em
= emsgs
+ emsgs_tail_idx
;
132 if ((em
->flags
& EMFL_VALID
)) {
133 bail
: spin_unlock_irqrestore(&emsgs_lock
, flags
);
137 mp
= kmalloc(n
, GFP_ATOMIC
);
139 printk(KERN_ERR
"aoe: allocation failure, len=%ld\n", n
);
145 em
->flags
|= EMFL_VALID
;
149 emsgs_tail_idx
%= ARRAY_SIZE(emsgs
);
151 spin_unlock_irqrestore(&emsgs_lock
, flags
);
153 if (nblocked_emsgs_readers
)
154 complete(&emsgs_comp
);
158 aoechr_write(struct file
*filp
, const char __user
*buf
, size_t cnt
, loff_t
*offp
)
162 switch ((unsigned long) filp
->private_data
) {
164 printk(KERN_INFO
"aoe: can't write to that file.\n");
169 case MINOR_INTERFACES
:
170 ret
= interfaces(buf
, cnt
);
172 case MINOR_REVALIDATE
:
173 ret
= revalidate(buf
, cnt
);
176 ret
= aoedev_flush(buf
, cnt
);
185 aoechr_open(struct inode
*inode
, struct file
*filp
)
189 mutex_lock(&aoechr_mutex
);
191 filp
->private_data
= (void *) (unsigned long) n
;
193 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
194 if (chardevs
[i
].minor
== n
) {
195 mutex_unlock(&aoechr_mutex
);
198 mutex_unlock(&aoechr_mutex
);
203 aoechr_rel(struct inode
*inode
, struct file
*filp
)
209 aoechr_read(struct file
*filp
, char __user
*buf
, size_t cnt
, loff_t
*off
)
217 n
= (unsigned long) filp
->private_data
;
221 spin_lock_irqsave(&emsgs_lock
, flags
);
224 em
= emsgs
+ emsgs_head_idx
;
225 if ((em
->flags
& EMFL_VALID
) != 0)
227 if (filp
->f_flags
& O_NDELAY
) {
228 spin_unlock_irqrestore(&emsgs_lock
, flags
);
231 nblocked_emsgs_readers
++;
233 spin_unlock_irqrestore(&emsgs_lock
, flags
);
235 n
= wait_for_completion_interruptible(&emsgs_comp
);
237 spin_lock_irqsave(&emsgs_lock
, flags
);
239 nblocked_emsgs_readers
--;
242 spin_unlock_irqrestore(&emsgs_lock
, flags
);
247 spin_unlock_irqrestore(&emsgs_lock
, flags
);
253 em
->flags
&= ~EMFL_VALID
;
256 emsgs_head_idx
%= ARRAY_SIZE(emsgs
);
258 spin_unlock_irqrestore(&emsgs_lock
, flags
);
260 n
= copy_to_user(buf
, mp
, len
);
262 return n
== 0 ? len
: -EFAULT
;
265 static const struct file_operations aoe_fops
= {
266 .write
= aoechr_write
,
269 .release
= aoechr_rel
,
270 .owner
= THIS_MODULE
,
271 .llseek
= noop_llseek
,
274 static char *aoe_devnode(struct device
*dev
, umode_t
*mode
)
276 return kasprintf(GFP_KERNEL
, "etherd/%s", dev_name(dev
));
284 n
= register_chrdev(AOE_MAJOR
, "aoechr", &aoe_fops
);
286 printk(KERN_ERR
"aoe: can't register char device\n");
289 init_completion(&emsgs_comp
);
290 spin_lock_init(&emsgs_lock
);
291 aoe_class
= class_create(THIS_MODULE
, "aoe");
292 if (IS_ERR(aoe_class
)) {
293 unregister_chrdev(AOE_MAJOR
, "aoechr");
294 return PTR_ERR(aoe_class
);
296 aoe_class
->devnode
= aoe_devnode
;
298 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
299 device_create(aoe_class
, NULL
,
300 MKDEV(AOE_MAJOR
, chardevs
[i
].minor
), NULL
,
311 for (i
= 0; i
< ARRAY_SIZE(chardevs
); ++i
)
312 device_destroy(aoe_class
, MKDEV(AOE_MAJOR
, chardevs
[i
].minor
));
313 class_destroy(aoe_class
);
314 unregister_chrdev(AOE_MAJOR
, "aoechr");
This page took 0.048202 seconds and 5 git commands to generate.