aoedev.c 6.6 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 23 24 25 26 27 28 29 30 31 32
/*
 * Users who grab a pointer to the device with aoedev_by_aoeaddr or
 * aoedev_by_sysminor_m automatically get a reference count and must
 * be responsible for performing a aoedev_put.  With the addition of
 * async kthread processing I'm no longer confident that we can
 * guarantee consistency in the face of device flushes.
 *
 * For the time being, we only bother to add extra references for
 * frames sitting on the iocq.  When the kthreads finish processing
 * these frames, they will aoedev_put the device.
 */
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 43
		if (d->aoemajor == maj && d->aoeminor == min) {
			d->ref++;
L
Linus Torvalds 已提交
44
			break;
45
		}
L
Linus Torvalds 已提交
46 47 48 49 50

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

51 52 53 54 55 56 57 58 59 60
void
aoedev_put(struct aoedev *d)
{
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);
	d->ref--;
	spin_unlock_irqrestore(&devlist_lock, flags);
}

61 62 63 64 65 66 67 68 69 70 71 72
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);
}

73 74
static void
aoe_failip(struct aoedev *d)
L
Linus Torvalds 已提交
75
{
76
	struct request *rq;
L
Linus Torvalds 已提交
77
	struct bio *bio;
78 79 80
	unsigned long n;

	aoe_failbuf(d, d->ip.buf);
L
Linus Torvalds 已提交
81

82 83
	rq = d->ip.rq;
	if (rq == NULL)
84
		return;
85 86 87 88 89
	while ((bio = d->ip.nxbio)) {
		clear_bit(BIO_UPTODATE, &bio->bi_flags);
		d->ip.nxbio = bio->bi_next;
		n = (unsigned long) rq->special;
		rq->special = (void *) --n;
L
Linus Torvalds 已提交
90
	}
91 92
	if ((unsigned long) rq->special == 0)
		aoe_end_request(d, rq, 0);
93 94 95 96 97 98 99 100
}

void
aoedev_downdev(struct aoedev *d)
{
	struct aoetgt *t, **tt, **te;
	struct frame *f;
	struct list_head *head, *pos, *nx;
101
	struct request *rq;
102 103
	int i;

104 105
	d->flags &= ~DEVFL_UP;

106 107 108 109 110 111 112 113 114 115 116 117 118 119
	/* clean out active buffers */
	for (i = 0; i < NFACTIVE; i++) {
		head = &d->factive[i];
		list_for_each_safe(pos, nx, head) {
			f = list_entry(pos, struct frame, head);
			list_del(pos);
			if (f->buf) {
				f->buf->nframesout--;
				aoe_failbuf(d, f->buf);
			}
			aoe_freetframe(f);
		}
	}
	/* reset window dressings */
120 121 122 123 124 125 126
	tt = d->targets;
	te = tt + NTARGETS;
	for (; tt < te && (t = *tt); tt++) {
		t->maxout = t->nframes;
		t->nout = 0;
	}

127 128
	/* clean out the in-process request (if any) */
	aoe_failip(d);
129
	d->htgt = NULL;
L
Linus Torvalds 已提交
130

131 132 133 134 135 136
	/* fast fail all pending I/O */
	if (d->blkq) {
		while ((rq = blk_peek_request(d->blkq))) {
			blk_start_request(rq);
			aoe_end_request(d, rq, 1);
		}
L
Linus Torvalds 已提交
137 138 139
	}

	if (d->gd)
140
		set_capacity(d->gd, 0);
L
Linus Torvalds 已提交
141 142
}

143 144 145 146 147
static void
aoedev_freedev(struct aoedev *d)
{
	struct aoetgt **t, **e;

T
Tejun Heo 已提交
148
	cancel_work_sync(&d->work);
149 150 151 152
	if (d->gd) {
		aoedisk_rm_sysfs(d);
		del_gendisk(d->gd);
		put_disk(d->gd);
153
		blk_cleanup_queue(d->blkq);
154 155 156 157
	}
	t = d->targets;
	e = t + NTARGETS;
	for (; t < e && *t; t++)
158
		freetgt(d, *t);
159 160
	if (d->bufpool)
		mempool_destroy(d->bufpool);
161
	skbpoolfree(d);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	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))
