aoe  87
About: AoE (ATA over Ethernet) Protocol Driver (Linux kernel 4.8.x and greater are not supported).
  Fossies Dox: aoe-87.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

aoedev.c
Go to the documentation of this file.
1 /* Copyright (c) 2013 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 <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/bitmap.h>
13 #include <linux/kdev_t.h>
14 #include <linux/moduleparam.h>
15 #include "aoe.h"
16 
17 static void dummy_timer(ulong);
18 static void freetgt(struct aoedev *d, struct aoetgt *t);
19 static void skbpoolfree(struct aoedev *d);
20 
21 static int aoe_dyndevs = AOE_DYNDEVS; /* AOE_DYNDEVS set in Makefile */
22 module_param(aoe_dyndevs, int, 0644);
23 MODULE_PARM_DESC(aoe_dyndevs, "Use dynamic minor numbers for devices.");
24 
25 static struct aoedev *devlist;
26 static DEFINE_SPINLOCK(devlist_lock);
27 
28 /* Because some systems will have one, many, or no
29  * - partitions,
30  * - slots per shelf,
31  * - or shelves,
32  * we need some flexibility in the way the minor numbers
33  * are allocated. So they are dynamic.
34  */
35 #define N_DEVS ((1U<<MINORBITS)/AOE_PARTITIONS)
36 
37 static DEFINE_SPINLOCK(used_minors_lock);
38 static DECLARE_BITMAP(used_minors, N_DEVS);
39 
40 static int
42 {
43  ulong flags;
44  ulong n;
45  int error = 0;
46 
47  spin_lock_irqsave(&used_minors_lock, flags);
48  n = find_first_zero_bit(used_minors, N_DEVS);
49  if (n < N_DEVS)
50  set_bit(n, used_minors);
51  else
52  error = -1;
53  spin_unlock_irqrestore(&used_minors_lock, flags);
54 
55  *sysminor = n * AOE_PARTITIONS;
56  return error;
57 }
58 
59 static int
60 minor_get_static(ulong *sysminor, ulong aoemaj, int aoemin)
61 {
62  ulong flags;
63  ulong n;
64  int error = 0;
65  enum {
66  /* for backwards compatibility when !aoe_dyndevs,
67  * a static number of supported slots per shelf */
68  NPERSHELF = 16,
69  };
70 
71  if (aoemin >= NPERSHELF) {
72  printk(KERN_ERR
73  "aoe: e%ld.%d: %s %d slots per shelf\n",
74  aoemaj, aoemin,
75  "static minor device numbers support only",
76  NPERSHELF);
77  error = -1;
78  goto out;
79  }
80 
81  n = aoemaj * NPERSHELF + aoemin;
82  if (n >= N_DEVS) {
83  printk(KERN_ERR
84  "aoe: %s with e%ld.%d\n",
85  "cannot use static minor device numbers",
86  aoemaj, aoemin);
87  error = -1;
88  goto out;
89  }
90 
91  spin_lock_irqsave(&used_minors_lock, flags);
92  if (test_bit(n, used_minors)) {
93  printk(KERN_ERR
94  "aoe: %s %lu\n",
95  "existing device already has static minor number",
96  n);
97  error = -1;
98  } else
99  set_bit(n, used_minors);
100  spin_unlock_irqrestore(&used_minors_lock, flags);
101  *sysminor = n * AOE_PARTITIONS;
102 out:
103  return error;
104 }
105 
106 static int
107 minor_get(ulong *sysminor, ulong aoemaj, int aoemin)
108 {
109  if (aoe_dyndevs)
110  return minor_get_dyn(sysminor);
111  else
112  return minor_get_static(sysminor, aoemaj, aoemin);
113 }
114 
115 static void
117 {
118  ulong flags;
119 
120  minor /= AOE_PARTITIONS;
121  BUG_ON(minor >= N_DEVS);
122 
123  spin_lock_irqsave(&used_minors_lock, flags);
124  clear_bit(minor, used_minors);
125  spin_unlock_irqrestore(&used_minors_lock, flags);
126 }
127 
128 /*
129  * Users who grab a pointer to the device with aoedev_by_aoeaddr
130  * automatically get a reference count and must be responsible
131  * for performing a aoedev_put. With the addition of async
132  * kthread processing I'm no longer confident that we can
133  * guarantee consistency in the face of device flushes.
134  *
135  * For the time being, we only bother to add extra references for
136  * frames sitting on the iocq. When the kthreads finish processing
137  * these frames, they will aoedev_put the device.
138  */
139 
140 void
141 aoedev_put(struct aoedev *d)
142 {
143  ulong flags;
144 
145  spin_lock_irqsave(&devlist_lock, flags);
146  d->ref--;
147  spin_unlock_irqrestore(&devlist_lock, flags);
148 }
149 
150 static void
152 {
153  struct aoedev *d;
154 
155  d = (struct aoedev *)vp;
156  if (d->flags & DEVFL_TKILL)
157  return;
158  d->timer.expires = jiffies + HZ;
159  add_timer(&d->timer);
160 }
161 
162 static void
163 aoe_failip(struct aoedev *d)
164 {
165  struct request *rq;
166  struct bio *bio;
167  unsigned long n;
168 
169  aoe_failbuf(d, d->ip.buf);
170 
171  rq = d->ip.rq;
172  if (rq == NULL)
173  return;
174  while ((bio = d->ip.nxbio)) {
175  bio->bi_error = -EIO;
176  d->ip.nxbio = bio->bi_next;
177  n = (unsigned long) rq->special;
178  rq->special = (void *) --n;
179  }
180  if ((unsigned long) rq->special == 0)
181  aoe_end_request(d, rq, 0);
182 }
183 
184 static void
185 downdev_frame(struct list_head *pos)
186 {
187  struct frame *f;
188 
189  f = list_entry(pos, struct frame, head);
190  list_del(pos);
191  if (f->buf) {
192  f->buf->nframesout--;
193  aoe_failbuf(f->t->d, f->buf);
194  }
195  aoe_freetframe(f);
196 }
197 
198 void
200 {
201  struct aoetgt *t, **tt, **te;
202  struct list_head *head, *pos, *nx;
203  struct request *rq;
204  int i;
205 
206  d->flags &= ~DEVFL_UP;
207 
208  /* clean out active and to-be-retransmitted buffers */
209  for (i = 0; i < NFACTIVE; i++) {
210  head = &d->factive[i];
211  list_for_each_safe(pos, nx, head)
212  downdev_frame(pos);
213  }
214  head = &d->rexmitq;
215  list_for_each_safe(pos, nx, head)
216  downdev_frame(pos);
217 
218  /* reset window dressings */
219  tt = d->targets;
220  te = tt + d->ntargets;
221  for (; tt < te && (t = *tt); tt++) {
222  aoecmd_wreset(t);
223  t->nout = 0;
224  }
225 
226  /* clean out the in-process request (if any) */
227  aoe_failip(d);
228 
229  /* fast fail all pending I/O */
230  if (d->blkq) {
231  while ((rq = blk_peek_request(d->blkq))) {
232  blk_start_request(rq);
233  aoe_end_request(d, rq, 1);
234  }
235  }
236 
237  if (d->gd)
238  set_capacity(d->gd, 0);
239 }
240 
241 /* return whether the user asked for this particular
242  * device to be flushed
243  */
244 static int
245 user_req(char *s, size_t slen, struct aoedev *d)
246 {
247  char *p;
248  size_t lim;
249 
250  if (!d->gd)
251  return 0;
252  p = strrchr(d->gd->disk_name, '/');
253  if (!p)
254  p = d->gd->disk_name;
255  else
256  p += 1;
257  lim = sizeof(d->gd->disk_name);
258  lim -= p - d->gd->disk_name;
259  if (slen < lim)
260  lim = slen;
261 
262  return !strncmp(s, p, lim);
263 }
264 
265 static void
266 freedev(struct aoedev *d)
267 {
268  struct aoetgt **t, **e;
269  int freeing = 0;
270  unsigned long flags;
271 
272  spin_lock_irqsave(&d->lock, flags);
273  if (d->flags & DEVFL_TKILL
274  && !(d->flags & DEVFL_FREEING)) {
275  d->flags |= DEVFL_FREEING;
276  freeing = 1;
277  }
278  spin_unlock_irqrestore(&d->lock, flags);
279  if (!freeing)
280  return;
281 
282  del_timer_sync(&d->timer);
283  aoedbg_undbg(d);
284  if (d->gd) {
287  del_gendisk(d->gd);
288  put_disk(d->gd);
289  blk_cleanup_queue(d->blkq);
290  }
291  t = d->targets;
292  e = t + d->ntargets;
293  for (; t < e && *t; t++)
294  freetgt(d, *t);
295  if (d->bufpool)
296  mempool_destroy(d->bufpool);
297  skbpoolfree(d);
299 
300  spin_lock_irqsave(&d->lock, flags);
301  d->flags |= DEVFL_FREED;
302  spin_unlock_irqrestore(&d->lock, flags);
303 }
304 
307  EXITING = 1,
308 };
309 
310 static int
311 flush(const char __user *str, size_t cnt, int exiting)
312 {
313  ulong flags;
314  struct aoedev *d, **dd;
315  char buf[16];
316  int all = 0;
317  int specified = 0; /* flush a specific device */
318  unsigned int skipflags;
319 
320  skipflags = DEVFL_GDALLOC | DEVFL_NEWSIZE | DEVFL_TKILL;
321 
322  if (!exiting && cnt >= 3) {
323  if (cnt > sizeof buf)
324  cnt = sizeof buf;
325  if (copy_from_user(buf, str, cnt))
326  return -EFAULT;
327  all = !strncmp(buf, "all", 3);
328  if (!all)
329  specified = 1;
330  }
331 
332  flush_scheduled_work();
333  /* pass one: without sleeping, do aoedev_downdev */
334  spin_lock_irqsave(&devlist_lock, flags);
335  for (d = devlist; d; d = d->next) {
336  spin_lock(&d->lock);
337  if (exiting) {
338  /* unconditionally take each device down */
339  } else if (specified) {
340  if (!user_req(buf, cnt, d))
341  goto cont;
342  } else if ((!all && (d->flags & DEVFL_UP))
343  || d->flags & skipflags
344  || d->nopen
345  || d->ref)
346  goto cont;
347 
348  aoedev_downdev(d);
349  d->flags |= DEVFL_TKILL;
350 cont:
351  spin_unlock(&d->lock);
352  }
353  spin_unlock_irqrestore(&devlist_lock, flags);
354 
355  /* pass two: call freedev, which might sleep,
356  * for aoedevs marked with DEVFL_TKILL
357  */
358 restart:
359  spin_lock_irqsave(&devlist_lock, flags);
360  for (d = devlist; d; d = d->next) {
361  spin_lock(&d->lock);
362  if (d->flags & DEVFL_TKILL
363  && !(d->flags & DEVFL_FREEING)) {
364  spin_unlock(&d->lock);
365  spin_unlock_irqrestore(&devlist_lock, flags);
366  freedev(d);
367  goto restart;
368  }
369  spin_unlock(&d->lock);
370  }
371 
372  /* pass three: remove aoedevs marked with DEVFL_FREED */
373  for (dd = &devlist, d = *dd; d; d = *dd) {
374  struct aoedev *doomed = NULL;
375 
376  spin_lock(&d->lock);
377  if (d->flags & DEVFL_FREED) {
378  *dd = d->next;
379  doomed = d;
380  } else {
381  dd = &d->next;
382  }
383  spin_unlock(&d->lock);
384  if (doomed)
385  kfree(doomed->targets);
386  kfree(doomed);
387  }
388  spin_unlock_irqrestore(&devlist_lock, flags);
389 
390  return 0;
391 }
392 
393 int
394 aoedev_flush(const char __user *str, size_t cnt)
395 {
396  return flush(str, cnt, NOT_EXITING);
397 }
398 
399 /* This has been confirmed to occur once with Tms=3*1000 due to the
400  * driver changing link and not processing its transmit ring. The
401  * problem is hard enough to solve by returning an error that I'm
402  * still punting on "solving" this.
403  */
404 static void
405 skbfree(struct sk_buff *skb)
406 {
407  enum { Sms = 250, Tms = 30 * 1000};
408  int i = Tms / Sms;
409 
410  if (skb == NULL)
411  return;
412  while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
413  msleep_interruptible(Sms);
414  if (i < 0) {
415  printk(KERN_ERR
416  "aoe: %s holds ref: %s\n",
417  skb->dev ? skb->dev->name : "netif",
418  "cannot free skb -- memory leaked.");
419  return;
420  }
421  skb->truesize -= skb->data_len;
422  skb_shinfo(skb)->nr_frags = skb->data_len = 0;
423  skb_trim(skb, 0);
424  dev_kfree_skb(skb);
425 }
426 
427 static void
428 skbpoolfree(struct aoedev *d)
429 {
430  struct sk_buff *skb;
431 
432  while ((skb = d->skbpool_hd)) {
433  d->skbpool_hd = skb->next;
434  skb->next = NULL;
435  skbfree(skb);
436  }
437  d->skbpool_tl = NULL;
438 }
439 
440 /* find it or allocate it */
441 struct aoedev *
442 aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
443 {
444  struct aoedev *d;
445  int i;
446  ulong flags;
447  ulong sysminor = 0;
448 
449  spin_lock_irqsave(&devlist_lock, flags);
450 
451  for (d=devlist; d; d=d->next)
452  if (d->aoemajor == maj && d->aoeminor == min) {
453  spin_lock(&d->lock);
454  if (d->flags & DEVFL_TKILL) {
455  spin_unlock(&d->lock);
456  d = NULL;
457  goto out;
458  }
459  d->ref++;
460  spin_unlock(&d->lock);
461  break;
462  }
463  if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
464  goto out;
465  d = aoe_kcalloc(1, sizeof *d, GFP_ATOMIC);
466  if (!d)
467  goto out;
468  d->targets = aoe_kcalloc(NTARGETS, sizeof(*d->targets), GFP_ATOMIC);
469  if (!d->targets) {
470  kfree(d);
471  d = NULL;
472  goto out;
473  }
474  d->ntargets = NTARGETS;
475  INIT_WORK(&d->work, aoecmd_sleepwork);
476  spin_lock_init(&d->lock);
477  init_timer(&d->timer);
478  d->timer.data = (ulong) d;
479  d->timer.function = dummy_timer;
480  d->timer.expires = jiffies + HZ;
481  add_timer(&d->timer);
482  d->bufpool = NULL; /* defer to aoeblk_gdalloc */
483  d->tgt = d->targets;
484  d->ref = 1;
485  for (i = 0; i < NFACTIVE; i++)
486  INIT_LIST_HEAD(&d->factive[i]);
487  INIT_LIST_HEAD(&d->rexmitq);
488  d->sysminor = sysminor;
489  d->aoemajor = maj;
490  d->aoeminor = min;
491  d->rttavg = RTTAVG_INIT;
492  d->rttdev = RTTDEV_INIT;
493  d->next = devlist;
494  devlist = d;
495  out:
496  spin_unlock_irqrestore(&devlist_lock, flags);
497  return d;
498 }
499 
500 static void
501 freetgt(struct aoedev *d, struct aoetgt *t)
502 {
503  struct frame *f;
504  struct list_head *pos, *nx, *head;
505  struct aoeif *ifp;
506 
507  for (ifp = t->ifs; ifp < &t->ifs[NAOEIFS]; ++ifp) {
508  if (!ifp->nd)
509  break;
510  dev_put(ifp->nd);
511  }
512 
513  head = &t->ffree;
514  list_for_each_safe(pos, nx, head) {
515  list_del(pos);
516  f = list_entry(pos, struct frame, head);
517  skbfree(f->skb);
518  kfree(f);
519  }
520  kfree(t);
521 }
522 
523 void
525 {
526  flush_scheduled_work();
527  flush(NULL, 0, EXITING);
528 }
529 
530 int __init
532 {
533  return 0;
534 }
aoe_failip
static void aoe_failip(struct aoedev *d)
Definition: aoedev.c:163
aoecmd_sleepwork
void aoecmd_sleepwork(struct work_struct *)
Definition: aoecmd.c:966
aoedev::ref
ulong ref
Definition: aoe.h:167
aoedev_put
void aoedev_put(struct aoedev *d)
Definition: aoedev.c:141
aoedev::skbpool_hd
struct sk_buff * skbpool_hd
Definition: aoe.h:176
buf
static char buf[PAGE_SIZE]
Definition: aoedbg.c:22
aoedev::nxbio
struct bio * nxbio
Definition: aoe.h:182
aoedev::work
struct work_struct work
Definition: aoe.h:168
aoedev::rttdev
u32 rttdev
Definition: aoe.h:160
NTARGETS
Definition: aoe.h:88
freedev
static void freedev(struct aoedev *d)
Definition: aoedev.c:266
RTTDEV_INIT
Definition: aoe.h:97
aoe_kcalloc
void * aoe_kcalloc(size_t, size_t, int)
Definition: aoemain.c:32
minor_free
static void minor_free(ulong minor)
Definition: aoedev.c:116
MODULE_PARM_DESC
MODULE_PARM_DESC(aoe_dyndevs, "Use dynamic minor numbers for devices.")
aoedev::skbpool_tl
struct sk_buff * skbpool_tl
Definition: aoe.h:177
aoe_freetframe
void aoe_freetframe(struct frame *)
Definition: aoecmd.c:205
DEVFL_FREED
Definition: aoe.h:82
N_DEVS
#define N_DEVS
Definition: aoedev.c:35
user_req
static int user_req(char *s, size_t slen, struct aoedev *d)
Definition: aoedev.c:245
minor_get_dyn
static int minor_get_dyn(ulong *sysminor)
Definition: aoedev.c:41
frame::skb
struct sk_buff * skb
Definition: aoe.h:122
aoedev::blkq
struct request_queue * blkq
Definition: aoe.h:171
DECLARE_BITMAP
static DECLARE_BITMAP(used_minors,((1U<< MINORBITS)/AOE_PARTITIONS))
frame::buf
struct buf * buf
Definition: aoe.h:124
frame
Definition: aoe.h:114
aoedev::buf
struct buf * buf
Definition: aoe.h:181
aoedev_exit
void aoedev_exit(void)
Definition: aoedev.c:524
aoe.h
NOT_EXITING
Definition: aoedev.c:306
aoedev::timer
struct timer_list timer
Definition: aoe.h:174
downdev_frame
static void downdev_frame(struct list_head *pos)
Definition: aoedev.c:185
EXITING
Definition: aoedev.c:307
skbfree
static void skbfree(struct sk_buff *skb)
Definition: aoedev.c:405
aoeif::nd
struct net_device * nd
Definition: aoe.h:131
aoetgt
Definition: aoe.h:136
DEVFL_UP
Definition: aoe.h:74
aoedev::flags
u16 flags
Definition: aoe.h:162
aoedev::factive
struct list_head factive[NFACTIVE]
Definition: aoe.h:186
aoedev::rq
struct request * rq
Definition: aoe.h:183
flush
static int flush(const char __user *str, size_t cnt, int exiting)
Definition: aoedev.c:311
aoedev_flush
int aoedev_flush(const char __user *str, size_t cnt)
Definition: aoedev.c:394
aoedev::lock
spinlock_t lock
Definition: aoe.h:175
aoedev::sysminor
ulong sysminor
Definition: aoe.h:157
DEFINE_SPINLOCK
static DEFINE_SPINLOCK(devlist_lock)
buf
Definition: aoe.h:103
devlist
static struct aoedev * devlist
Definition: aoedev.c:25
aoedev
Definition: aoe.h:155
aoe_failbuf
void aoe_failbuf(struct aoedev *, struct buf *)
Definition: aoecmd.c:1718
NAOEIFS
Definition: aoe.h:89
ulong
unsigned long ulong
Definition: aoe-sancheck.c:30
aoedbg_undbg
void aoedbg_undbg(struct aoedev *d)
Definition: aoedbg.c:253
aoecmd_wreset
void aoecmd_wreset(struct aoetgt *t)
Definition: aoecmd.c:1695
aoedev::aoemajor
ulong aoemajor
Definition: aoe.h:158
aoedev::next
struct aoedev * next
Definition: aoe.h:156
minor_get_static
static int minor_get_static(ulong *sysminor, ulong aoemaj, int aoemin)
Definition: aoedev.c:60
skbpoolfree
static void skbpoolfree(struct aoedev *d)
Definition: aoedev.c:428
DEVFL_GDALLOC
Definition: aoe.h:77
aoedev::ntargets
ulong ntargets
Definition: aoe.h:189
aoetgt::ffree
struct list_head ffree
Definition: aoe.h:140
aoedev::targets
struct aoetgt ** targets
Definition: aoe.h:188
aoetgt::ifs
struct aoeif ifs[NAOEIFS]
Definition: aoe.h:141
aoedev::nopen
u16 nopen
Definition: aoe.h:163
aoedev::gd
struct gendisk * gd
Definition: aoe.h:169
aoedev::rttavg
u32 rttavg
Definition: aoe.h:159
minor_get
static int minor_get(ulong *sysminor, ulong aoemaj, int aoemin)
Definition: aoedev.c:107
DEVFL_TKILL
Definition: aoe.h:75
aoedev::bufpool
mempool_t * bufpool
Definition: aoe.h:179
aoedev_by_aoeaddr
struct aoedev * aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
Definition: aoedev.c:442
dummy_timer
static void dummy_timer(ulong)
Definition: aoedev.c:151
RTTAVG_INIT
Definition: aoe.h:96
aoe_end_request
void aoe_end_request(struct aoedev *, struct request *, int)
Definition: aoecmd.c:1119
aoedev::aoeminor
u16 aoeminor
Definition: aoe.h:161
aoedev::rexmitq
struct list_head rexmitq
Definition: aoe.h:187
frame::head
struct list_head head
Definition: aoe.h:115
aoedev_downdev
void aoedev_downdev(struct aoedev *d)
Definition: aoedev.c:199
aoeif
Definition: aoe.h:130
module_param
module_param(aoe_dyndevs, int, 0644)
aoedev::tgt
struct aoetgt ** tgt
Definition: aoe.h:190
aoedev_init
int __init aoedev_init(void)
Definition: aoedev.c:531
buf::nframesout
ulong nframesout
Definition: aoe.h:104
NFACTIVE
Definition: aoe.h:91
freetgt
static void freetgt(struct aoedev *d, struct aoetgt *t)
Definition: aoedev.c:501
aoe_dyndevs
static int aoe_dyndevs
Definition: aoedev.c:21
flush_parms
flush_parms
Definition: aoedev.c:305
aoedev::ip
struct aoedev::@5 ip
aoedisk_rm_sysfs
void aoedisk_rm_sysfs(struct aoedev *d)
Definition: aoeblk.c:238
aoetgt::d
struct aoedev * d
Definition: aoe.h:139
frame::t
struct aoetgt * t
Definition: aoe.h:121
DEVFL_NEWSIZE
Definition: aoe.h:80
DEVFL_FREEING
Definition: aoe.h:81
aoedisk_rm_debugfs
void aoedisk_rm_debugfs(struct aoedev *d)
Definition: aoeblk.c:226