Ed L. Cashin | 52e112b | 2008-02-08 04:20:09 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2007 Coraid, Inc. See COPYING for GPL terms. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 10 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "aoe.h" |
| 12 | |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 13 | static void dummy_timer(ulong); |
| 14 | static void aoedev_freedev(struct aoedev *); |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 15 | static void freetgt(struct aoedev *d, struct aoetgt *t); |
| 16 | static void skbpoolfree(struct aoedev *d); |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | static struct aoedev *devlist; |
Andrew Morton | 476aed3 | 2008-02-08 04:20:10 -0800 | [diff] [blame] | 19 | static DEFINE_SPINLOCK(devlist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | struct aoedev * |
ecashin@coraid.com | 32465c6 | 2005-04-18 22:00:18 -0700 | [diff] [blame] | 22 | aoedev_by_aoeaddr(int maj, int min) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 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.com | 32465c6 | 2005-04-18 22:00:18 -0700 | [diff] [blame] | 30 | if (d->aoemajor == maj && d->aoeminor == min) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | break; |
| 32 | |
| 33 | spin_unlock_irqrestore(&devlist_lock, flags); |
| 34 | return d; |
| 35 | } |
| 36 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 37 | static void |
| 38 | dummy_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | void |
| 50 | aoedev_downdev(struct aoedev *d) |
| 51 | { |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 52 | struct aoetgt **t, **te; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | struct frame *f, *e; |
| 54 | struct buf *buf; |
| 55 | struct bio *bio; |
| 56 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 57 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 73 | (*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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | d->inprocess = NULL; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 83 | d->htgt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 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); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 90 | bio_endio(bio, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (d->gd) |
Tejun Heo | 80795ae | 2008-08-25 19:56:07 +0900 | [diff] [blame] | 94 | set_capacity(d->gd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 96 | d->flags &= ~DEVFL_UP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 99 | static void |
| 100 | aoedev_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. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 112 | freetgt(d, *t); |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 113 | if (d->bufpool) |
| 114 | mempool_destroy(d->bufpool); |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 115 | skbpoolfree(d); |
Ed L. Cashin | 262bf54 | 2008-02-08 04:20:03 -0800 | [diff] [blame] | 116 | kfree(d); |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | aoedev_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. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 164 | /* I'm not really sure that this is a realistic problem, but if the |
| 165 | network driver goes gonzo let's just leak memory after complaining. */ |
| 166 | static void |
| 167 | skbfree(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 Kluin | 9487311 | 2009-03-04 00:07:57 -0800 | [diff] [blame] | 176 | if (i < 0) { |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 177 | 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 | |
| 188 | static void |
| 189 | skbpoolfree(struct aoedev *d) |
| 190 | { |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 191 | struct sk_buff *skb, *tmp; |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 192 | |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 193 | skb_queue_walk_safe(&d->skbpool, skb, tmp) |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 194 | skbfree(skb); |
David S. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 195 | |
| 196 | __skb_queue_head_init(&d->skbpool); |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Ed L. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 199 | /* find it or malloc it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | struct aoedev * |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 201 | aoedev_by_sysminor_m(ulong sysminor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 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 Cashin | 93d489f | 2005-04-29 10:24:22 -0400 | [diff] [blame] | 209 | if (d->sysminor == sysminor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | break; |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 211 | 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. Miller | e9bb8fb0 | 2008-09-21 22:36:49 -0700 | [diff] [blame] | 218 | skb_queue_head_init(&d->sendq); |
| 219 | skb_queue_head_init(&d->skbpool); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 220 | 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. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 235 | spin_unlock_irqrestore(&devlist_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return d; |
| 237 | } |
| 238 | |
| 239 | static void |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 240 | freetgt(struct aoedev *d, struct aoetgt *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Ed L. Cashin | e407a7f | 2006-09-20 14:36:49 -0400 | [diff] [blame] | 242 | struct frame *f, *e; |
| 243 | |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 244 | f = t->frames; |
| 245 | e = f + t->nframes; |
Ed L. Cashin | 9bb237b | 2008-02-08 04:20:05 -0800 | [diff] [blame] | 246 | for (; f < e; f++) |
| 247 | skbfree(f->skb); |
Ed L. Cashin | 68e0d42 | 2008-02-08 04:20:00 -0800 | [diff] [blame] | 248 | kfree(t->frames); |
| 249 | kfree(t); |
| 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | void |
| 253 | aoedev_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. Cashin | 3ae1c24 | 2006-01-19 13:46:19 -0500 | [diff] [blame] | 265 | d->flags |= DEVFL_TKILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | spin_unlock_irqrestore(&d->lock, flags); |
| 267 | |
| 268 | del_timer_sync(&d->timer); |
| 269 | aoedev_freedev(d); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | int __init |
| 274 | aoedev_init(void) |
| 275 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return 0; |
| 277 | } |