blob: fa67027789aab80ca8c11deccad4e2d3ec225697 [file] [log] [blame]
Ed L. Cashin52e112b2008-02-08 04:20:09 -08001/* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * aoedev.c
4 * AoE device utility functions; maintains device list.
5 */
6
7#include <linux/hdreg.h>
8#include <linux/blkdev.h>
9#include <linux/netdevice.h>
Ed L. Cashin9bb237b2008-02-08 04:20:05 -080010#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "aoe.h"
12
Ed L. Cashin262bf542008-02-08 04:20:03 -080013static void dummy_timer(ulong);
14static void aoedev_freedev(struct aoedev *);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -080015static void freetgt(struct aoedev *d, struct aoetgt *t);
16static void skbpoolfree(struct aoedev *d);
Ed L. Cashin262bf542008-02-08 04:20:03 -080017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018static struct aoedev *devlist;
Andrew Morton476aed32008-02-08 04:20:10 -080019static DEFINE_SPINLOCK(devlist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct aoedev *
ecashin@coraid.com32465c62005-04-18 22:00:18 -070022aoedev_by_aoeaddr(int maj, int min)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 struct aoedev *d;
25 ulong flags;
26
27 spin_lock_irqsave(&devlist_lock, flags);
28
29 for (d=devlist; d; d=d->next)
ecashin@coraid.com32465c62005-04-18 22:00:18 -070030 if (d->aoemajor == maj && d->aoeminor == min)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 break;
32
33 spin_unlock_irqrestore(&devlist_lock, flags);
34 return d;
35}
36
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050037static void
38dummy_timer(ulong vp)
39{
40 struct aoedev *d;
41
42 d = (struct aoedev *)vp;
43 if (d->flags & DEVFL_TKILL)
44 return;
45 d->timer.expires = jiffies + HZ;
46 add_timer(&d->timer);
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049void
50aoedev_downdev(struct aoedev *d)
51{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080052 struct aoetgt **t, **te;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct frame *f, *e;
54 struct buf *buf;
55 struct bio *bio;
56
Ed L. Cashin68e0d422008-02-08 04:20:00 -080057 t = d->targets;
58 te = t + NTARGETS;
59 for (; t < te && *t; t++) {
60 f = (*t)->frames;
61 e = f + (*t)->nframes;
62 for (; f < e; f->tag = FREETAG, f->buf = NULL, f++) {
63 if (f->tag == FREETAG || f->buf == NULL)
64 continue;
65 buf = f->buf;
66 bio = buf->bio;
67 if (--buf->nframesout == 0
68 && buf != d->inprocess) {
69 mempool_free(buf, d->bufpool);
70 bio_endio(bio, -EIO);
71 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -080073 (*t)->maxout = (*t)->nframes;
74 (*t)->nout = 0;
75 }
76 buf = d->inprocess;
77 if (buf) {
78 bio = buf->bio;
79 mempool_free(buf, d->bufpool);
80 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 d->inprocess = NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -080083 d->htgt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 while (!list_empty(&d->bufq)) {
86 buf = container_of(d->bufq.next, struct buf, bufs);
87 list_del(d->bufq.next);
88 bio = buf->bio;
89 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +020090 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (d->gd)
Tejun Heo80795ae2008-08-25 19:56:07 +090094 set_capacity(d->gd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Ed L. Cashin68e0d422008-02-08 04:20:00 -080096 d->flags &= ~DEVFL_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Ed L. Cashin262bf542008-02-08 04:20:03 -080099static void
100aoedev_freedev(struct aoedev *d)
101{
102 struct aoetgt **t, **e;
103
104 if (d->gd) {
105 aoedisk_rm_sysfs(d);
106 del_gendisk(d->gd);
107 put_disk(d->gd);
108 }
109 t = d->targets;
110 e = t + NTARGETS;
111 for (; t < e && *t; t++)
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800112 freetgt(d, *t);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800113 if (d->bufpool)
114 mempool_destroy(d->bufpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800115 skbpoolfree(d);
Ed Cashin7135a712009-09-09 14:10:18 +0200116 blk_cleanup_queue(d->blkq);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800117 kfree(d);
118}
119
120int
121aoedev_flush(const char __user *str, size_t cnt)
122{
123 ulong flags;
124 struct aoedev *d, **dd;
125 struct aoedev *rmd = NULL;
126 char buf[16];
127 int all = 0;
128
129 if (cnt >= 3) {
130 if (cnt > sizeof buf)
131 cnt = sizeof buf;
132 if (copy_from_user(buf, str, cnt))
133 return -EFAULT;
134 all = !strncmp(buf, "all", 3);
135 }
136
137 flush_scheduled_work();
138 spin_lock_irqsave(&devlist_lock, flags);
139 dd = &devlist;
140 while ((d = *dd)) {
141 spin_lock(&d->lock);
142 if ((!all && (d->flags & DEVFL_UP))
143 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
144 || d->nopen) {
145 spin_unlock(&d->lock);
146 dd = &d->next;
147 continue;
148 }
149 *dd = d->next;
150 aoedev_downdev(d);
151 d->flags |= DEVFL_TKILL;
152 spin_unlock(&d->lock);
153 d->next = rmd;
154 rmd = d;
155 }
156 spin_unlock_irqrestore(&devlist_lock, flags);
157 while ((d = rmd)) {
158 rmd = d->next;
159 del_timer_sync(&d->timer);
160 aoedev_freedev(d); /* must be able to sleep */
161 }
162 return 0;
163}
164
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800165/* I'm not really sure that this is a realistic problem, but if the
166network driver goes gonzo let's just leak memory after complaining. */
167static void
168skbfree(struct sk_buff *skb)
169{
170 enum { Sms = 100, Tms = 3*1000};
171 int i = Tms / Sms;
172
173 if (skb == NULL)
174 return;
175 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
176 msleep(Sms);
Roel Kluin94873112009-03-04 00:07:57 -0800177 if (i < 0) {
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800178 printk(KERN_ERR
179 "aoe: %s holds ref: %s\n",
180 skb->dev ? skb->dev->name : "netif",
181 "cannot free skb -- memory leaked.");
182 return;
183 }
184 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
185 skb_trim(skb, 0);
186 dev_kfree_skb(skb);
187}
188
189static void
190skbpoolfree(struct aoedev *d)
191{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700192 struct sk_buff *skb, *tmp;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800193
David S. Millere9bb8fb2008-09-21 22:36:49 -0700194 skb_queue_walk_safe(&d->skbpool, skb, tmp)
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800195 skbfree(skb);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700196
197 __skb_queue_head_init(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800198}
199
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500200/* find it or malloc it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201struct aoedev *
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800202aoedev_by_sysminor_m(ulong sysminor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct aoedev *d;
205 ulong flags;
206
207 spin_lock_irqsave(&devlist_lock, flags);
208
209 for (d=devlist; d; d=d->next)
Ed L Cashin93d489f2005-04-29 10:24:22 -0400210 if (d->sysminor == sysminor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800212 if (d)
213 goto out;
214 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
215 if (!d)
216 goto out;
217 INIT_WORK(&d->work, aoecmd_sleepwork);
218 spin_lock_init(&d->lock);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700219 skb_queue_head_init(&d->sendq);
220 skb_queue_head_init(&d->skbpool);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800221 init_timer(&d->timer);
222 d->timer.data = (ulong) d;
223 d->timer.function = dummy_timer;
224 d->timer.expires = jiffies + HZ;
225 add_timer(&d->timer);
226 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
227 d->tgt = d->targets;
228 INIT_LIST_HEAD(&d->bufq);
229 d->sysminor = sysminor;
230 d->aoemajor = AOEMAJOR(sysminor);
231 d->aoeminor = AOEMINOR(sysminor);
232 d->mintimer = MINTIMER;
233 d->next = devlist;
234 devlist = d;
235 out:
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500236 spin_unlock_irqrestore(&devlist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return d;
238}
239
240static void
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800241freetgt(struct aoedev *d, struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400243 struct frame *f, *e;
244
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800245 f = t->frames;
246 e = f + t->nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800247 for (; f < e; f++)
248 skbfree(f->skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800249 kfree(t->frames);
250 kfree(t);
251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253void
254aoedev_exit(void)
255{
256 struct aoedev *d;
257 ulong flags;
258
259 flush_scheduled_work();
260
261 while ((d = devlist)) {
262 devlist = d->next;
263
264 spin_lock_irqsave(&d->lock, flags);
265 aoedev_downdev(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500266 d->flags |= DEVFL_TKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_unlock_irqrestore(&d->lock, flags);
268
269 del_timer_sync(&d->timer);
270 aoedev_freedev(d);
271 }
272}
273
274int __init
275aoedev_init(void)
276{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278}