1 /* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */
4 * AoE character device driver
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
12 //MINOR_STAT = 1, (moved to sysfs)
18 NMSG = 100, /* message backlog to retain */
26 enum { EMFL_VALID = 1 };
34 static struct ErrMsg emsgs[NMSG];
35 static int emsgs_head_idx, emsgs_tail_idx;
36 static struct semaphore emsgs_sema;
37 static spinlock_t emsgs_lock;
38 static int nblocked_emsgs_readers;
39 static struct class *aoe_class;
40 static struct aoe_chardev chardevs[] = {
42 { MINOR_DISCOVER, "discover" },
43 { MINOR_INTERFACES, "interfaces" },
49 aoecmd_cfg(0xffff, 0xff);
54 interfaces(const char __user *str, size_t size)
56 if (set_aoe_iflist(str, size)) {
58 "%s: could not set interface list: %s\n",
59 __FUNCTION__, "too many interfaces");
66 aoechr_error(char *msg)
74 spin_lock_irqsave(&emsgs_lock, flags);
76 em = emsgs + emsgs_tail_idx;
77 if ((em->flags & EMFL_VALID)) {
78 bail: spin_unlock_irqrestore(&emsgs_lock, flags);
82 mp = kmalloc(n, GFP_ATOMIC);
84 printk(KERN_CRIT "aoe: aoechr_error: allocation failure, len=%ld\n", n);
90 em->flags |= EMFL_VALID;
94 emsgs_tail_idx %= ARRAY_SIZE(emsgs);
96 spin_unlock_irqrestore(&emsgs_lock, flags);
98 if (nblocked_emsgs_readers)
103 aoechr_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offp)
107 switch ((unsigned long) filp->private_data) {
109 printk(KERN_INFO "aoe: aoechr_write: can't write to that file.\n");
114 case MINOR_INTERFACES:
115 ret = interfaces(buf, cnt);
124 aoechr_open(struct inode *inode, struct file *filp)
128 n = MINOR(inode->i_rdev);
129 filp->private_data = (void *) (unsigned long) n;
131 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
132 if (chardevs[i].minor == n)
138 aoechr_rel(struct inode *inode, struct file *filp)
144 aoechr_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
152 n = (unsigned long) filp->private_data;
155 spin_lock_irqsave(&emsgs_lock, flags);
157 em = emsgs + emsgs_head_idx;
158 if ((em->flags & EMFL_VALID) == 0) {
159 if (filp->f_flags & O_NDELAY) {
160 spin_unlock_irqrestore(&emsgs_lock, flags);
163 nblocked_emsgs_readers++;
165 spin_unlock_irqrestore(&emsgs_lock, flags);
167 n = down_interruptible(&emsgs_sema);
169 spin_lock_irqsave(&emsgs_lock, flags);
171 nblocked_emsgs_readers--;
174 spin_unlock_irqrestore(&emsgs_lock, flags);
180 spin_unlock_irqrestore(&emsgs_lock, flags);
186 em->flags &= ~EMFL_VALID;
189 emsgs_head_idx %= ARRAY_SIZE(emsgs);
191 spin_unlock_irqrestore(&emsgs_lock, flags);
193 n = copy_to_user(buf, mp, len);
195 return n == 0 ? len : -EFAULT;
201 static struct file_operations aoe_fops = {
202 .write = aoechr_write,
205 .release = aoechr_rel,
206 .owner = THIS_MODULE,
214 n = register_chrdev(AOE_MAJOR, "aoechr", &aoe_fops);
216 printk(KERN_ERR "aoe: aoechr_init: can't register char device\n");
219 sema_init(&emsgs_sema, 0);
220 spin_lock_init(&emsgs_lock);
221 aoe_class = class_create(THIS_MODULE, "aoe");
222 if (IS_ERR(aoe_class)) {
223 unregister_chrdev(AOE_MAJOR, "aoechr");
224 return PTR_ERR(aoe_class);
226 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
227 class_device_create(aoe_class, NULL,
228 MKDEV(AOE_MAJOR, chardevs[i].minor),
229 NULL, chardevs[i].name);
239 for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
240 class_device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
241 class_destroy(aoe_class);
242 unregister_chrdev(AOE_MAJOR, "aoechr");