aoe: zero copy write 1 of 2
[safe/jmp/linux-2.6] / drivers / block / aoe / aoedev.c
1 /* Copyright (c) 2006 Coraid, Inc.  See COPYING for GPL terms. */
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>
10 #include "aoe.h"
11
12 static struct aoedev *devlist;
13 static spinlock_t devlist_lock;
14
15 int
16 aoedev_isbusy(struct aoedev *d)
17 {
18         struct frame *f, *e;
19
20         f = d->frames;
21         e = f + d->nframes;
22         do {
23                 if (f->tag != FREETAG)
24                         return 1;
25         } while (++f < e);
26
27         return 0;
28 }
29
30 struct aoedev *
31 aoedev_by_aoeaddr(int maj, int min)
32 {
33         struct aoedev *d;
34         ulong flags;
35
36         spin_lock_irqsave(&devlist_lock, flags);
37
38         for (d=devlist; d; d=d->next)
39                 if (d->aoemajor == maj && d->aoeminor == min)
40                         break;
41
42         spin_unlock_irqrestore(&devlist_lock, flags);
43         return d;
44 }
45
46 static void
47 dummy_timer(ulong vp)
48 {
49         struct aoedev *d;
50
51         d = (struct aoedev *)vp;
52         if (d->flags & DEVFL_TKILL)
53                 return;
54         d->timer.expires = jiffies + HZ;
55         add_timer(&d->timer);
56 }
57
58 /* called with devlist lock held */
59 static struct aoedev *
60 aoedev_newdev(ulong nframes)
61 {
62         struct aoedev *d;
63         struct frame *f, *e;
64
65         d = kzalloc(sizeof *d, GFP_ATOMIC);
66         f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
67         switch (!d || !f) {
68         case 0:
69                 d->nframes = nframes;
70                 d->frames = f;
71                 e = f + nframes;
72                 for (; f<e; f++) {
73                         f->tag = FREETAG;
74                         f->skb = new_skb(ETH_ZLEN);
75                         if (!f->skb)
76                                 break;
77                 }
78                 if (f == e)
79                         break;
80                 while (f > d->frames) {
81                         f--;
82                         dev_kfree_skb(f->skb);
83                 }
84         default:
85                 if (f)
86                         kfree(f);
87                 if (d)
88                         kfree(d);
89                 return NULL;
90         }
91         INIT_WORK(&d->work, aoecmd_sleepwork, d);
92         spin_lock_init(&d->lock);
93         init_timer(&d->timer);
94         d->timer.data = (ulong) d;
95         d->timer.function = dummy_timer;
96         d->timer.expires = jiffies + HZ;
97         add_timer(&d->timer);
98         d->bufpool = NULL;      /* defer to aoeblk_gdalloc */
99         INIT_LIST_HEAD(&d->bufq);
100         d->next = devlist;
101         devlist = d;
102
103         return d;
104 }
105
106 void
107 aoedev_downdev(struct aoedev *d)
108 {
109         struct frame *f, *e;
110         struct buf *buf;
111         struct bio *bio;
112
113         f = d->frames;
114         e = f + d->nframes;
115         for (; f<e; f->tag = FREETAG, f->buf = NULL, f++) {
116                 if (f->tag == FREETAG || f->buf == NULL)
117                         continue;
118                 buf = f->buf;
119                 bio = buf->bio;
120                 if (--buf->nframesout == 0) {
121                         mempool_free(buf, d->bufpool);
122                         bio_endio(bio, bio->bi_size, -EIO);
123                 }
124         }
125         d->inprocess = NULL;
126
127         while (!list_empty(&d->bufq)) {
128                 buf = container_of(d->bufq.next, struct buf, bufs);
129                 list_del(d->bufq.next);
130                 bio = buf->bio;
131                 mempool_free(buf, d->bufpool);
132                 bio_endio(bio, bio->bi_size, -EIO);
133         }
134
135         if (d->gd)
136                 d->gd->capacity = 0;
137
138         d->flags &= ~(DEVFL_UP | DEVFL_PAUSE);
139 }
140
141 /* find it or malloc it */
142 struct aoedev *
143 aoedev_by_sysminor_m(ulong sysminor, ulong bufcnt)
144 {
145         struct aoedev *d;
146         ulong flags;
147
148         spin_lock_irqsave(&devlist_lock, flags);
149
150         for (d=devlist; d; d=d->next)
151                 if (d->sysminor == sysminor)
152                         break;
153
154         if (d == NULL) {
155                 d = aoedev_newdev(bufcnt);
156                 if (d == NULL) {
157                         spin_unlock_irqrestore(&devlist_lock, flags);
158                         printk(KERN_INFO "aoe: aoedev_set: aoedev_newdev failure.\n");
159                         return NULL;
160                 }
161                 d->sysminor = sysminor;
162                 d->aoemajor = AOEMAJOR(sysminor);
163                 d->aoeminor = AOEMINOR(sysminor);
164         }
165
166         spin_unlock_irqrestore(&devlist_lock, flags);
167         return d;
168 }
169
170 static void
171 aoedev_freedev(struct aoedev *d)
172 {
173         struct frame *f, *e;
174
175         if (d->gd) {
176                 aoedisk_rm_sysfs(d);
177                 del_gendisk(d->gd);
178                 put_disk(d->gd);
179         }
180         f = d->frames;
181         e = f + d->nframes;
182         for (; f<e; f++) {
183                 skb_shinfo(f->skb)->nr_frags = 0;
184                 dev_kfree_skb(f->skb);
185         }
186         kfree(d->frames);
187         if (d->bufpool)
188                 mempool_destroy(d->bufpool);
189         kfree(d);
190 }
191
192 void
193 aoedev_exit(void)
194 {
195         struct aoedev *d;
196         ulong flags;
197
198         flush_scheduled_work();
199
200         while ((d = devlist)) {
201                 devlist = d->next;
202
203                 spin_lock_irqsave(&d->lock, flags);
204                 aoedev_downdev(d);
205                 d->flags |= DEVFL_TKILL;
206                 spin_unlock_irqrestore(&d->lock, flags);
207
208                 del_timer_sync(&d->timer);
209                 aoedev_freedev(d);
210         }
211 }
212
213 int __init
214 aoedev_init(void)
215 {
216         spin_lock_init(&devlist_lock);
217         return 0;
218 }
219