aoedev.c 3.7 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
int
aoedev_isbusy(struct aoedev *d)
{
18
	struct aoetgt **t, **te;
19 20
	struct frame *f, *e;

21 22 23 24 25 26 27 28 29
	t = d->targets;
	te = t + NTARGETS;
	for (; t < te && *t; t++) {
		f = (*t)->frames;
		e = f + (*t)->nframes;
		for (; f < e; f++)
			if (f->tag != FREETAG)
				return 1;
	}
30 31 32
	return 0;
}

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

	spin_lock_irqsave(&devlist_lock, flags);

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

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

49 50 51 52 53 54 55 56 57 58 59 60
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 已提交
61 62 63
void
aoedev_downdev(struct aoedev *d)
{
64
	struct aoetgt **t, **te;
L
Linus Torvalds 已提交
65 66 67 68
	struct frame *f, *e;
	struct buf *buf;
	struct bio *bio;

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	t = d->targets;
	te = t + NTARGETS;
	for (; t < te && *t; t++) {
		f = (*t)->frames;
		e = f + (*t)->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
			&& buf != d->inprocess) {
				mempool_free(buf, d->bufpool);
				bio_endio(bio, -EIO);
			}
L
Linus Torvalds 已提交
84
		}
85 86 87 88 89 90 91 92
		(*t)->maxout = (*t)->nframes;
		(*t)->nout = 0;
	}
	buf = d->inprocess;
	if (buf) {
		bio = buf->bio;
		mempool_free(buf, d->bufpool);
		bio_endio(bio, -EIO);
L
Linus Torvalds 已提交
93 94
	}
	d->inprocess = NULL;
95
	d->htgt = NULL;
L
Linus Torvalds 已提交
96 97 98 99 100 101

	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);
102
		bio_endio(bio, -EIO);
L
Linus Torvalds 已提交
103 104 105 106 107
	}

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

108
	d->flags &= ~DEVFL_UP;
L
Linus Torvalds 已提交
109 110
}

111
/* find it or malloc it */
L
Linus Torvalds 已提交
112
struct aoedev *
113
aoedev_by_sysminor_m(ulong sysminor)
L
Linus Torvalds 已提交
114 115 116 117 118 119 120
{
	struct aoedev *d;
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);

	for (d=devlist; d; d=d->next)
121
		if (d->sysminor == sysminor)
L
Linus Torvalds 已提交
122
			break;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	if (d)
		goto out;
	d = kcalloc(1, sizeof *d, GFP_ATOMIC);
	if (!d)
		goto out;
	INIT_WORK(&d->work, aoecmd_sleepwork);
	spin_lock_init(&d->lock);
	init_timer(&d->timer);
	d->timer.data = (ulong) d;
	d->timer.function = dummy_timer;
	d->timer.expires = jiffies + HZ;
	add_timer(&d->timer);
	d->bufpool = NULL;	/* defer to aoeblk_gdalloc */
	d->tgt = d->targets;
	INIT_LIST_HEAD(&d->bufq);
	d->sysminor = sysminor;
	d->aoemajor = AOEMAJOR(sysminor);
	d->aoeminor = AOEMINOR(sysminor);
	d->mintimer = MINTIMER;
	d->next = devlist;
	devlist = d;
 out:
145
	spin_unlock_irqrestore(&devlist_lock, flags);
L
Linus Torvalds 已提交
146 147 148 149
	return d;
}

static void
150
freetgt(struct aoetgt *t)
L
Linus Torvalds 已提交
151
{
E
Ed L. Cashin 已提交
152 153
	struct frame *f, *e;

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	f = t->frames;
	e = f + t->nframes;
	for (; f < e; f++) {
		skb_shinfo(f->skb)->nr_frags = 0;
		dev_kfree_skb(f->skb);
	}
	kfree(t->frames);
	kfree(t);
}

static void
aoedev_freedev(struct aoedev *d)
{
	struct aoetgt **t, **e;

L
Linus Torvalds 已提交
169 170 171 172 173
	if (d->gd) {
		aoedisk_rm_sysfs(d);
		del_gendisk(d->gd);
		put_disk(d->gd);
	}
174 175 176 177
	t = d->targets;
	e = t + NTARGETS;
	for (; t < e && *t; t++)
		freetgt(*t);
178 179
	if (d->bufpool)
		mempool_destroy(d->bufpool);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	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);
196
		d->flags |= DEVFL_TKILL;
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210
		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;
}