aoedev.c 3.9 KB
Newer Older
E
Ed L. Cashin 已提交
1
/* Copyright (c) 2006 Coraid, Inc.  See COPYING for GPL terms. */
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * aoedev.c
 * AoE device utility functions; maintains device list.
 */

#include <linux/hdreg.h>
#include <linux/blkdev.h>
#include <linux/netdevice.h>
#include "aoe.h"

static struct aoedev *devlist;
static spinlock_t devlist_lock;

15 16 17 18 19 20 21 22
int
aoedev_isbusy(struct aoedev *d)
{
	struct frame *f, *e;

	f = d->frames;
	e = f + d->nframes;
	do {
E
Ed L. Cashin 已提交
23
		if (f->tag != FREETAG)
24 25 26 27 28 29
			return 1;
	} while (++f < e);

	return 0;
}

L
Linus Torvalds 已提交
30
struct aoedev *
31
aoedev_by_aoeaddr(int maj, int min)
L
Linus Torvalds 已提交
32 33 34 35 36 37 38
{
	struct aoedev *d;
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);

	for (d=devlist; d; d=d->next)
39
		if (d->aoemajor == maj && d->aoeminor == min)
L
Linus Torvalds 已提交
40 41 42 43 44 45
			break;

	spin_unlock_irqrestore(&devlist_lock, flags);
	return d;
}

46 47 48 49 50 51 52 53 54 55 56 57
static void
dummy_timer(ulong vp)
{
	struct aoedev *d;

	d = (struct aoedev *)vp;
	if (d->flags & DEVFL_TKILL)
		return;
	d->timer.expires = jiffies + HZ;
	add_timer(&d->timer);
}

L
Linus Torvalds 已提交
58 59 60 61 62 63 64
/* called with devlist lock held */
static struct aoedev *
aoedev_newdev(ulong nframes)
{
	struct aoedev *d;
	struct frame *f, *e;

65
	d = kzalloc(sizeof *d, GFP_ATOMIC);
L
Linus Torvalds 已提交
66
	f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
E
Ed L. Cashin 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
 	switch (!d || !f) {
 	case 0:
 		d->nframes = nframes;
 		d->frames = f;
 		e = f + nframes;
 		for (; f<e; f++) {
 			f->tag = FREETAG;
 			f->skb = new_skb(ETH_ZLEN);
 			if (!f->skb)
 				break;
 		}
 		if (f == e)
 			break;
 		while (f > d->frames) {
 			f--;
 			dev_kfree_skb(f->skb);
 		}
 	default:
 		if (f)
 			kfree(f);
 		if (d)
 			kfree(d);
L
Linus Torvalds 已提交
89 90
		return NULL;
	}
91
	INIT_WORK(&d->work, aoecmd_sleepwork, d);
L
Linus Torvalds 已提交
92 93
	spin_lock_init(&d->lock);
	init_timer(&d->timer);
94 95 96 97
	d->timer.data = (ulong) d;
	d->timer.function = dummy_timer;
	d->timer.expires = jiffies + HZ;
	add_timer(&d->timer);
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	d->bufpool = NULL;	/* defer to aoeblk_gdalloc */
	INIT_LIST_HEAD(&d->bufq);
	d->next = devlist;
	devlist = d;

	return d;
}

void
aoedev_downdev(struct aoedev *d)
{
	struct frame *f, *e;
	struct buf *buf;
	struct bio *bio;

	f = d->frames;
	e = f + d->nframes;
	for (; f<e; f->tag = FREETAG, f->buf = NULL, f++) {
		if (f->tag == FREETAG || f->buf == NULL)
			continue;
		buf = f->buf;
		bio = buf->bio;
		if (--buf->nframesout == 0) {
			mempool_free(buf, d->bufpool);
			bio_endio(bio, bio->bi_size, -EIO);
		}
E
Ed L. Cashin 已提交
124
		skb_shinfo(f->skb)->nr_frags = f->skb->data_len = 0;
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
	}
	d->inprocess = NULL;

	while (!list_empty(&d->bufq)) {
		buf = container_of(d->bufq.next, struct buf, bufs);
		list_del(d->bufq.next);
		bio = buf->bio;
		mempool_free(buf, d->bufpool);
		bio_endio(bio, bio->bi_size, -EIO);
	}

	if (d->gd)
		d->gd->capacity = 0;

139
	d->flags &= ~(DEVFL_UP | DEVFL_PAUSE);
L
Linus Torvalds 已提交
140 141
}

142
/* find it or malloc it */
L
Linus Torvalds 已提交
143
struct aoedev *
144
aoedev_by_sysminor_m(ulong sysminor, ulong bufcnt)
L
Linus Torvalds 已提交
145 146 147 148 149 150 151
{
	struct aoedev *d;
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);

	for (d=devlist; d; d=d->next)
152
		if (d->sysminor == sysminor)
L
Linus Torvalds 已提交
153 154
			break;

155 156 157 158
	if (d == NULL) {
		d = aoedev_newdev(bufcnt);
	 	if (d == NULL) {
			spin_unlock_irqrestore(&devlist_lock, flags);
E
Ed L. Cashin 已提交
159
			iprintk("aoedev_newdev failure.\n");
160 161
			return NULL;
		}
L
Linus Torvalds 已提交
162 163 164 165 166
		d->sysminor = sysminor;
		d->aoemajor = AOEMAJOR(sysminor);
		d->aoeminor = AOEMINOR(sysminor);
	}

167
	spin_unlock_irqrestore(&devlist_lock, flags);
L
Linus Torvalds 已提交
168 169 170 171 172 173
	return d;
}

static void
aoedev_freedev(struct aoedev *d)
{
E
Ed L. Cashin 已提交
174 175
	struct frame *f, *e;

L
Linus Torvalds 已提交
176 177 178 179 180
	if (d->gd) {
		aoedisk_rm_sysfs(d);
		del_gendisk(d->gd);
		put_disk(d->gd);
	}
E
Ed L. Cashin 已提交
181 182 183 184 185 186
	f = d->frames;
	e = f + d->nframes;
	for (; f<e; f++) {
		skb_shinfo(f->skb)->nr_frags = 0;
		dev_kfree_skb(f->skb);
	}
L
Linus Torvalds 已提交
187
	kfree(d->frames);
188 189
	if (d->bufpool)
		mempool_destroy(d->bufpool);
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	kfree(d);
}

void
aoedev_exit(void)
{
	struct aoedev *d;
	ulong flags;

	flush_scheduled_work();

	while ((d = devlist)) {
		devlist = d->next;

		spin_lock_irqsave(&d->lock, flags);
		aoedev_downdev(d);
206
		d->flags |= DEVFL_TKILL;
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220
		spin_unlock_irqrestore(&d->lock, flags);

		del_timer_sync(&d->timer);
		aoedev_freedev(d);
	}
}

int __init
aoedev_init(void)
{
	spin_lock_init(&devlist_lock);
	return 0;
}