blob: eeea477d96016596ccd729a6f75d37d5e3d30a0e [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 L. Cashin262bf542008-02-08 04:20:03 -0800116 kfree(d);
117}
118
119int
120aoedev_flush(const char __user *str, size_t cnt)
121{
122 ulong flags;
123 struct aoedev *d, **dd;
124 struct aoedev *rmd = NULL;
125 char buf[16];
126 int all = 0;
127
128 if (cnt >= 3) {
129 if (cnt > sizeof buf)
130 cnt = sizeof buf;
131 if (copy_from_user(buf, str, cnt))
132 return -EFAULT;
133 all = !strncmp(buf, "all", 3);
134 }
135
136 flush_scheduled_work();
137 spin_lock_irqsave(&devlist_lock, flags);
138 dd = &devlist;
139 while ((d = *dd)) {
140 spin_lock(&d->lock);
141 if ((!all && (d->flags & DEVFL_UP))
142 || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
143 || d->nopen) {
144 spin_unlock(&d->lock);
145 dd = &d->next;
146 continue;
147 }
148 *dd = d->next;
149 aoedev_downdev(d);
150 d->flags |= DEVFL_TKILL;
151 spin_unlock(&d->lock);
152 d->next = rmd;
153 rmd = d;
154 }
155 spin_unlock_irqrestore(&devlist_lock, flags);
156 while ((d = rmd)) {
157 rmd = d->next;
158 del_timer_sync(&d->timer);
159 aoedev_freedev(d); /* must be able to sleep */
160 }
161 return 0;
162}
163
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800164/* I'm not really sure that this is a realistic problem, but if the
165network driver goes gonzo let's just leak memory after complaining. */
166static void
167skbfree(struct sk_buff *skb)
168{
169 enum { Sms = 100, Tms = 3*1000};
170 int i = Tms / Sms;
171
172 if (skb == NULL)
173 return;
174 while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
175 msleep(Sms);
Roel Kluin94873112009-03-04 00:07:57 -0800176 if (i < 0) {
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800177 printk(KERN_ERR
178 "aoe: %s holds ref: %s\n",
179 skb->dev ? skb->dev->name : "netif",
180 "cannot free skb -- memory leaked.");
181 return;
182 }
183 skb_shinfo(skb)->nr_frags = skb->data_len = 0;
184 skb_trim(skb, 0);
185 dev_kfree_skb(skb);
186}
187
188static void
189skbpoolfree(struct aoedev *d)
190{
David S. Millere9bb8fb2008-09-21 22:36:49 -0700191 struct sk_buff *skb, *tmp;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800192
David S. Millere9bb8fb2008-09-21 22:36:49 -0700193 skb_queue_walk_safe(&d->skbpool, skb, tmp)
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800194 skbfree(skb);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700195
196 __skb_queue_head_init(&d->skbpool);
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800197}
198
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500199/* find it or malloc it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200struct aoedev *
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800201aoedev_by_sysminor_m(ulong sysminor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct aoedev *d;
204 ulong flags;
205
206 spin_lock_irqsave(&devlist_lock, flags);
207
208 for (d=devlist; d; d=d->next)
Ed L Cashin93d489f2005-04-29 10:24:22 -0400209 if (d->sysminor == sysminor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800211 if (d)
212 goto out;
213 d = kcalloc(1, sizeof *d, GFP_ATOMIC);
214 if (!d)
215 goto out;
216 INIT_WORK(&d->work, aoecmd_sleepwork);
217 spin_lock_init(&d->lock);
David S. Millere9bb8fb2008-09-21 22:36:49 -0700218 skb_queue_head_init(&d->sendq);
219 skb_queue_head_init(&d->skbpool);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800220 init_timer(&d->timer);
221 d->timer.data = (ulong) d;
222 d->timer.function = dummy_timer;
223 d->timer.expires = jiffies + HZ;
224 add_timer(&d->timer);
225 d->bufpool = NULL; /* defer to aoeblk_gdalloc */
226 d->tgt = d->targets;
227 INIT_LIST_HEAD(&d->bufq);
228 d->sysminor = sysminor;
229 d->aoemajor = AOEMAJOR(sysminor);
230 d->aoeminor = AOEMINOR(sysminor);
231 d->mintimer = MINTIMER;
232 d->next = devlist;
233 devlist = d;
234 out:
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500235 spin_unlock_irqrestore(&devlist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return d;
237}
238
239static void
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800240freetgt(struct aoedev *d, struct aoetgt *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Ed L. Cashine407a7f2006-09-20 14:36:49 -0400242 struct frame *f, *e;
243
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800244 f = t->frames;
245 e = f + t->nframes;
Ed L. Cashin9bb237b2008-02-08 04:20:05 -0800246 for (; f < e; f++)
247 skbfree(f->skb);
Ed L. Cashin68e0d422008-02-08 04:20:00 -0800248 kfree(t->frames);
249 kfree(t);
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252void
253aoedev_exit(void)
254{
255 struct aoedev *d;
256 ulong flags;
257
258 flush_scheduled_work();
259
260 while ((d = devlist)) {
261 devlist = d->next;
262
263 spin_lock_irqsave(&d->lock, flags);
264 aoedev_downdev(d);
Ed L. Cashin3ae1c242006-01-19 13:46:19 -0500265 d->flags |= DEVFL_TKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 spin_unlock_irqrestore(&d->lock, flags);
267
268 del_timer_sync(&d->timer);
269 aoedev_freedev(d);
270 }
271}
272
273int __init
274aoedev_init(void)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}