188 189
		|| d->nopen
		|| d->ref) {
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
			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;
}

210 211 212 213 214
/* This has been confirmed to occur once with Tms=3*1000 due to the
 * driver changing link and not processing its transmit ring.  The
 * problem is hard enough to solve by returning an error that I'm
 * still punting on "solving" this.
 */
215 216 217
static void
skbfree(struct sk_buff *skb)
{
218
	enum { Sms = 250, Tms = 30 * 1000};
219 220 221 222 223 224
	int i = Tms / Sms;

	if (skb == NULL)
		return;
	while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
		msleep(Sms);
R
Roel Kluin 已提交
225
	if (i < 0) {
226 227 228 229 230 231
		printk(KERN_ERR
			"aoe: %s holds ref: %s\n",
			skb->dev ? skb->dev->name : "netif",
			"cannot free skb -- memory leaked.");
		return;
	}
232
	skb->truesize -= skb->data_len;
233 234 235 236 237 238 239 240
	skb_shinfo(skb)->nr_frags = skb->data_len = 0;
	skb_trim(skb, 0);
	dev_kfree_skb(skb);
}

static void
skbpoolfree(struct aoedev *d)
{
241
	struct sk_buff *skb, *tmp;
242

243
	skb_queue_walk_safe(&d->skbpool, skb, tmp)
244
		skbfree(skb);
245 246

	__skb_queue_head_init(&d->skbpool);
247 248
}

249
/* find it or malloc it */
L
Linus Torvalds 已提交
250
struct aoedev *
251
aoedev_by_sysminor_m(ulong sysminor)
L
Linus Torvalds 已提交
252 253
{
	struct aoedev *d;
254
	int i;
L
Linus Torvalds 已提交
255 256 257 258 259
	ulong flags;

	spin_lock_irqsave(&devlist_lock, flags);

	for (d=devlist; d; d=d->next)
260 261
		if (d->sysminor == sysminor) {
			d->ref++;
L
Linus Torvalds 已提交
262
			break;
263
		}
264 265 266 267 268 269 270
	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);
271
	skb_queue_head_init(&d->skbpool);
272 273 274 275 276 277 278
	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;
279
	d->ref = 1;
280 281
	for (i = 0; i < NFACTIVE; i++)
		INIT_LIST_HEAD(&d->factive[i]);
282 283 284 285 286 287 288
	d->sysminor = sysminor;
	d->aoemajor = AOEMAJOR(sysminor);
	d->aoeminor = AOEMINOR(sysminor);
	d->mintimer = MINTIMER;
	d->next = devlist;
	devlist = d;
 out:
289
	spin_unlock_irqrestore(&devlist_lock, flags);
L
Linus Torvalds 已提交
290 291 292 293
	return d;
}

static void
294
freetgt(struct aoedev *d, struct aoetgt *t)
L
Linus Torvalds 已提交
295
{
296 297
	struct frame *f;
	struct list_head *pos, *nx, *head;
E
Ed L. Cashin 已提交
298

299 300 301 302
	head = &t->ffree;
	list_for_each_safe(pos, nx, head) {
		list_del(pos);
		f = list_entry(pos, struct frame, head);
303
		skbfree(f->skb);
304 305
		kfree(f);
	}
306 307 308
	kfree(t);
}

L
Linus Torvalds 已提交
309 310 311 312 313 314
void
aoedev_exit(void)
{
	struct aoedev *d;
	ulong flags;

315
	aoe_flush_iocq();
L
Linus Torvalds 已提交
316 317 318 319 320
	while ((d = devlist)) {
		devlist = d->next;

		spin_lock_irqsave(&d->lock, flags);
		aoedev_downdev(d);
321
		d->flags |= DEVFL_TKILL;
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329 330 331 332 333
		spin_unlock_irqrestore(&d->lock, flags);

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

int __init
aoedev_init(void)
{
	return 0;
}