1 /* Copyright (c) 2004 Coraid, Inc. See COPYING for GPL terms. */
4 * AoE device utility functions; maintains device list.
7 #include <linux/hdreg.h>
8 #include <linux/blkdev.h>
9 #include <linux/netdevice.h>
12 static struct aoedev *devlist;
13 static spinlock_t devlist_lock;
16 aoedev_isbusy(struct aoedev *d)
23 if (f->tag != FREETAG) {
24 printk(KERN_DEBUG "aoe: %ld.%ld isbusy\n",
25 d->aoemajor, d->aoeminor);
34 aoedev_by_aoeaddr(int maj, int min)
39 spin_lock_irqsave(&devlist_lock, flags);
41 for (d=devlist; d; d=d->next)
42 if (d->aoemajor == maj && d->aoeminor == min)
45 spin_unlock_irqrestore(&devlist_lock, flags);
54 d = (struct aoedev *)vp;
55 if (d->flags & DEVFL_TKILL)
57 d->timer.expires = jiffies + HZ;
61 /* called with devlist lock held */
62 static struct aoedev *
63 aoedev_newdev(ulong nframes)
68 d = kzalloc(sizeof *d, GFP_ATOMIC);
71 f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
77 INIT_WORK(&d->work, aoecmd_sleepwork, d);
85 spin_lock_init(&d->lock);
86 init_timer(&d->timer);
87 d->timer.data = (ulong) d;
88 d->timer.function = dummy_timer;
89 d->timer.expires = jiffies + HZ;
91 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
92 INIT_LIST_HEAD(&d->bufq);
100 aoedev_downdev(struct aoedev *d)
108 for (; f<e; f->tag = FREETAG, f->buf = NULL, f++) {
109 if (f->tag == FREETAG || f->buf == NULL)
113 if (--buf->nframesout == 0) {
114 mempool_free(buf, d->bufpool);
115 bio_endio(bio, bio->bi_size, -EIO);
120 while (!list_empty(&d->bufq)) {
121 buf = container_of(d->bufq.next, struct buf, bufs);
122 list_del(d->bufq.next);
124 mempool_free(buf, d->bufpool);
125 bio_endio(bio, bio->bi_size, -EIO);
131 d->flags &= ~(DEVFL_UP | DEVFL_PAUSE);
134 /* find it or malloc it */
136 aoedev_by_sysminor_m(ulong sysminor, ulong bufcnt)
141 spin_lock_irqsave(&devlist_lock, flags);
143 for (d=devlist; d; d=d->next)
144 if (d->sysminor == sysminor)
148 d = aoedev_newdev(bufcnt);
150 spin_unlock_irqrestore(&devlist_lock, flags);
151 printk(KERN_INFO "aoe: aoedev_set: aoedev_newdev failure.\n");
154 d->sysminor = sysminor;
155 d->aoemajor = AOEMAJOR(sysminor);
156 d->aoeminor = AOEMINOR(sysminor);
159 spin_unlock_irqrestore(&devlist_lock, flags);
164 aoedev_freedev(struct aoedev *d)
173 mempool_destroy(d->bufpool);
183 flush_scheduled_work();
185 while ((d = devlist)) {
188 spin_lock_irqsave(&d->lock, flags);
190 d->flags |= DEVFL_TKILL;
191 spin_unlock_irqrestore(&d->lock, flags);
193 del_timer_sync(&d->timer);
201 spin_lock_init(&devlist_lock);