blob: f9a1cd9edb77e156e5a7f81c389f26ea21161f41 [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
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050021int
22aoedev_isbusy(struct aoedev *d)
23{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080024 struct aoetgt **t, **te;
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050025 struct frame *f, *e;
26
Ed L. Cashin68e0d422008-02-08 04:20:00 -080027 t = d->targets;
28 te = t + NTARGETS;
29 for (; t < te && *t; t++) {
30 f = (*t)->frames;
31 e = f + (*t)->nframes;
32 for (; f < e; f++)
33 if (f->tag != FREETAG)
34 return 1;
35 }
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050036 return 0;
37}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct aoedev *
ecashin@coraid.com32465c62005-04-18 22:00:18 -070040aoedev_by_aoeaddr(int maj, int min)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct aoedev *d;
43 ulong flags;
44
45 spin_lock_irqsave(&devlist_lock, flags);
46
47 for (d=devlist; d; d=d->next)
ecashin@coraid.com32465c62005-04-18 22:00:18 -070048 if (d->aoemajor == maj && d->aoeminor == min)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 break;
50
51 spin_unlock_irqrestore(&devlist_lock, flags);
52 return d;
53}
54
Ed L. Cashin3ae1c242006-01-19 13:46:19 -050055static void
56dummy_timer(ulong vp)
57{
58 struct aoedev *d;
59
60 d = (struct aoedev *)vp;
61 if (d->flags & DEVFL_TKILL)
62 return;
63 d->timer.expires = jiffies + HZ;
64 add_timer(&d->timer);
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067void
68aoedev_downdev(struct aoedev *d)
69{
Ed L. Cashin68e0d422008-02-08 04:20:00 -080070 struct aoetgt **t, **te;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 struct frame *f, *e;
72 struct buf *buf;
73 struct bio *bio;
74
Ed L. Cashin68e0d422008-02-08 04:20:00 -080075 t = d->targets;
76 te = t + NTARGETS;
77 for (; t < te && *t; t++) {
78 f = (*t)->frames;
79 e = f + (*t)->nframes;
80 for (; f < e; f->tag = FREETAG, f->buf = NULL, f++) {
81 if (f->tag == FREETAG || f->buf == NULL)
82 continue;
83 buf = f->buf;
84 bio = buf->bio;
85 if (--buf->nframesout == 0
86 && buf != d->inprocess) {
87 mempool_free(buf, d->bufpool);
88 bio_endio(bio, -EIO);
89 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Ed L. Cashin68e0d422008-02-08 04:20:00 -080091 (*t)->maxout = (*t)->nframes;
92 (*t)->nout = 0;
93 }
94 buf = d->inprocess;
95 if (buf) {
96 bio = buf->bio;
97 mempool_free(buf, d->bufpool);
98 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 d->inprocess = NULL;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800101 d->htgt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 while (!list_empty(&d->bufq)) {
104 buf = container_of(d->bufq.next, struct buf, bufs);
105 list_del(d->bufq.next);
106 bio = buf->bio;
107 mempool_free(buf, d->bufpool);
NeilBrown6712ecf2007-09-27 12:47:43 +0200108 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (d->gd)
112 d->gd->capacity = 0;
113
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800114 d->flags &= ~DEVFL_UP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Ed L. Cashin262bf542008-02-08 04:20:03 -0800117static void
118aoedev_freedev(struct aoedev *d)
119{
120 struct aoetgt **t, **e;
121
122 if (d->gd) {
123 aoedisk_rm_sysfs(d);
124 del_gendisk(d->gd);
125 put_disk(d->gd);
126 }
127 t = d->targets;
128 e = t + NTARGETS;
129 for (; t < e && *t; t++)
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800130 freetgt(d, *t);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800131 if (d->bufpool)
132 mempool_destroy(d->bufpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800133 skbpoolfree(d);
Ed L. Cashin262bf542008-02-08 04:20:03 -0800134 kfree(d);
135}
136
137int
138aoedev_flush(const char __user *str, size_t cnt)
139{
140 ulong flags;
141 struct aoedev *d, **dd;
142 struct aoedev *rmd = NULL;
143 char buf[16];
144 int all = 0;
145
146 if (cnt >= 3) {
147 if (cnt > sizeof buf)
148 cnt = sizeof buf;
149 if (copy_from_user(buf, str, cnt))
150 return -EFAULT;
151 all = !strncmp(buf, "all", 3);
152 }
153
154 flush_scheduled_work();
155 spin_lock_irqsave(&devlist_lock, flags);
156 dd = &devlist;
157 while ((d = *dd)) {
158 spin_lock(&d->lock);
159 if ((!all && (d->flags & DEVFL_UP))
160 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
161 || d->nopen) {
162 spin_unlock(&d->lock);
163 dd = &d->next;
164 continue;
165 }
166 *dd = d->next;
167 aoedev_downdev(d);
168 d->flags |= DEVFL_TKILL;
169 spin_unlock(&d->lock);
170 d->next = rmd;
171 rmd = d;
172 }
173 spin_unlock_irqrestore(&devlist_lock, flags);
174 while ((d = rmd)) {
175 rmd = d->next;
176 del_timer_sync(&d->timer);
177 aoedev_freedev(d); /* must be able to sleep */
178 }
179 return 0;
180}
181
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800182/* I'm not really sure that this is a realistic problem, but if the
183network driver goes gonzo let's just leak memory after complaining. */
184static void
185skbfree(struct sk_buff *skb)
186{
187 enum { Sms = 100, Tms = 3*1000};
188 int i = Tms / Sms;
189
190 if (skb == NULL)
191 return;
192 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
193 msleep(Sms);
194 if (i <= 0) {
195 printk(KERN_ERR
196 "aoe: %s holds ref: %s\n",
197 skb->dev ? skb->dev->name : "netif",
198 "cannot free skb -- memory leaked.");
199 return;
200 }
201 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
202 skb_trim(skb, 0);
203 dev_kfree_skb(skb);
204}
205
206static void
207skbpoolfree(struct aoedev *d)
208{
209 struct sk_buff *skb;
210
211 while ((skb = d->skbpool_hd)) {
212 d->skbpool_hd = skb->next;
213 skb->next = NULL;
214 skbfree(skb);
215 }
216 d->skbpool_tl = NULL;
217}
218
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500219/* find it or malloc it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220struct aoedev *
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800221aoedev_by_sysminor_m(ulong sysminor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct aoedev *d;
224 ulong flags;
225
226 spin_lock_irqsave(&devlist_lock, flags);
227
228 for (d=devlist; d; d=d->next)
Ed L Cashin93d489f2005-04-29 10:24:22 -0400229 if (d->sysminor == sysminor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800231 if (d)
232 goto out;
233 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
234 if (!d)
235 goto out;
236 INIT_WORK(&d->work, aoecmd_sleepwork);
237 spin_lock_init(&d->lock);
238 init_timer(&d->timer);
239 d->timer.data = (ulong) d;
240 d->timer.function = dummy_timer;
241 d->timer.expires = jiffies + HZ;
242 add_timer(&d->timer);
243 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
244 d->tgt = d->targets;
245 INIT_LIST_HEAD(&d->bufq);
246 d->sysminor = sysminor;
247 d->aoemajor = AOEMAJOR(sysminor);
248 d->aoeminor = AOEMINOR(sysminor);
249 d->mintimer = MINTIMER;
250 d->next = devlist;
251 devlist = d;
252 out:
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500253 spin_unlock_irqrestore(&devlist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return d;
255}
256
257static void
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800258freetgt(struct aoedev *d, struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400260 struct frame *f, *e;
261
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800262 f = t->frames;
263 e = f + t->nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800264 for (; f < e; f++)
265 skbfree(f->skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800266 kfree(t->frames);
267 kfree(t);
268}
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270void
271aoedev_exit(void)
272{
273 struct aoedev *d;
274 ulong flags;
275
276 flush_scheduled_work();
277
278 while ((d = devlist)) {
279 devlist = d->next;
280
281 spin_lock_irqsave(&d->lock, flags);
282 aoedev_downdev(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500283 d->flags |= DEVFL_TKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 spin_unlock_irqrestore(&d->lock, flags);
285
286 del_timer_sync(&d->timer);
287 aoedev_freedev(d);
288 }
289}
290
291int __init
292aoedev_init(void)
293{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}