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

#include <linux/hdreg.h>
#include <linux/blkdev.h>
#include <linux/netdevice.h>
10
#include <linux/delay.h>
11
#include <linux/slab.h>
L
Linus Torvalds 已提交
12 13
#include "aoe.h"

14 15
static void dummy_timer(ulong);
static void aoedev_freedev(struct aoedev *);
16 17
static void freetgt(struct aoedev *d, struct aoetgt *t);
static void skbpoolfree(struct aoedev *d);
18

L
Linus Torvalds 已提交
19
static struct aoedev *devlist;
20
static DEFINE_SPINLOCK(devlist_lock);
L
Linus Torvalds 已提交
21 22

struct aoedev *
23
aoedev_by_aoeaddr(int maj, int min)
L
Linus Torvalds 已提交
24 25 26 27 28 29 30
{
	struct aoedev *d;
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);

	for (d=devlist; d; d=d->next)
31
		if (d->aoemajor == maj && d->aoeminor == min)
L
Linus Torvalds 已提交
32 33 34 35 36 37
			break;

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

38 39 40 41 42 43 44 45 46 47 48 49
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 已提交
50 51 52
void
aoedev_downdev(struct aoedev *d)
{
53
	struct aoetgt **t, **te;
L
Linus Torvalds 已提交
54 55 56 57
	struct frame *f, *e;
	struct buf *buf;
	struct bio *bio;

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	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 已提交
73
		}
74 75 76 77 78 79 80 81
		(*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 已提交
82 83
	}
	d->inprocess = NULL;
84
	d->htgt = NULL;
L
Linus Torvalds 已提交
85 86 87 88 89 90

	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);
91
		bio_endio(bio, -EIO);
L
Linus Torvalds 已提交
92 93 94
	}

	if (d->gd)
95
		set_capacity(d->gd, 0);
L
Linus Torvalds 已提交
96

97
	d->flags &= ~DEVFL_UP;
L
Linus Torvalds 已提交
98 99
}

100 101 102 103 104
static void
aoedev_freedev(struct aoedev *d)
{
	struct aoetgt **t, **e;

T
Tejun Heo 已提交
105
	cancel_work_sync(&d->work);
106 107 108 109 110 111 112 113
	if (d->gd) {
		aoedisk_rm_sysfs(d);
		del_gendisk(d->gd);
		put_disk(d->gd);
	}
	t = d->targets;
	e = t + NTARGETS;
	for (; t < e && *t; t++)
114
		freetgt(d, *t);
115 116
	if (d->bufpool)
		mempool_destroy(d->bufpool);
117
	skbpoolfree(d);
118
	blk_cleanup_queue(d->blkq);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	kfree(d);
}

int
aoedev_flush(const char __user *str, size_t cnt)
{
	ulong flags;
	struct aoedev *d, **dd;
	struct aoedev *rmd = NULL;
	char buf[16];
	int all = 0;

	if (cnt >= 3) {
		if (cnt > sizeof buf)
			cnt = sizeof buf;
		if (copy_from_user(buf, str, cnt))
			return -EFAULT;
		all = !strncmp(buf, "all", 3);
	}

	spin_lock_irqsave(&devlist_lock, flags);
	dd = &devlist;
	while ((d = *dd)) {
		spin_lock(&d->lock);
		if ((!all && (d->flags & DEVFL_UP))
		|| (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
		|| d->nopen) {
			spin_unlock(&d->lock);
			dd = &d->next;
			continue;
		}
		*dd = d->next;
		aoedev_downdev(d);
		d->flags |= DEVFL_TKILL;
		spin_unlock(&d->lock);
		d->next = rmd;
		rmd = d;
	}
	spin_unlock_irqrestore(&devlist_lock, flags);
	while ((d = rmd)) {
		rmd = d->next;
		del_timer_sync(&d->timer);
		aoedev_freedev(d);	/* must be able to sleep */
	}
	return 0;
}

166 167 168 169 170 171 172 173 174 175 176 177
/* I'm not really sure that this is a realistic problem, but if the
network driver goes gonzo let's just leak memory after complaining. */
static void
skbfree(struct sk_buff *skb)
{
	enum { Sms = 100, Tms = 3*1000};
	int i = Tms / Sms;

	if (skb == NULL)
		return;
	while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
		msleep(Sms);
R
Roel Kluin 已提交
178
	if (i < 0) {
179 180 181 182 183 184
		printk(KERN_ERR
			"aoe: %s holds ref: %s\n",
			skb->dev ? skb->dev->name : "netif",
			"cannot free skb -- memory leaked.");
		return;
	}
185
	skb->truesize -= skb->data_len;
186 187 188 189 190 191 192 193
	skb_shinfo(skb)->nr_frags = skb->data_len = 0;
	skb_trim(skb, 0);
	dev_kfree_skb(skb);
}

static void
skbpoolfree(struct aoedev *d)
{
194
	struct sk_buff *skb, *tmp;
195

196
	skb_queue_walk_safe(&d->skbpool, skb, tmp)
197
		skbfree(skb);
198 199

	__skb_queue_head_init(&d->skbpool);
200 201
}

202
/* find it or malloc it */
L
Linus Torvalds 已提交
203
struct aoedev *
204
aoedev_by_sysminor_m(ulong sysminor)
L
Linus Torvalds 已提交
205 206 207 208 209 210 211
{
	struct aoedev *d;
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);

	for (d=devlist; d; d=d->next)
212
		if (d->sysminor == sysminor)
L
Linus Torvalds 已提交
213
			break;
214 215 216 217 218 219 220
	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);
221 222
	skb_queue_head_init(&d->sendq);
	skb_queue_head_init(&d->skbpool);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	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:
238
	spin_unlock_irqrestore(&devlist_lock, flags);
L
Linus Torvalds 已提交
239 240 241 242
	return d;
}

static void
243
freetgt(struct aoedev *d, struct aoetgt *t)
L
Linus Torvalds 已提交
244
{
E
Ed L. Cashin 已提交
245 246
	struct frame *f, *e;

247 248
	f = t->frames;
	e = f + t->nframes;
249 250
	for (; f < e; f++)
		skbfree(f->skb);
251 252 253 254
	kfree(t->frames);
	kfree(t);
}

L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262 263 264 265
void
aoedev_exit(void)
{
	struct aoedev *d;
	ulong flags;

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

		spin_lock_irqsave(&d->lock, flags);
		aoedev_downdev(d);
266
		d->flags |= DEVFL_TKILL;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278
		spin_unlock_irqrestore(&d->lock, flags);

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

int __init
aoedev_init(void)
{
	return 0;
}